stratgame
view src/game_part.cc @ 5:2e38715de41b
terrain
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 27 May 2012 07:00:48 +0300 |
parents | 8d95187cb3ee |
children |
line source
1 #include <stdlib.h>
2 #include "opengl.h"
3 #include "game_part.h"
4 #include "level.h"
6 Game::Game()
7 {
8 cam_theta = 0;
9 cam_phi = 45;
10 cam_dist = 5;
11 }
13 Game::~Game() {}
15 bool Game::init()
16 {
17 if(!level.load("data/test.level")) {
18 return false;
19 }
20 return true;
21 }
23 void Game::start()
24 {
25 Part::start();
27 glEnable(GL_DEPTH_TEST);
28 }
30 void Game::draw() const
31 {
32 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
34 glMatrixMode(GL_MODELVIEW);
35 glLoadIdentity();
36 glTranslatef(0, 0, -cam_dist);
37 glRotatef(cam_phi, 1, 0, 0);
38 glRotatef(cam_theta, 0, 1, 0);
40 level.draw();
41 }
43 void Game::reshape(int x, int y)
44 {
45 glMatrixMode(GL_PROJECTION);
46 glLoadIdentity();
47 gluPerspective(45.0, (float)x / (float)y, 0.5, 500);
48 }
50 void Game::key(int key, bool pressed)
51 {
52 switch(key) {
53 case 27:
54 exit(0);
55 //cur_part = menu_part;
56 break;
58 default:
59 break;
60 }
61 }
63 static bool bnstate[32];
64 static int prev_x, prev_y;
66 void Game::mouse_button(int bn, bool pressed)
67 {
68 bnstate[bn] = pressed;
70 if(pressed) {
71 if(bn == 3) {
72 cam_dist -= 0.2;
73 if(cam_dist < 0) {
74 cam_dist = 0;
75 }
76 }
77 if(bn == 4) {
78 cam_dist += 0.2;
79 }
80 }
81 }
83 void Game::mouse_motion(int x, int y)
84 {
85 int dx = x - prev_x;
86 int dy = y - prev_y;
87 prev_x = x;
88 prev_y = y;
90 if(bnstate[2]) {
91 cam_theta += dx * 0.5;
92 cam_phi += dy * 0.5;
94 if(cam_phi < 0) cam_phi = 0;
95 if(cam_phi > 90) cam_phi = 90;
96 }
97 }