intravenous
view 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 |
line source
1 #include <stdlib.h>
2 #include "opengl.h"
3 #include "game.h"
4 #include "vein.h"
5 #include "ship.h"
6 #include "camera.h"
8 static Vein *vein;
9 static Ship ship;
11 static int keystate[256];
13 static OrbitCamera dbgcam;
15 static unsigned long msec, start_time = -1;
17 bool game_init()
18 {
19 glEnable(GL_DEPTH_TEST);
20 glEnable(GL_CULL_FACE);
21 glEnable(GL_LIGHTING);
22 glEnable(GL_LIGHT0);
24 float lpos[] = {-1, 1, 1, 0};
25 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
27 // initialize the only level (vein)
28 vein = new Vein;
30 return true;
31 }
33 void game_shutdown()
34 {
35 exit(0);
36 }
38 void game_update(unsigned long ms)
39 {
40 static unsigned long last_upd;
42 if(start_time == 0) {
43 start_time = ms;
44 }
45 msec = ms - start_time;
47 time_sec_t dt = (msec - last_upd) / 1000.0;
49 // handle key input
50 if(keystate['w'] || keystate['W']) {
51 ship.accelerate(1.0 * dt);
52 }
53 if(keystate['s'] || keystate['S']) {
54 ship.accelerate(-1.0 * dt);
55 }
56 // XXX change to mouselook
57 if(keystate['d'] || keystate['D']) {
58 ship.turn(-1.0 * dt, 0.0);
59 }
60 if(keystate['a'] || keystate['A']) {
61 ship.turn(1.0 * dt, 0.0);
62 }
63 if(keystate['e'] || keystate['E']) {
64 ship.turn(0.0, -1.0 * dt);
65 }
66 if(keystate['c'] || keystate['C']) {
67 ship.turn(0.0, 1.0 * dt);
68 }
70 ship.update(dt);
72 last_upd = msec;
73 }
75 void game_draw()
76 {
77 glMatrixMode(GL_MODELVIEW);
78 dbgcam.use();
80 vein->draw(ship.get_position());
82 ship.dbg_draw();
83 }
85 void game_input_keyb(int key, int state)
86 {
87 if(state) {
88 switch(key) {
89 case 27:
90 game_shutdown();
91 break;
93 default:
94 break;
95 }
96 }
98 if(key < 256) {
99 keystate[key] = state;
100 }
101 }
103 static int bnstate[16];
104 static int prev_x, prev_y;
106 void game_input_mbutton(int bn, int state, int x, int y)
107 {
108 if(bn < 16) {
109 bnstate[bn] = state;
110 }
111 prev_x = x;
112 prev_y = y;
113 }
115 void game_input_mmotion(int x, int y)
116 {
117 int dx = x - prev_x;
118 int dy = y - prev_y;
119 prev_x = x;
120 prev_y = y;
122 if(bnstate[0]) {
123 dbgcam.input_rotate(10.0 * dx / win_xsz, 10.0 * dy / win_ysz, 0);
124 }
125 if(bnstate[2]) {
126 dbgcam.input_zoom(10.0 * dy / win_ysz);
127 }
128 }