dungeon_crawler

view prototype/src/main.cc @ 46:f3030df27110

debugging the particles
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 13 Sep 2012 06:33:51 +0300
parents 6d71dd4760f9
children d52711f2b9a1
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 "texman.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"
18 bool init(int xsz, int ysz);
19 void cleanup();
20 void idle();
21 void disp();
22 void draw();
23 void view_matrix(int eye);
24 void proj_matrix(int eye);
25 void update(unsigned long msec);
26 void reshape(int x, int y);
27 void keyb(unsigned char key, int x, int y);
28 void key_release(unsigned char key, int x, int y);
29 void keyb_con(unsigned char key, int x, int y);
30 void mouse(int bn, int state, int x, int y);
31 void motion(int x, int y);
32 unsigned int load_psys_tex(const char *fname, void *cls);
34 static TileSet *tileset;
35 static Level *level;
37 static FpsCamera cam;
38 static bool keystate[256];
40 static float stereo_focus_dist = 0.25;
41 static float stereo_eye_sep = stereo_focus_dist / 30.0;
43 static bool show_con;
45 int main(int argc, char **argv)
46 {
47 glutInit(&argc, argv);
49 if(!cfg.parse_args(argc, argv)) {
50 return 1;
51 }
53 glutInitWindowSize(cfg.width, cfg.height);
54 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
55 glutCreateWindow("dungeon crawler prototype");
57 glutIdleFunc(idle);
58 glutDisplayFunc(disp);
59 glutReshapeFunc(reshape);
60 glutKeyboardFunc(keyb);
61 glutKeyboardUpFunc(key_release);
62 glutMouseFunc(mouse);
63 glutMotionFunc(motion);
65 glewInit();
67 if(!init(cfg.width, cfg.height)) {
68 return 1;
69 }
70 atexit(cleanup);
72 glutMainLoop();
73 }
75 bool init(int xsz, int ysz)
76 {
77 // backup light for the forward crappy renderer
78 glEnable(GL_LIGHTING);
79 glEnable(GL_LIGHT0);
81 float ldir[] = {0, 0, -0.5, 1};
82 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
83 float lcol[] = {1, 1, 1, 1};
84 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
85 glLightfv(GL_LIGHT0, GL_SPECULAR, lcol);
87 glEnable(GL_DEPTH_TEST);
88 glEnable(GL_CULL_FACE);
89 glEnable(GL_MULTISAMPLE);
91 add_data_path(".");
92 add_data_path("data");
93 add_data_path("sdr");
95 rend = new DeferredRenderer();
96 if(!cfg.use_deferred || !rend->init(xsz, ysz)) {
97 printf("falling back to crappy renderer...\n");
99 rend = new FwdRenderer();
100 if(!rend->init(xsz, ysz)) {
101 fprintf(stderr, "failed to create renderer\n");
102 return false;
103 }
104 }
106 if(!init_cmdcon()) {
107 return false;
108 }
110 psys_texture_loader(load_psys_tex, 0, 0);
112 // load a tileset
113 tileset = new TileSet;
114 printf("loading tileset: %s\n", cfg.tileset_file);
115 if(!tileset->load(datafile_path(cfg.tileset_file))) {
116 return false;
117 }
118 set_active_tileset(tileset);
120 level = new Level;
121 printf("loading level: %s\n", cfg.level_file);
122 if(!level->load(datafile_path(cfg.level_file))) {
123 return false;
124 }
126 cam.input_move(0, 0.5, 0);
128 return true;
129 }
131 void cleanup()
132 {
133 delete level;
134 delete tileset;
135 delete rend;
137 cleanup_cmdcon();
138 }
140 void idle()
141 {
142 glutPostRedisplay();
143 }
145 void disp()
146 {
147 update(get_time_msec());
149 if(cfg.stereo) {
150 glDrawBuffer(GL_BACK_LEFT);
152 glMatrixMode(GL_PROJECTION);
153 glLoadIdentity();
154 proj_matrix(-1);
155 glMatrixMode(GL_MODELVIEW);
156 glLoadIdentity();
157 view_matrix(-1);
159 draw();
161 glDrawBuffer(GL_BACK_RIGHT);
163 glMatrixMode(GL_PROJECTION);
164 glLoadIdentity();
165 proj_matrix(1);
166 glMatrixMode(GL_MODELVIEW);
167 glLoadIdentity();
168 view_matrix(1);
170 draw();
171 } else {
172 glMatrixMode(GL_PROJECTION);
173 glLoadIdentity();
174 proj_matrix(0);
175 glMatrixMode(GL_MODELVIEW);
176 glLoadIdentity();
177 view_matrix(0);
179 draw();
180 }
182 glutSwapBuffers();
183 assert(glGetError() == GL_NO_ERROR);
184 }
186 void draw()
187 {
188 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
190 rend->render(level);
192 if(show_con) {
193 draw_cmdcon();
194 }
195 }
197 void view_matrix(int eye)
198 {
199 float offs = stereo_eye_sep * eye * 0.5;
200 glTranslatef(-offs, 0, 0);
201 cam.use_inverse();
202 }
204 void proj_matrix(int eye)
205 {
206 static const float fov = M_PI / 4.0;
207 static const float near_clip = 0.1;
208 static const float far_clip = 100.0;
210 float top = near_clip * tan(fov * 0.5);
211 float right = top * (float)cfg.width / (float)cfg.height;
213 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
214 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
215 }
218 void update(unsigned long msec)
219 {
220 static unsigned long last_upd;
222 if(last_upd == 0) {
223 last_upd = msec;
224 }
225 float dt = (float)(msec - last_upd) / 1000.0;
227 float offs = 2.5 * dt;
228 float dx = 0, dy = 0;
230 // handle key input
231 if(keystate['w'] || keystate['W']) {
232 dy -= offs;
233 }
234 if(keystate['s'] || keystate['S']) {
235 dy += offs;
236 }
237 if(keystate['d'] || keystate['D']) {
238 dx += offs;
239 }
240 if(keystate['a'] || keystate['A']) {
241 dx -= offs;
242 }
244 cam.input_move(dx, 0, dy);
246 tileset->update_tiles(msec);
247 level->update(msec, dt);
249 last_upd = msec;
250 }
252 void reshape(int x, int y)
253 {
254 glViewport(0, 0, x, y);
255 cfg.width = x;
256 cfg.height = y;
258 rend->resize(x, y);
259 }
261 static bool stereo_shift_pressed;
263 void keyb(unsigned char key, int x, int y)
264 {
265 switch(key) {
266 case 27:
267 exit(0);
269 case '`':
270 show_con = true;
271 glutKeyboardFunc(keyb_con);
272 glutKeyboardUpFunc(0);
273 glutPostRedisplay();
274 break;
276 case 'z':
277 stereo_shift_pressed = true;
278 break;
280 case '\n':
281 case '\r':
282 {
283 static bool fullscr;
284 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
285 fullscr = !fullscr;
286 if(fullscr) {
287 glutFullScreen();
288 } else {
289 glutPositionWindow(20, 20);
290 }
291 }
292 }
293 break;
295 default:
296 break;
297 }
299 keystate[key] = true;
300 }
302 void key_release(unsigned char key, int x, int y)
303 {
304 switch(key) {
305 case 'z':
306 stereo_shift_pressed = false;
307 break;
309 default:
310 break;
311 }
313 keystate[key] = false;
314 }
316 void keyb_con(unsigned char key, int x, int y)
317 {
318 if(key == '`' || key == 27) {
319 show_con = false;
320 glutKeyboardFunc(keyb);
321 glutKeyboardUpFunc(key_release);
322 glutPostRedisplay();
323 } else {
324 cmdcon_keypress(key);
325 }
326 }
328 static int prev_x, prev_y;
329 static bool bnstate[32];
331 void mouse(int bn, int state, int x, int y)
332 {
333 prev_x = x;
334 prev_y = y;
335 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
336 }
338 void motion(int x, int y)
339 {
340 int dx = x - prev_x;
341 int dy = y - prev_y;
342 prev_x = x;
343 prev_y = y;
345 if(stereo_shift_pressed) {
346 if(dy != 0) {
347 stereo_focus_dist += dy * 0.01;
348 stereo_eye_sep = stereo_focus_dist / 30.0;
349 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
350 glutPostRedisplay();
351 }
352 return;
353 }
355 if(bnstate[0]) {
356 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
357 glutPostRedisplay();
358 }
359 if(bnstate[2]) {
360 cam.input_zoom(dy * 0.1);
361 glutPostRedisplay();
362 }
363 }
365 unsigned int load_psys_tex(const char *fname, void *cls)
366 {
367 TextureSet *texset = tileset->get_textures();
368 return texset->get_texture(fname);
369 }