vrshoot

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