intravenous

annotate src/game.cc @ 10:8fbdc6f84f64

fixed after the change in vmath
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 31 May 2013 01:30:14 +0300
parents 0f305fc0b548 2723dc026c4f
children
rev   line source
nuclear@0 1 #include <stdlib.h>
nuclear@0 2 #include "opengl.h"
nuclear@0 3 #include "game.h"
nuclear@1 4 #include "vein.h"
nuclear@1 5 #include "ship.h"
nuclear@1 6 #include "camera.h"
nuclear@3 7 #include "cockpit.h"
nuclear@0 8
nuclear@6 9 static const Vector3 fog_color(0.64, 0.1, 0.1);
nuclear@5 10
nuclear@1 11 static Vein *vein;
nuclear@6 12 static Ship *ship;
nuclear@1 13
nuclear@1 14 static int keystate[256];
nuclear@2 15 static int dbg_inside = false;
nuclear@4 16 static bool ignore_next_motion;
nuclear@1 17
nuclear@1 18 static OrbitCamera dbgcam;
nuclear@1 19
nuclear@1 20 static unsigned long msec, start_time = -1;
nuclear@0 21
nuclear@0 22 bool game_init()
nuclear@0 23 {
nuclear@0 24 glEnable(GL_DEPTH_TEST);
nuclear@0 25 glEnable(GL_CULL_FACE);
nuclear@0 26
nuclear@0 27 float lpos[] = {-1, 1, 1, 0};
nuclear@0 28 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@0 29
nuclear@3 30 // initialize the cockpit
nuclear@3 31 if(!cockpit_init()) {
nuclear@3 32 return false;
nuclear@3 33 }
nuclear@3 34
nuclear@3 35 // initialize the only level
nuclear@1 36 vein = new Vein;
nuclear@3 37 if(!vein->init()) {
nuclear@3 38 return false;
nuclear@3 39 }
nuclear@5 40 vein->set_fog_color(fog_color);
nuclear@5 41
nuclear@6 42 // create the player's ship
nuclear@6 43 ship = new Ship(vein);
nuclear@6 44
nuclear@5 45 glClearColor(fog_color.x, fog_color.y, fog_color.z, 1);
nuclear@1 46
nuclear@0 47 return true;
nuclear@0 48 }
nuclear@0 49
nuclear@0 50 void game_shutdown()
nuclear@0 51 {
nuclear@3 52 delete vein;
nuclear@3 53 cockpit_destroy();
nuclear@0 54 exit(0);
nuclear@0 55 }
nuclear@0 56
nuclear@1 57 void game_update(unsigned long ms)
nuclear@0 58 {
nuclear@1 59 static unsigned long last_upd;
nuclear@1 60
nuclear@0 61 if(start_time == 0) {
nuclear@1 62 start_time = ms;
nuclear@0 63 }
nuclear@1 64 msec = ms - start_time;
nuclear@1 65
nuclear@1 66 time_sec_t dt = (msec - last_upd) / 1000.0;
nuclear@1 67
nuclear@1 68 // handle key input
nuclear@1 69 if(keystate['w'] || keystate['W']) {
nuclear@9 70 ship->accelerate(4.0 * dt);
nuclear@1 71 }
nuclear@1 72 if(keystate['s'] || keystate['S']) {
nuclear@9 73 ship->accelerate(-4.0 * dt);
nuclear@1 74 }
nuclear@1 75 if(keystate['d'] || keystate['D']) {
nuclear@9 76 ship->accelerate_side(4.0 * dt);
nuclear@1 77 }
nuclear@1 78 if(keystate['a'] || keystate['A']) {
nuclear@9 79 ship->accelerate_side(-4.0 * dt);
nuclear@1 80 }
nuclear@1 81
nuclear@6 82 ship->update(dt);
nuclear@1 83
nuclear@1 84 last_upd = msec;
nuclear@0 85 }
nuclear@0 86
nuclear@0 87 void game_draw()
nuclear@0 88 {
nuclear@0 89 glMatrixMode(GL_MODELVIEW);
nuclear@0 90
nuclear@2 91 if(dbg_inside) {
nuclear@6 92 load_gl_matrix(ship->get_matrix().inverse());
nuclear@3 93 } else {
nuclear@3 94 dbgcam.use();
nuclear@2 95 }
nuclear@2 96
nuclear@6 97 vein->draw(ship->get_position());
nuclear@3 98
nuclear@3 99 if(!dbg_inside) {
nuclear@6 100 ship->dbg_draw();
nuclear@3 101 } else {
nuclear@3 102 cockpit_draw();
nuclear@3 103 }
nuclear@0 104 }
nuclear@0 105
nuclear@0 106 void game_input_keyb(int key, int state)
nuclear@0 107 {
nuclear@0 108 if(state) {
nuclear@0 109 switch(key) {
nuclear@0 110 case 27:
nuclear@0 111 game_shutdown();
nuclear@1 112 break;
nuclear@0 113
nuclear@2 114 case '\b':
nuclear@6 115 case 127:
nuclear@2 116 dbg_inside = !dbg_inside;
nuclear@3 117 if(dbg_inside) {
nuclear@3 118 glutPassiveMotionFunc(game_input_mmotion);
nuclear@3 119 glutSetCursor(GLUT_CURSOR_NONE);
nuclear@4 120
nuclear@4 121 ignore_next_motion = true;
nuclear@4 122 glutWarpPointer(win_xsz / 2, win_ysz / 2);
nuclear@3 123 } else {
nuclear@3 124 glutPassiveMotionFunc(0);
nuclear@3 125 glutSetCursor(GLUT_CURSOR_INHERIT);
nuclear@3 126 }
nuclear@2 127 break;
nuclear@2 128
nuclear@0 129 default:
nuclear@0 130 break;
nuclear@0 131 }
nuclear@0 132 }
nuclear@1 133
nuclear@1 134 if(key < 256) {
nuclear@1 135 keystate[key] = state;
nuclear@1 136 }
nuclear@0 137 }
nuclear@1 138
nuclear@1 139 static int bnstate[16];
nuclear@1 140 static int prev_x, prev_y;
nuclear@1 141
nuclear@1 142 void game_input_mbutton(int bn, int state, int x, int y)
nuclear@1 143 {
nuclear@1 144 if(bn < 16) {
nuclear@1 145 bnstate[bn] = state;
nuclear@1 146 }
nuclear@1 147 prev_x = x;
nuclear@1 148 prev_y = y;
nuclear@1 149 }
nuclear@1 150
nuclear@8 151 #define EDGE_PIXELS (win_ysz / 4)
nuclear@8 152
nuclear@1 153 void game_input_mmotion(int x, int y)
nuclear@1 154 {
nuclear@1 155 int dx = x - prev_x;
nuclear@1 156 int dy = y - prev_y;
nuclear@1 157 prev_x = x;
nuclear@1 158 prev_y = y;
nuclear@1 159
nuclear@8 160 if(ignore_next_motion || abs(dx) >= win_xsz / 2 - EDGE_PIXELS ||
nuclear@8 161 abs(dy) >= win_ysz / 2 - EDGE_PIXELS) {
nuclear@4 162 ignore_next_motion = false;
nuclear@3 163 return;
nuclear@1 164 }
nuclear@3 165
nuclear@3 166 if(dbg_inside) {
nuclear@6 167 ship->turn((float)-dx / win_xsz, (float)-dy / win_ysz);
nuclear@3 168
nuclear@8 169 if(x < EDGE_PIXELS || x >= win_xsz - EDGE_PIXELS || y < EDGE_PIXELS || y >= win_ysz - EDGE_PIXELS) {
nuclear@8 170 glutWarpPointer(win_xsz / 2, win_ysz / 2);
nuclear@8 171 }
nuclear@3 172 } else {
nuclear@3 173 if(bnstate[0]) {
nuclear@3 174 dbgcam.input_rotate(10.0 * dx / win_xsz, 10.0 * dy / win_ysz, 0);
nuclear@3 175 }
nuclear@3 176 if(bnstate[2]) {
nuclear@3 177 dbgcam.input_zoom(10.0 * dy / win_ysz);
nuclear@3 178 }
nuclear@1 179 }
nuclear@1 180 }