vrshoot

annotate src/scr_game.cc @ 3:c179c72369be

rename candy->vr
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 03 Feb 2014 08:52:13 +0200
parents b2f14e535253
children
rev   line source
nuclear@0 1 #include "scr_game.h"
nuclear@0 2 #include "object.h"
nuclear@0 3 #include "game.h"
nuclear@0 4 #include "meshgen.h"
nuclear@0 5 #include "opengl.h"
nuclear@0 6 #include "unistate.h"
nuclear@0 7 #include "logger.h"
nuclear@0 8 #include "level.h"
nuclear@0 9
nuclear@0 10 static int win_width, win_height;
nuclear@0 11 static bool bnstate[32];
nuclear@0 12 static int prev_x, prev_y;
nuclear@1 13 static float cam_theta, cam_phi = 23, cam_dist = 0;
nuclear@0 14 static Object *floor_obj;
nuclear@0 15 static Level *level;
nuclear@1 16 static unsigned long msec;
nuclear@0 17
nuclear@0 18 const char *GameScreen::get_name() const
nuclear@0 19 {
nuclear@0 20 return "game";
nuclear@0 21 }
nuclear@0 22
nuclear@0 23 bool GameScreen::init()
nuclear@0 24 {
nuclear@0 25 glEnable(GL_LIGHTING);
nuclear@0 26 glEnable(GL_LIGHT0);
nuclear@0 27
nuclear@0 28 level = new Level;
nuclear@0 29
nuclear@0 30 return true;
nuclear@0 31 }
nuclear@0 32
nuclear@0 33 void GameScreen::cleanup()
nuclear@0 34 {
nuclear@0 35 }
nuclear@0 36
nuclear@0 37 void GameScreen::update(unsigned long tmsec)
nuclear@0 38 {
nuclear@1 39 msec = tmsec;
nuclear@0 40 }
nuclear@0 41
nuclear@0 42 void GameScreen::display() const
nuclear@0 43 {
nuclear@0 44 Matrix4x4 matrix;
nuclear@0 45 matrix.translate(Vector3(0, 0, -cam_dist));
nuclear@0 46 matrix.rotate(Vector3(1, 0, 0), DEG_TO_RAD(cam_phi));
nuclear@0 47 matrix.rotate(Vector3(0, 1, 0), DEG_TO_RAD(cam_theta));
nuclear@1 48 matrix.translate(Vector3(0, -2, 0));
nuclear@0 49 set_view_matrix(matrix);
nuclear@0 50
nuclear@0 51 //glUseProgram(0);
nuclear@0 52
nuclear@0 53 bind_shader(defsdr);
nuclear@1 54 level->draw(msec);
nuclear@0 55 }
nuclear@0 56
nuclear@0 57 void GameScreen::reshape(int x, int y)
nuclear@0 58 {
nuclear@0 59 glViewport(0, 0, x, y);
nuclear@0 60
nuclear@0 61 win_width = x;
nuclear@0 62 win_height = y;
nuclear@0 63
nuclear@0 64 Matrix4x4 proj;
nuclear@0 65 proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
nuclear@0 66 set_projection_matrix(proj);
nuclear@0 67 }
nuclear@0 68
nuclear@0 69 void GameScreen::button(int bn, bool pressed, int x, int y)
nuclear@0 70 {
nuclear@0 71 prev_x = x;
nuclear@0 72 prev_y = y;
nuclear@0 73 bnstate[bn] = pressed;
nuclear@0 74 }
nuclear@0 75
nuclear@0 76 void GameScreen::motion(int x, int y, bool pressed)
nuclear@0 77 {
nuclear@0 78 int dx = x - prev_x;
nuclear@0 79 int dy = y - prev_y;
nuclear@0 80 prev_x = x;
nuclear@0 81 prev_y = y;
nuclear@0 82
nuclear@0 83 if(!dx && !dy) return;
nuclear@0 84
nuclear@0 85 if(bnstate[0]) {
nuclear@0 86 cam_theta += dx * 0.5;
nuclear@0 87 cam_phi += dy * 0.5;
nuclear@0 88
nuclear@0 89 if(cam_phi > 90) cam_phi = 90;
nuclear@0 90 if(cam_phi < 0) cam_phi = 0;
nuclear@0 91 }
nuclear@0 92
nuclear@0 93 if(bnstate[2]) {
nuclear@0 94 cam_dist += dy * 0.1;
nuclear@0 95
nuclear@0 96 if(cam_dist < 0) cam_dist = 0;
nuclear@0 97 }
nuclear@0 98 }
nuclear@0 99
nuclear@0 100 long GameScreen::redisplay_interval() const
nuclear@0 101 {
nuclear@0 102 return 1;
nuclear@0 103 }