intravenous

annotate src/game.cc @ 1:3ea290d35984

it's never going to finish but wth :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Apr 2012 22:42:43 +0300
parents 2b30f261f641
children 472c28b8b875
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@0 7
nuclear@1 8 static Vein *vein;
nuclear@1 9 static Ship ship;
nuclear@1 10
nuclear@1 11 static int keystate[256];
nuclear@1 12
nuclear@1 13 static OrbitCamera dbgcam;
nuclear@1 14
nuclear@1 15 static unsigned long msec, start_time = -1;
nuclear@0 16
nuclear@0 17 bool game_init()
nuclear@0 18 {
nuclear@0 19 glEnable(GL_DEPTH_TEST);
nuclear@0 20 glEnable(GL_CULL_FACE);
nuclear@0 21 glEnable(GL_LIGHTING);
nuclear@0 22 glEnable(GL_LIGHT0);
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@1 27 // initialize the only level (vein)
nuclear@1 28 vein = new Vein;
nuclear@1 29
nuclear@0 30 return true;
nuclear@0 31 }
nuclear@0 32
nuclear@0 33 void game_shutdown()
nuclear@0 34 {
nuclear@0 35 exit(0);
nuclear@0 36 }
nuclear@0 37
nuclear@1 38 void game_update(unsigned long ms)
nuclear@0 39 {
nuclear@1 40 static unsigned long last_upd;
nuclear@1 41
nuclear@0 42 if(start_time == 0) {
nuclear@1 43 start_time = ms;
nuclear@0 44 }
nuclear@1 45 msec = ms - start_time;
nuclear@1 46
nuclear@1 47 time_sec_t dt = (msec - last_upd) / 1000.0;
nuclear@1 48
nuclear@1 49 // handle key input
nuclear@1 50 if(keystate['w'] || keystate['W']) {
nuclear@1 51 ship.accelerate(1.0 * dt);
nuclear@1 52 }
nuclear@1 53 if(keystate['s'] || keystate['S']) {
nuclear@1 54 ship.accelerate(-1.0 * dt);
nuclear@1 55 }
nuclear@1 56 // XXX change to mouselook
nuclear@1 57 if(keystate['d'] || keystate['D']) {
nuclear@1 58 ship.turn(-1.0 * dt, 0.0);
nuclear@1 59 }
nuclear@1 60 if(keystate['a'] || keystate['A']) {
nuclear@1 61 ship.turn(1.0 * dt, 0.0);
nuclear@1 62 }
nuclear@1 63 if(keystate['e'] || keystate['E']) {
nuclear@1 64 ship.turn(0.0, -1.0 * dt);
nuclear@1 65 }
nuclear@1 66 if(keystate['c'] || keystate['C']) {
nuclear@1 67 ship.turn(0.0, 1.0 * dt);
nuclear@1 68 }
nuclear@1 69
nuclear@1 70 ship.update(dt);
nuclear@1 71
nuclear@1 72 last_upd = msec;
nuclear@0 73 }
nuclear@0 74
nuclear@0 75 void game_draw()
nuclear@0 76 {
nuclear@0 77 glMatrixMode(GL_MODELVIEW);
nuclear@1 78 dbgcam.use();
nuclear@0 79
nuclear@1 80 vein->draw(ship.get_position());
nuclear@1 81
nuclear@1 82 ship.dbg_draw();
nuclear@0 83 }
nuclear@0 84
nuclear@0 85 void game_input_keyb(int key, int state)
nuclear@0 86 {
nuclear@0 87 if(state) {
nuclear@0 88 switch(key) {
nuclear@0 89 case 27:
nuclear@0 90 game_shutdown();
nuclear@1 91 break;
nuclear@0 92
nuclear@0 93 default:
nuclear@0 94 break;
nuclear@0 95 }
nuclear@0 96 }
nuclear@1 97
nuclear@1 98 if(key < 256) {
nuclear@1 99 keystate[key] = state;
nuclear@1 100 }
nuclear@0 101 }
nuclear@1 102
nuclear@1 103 static int bnstate[16];
nuclear@1 104 static int prev_x, prev_y;
nuclear@1 105
nuclear@1 106 void game_input_mbutton(int bn, int state, int x, int y)
nuclear@1 107 {
nuclear@1 108 if(bn < 16) {
nuclear@1 109 bnstate[bn] = state;
nuclear@1 110 }
nuclear@1 111 prev_x = x;
nuclear@1 112 prev_y = y;
nuclear@1 113 }
nuclear@1 114
nuclear@1 115 void game_input_mmotion(int x, int y)
nuclear@1 116 {
nuclear@1 117 int dx = x - prev_x;
nuclear@1 118 int dy = y - prev_y;
nuclear@1 119 prev_x = x;
nuclear@1 120 prev_y = y;
nuclear@1 121
nuclear@1 122 if(bnstate[0]) {
nuclear@1 123 dbgcam.input_rotate(10.0 * dx / win_xsz, 10.0 * dy / win_ysz, 0);
nuclear@1 124 }
nuclear@1 125 if(bnstate[2]) {
nuclear@1 126 dbgcam.input_zoom(10.0 * dy / win_ysz);
nuclear@1 127 }
nuclear@1 128 }