vrshoot
diff src/scr_game.cc @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children | e7ca128b8713 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/scr_game.cc Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,107 @@ 1.4 +#include "scr_game.h" 1.5 +#include "object.h" 1.6 +#include "game.h" 1.7 +#include "meshgen.h" 1.8 +#include "opengl.h" 1.9 +#include "unistate.h" 1.10 +#include "logger.h" 1.11 +#include "level.h" 1.12 + 1.13 +static int win_width, win_height; 1.14 +static bool bnstate[32]; 1.15 +static int prev_x, prev_y; 1.16 +static float cam_theta, cam_phi = 20, cam_dist = 10; 1.17 +static Object *floor_obj; 1.18 +static Level *level; 1.19 + 1.20 +const char *GameScreen::get_name() const 1.21 +{ 1.22 + return "game"; 1.23 +} 1.24 + 1.25 +bool GameScreen::init() 1.26 +{ 1.27 + glEnable(GL_LIGHTING); 1.28 + glEnable(GL_LIGHT0); 1.29 + 1.30 + floor_obj = new Object; 1.31 + floor_obj->set_mesh(new Mesh); 1.32 + gen_plane(floor_obj->get_mesh(), 10, 10, 4, 4); 1.33 + floor_obj->set_rotation(Quaternion(Vector3(1, 0, 0), -DEG_TO_RAD(90))); 1.34 + 1.35 + level = new Level; 1.36 + 1.37 + return true; 1.38 +} 1.39 + 1.40 +void GameScreen::cleanup() 1.41 +{ 1.42 +} 1.43 + 1.44 +void GameScreen::update(unsigned long tmsec) 1.45 +{ 1.46 +} 1.47 + 1.48 +void GameScreen::display() const 1.49 +{ 1.50 + Matrix4x4 matrix; 1.51 + matrix.translate(Vector3(0, 0, -cam_dist)); 1.52 + matrix.rotate(Vector3(1, 0, 0), DEG_TO_RAD(cam_phi)); 1.53 + matrix.rotate(Vector3(0, 1, 0), DEG_TO_RAD(cam_theta)); 1.54 + set_view_matrix(matrix); 1.55 + 1.56 + //glUseProgram(0); 1.57 + 1.58 + bind_shader(defsdr); 1.59 + //floor_obj->draw(); 1.60 + 1.61 + level->draw(); 1.62 +} 1.63 + 1.64 +void GameScreen::reshape(int x, int y) 1.65 +{ 1.66 + glViewport(0, 0, x, y); 1.67 + 1.68 + win_width = x; 1.69 + win_height = y; 1.70 + 1.71 + Matrix4x4 proj; 1.72 + proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0); 1.73 + set_projection_matrix(proj); 1.74 +} 1.75 + 1.76 +void GameScreen::button(int bn, bool pressed, int x, int y) 1.77 +{ 1.78 + prev_x = x; 1.79 + prev_y = y; 1.80 + bnstate[bn] = pressed; 1.81 +} 1.82 + 1.83 +void GameScreen::motion(int x, int y, bool pressed) 1.84 +{ 1.85 + int dx = x - prev_x; 1.86 + int dy = y - prev_y; 1.87 + prev_x = x; 1.88 + prev_y = y; 1.89 + 1.90 + if(!dx && !dy) return; 1.91 + 1.92 + if(bnstate[0]) { 1.93 + cam_theta += dx * 0.5; 1.94 + cam_phi += dy * 0.5; 1.95 + 1.96 + if(cam_phi > 90) cam_phi = 90; 1.97 + if(cam_phi < 0) cam_phi = 0; 1.98 + } 1.99 + 1.100 + if(bnstate[2]) { 1.101 + cam_dist += dy * 0.1; 1.102 + 1.103 + if(cam_dist < 0) cam_dist = 0; 1.104 + } 1.105 +} 1.106 + 1.107 +long GameScreen::redisplay_interval() const 1.108 +{ 1.109 + return 1; 1.110 +}