labyrinth

view src/game.c @ 8:d3f1f74067b0

ops, forgot to swapbuffers on the android side
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 04 May 2015 04:56:44 +0300
parents 45b91185b298
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include "opengl.h"
5 #include "game.h"
6 #include "player.h"
7 #include "level.h"
8 #include "noise.h"
9 #include "texture.h"
10 #include "mesh.h"
12 #define PLAYER_EYE_HEIGHT 1.65
14 static void draw_scene(unsigned long msec);
16 static int win_width, win_height;
17 static struct level level;
18 static struct player player;
19 static int mouselook, freelook = 1;
20 static char keystate[256];
22 static struct mesh *monkey_mesh;
23 static unsigned int envmap;
24 static unsigned int win_tex;
26 static int game_won;
28 static float dbg_cam_dist = 0.0;
31 int game_init(void)
32 {
33 glEnable(GL_DEPTH_TEST);
34 glEnable(GL_CULL_FACE);
35 glEnable(GL_LIGHTING);
36 glEnable(GL_NORMALIZE);
38 glEnable(GL_LIGHT0);
39 glEnable(GL_LIGHT1);
41 /* set a slightly bluish cold ambient light */
42 {
43 float ambient[] = {0.08, 0.1, 0.3, 1.0};
44 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
45 }
47 level_init(&level);
48 if(level_load(&level, "data/0.level") == -1) {
49 fprintf(stderr, "level loading failed\n");
50 return -1;
51 }
53 if(!(level.wall_tex = load_texture("data/wall.ppm"))) {
54 return -1;
55 }
56 if(!(level.floor_tex = load_texture("data/floor.ppm"))) {
57 return -1;
58 }
59 level.floor_tex_scale = 1.0;
60 if(!(level.ceil_tex = load_texture("data/ceil.ppm"))) {
61 return -1;
62 }
63 level.ceil_tex_scale = 2.0;
66 if(!(monkey_mesh = load_mesh("data/monkey.obj"))) {
67 return -1;
68 }
69 if(!(envmap = load_texture("data/refmap.ppm"))) {
70 return -1;
71 }
73 if(!(win_tex = load_texture("data/done.ppm"))) {
74 return -1;
75 }
77 player_init(&player, &level);
79 glClearColor(1, 0, 0, 1);
80 return 0;
81 }
83 void game_shutdown(void)
84 {
85 }
87 static void update(unsigned long msec)
88 {
89 static unsigned long prev_upd;
90 float dfwd = 0.0f;
91 float dright = 0.0f;
92 float walk_speed = 8.0;
94 float dt = (float)(msec - prev_upd) / 1000.0f;
95 prev_upd = msec;
97 if(game_won) return;
99 if(freelook) {
100 if(keystate['w'] || keystate['W']) {
101 dfwd += walk_speed * dt;
102 }
103 if(keystate['s'] || keystate['S']) {
104 dfwd -= walk_speed * dt;
105 }
106 if(keystate['d'] || keystate['D']) {
107 dright += walk_speed * dt * 0.6;
108 }
109 if(keystate['a'] || keystate['A']) {
110 dright -= walk_speed * dt * 0.6;
111 }
113 player_move(&player, dfwd, dright);
114 }
116 if(level_cell_at(&level, player.x, player.y) == 'x') {
117 game_won = 1;
118 }
119 }
121 void game_display(unsigned long msec)
122 {
123 float flicker = 1.0f;
125 update(msec);
127 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
129 glMatrixMode(GL_MODELVIEW);
130 glLoadIdentity();
131 glTranslatef(0, 0, -dbg_cam_dist);
132 /* set view matrix according to player pos/rot */
133 player_setup_view_matrix(&player);
134 glTranslatef(0, -PLAYER_EYE_HEIGHT, 0);
136 /* setup lights */
137 flicker = fbm1((float)msec / 500.0f, 2) * 0.4 + 0.6;
138 set_light_position(0, player.x, PLAYER_EYE_HEIGHT + 0.2, player.y);
139 set_light_color(0, 1.0 * flicker, 0.6 * flicker, 0.3 * flicker);
140 set_light_attenuation(0, 0.5, 0, 0.04);
142 set_light_position(1, level.goal_pos[0], 0.8, level.goal_pos[1]);
143 set_light_color(1, 0.955, 0.75, 0.06);
144 set_light_attenuation(1, 0.9, 0, 0.05);
146 /* draw the scene */
147 draw_scene(msec);
148 }
150 void game_reshape(int x, int y)
151 {
152 win_width = x;
153 win_height = y;
154 glViewport(0, 0, x, y);
155 glMatrixMode(GL_PROJECTION);
156 glLoadIdentity();
157 gluPerspective(55.0, (float)x / (float)y, 0.5, 500.0);
158 }
161 static void draw_scene(unsigned long msec)
162 {
163 float x, y;
164 float tsec = (float)msec / 1000.0f;
166 level_draw(&level);
168 /* draw the golden monkey */
169 if(level_obj_pos(&level, 'x', &x, &y)) {
170 /* TODO: GLES doesn't have tex-gen... recalc manually */
171 #ifndef GL_MOBILE
172 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
173 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
174 glEnable(GL_TEXTURE_GEN_S);
175 glEnable(GL_TEXTURE_GEN_T);
177 glBindTexture(GL_TEXTURE_2D, envmap);
178 glEnable(GL_TEXTURE_2D);
179 #endif
181 set_mtl_diffuse(0.1, 0.1, 0.1, 1);
182 set_mtl_specular(0.955, 0.75, 0.06);
183 set_mtl_shininess(80.0);
184 set_mtl_emission(0.955, 0.75 * 0.7, 0.06 * 0.1);
186 glMatrixMode(GL_MODELVIEW);
187 glPushMatrix();
188 glTranslatef(x, 0.8 + sin(tsec * 2.0) * 0.1, y);
189 glRotatef(tsec * 100.0, 0, 1, 0);
190 glRotatef(sin(tsec * 3.0) * 10.0, 1, 0, 0);
191 glScalef(0.3, 0.3, 0.3);
193 render_mesh(monkey_mesh);
195 glPopMatrix();
197 set_mtl_emission(0, 0, 0);
199 #ifndef GL_MOBILE
200 glDisable(GL_TEXTURE_2D);
202 glDisable(GL_TEXTURE_GEN_S);
203 glDisable(GL_TEXTURE_GEN_T);
204 #endif
205 }
207 /* draw the win text */
208 if(game_won) {
209 glMatrixMode(GL_MODELVIEW);
210 glPushMatrix();
211 glLoadIdentity();
212 glMatrixMode(GL_PROJECTION);
213 glPushMatrix();
214 glLoadIdentity();
215 glScalef((float)win_height / (float)win_width, 1, 1);
217 glBindTexture(GL_TEXTURE_2D, win_tex);
218 glEnable(GL_TEXTURE_2D);
220 glEnable(GL_BLEND);
221 glBlendFunc(GL_ONE, GL_ONE);
222 glDisable(GL_LIGHTING);
224 glBegin(GL_QUADS);
225 glTexCoord2f(0, 1); glVertex2f(-1, -1);
226 glTexCoord2f(1, 1); glVertex2f(1, -1);
227 glTexCoord2f(1, 0); glVertex2f(1, 1);
228 glTexCoord2f(0, 0); glVertex2f(-1, 1);
229 glEnd();
231 glDisable(GL_BLEND);
232 glDisable(GL_TEXTURE_2D);
233 glEnable(GL_LIGHTING);
235 glPopMatrix();
236 glMatrixMode(GL_MODELVIEW);
237 glPopMatrix();
238 }
239 }
241 static int warping_mouse;
243 void game_keyboard(int key, int pressed)
244 {
245 keystate[key] = pressed;
247 if(!pressed) return;
249 switch(key) {
250 case 27:
251 exit(0);
253 case 'a':
254 if(!freelook && !game_won) {
255 player_turn(&player, -90, 0);
256 }
257 break;
259 case 'd':
260 if(!freelook && !game_won) {
261 player_turn(&player, 90, 0);
262 }
263 break;
265 case 'w':
266 case 's':
267 if(!freelook && !game_won) {
268 float prev_x = player.x;
269 float prev_y = player.y;
270 float sign = key == 'w' ? 1.0 : -1.0;
272 if(!player_move(&player, level.cell_size * sign, 0)) {
273 player.x = prev_x;
274 player.y = prev_y;
275 }
276 }
277 break;
279 case '`':
280 case '~':
281 mouselook = !mouselook;
282 if(mouselook) {
283 warping_mouse = 1;
284 set_mouse_pos(win_width / 2, win_height / 2);
285 set_mouse_cursor(0);
286 } else {
287 set_mouse_cursor(1);
288 }
289 break;
291 case 'b':
292 case 'B':
293 freelook = !freelook;
294 break;
296 case 'r':
297 case 'R':
298 game_won = 0;
299 player_init(&player, &level);
300 break;
302 default:
303 break;
304 }
305 }
307 #define MAX_TOUCH_IDS 16
308 static struct {
309 int bnstate[8];
310 int prev_x, prev_y;
311 } mstate[MAX_TOUCH_IDS];
313 void game_mouse_button(int id, int bn, int pressed, int x, int y)
314 {
315 if(id >= MAX_TOUCH_IDS) return;
317 mstate[id].prev_x = x;
318 mstate[id].prev_y = y;
319 mstate[id].bnstate[bn] = pressed;
320 }
322 void game_mouse_motion(int id, int x, int y)
323 {
324 int dx, dy, cx, cy;
326 if(id >= MAX_TOUCH_IDS) return;
328 cx = win_width / 2;
329 cy = win_height / 2;
331 if(warping_mouse) {
332 warping_mouse = 0;
333 return;
334 }
336 dx = x - (mouselook ? cx : mstate[id].prev_x);
337 dy = y - (mouselook ? cy : mstate[id].prev_y);
338 mstate[id].prev_x = x;
339 mstate[id].prev_y = y;
341 if(!dx && !dy) return;
343 if(mouselook || mstate[id].bnstate[0]) {
344 player_turn(&player, dx * 0.5, dy * 0.5);
345 }
346 if(mstate[id].bnstate[2]) {
347 dbg_cam_dist += 0.1 * dy;
348 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
349 }
351 if(mouselook) {
352 warping_mouse = 1;
353 set_mouse_pos(cx, cy);
354 mstate[id].prev_x = cx;
355 mstate[id].prev_y = cy;
356 }
357 }