dungeon_crawler

view prototype/src/main.cc @ 39:060a44040577

forgot to set it up so cleanup is called on exit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 30 Aug 2012 01:15:23 +0300
parents 862461b686f4
children acfe0c0110fc
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <unistd.h>
5 #include "opengl.h"
6 #include "level.h"
7 #include "camera.h"
8 #include "datapath.h"
9 #include "tileset.h"
10 #include "renderer.h"
11 #include "cmdcon.h"
12 #include "cfg.h"
13 #include "timer.h"
15 bool init(int xsz, int ysz);
16 void cleanup();
17 void idle();
18 void disp();
19 void draw();
20 void view_matrix(int eye);
21 void proj_matrix(int eye);
22 void update(unsigned long msec);
23 void reshape(int x, int y);
24 void keyb(unsigned char key, int x, int y);
25 void key_release(unsigned char key, int x, int y);
26 void keyb_con(unsigned char key, int x, int y);
27 void mouse(int bn, int state, int x, int y);
28 void motion(int x, int y);
30 static TileSet *tileset;
31 static Level *level;
33 static FpsCamera cam;
34 static bool keystate[256];
36 static float stereo_focus_dist = 0.25;
37 static float stereo_eye_sep = stereo_focus_dist / 30.0;
39 static bool show_con;
41 int main(int argc, char **argv)
42 {
43 glutInit(&argc, argv);
45 if(!cfg.parse_args(argc, argv)) {
46 return 1;
47 }
49 glutInitWindowSize(cfg.width, cfg.height);
50 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
51 glutCreateWindow("dungeon crawler prototype");
53 glutIdleFunc(idle);
54 glutDisplayFunc(disp);
55 glutReshapeFunc(reshape);
56 glutKeyboardFunc(keyb);
57 glutKeyboardUpFunc(key_release);
58 glutMouseFunc(mouse);
59 glutMotionFunc(motion);
61 glewInit();
63 if(!init(cfg.width, cfg.height)) {
64 return 1;
65 }
66 atexit(cleanup);
68 glutMainLoop();
69 }
71 bool init(int xsz, int ysz)
72 {
73 glEnable(GL_LIGHTING);
74 glEnable(GL_LIGHT0);
75 float ldir[] = {-1, 1, 2, 0};
76 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
77 glEnable(GL_NORMALIZE);
79 glEnable(GL_DEPTH_TEST);
80 glEnable(GL_CULL_FACE);
81 glEnable(GL_MULTISAMPLE);
83 add_data_path(".");
84 add_data_path("data");
85 add_data_path("sdr");
87 if(!init_renderer(xsz, ysz)) {
88 return false;
89 }
91 if(!init_cmdcon()) {
92 return false;
93 }
95 // load a tileset
96 tileset = new TileSet;
97 printf("loading tileset: %s\n", cfg.tileset_file);
98 if(!tileset->load(datafile_path(cfg.tileset_file))) {
99 return false;
100 }
101 set_active_tileset(tileset);
103 level = new Level;
104 printf("loading level: %s\n", cfg.level_file);
105 if(!level->load(datafile_path(cfg.level_file))) {
106 return false;
107 }
109 cam.input_move(0, 0.5, 0);
111 return true;
112 }
114 void cleanup()
115 {
116 delete level;
117 delete tileset;
119 destroy_renderer();
121 cleanup_cmdcon();
122 }
124 void idle()
125 {
126 glutPostRedisplay();
127 }
129 void disp()
130 {
131 update(get_time_msec());
133 if(cfg.stereo) {
134 glDrawBuffer(GL_BACK_LEFT);
136 glMatrixMode(GL_PROJECTION);
137 glLoadIdentity();
138 proj_matrix(-1);
139 glMatrixMode(GL_MODELVIEW);
140 glLoadIdentity();
141 view_matrix(-1);
143 draw();
145 glDrawBuffer(GL_BACK_RIGHT);
147 glMatrixMode(GL_PROJECTION);
148 glLoadIdentity();
149 proj_matrix(1);
150 glMatrixMode(GL_MODELVIEW);
151 glLoadIdentity();
152 view_matrix(1);
154 draw();
155 } else {
156 glMatrixMode(GL_PROJECTION);
157 glLoadIdentity();
158 proj_matrix(0);
159 glMatrixMode(GL_MODELVIEW);
160 glLoadIdentity();
161 view_matrix(0);
163 draw();
164 }
166 glutSwapBuffers();
167 assert(glGetError() == GL_NO_ERROR);
169 usleep(10000);
170 }
172 void draw()
173 {
174 glClear(GL_COLOR_BUFFER_BIT);
176 render_deferred(level);
178 if(show_con) {
179 draw_cmdcon();
180 }
181 }
183 void view_matrix(int eye)
184 {
185 float offs = stereo_eye_sep * eye * 0.5;
186 glTranslatef(-offs, 0, 0);
187 cam.use_inverse();
188 }
190 void proj_matrix(int eye)
191 {
192 static const float fov = M_PI / 4.0;
193 static const float near_clip = 0.1;
194 static const float far_clip = 100.0;
196 float top = near_clip * tan(fov * 0.5);
197 float right = top * (float)cfg.width / (float)cfg.height;
199 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
200 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
201 }
204 void update(unsigned long msec)
205 {
206 static unsigned long last_upd;
208 if(last_upd == 0) {
209 last_upd = msec;
210 }
211 float dt = (float)(msec - last_upd) / 1000.0;
213 float offs = 2.5 * dt;
214 float dx = 0, dy = 0;
216 // handle key input
217 if(keystate['w'] || keystate['W']) {
218 dy -= offs;
219 }
220 if(keystate['s'] || keystate['S']) {
221 dy += offs;
222 }
223 if(keystate['d'] || keystate['D']) {
224 dx += offs;
225 }
226 if(keystate['a'] || keystate['A']) {
227 dx -= offs;
228 }
230 cam.input_move(dx, 0, dy);
232 level->update(msec, dt);
234 last_upd = msec;
235 }
237 void reshape(int x, int y)
238 {
239 glViewport(0, 0, x, y);
240 cfg.width = x;
241 cfg.height = y;
243 resize_renderer(x, y);
244 }
246 static bool stereo_shift_pressed;
248 void keyb(unsigned char key, int x, int y)
249 {
250 switch(key) {
251 case 27:
252 exit(0);
254 case '`':
255 show_con = true;
256 glutKeyboardFunc(keyb_con);
257 glutKeyboardUpFunc(0);
258 glutPostRedisplay();
259 break;
261 case 'z':
262 stereo_shift_pressed = true;
263 break;
265 case '\n':
266 case '\r':
267 {
268 static bool fullscr;
269 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
270 fullscr = !fullscr;
271 if(fullscr) {
272 glutFullScreen();
273 } else {
274 glutPositionWindow(20, 20);
275 }
276 }
277 }
278 break;
280 default:
281 break;
282 }
284 keystate[key] = true;
285 }
287 void key_release(unsigned char key, int x, int y)
288 {
289 switch(key) {
290 case 'z':
291 stereo_shift_pressed = false;
292 break;
294 default:
295 break;
296 }
298 keystate[key] = false;
299 }
301 void keyb_con(unsigned char key, int x, int y)
302 {
303 if(key == '`' || key == 27) {
304 show_con = false;
305 glutKeyboardFunc(keyb);
306 glutKeyboardUpFunc(key_release);
307 glutPostRedisplay();
308 } else {
309 cmdcon_keypress(key);
310 }
311 }
313 static int prev_x, prev_y;
314 static bool bnstate[32];
316 void mouse(int bn, int state, int x, int y)
317 {
318 prev_x = x;
319 prev_y = y;
320 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
321 }
323 void motion(int x, int y)
324 {
325 int dx = x - prev_x;
326 int dy = y - prev_y;
327 prev_x = x;
328 prev_y = y;
330 if(stereo_shift_pressed) {
331 if(dy != 0) {
332 stereo_focus_dist += dy * 0.01;
333 stereo_eye_sep = stereo_focus_dist / 30.0;
334 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
335 glutPostRedisplay();
336 }
337 return;
338 }
340 if(bnstate[0]) {
341 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
342 glutPostRedisplay();
343 }
344 if(bnstate[2]) {
345 cam.input_zoom(dy * 0.1);
346 glutPostRedisplay();
347 }
348 }