stratgame
changeset 4:cd12944a8ea8
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 25 May 2012 05:28:20 +0300 |
parents | 8d95187cb3ee |
children | 2e38715de41b |
files | level/src/terrain.cc level/src/terrain.h src/game_part.cc src/game_part.h |
diffstat | 4 files changed, 141 insertions(+), 3 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/level/src/terrain.cc Fri May 25 05:28:20 2012 +0300 1.3 @@ -0,0 +1,53 @@ 1.4 +#include "terrain.h" 1.5 + 1.6 +Terrain::Terrain() 1.7 +{ 1.8 + root = 0; 1.9 +} 1.10 + 1.11 +Terrain::~Terrain() 1.12 +{ 1.13 + delete root; 1.14 +} 1.15 + 1.16 +float Terrain::get_height(float x, float y) const 1.17 +{ 1.18 + return root->get_height(x, y); 1.19 +} 1.20 + 1.21 + 1.22 +TerrainNode::TerrainNode() 1.23 +{ 1.24 + height = 0; 1.25 + child[0] = child[1] = child[2] = child[3]; 1.26 +} 1.27 + 1.28 +TerrainNode::~TerrainNode() 1.29 +{ 1.30 + for(auto c : child) { 1.31 + delete c; 1.32 + } 1.33 + delete [] height; 1.34 +} 1.35 + 1.36 +TerrainNode *TerrainNode::get_child(float x, float y) 1.37 +{ 1.38 + int xidx = x >= 0.0 ? 1 : 0; 1.39 + int yidx = y >= 0.0 ? 1 : 0; 1.40 + return child[y * 2 + x]; 1.41 +} 1.42 + 1.43 +const TerrainNode *TerrainNode::get_child(float x, float y) const 1.44 +{ 1.45 + int xidx = x >= 0.0 ? 1 : 0; 1.46 + int yidx = y >= 0.0 ? 1 : 0; 1.47 + return child[y * 2 + x]; 1.48 +} 1.49 + 1.50 +float TerrainNode::get_height(float x, float y) const 1.51 +{ 1.52 + TerrainNode *child = get_child(x, y); 1.53 + if(child) { 1.54 + 1.55 + } 1.56 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/level/src/terrain.h Fri May 25 05:28:20 2012 +0300 2.3 @@ -0,0 +1,37 @@ 2.4 +#ifndef TERRAIN_H_ 2.5 +#define TERRAIN_H_ 2.6 + 2.7 +#include <string> 2.8 +#include <map> 2.9 + 2.10 +class TerrainNode; 2.11 + 2.12 +class Terrain { 2.13 +private: 2.14 + TerrainNode *root; 2.15 + 2.16 +public: 2.17 + Terrain(); 2.18 + ~Terrain(); 2.19 + 2.20 + float get_height(float x, float y) const; 2.21 +}; 2.22 + 2.23 + 2.24 +class TerrainNode { 2.25 +private: 2.26 + unsigned char *height; 2.27 + 2.28 +public: 2.29 + TerrainNode *child[4]; 2.30 + 2.31 + TerrainNode(); 2.32 + ~TerrainNode(); 2.33 + 2.34 + TerrainNode *get_child(float x, float y); 2.35 + const TerrainNode *get_child(float x, float y) const; 2.36 + 2.37 + float get_height(float x, float y) const; 2.38 +}; 2.39 + 2.40 +#endif // TERRAIN_H_
3.1 --- a/src/game_part.cc Wed May 23 17:10:46 2012 +0300 3.2 +++ b/src/game_part.cc Fri May 25 05:28:20 2012 +0300 3.3 @@ -3,6 +3,13 @@ 3.4 #include "game_part.h" 3.5 #include "level.h" 3.6 3.7 +Game::Game() 3.8 +{ 3.9 + cam_theta = 0; 3.10 + cam_phi = 45; 3.11 + cam_dist = 5; 3.12 +} 3.13 + 3.14 Game::~Game() {} 3.15 3.16 bool Game::init() 3.17 @@ -26,9 +33,9 @@ 3.18 3.19 glMatrixMode(GL_MODELVIEW); 3.20 glLoadIdentity(); 3.21 - glTranslatef(0, 0, -4); 3.22 - glRotatef(50, 1, 0, 0); 3.23 - glRotatef(current_time / 100.0, 0, 1, 0); 3.24 + glTranslatef(0, 0, -cam_dist); 3.25 + glRotatef(cam_phi, 1, 0, 0); 3.26 + glRotatef(cam_theta, 0, 1, 0); 3.27 3.28 level.draw(); 3.29 } 3.30 @@ -52,3 +59,39 @@ 3.31 break; 3.32 } 3.33 } 3.34 + 3.35 +static bool bnstate[32]; 3.36 +static int prev_x, prev_y; 3.37 + 3.38 +void Game::mouse_button(int bn, bool pressed) 3.39 +{ 3.40 + bnstate[bn] = pressed; 3.41 + 3.42 + if(pressed) { 3.43 + if(bn == 3) { 3.44 + cam_dist -= 0.2; 3.45 + if(cam_dist < 0) { 3.46 + cam_dist = 0; 3.47 + } 3.48 + } 3.49 + if(bn == 4) { 3.50 + cam_dist += 0.2; 3.51 + } 3.52 + } 3.53 +} 3.54 + 3.55 +void Game::mouse_motion(int x, int y) 3.56 +{ 3.57 + int dx = x - prev_x; 3.58 + int dy = y - prev_y; 3.59 + prev_x = x; 3.60 + prev_y = y; 3.61 + 3.62 + if(bnstate[2]) { 3.63 + cam_theta += dx * 0.5; 3.64 + cam_phi += dy * 0.5; 3.65 + 3.66 + if(cam_phi < 0) cam_phi = 0; 3.67 + if(cam_phi > 90) cam_phi = 90; 3.68 + } 3.69 +}
4.1 --- a/src/game_part.h Wed May 23 17:10:46 2012 +0300 4.2 +++ b/src/game_part.h Fri May 25 05:28:20 2012 +0300 4.3 @@ -7,7 +7,10 @@ 4.4 class Game : public Part { 4.5 private: 4.6 Level level; 4.7 + float cam_theta, cam_phi, cam_dist; 4.8 + 4.9 public: 4.10 + Game(); 4.11 ~Game(); 4.12 4.13 bool init(); 4.14 @@ -17,6 +20,8 @@ 4.15 void draw() const; 4.16 void reshape(int x, int y); 4.17 void key(int key, bool pressed); 4.18 + void mouse_button(int bn, bool pressed); 4.19 + void mouse_motion(int x, int y); 4.20 }; 4.21 4.22 #endif // GAME_PART_H_