intravenous

annotate src/game.cc @ 3:94d4c60af435

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