dungeon_crawler

view prototype/src/main.cc @ 52:bcdea26c8f27

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Sep 2012 18:34:28 +0300
parents d57df51f6b50
children 4c427e28ca00
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <unistd.h>
5 #include "opengl.h"
6 #include "psys/psys.h"
7 #include "level.h"
8 #include "texture.h"
9 #include "camera.h"
10 #include "datapath.h"
11 #include "tileset.h"
12 #include "renderer.h"
13 #include "renderer_deferred.h"
14 #include "cmdcon.h"
15 #include "cfg.h"
16 #include "timer.h"
17 #include "audio/audio.h"
18 #include "audio/source.h"
20 bool init(int xsz, int ysz);
21 void cleanup();
22 void idle();
23 void disp();
24 void draw();
25 void view_matrix(int eye);
26 void proj_matrix(int eye);
27 void update(unsigned long msec);
28 void reshape(int x, int y);
29 void keyb(unsigned char key, int x, int y);
30 void key_release(unsigned char key, int x, int y);
31 void keyb_con(unsigned char key, int x, int y);
32 void mouse(int bn, int state, int x, int y);
33 void motion(int x, int y);
34 unsigned int load_psys_tex(const char *fname, void *cls);
36 static TileSet *tileset;
37 static Level *level;
39 static FpsCamera cam;
40 static bool keystate[256];
42 static float stereo_focus_dist = 0.25;
43 static float stereo_eye_sep = stereo_focus_dist / 30.0;
45 static bool show_con;
47 static AudioSource *move_sound;
49 int main(int argc, char **argv)
50 {
51 glutInit(&argc, argv);
53 if(!cfg.parse_args(argc, argv)) {
54 return 1;
55 }
57 glutInitWindowSize(cfg.width, cfg.height);
58 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
59 glutCreateWindow("dungeon crawler prototype");
61 glutIdleFunc(idle);
62 glutDisplayFunc(disp);
63 glutReshapeFunc(reshape);
64 glutKeyboardFunc(keyb);
65 glutKeyboardUpFunc(key_release);
66 glutMouseFunc(mouse);
67 glutMotionFunc(motion);
69 glewInit();
71 if(!init(cfg.width, cfg.height)) {
72 return 1;
73 }
74 atexit(cleanup);
76 glutMainLoop();
77 }
79 bool init(int xsz, int ysz)
80 {
81 // backup light for the forward crappy renderer
82 glEnable(GL_LIGHTING);
83 glEnable(GL_LIGHT0);
85 float ldir[] = {0, 0, -0.5, 1};
86 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
87 float lcol[] = {1, 1, 1, 1};
88 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
89 glLightfv(GL_LIGHT0, GL_SPECULAR, lcol);
91 glEnable(GL_DEPTH_TEST);
92 glEnable(GL_CULL_FACE);
93 glEnable(GL_MULTISAMPLE);
95 add_data_path(".");
96 add_data_path("data");
97 add_data_path("data/audio");
98 add_data_path("sdr");
100 if(cfg.sound && !init_audio()) {
101 fprintf(stderr, "failed to initialize audio, continuing silently\n");
102 cfg.sound = false;
103 }
104 if(cfg.sound) {
105 move_sound = new AudioSource;
106 move_sound->set_volume(0.4);
107 }
109 rend = new DeferredRenderer();
110 if(!cfg.use_deferred || !rend->init(xsz, ysz)) {
111 printf("falling back to crappy renderer...\n");
113 rend = new FwdRenderer();
114 if(!rend->init(xsz, ysz)) {
115 fprintf(stderr, "failed to create renderer\n");
116 return false;
117 }
118 }
120 if(!init_cmdcon()) {
121 return false;
122 }
124 psys_texture_loader(load_psys_tex, 0, 0);
126 // load a tileset
127 tileset = new TileSet;
128 printf("loading tileset: %s\n", cfg.tileset_file);
129 if(!tileset->load(datafile_path(cfg.tileset_file))) {
130 return false;
131 }
132 set_active_tileset(tileset);
134 level = new Level;
135 printf("loading level: %s\n", cfg.level_file);
136 if(!level->load(datafile_path(cfg.level_file))) {
137 return false;
138 }
140 cam.input_move(0, 0.5, 0);
142 return true;
143 }
145 void cleanup()
146 {
147 delete level;
148 delete tileset;
149 delete rend;
151 cleanup_cmdcon();
153 if(cfg.sound) {
154 delete move_sound;
155 destroy_audio();
156 }
157 }
159 void idle()
160 {
161 glutPostRedisplay();
162 }
164 void disp()
165 {
166 update(get_time_msec());
168 if(cfg.stereo) {
169 glDrawBuffer(GL_BACK_LEFT);
171 glMatrixMode(GL_PROJECTION);
172 glLoadIdentity();
173 proj_matrix(-1);
174 glMatrixMode(GL_MODELVIEW);
175 glLoadIdentity();
176 view_matrix(-1);
178 draw();
180 glDrawBuffer(GL_BACK_RIGHT);
182 glMatrixMode(GL_PROJECTION);
183 glLoadIdentity();
184 proj_matrix(1);
185 glMatrixMode(GL_MODELVIEW);
186 glLoadIdentity();
187 view_matrix(1);
189 draw();
190 } else {
191 glMatrixMode(GL_PROJECTION);
192 glLoadIdentity();
193 proj_matrix(0);
194 glMatrixMode(GL_MODELVIEW);
195 glLoadIdentity();
196 view_matrix(0);
198 draw();
199 }
201 glutSwapBuffers();
202 assert(glGetError() == GL_NO_ERROR);
203 }
205 void draw()
206 {
207 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
209 rend->render(level);
211 if(show_con) {
212 draw_cmdcon();
213 }
214 }
216 void view_matrix(int eye)
217 {
218 float offs = stereo_eye_sep * eye * 0.5;
219 glTranslatef(-offs, 0, 0);
220 cam.use_inverse();
221 }
223 void proj_matrix(int eye)
224 {
225 static const float fov = M_PI / 4.0;
226 static const float near_clip = 0.1;
227 static const float far_clip = 100.0;
229 float top = near_clip * tan(fov * 0.5);
230 float right = top * (float)cfg.width / (float)cfg.height;
232 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
233 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
234 }
237 void update(unsigned long msec)
238 {
239 static unsigned long last_upd;
241 if(last_upd == 0) {
242 last_upd = msec;
243 }
244 float dt = (float)(msec - last_upd) / 1000.0;
246 float offs = 2.5 * dt;
247 float dx = 0, dy = 0;
249 // handle key input
250 bool did_move = false;
251 if(keystate['w'] || keystate['W']) {
252 dy -= offs;
253 did_move = true;
254 }
255 if(keystate['s'] || keystate['S']) {
256 dy += offs;
257 did_move = true;
258 }
259 if(keystate['d'] || keystate['D']) {
260 dx += offs;
261 did_move = true;
262 }
263 if(keystate['a'] || keystate['A']) {
264 dx -= offs;
265 did_move = true;
266 }
268 cam.input_move(dx, 0, dy);
270 tileset->update_tiles(msec);
272 level->set_player_position(cam.get_position());
273 level->update(msec, dt);
275 if(cfg.sound) {
276 // set the listener matrix
277 set_audio_listener(cam.matrix());
279 // play the walking sound if we're walking
280 int cellx, celly;
281 level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
283 const AudioSample *move_sample;
284 if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
285 if(move_sample != move_sound->get_sample()) {
286 move_sound->stop();
287 move_sound->set_sample(move_sample);
288 move_sound->play();
289 }
290 } else {
291 if(move_sound->get_sample()) {
292 move_sound->stop();
293 move_sound->set_sample(0);
294 }
295 }
296 }
298 last_upd = msec;
299 }
301 void reshape(int x, int y)
302 {
303 glViewport(0, 0, x, y);
304 cfg.width = x;
305 cfg.height = y;
307 rend->resize(x, y);
308 }
310 static bool stereo_shift_pressed;
312 void keyb(unsigned char key, int x, int y)
313 {
314 switch(key) {
315 case 27:
316 exit(0);
318 case '`':
319 show_con = true;
320 glutKeyboardFunc(keyb_con);
321 glutKeyboardUpFunc(0);
322 glutPostRedisplay();
323 break;
325 case 'z':
326 stereo_shift_pressed = true;
327 break;
329 case 'p':
330 {
331 Vector3 pos = cam.get_position();
332 int cell_x, cell_y;
333 level->get_cell_coords_at(pos, &cell_x, &cell_y);
334 printf("Current position: [%.2f %.2f %.2f] cell: [%d %d]\n", pos.x, pos.y, pos.z,
335 cell_x, cell_y);
336 }
337 break;
339 case 'P':
340 {
341 Vector3 pos = cam.get_position();
342 int cell_x, cell_y;
343 level->get_cell_coords_at(pos, &cell_x, &cell_y);
344 AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
345 printf("walk sample: %p\n", (void*)sample);
346 }
347 break;
350 case '\n':
351 case '\r':
352 {
353 static bool fullscr;
354 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
355 fullscr = !fullscr;
356 if(fullscr) {
357 glutFullScreen();
358 } else {
359 glutPositionWindow(20, 20);
360 }
361 }
362 }
363 break;
365 default:
366 break;
367 }
369 keystate[key] = true;
370 }
372 void key_release(unsigned char key, int x, int y)
373 {
374 switch(key) {
375 case 'z':
376 stereo_shift_pressed = false;
377 break;
379 default:
380 break;
381 }
383 keystate[key] = false;
384 }
386 void keyb_con(unsigned char key, int x, int y)
387 {
388 if(key == '`' || key == 27) {
389 show_con = false;
390 glutKeyboardFunc(keyb);
391 glutKeyboardUpFunc(key_release);
392 glutPostRedisplay();
393 } else {
394 cmdcon_keypress(key);
395 }
396 }
398 static int prev_x, prev_y;
399 static bool bnstate[32];
401 void mouse(int bn, int state, int x, int y)
402 {
403 prev_x = x;
404 prev_y = y;
405 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
406 }
408 void motion(int x, int y)
409 {
410 int dx = x - prev_x;
411 int dy = y - prev_y;
412 prev_x = x;
413 prev_y = y;
415 if(stereo_shift_pressed) {
416 if(dy != 0) {
417 stereo_focus_dist += dy * 0.01;
418 stereo_eye_sep = stereo_focus_dist / 30.0;
419 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
420 glutPostRedisplay();
421 }
422 return;
423 }
425 if(bnstate[0]) {
426 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
427 glutPostRedisplay();
428 }
429 if(bnstate[2]) {
430 cam.input_zoom(dy * 0.1);
431 glutPostRedisplay();
432 }
433 }
435 unsigned int load_psys_tex(const char *fname, void *cls)
436 {
437 TextureSet *texset = tileset->get_textures();
438 return texset->get(fname);
439 }