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