dungeon_crawler

view prototype/src/main.cc @ 30:938a6a155c94

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 27 Aug 2012 04:03:22 +0300
parents 2fc004802739
children 862461b686f4
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"
14 bool init(int xsz, int ysz);
15 void cleanup();
16 void idle();
17 void disp();
18 void draw();
19 void view_matrix(int eye);
20 void proj_matrix(int eye);
21 void update(unsigned long msec);
22 void reshape(int x, int y);
23 void keyb(unsigned char key, int x, int y);
24 void key_release(unsigned char key, int x, int y);
25 void keyb_con(unsigned char key, int x, int y);
26 void mouse(int bn, int state, int x, int y);
27 void motion(int x, int y);
29 static TileSet *tileset;
30 static Level *level;
32 static FpsCamera cam;
33 static bool keystate[256];
35 static float stereo_focus_dist = 0.25;
36 static float stereo_eye_sep = stereo_focus_dist / 30.0;
38 static bool show_con;
40 int main(int argc, char **argv)
41 {
42 glutInit(&argc, argv);
44 if(!cfg.parse_args(argc, argv)) {
45 return 1;
46 }
48 glutInitWindowSize(cfg.width, cfg.height);
49 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
50 glutCreateWindow("dungeon crawler prototype");
52 glutIdleFunc(idle);
53 glutDisplayFunc(disp);
54 glutReshapeFunc(reshape);
55 glutKeyboardFunc(keyb);
56 glutKeyboardUpFunc(key_release);
57 glutMouseFunc(mouse);
58 glutMotionFunc(motion);
60 glewInit();
62 if(!init(cfg.width, cfg.height)) {
63 return 1;
64 }
66 glutMainLoop();
67 }
69 bool init(int xsz, int ysz)
70 {
71 glEnable(GL_LIGHTING);
72 glEnable(GL_LIGHT0);
73 float ldir[] = {-1, 1, 2, 0};
74 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
75 glEnable(GL_NORMALIZE);
77 glEnable(GL_DEPTH_TEST);
78 glEnable(GL_CULL_FACE);
79 glEnable(GL_MULTISAMPLE);
81 add_data_path(".");
82 add_data_path("data");
83 add_data_path("sdr");
85 if(!init_renderer(xsz, ysz)) {
86 return false;
87 }
89 if(!init_cmdcon()) {
90 return false;
91 }
93 // load a tileset
94 tileset = new TileSet;
95 printf("loading tileset: %s\n", cfg.tileset_file);
96 if(!tileset->load(datafile_path(cfg.tileset_file))) {
97 return false;
98 }
99 set_active_tileset(tileset);
101 level = new Level;
102 printf("loading level: %s\n", cfg.level_file);
103 if(!level->load(datafile_path(cfg.level_file))) {
104 return false;
105 }
107 cam.input_move(0, 0.5, 0);
109 return true;
110 }
112 void cleanup()
113 {
114 delete level;
115 delete tileset;
117 destroy_renderer();
119 cleanup_cmdcon();
120 }
122 void idle()
123 {
124 glutPostRedisplay();
125 }
127 void disp()
128 {
129 update(glutGet(GLUT_ELAPSED_TIME));
131 if(cfg.stereo) {
132 glDrawBuffer(GL_BACK_LEFT);
134 glMatrixMode(GL_PROJECTION);
135 glLoadIdentity();
136 proj_matrix(-1);
137 glMatrixMode(GL_MODELVIEW);
138 glLoadIdentity();
139 view_matrix(-1);
141 draw();
143 glDrawBuffer(GL_BACK_RIGHT);
145 glMatrixMode(GL_PROJECTION);
146 glLoadIdentity();
147 proj_matrix(1);
148 glMatrixMode(GL_MODELVIEW);
149 glLoadIdentity();
150 view_matrix(1);
152 draw();
153 } else {
154 glMatrixMode(GL_PROJECTION);
155 glLoadIdentity();
156 proj_matrix(0);
157 glMatrixMode(GL_MODELVIEW);
158 glLoadIdentity();
159 view_matrix(0);
161 draw();
162 }
164 glutSwapBuffers();
165 assert(glGetError() == GL_NO_ERROR);
167 usleep(10000);
168 }
170 void draw()
171 {
172 glClear(GL_COLOR_BUFFER_BIT);
174 render_deferred(level);
176 if(show_con) {
177 draw_cmdcon();
178 }
179 }
181 void view_matrix(int eye)
182 {
183 float offs = stereo_eye_sep * eye * 0.5;
184 glTranslatef(-offs, 0, 0);
185 cam.use_inverse();
186 }
188 void proj_matrix(int eye)
189 {
190 static const float fov = M_PI / 4.0;
191 static const float near_clip = 0.1;
192 static const float far_clip = 100.0;
194 float top = near_clip * tan(fov * 0.5);
195 float right = top * (float)cfg.width / (float)cfg.height;
197 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
198 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
199 }
202 void update(unsigned long msec)
203 {
204 static unsigned long last_upd;
206 if(last_upd == 0) {
207 last_upd = msec;
208 }
209 float dt = (float)(msec - last_upd) / 1000.0;
211 float offs = 2.5 * dt;
212 float dx = 0, dy = 0;
214 // handle key input
215 if(keystate['w'] || keystate['W']) {
216 dy -= offs;
217 }
218 if(keystate['s'] || keystate['S']) {
219 dy += offs;
220 }
221 if(keystate['d'] || keystate['D']) {
222 dx += offs;
223 }
224 if(keystate['a'] || keystate['A']) {
225 dx -= offs;
226 }
228 cam.input_move(dx, 0, dy);
230 last_upd = msec;
231 }
233 void reshape(int x, int y)
234 {
235 glViewport(0, 0, x, y);
236 cfg.width = x;
237 cfg.height = y;
239 resize_renderer(x, y);
240 }
242 static bool stereo_shift_pressed;
244 void keyb(unsigned char key, int x, int y)
245 {
246 switch(key) {
247 case 27:
248 exit(0);
250 case '`':
251 show_con = true;
252 glutKeyboardFunc(keyb_con);
253 glutKeyboardUpFunc(0);
254 glutPostRedisplay();
255 break;
257 case 'z':
258 stereo_shift_pressed = true;
259 break;
261 case '\n':
262 case '\r':
263 {
264 static bool fullscr;
265 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
266 fullscr = !fullscr;
267 if(fullscr) {
268 glutFullScreen();
269 } else {
270 glutPositionWindow(20, 20);
271 }
272 }
273 }
274 break;
276 default:
277 break;
278 }
280 keystate[key] = true;
281 }
283 void key_release(unsigned char key, int x, int y)
284 {
285 switch(key) {
286 case 'z':
287 stereo_shift_pressed = false;
288 break;
290 default:
291 break;
292 }
294 keystate[key] = false;
295 }
297 void keyb_con(unsigned char key, int x, int y)
298 {
299 if(key == '`' || key == 27) {
300 show_con = false;
301 glutKeyboardFunc(keyb);
302 glutKeyboardUpFunc(key_release);
303 glutPostRedisplay();
304 } else {
305 cmdcon_keypress(key);
306 }
307 }
309 static int prev_x, prev_y;
310 static bool bnstate[32];
312 void mouse(int bn, int state, int x, int y)
313 {
314 prev_x = x;
315 prev_y = y;
316 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
317 }
319 void motion(int x, int y)
320 {
321 int dx = x - prev_x;
322 int dy = y - prev_y;
323 prev_x = x;
324 prev_y = y;
326 if(stereo_shift_pressed) {
327 if(dy != 0) {
328 stereo_focus_dist += dy * 0.01;
329 stereo_eye_sep = stereo_focus_dist / 30.0;
330 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
331 glutPostRedisplay();
332 }
333 return;
334 }
336 if(bnstate[0]) {
337 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
338 glutPostRedisplay();
339 }
340 if(bnstate[2]) {
341 cam.input_zoom(dy * 0.1);
342 glutPostRedisplay();
343 }
344 }