vrshoot

view 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 source
1 #include "scr_game.h"
2 #include "object.h"
3 #include "game.h"
4 #include "meshgen.h"
5 #include "opengl.h"
6 #include "unistate.h"
7 #include "logger.h"
8 #include "level.h"
10 static int win_width, win_height;
11 static bool bnstate[32];
12 static int prev_x, prev_y;
13 static float cam_theta, cam_phi = 20, cam_dist = 10;
14 static Object *floor_obj;
15 static Level *level;
17 const char *GameScreen::get_name() const
18 {
19 return "game";
20 }
22 bool GameScreen::init()
23 {
24 glEnable(GL_LIGHTING);
25 glEnable(GL_LIGHT0);
27 floor_obj = new Object;
28 floor_obj->set_mesh(new Mesh);
29 gen_plane(floor_obj->get_mesh(), 10, 10, 4, 4);
30 floor_obj->set_rotation(Quaternion(Vector3(1, 0, 0), -DEG_TO_RAD(90)));
32 level = new Level;
34 return true;
35 }
37 void GameScreen::cleanup()
38 {
39 }
41 void GameScreen::update(unsigned long tmsec)
42 {
43 }
45 void GameScreen::display() const
46 {
47 Matrix4x4 matrix;
48 matrix.translate(Vector3(0, 0, -cam_dist));
49 matrix.rotate(Vector3(1, 0, 0), DEG_TO_RAD(cam_phi));
50 matrix.rotate(Vector3(0, 1, 0), DEG_TO_RAD(cam_theta));
51 set_view_matrix(matrix);
53 //glUseProgram(0);
55 bind_shader(defsdr);
56 //floor_obj->draw();
58 level->draw();
59 }
61 void GameScreen::reshape(int x, int y)
62 {
63 glViewport(0, 0, x, y);
65 win_width = x;
66 win_height = y;
68 Matrix4x4 proj;
69 proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
70 set_projection_matrix(proj);
71 }
73 void GameScreen::button(int bn, bool pressed, int x, int y)
74 {
75 prev_x = x;
76 prev_y = y;
77 bnstate[bn] = pressed;
78 }
80 void GameScreen::motion(int x, int y, bool pressed)
81 {
82 int dx = x - prev_x;
83 int dy = y - prev_y;
84 prev_x = x;
85 prev_y = y;
87 if(!dx && !dy) return;
89 if(bnstate[0]) {
90 cam_theta += dx * 0.5;
91 cam_phi += dy * 0.5;
93 if(cam_phi > 90) cam_phi = 90;
94 if(cam_phi < 0) cam_phi = 0;
95 }
97 if(bnstate[2]) {
98 cam_dist += dy * 0.1;
100 if(cam_dist < 0) cam_dist = 0;
101 }
102 }
104 long GameScreen::redisplay_interval() const
105 {
106 return 1;
107 }