intravenous

view src/game.cc @ 5:aab0d8ea21cd

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