intravenous

annotate src/game.cc @ 4:c6a6a64df6de

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