dungeon_crawler

view prototype/src/main.cc @ 38:862461b686f4

start work on particle systems
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 29 Aug 2012 03:22:36 +0300
parents 938a6a155c94
children 060a44040577
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 }
67 glutMainLoop();
68 }
70 bool init(int xsz, int ysz)
71 {
72 glEnable(GL_LIGHTING);
73 glEnable(GL_LIGHT0);
74 float ldir[] = {-1, 1, 2, 0};
75 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
76 glEnable(GL_NORMALIZE);
78 glEnable(GL_DEPTH_TEST);
79 glEnable(GL_CULL_FACE);
80 glEnable(GL_MULTISAMPLE);
82 add_data_path(".");
83 add_data_path("data");
84 add_data_path("sdr");
86 if(!init_renderer(xsz, ysz)) {
87 return false;
88 }
90 if(!init_cmdcon()) {
91 return false;
92 }
94 // load a tileset
95 tileset = new TileSet;
96 printf("loading tileset: %s\n", cfg.tileset_file);
97 if(!tileset->load(datafile_path(cfg.tileset_file))) {
98 return false;
99 }
100 set_active_tileset(tileset);
102 level = new Level;
103 printf("loading level: %s\n", cfg.level_file);
104 if(!level->load(datafile_path(cfg.level_file))) {
105 return false;
106 }
108 cam.input_move(0, 0.5, 0);
110 return true;
111 }
113 void cleanup()
114 {
115 delete level;
116 delete tileset;
118 destroy_renderer();
120 cleanup_cmdcon();
121 }
123 void idle()
124 {
125 glutPostRedisplay();
126 }
128 void disp()
129 {
130 update(get_time_msec());
132 if(cfg.stereo) {
133 glDrawBuffer(GL_BACK_LEFT);
135 glMatrixMode(GL_PROJECTION);
136 glLoadIdentity();
137 proj_matrix(-1);
138 glMatrixMode(GL_MODELVIEW);
139 glLoadIdentity();
140 view_matrix(-1);
142 draw();
144 glDrawBuffer(GL_BACK_RIGHT);
146 glMatrixMode(GL_PROJECTION);
147 glLoadIdentity();
148 proj_matrix(1);
149 glMatrixMode(GL_MODELVIEW);
150 glLoadIdentity();
151 view_matrix(1);
153 draw();
154 } else {
155 glMatrixMode(GL_PROJECTION);
156 glLoadIdentity();
157 proj_matrix(0);
158 glMatrixMode(GL_MODELVIEW);
159 glLoadIdentity();
160 view_matrix(0);
162 draw();
163 }
165 glutSwapBuffers();
166 assert(glGetError() == GL_NO_ERROR);
168 usleep(10000);
169 }
171 void draw()
172 {
173 glClear(GL_COLOR_BUFFER_BIT);
175 render_deferred(level);
177 if(show_con) {
178 draw_cmdcon();
179 }
180 }
182 void view_matrix(int eye)
183 {
184 float offs = stereo_eye_sep * eye * 0.5;
185 glTranslatef(-offs, 0, 0);
186 cam.use_inverse();
187 }
189 void proj_matrix(int eye)
190 {
191 static const float fov = M_PI / 4.0;
192 static const float near_clip = 0.1;
193 static const float far_clip = 100.0;
195 float top = near_clip * tan(fov * 0.5);
196 float right = top * (float)cfg.width / (float)cfg.height;
198 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
199 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
200 }
203 void update(unsigned long msec)
204 {
205 static unsigned long last_upd;
207 if(last_upd == 0) {
208 last_upd = msec;
209 }
210 float dt = (float)(msec - last_upd) / 1000.0;
212 float offs = 2.5 * dt;
213 float dx = 0, dy = 0;
215 // handle key input
216 if(keystate['w'] || keystate['W']) {
217 dy -= offs;
218 }
219 if(keystate['s'] || keystate['S']) {
220 dy += offs;
221 }
222 if(keystate['d'] || keystate['D']) {
223 dx += offs;
224 }
225 if(keystate['a'] || keystate['A']) {
226 dx -= offs;
227 }
229 cam.input_move(dx, 0, dy);
231 level->update(msec, dt);
233 last_upd = msec;
234 }
236 void reshape(int x, int y)
237 {
238 glViewport(0, 0, x, y);
239 cfg.width = x;
240 cfg.height = y;
242 resize_renderer(x, y);
243 }
245 static bool stereo_shift_pressed;
247 void keyb(unsigned char key, int x, int y)
248 {
249 switch(key) {
250 case 27:
251 exit(0);
253 case '`':
254 show_con = true;
255 glutKeyboardFunc(keyb_con);
256 glutKeyboardUpFunc(0);
257 glutPostRedisplay();
258 break;
260 case 'z':
261 stereo_shift_pressed = true;
262 break;
264 case '\n':
265 case '\r':
266 {
267 static bool fullscr;
268 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
269 fullscr = !fullscr;
270 if(fullscr) {
271 glutFullScreen();
272 } else {
273 glutPositionWindow(20, 20);
274 }
275 }
276 }
277 break;
279 default:
280 break;
281 }
283 keystate[key] = true;
284 }
286 void key_release(unsigned char key, int x, int y)
287 {
288 switch(key) {
289 case 'z':
290 stereo_shift_pressed = false;
291 break;
293 default:
294 break;
295 }
297 keystate[key] = false;
298 }
300 void keyb_con(unsigned char key, int x, int y)
301 {
302 if(key == '`' || key == 27) {
303 show_con = false;
304 glutKeyboardFunc(keyb);
305 glutKeyboardUpFunc(key_release);
306 glutPostRedisplay();
307 } else {
308 cmdcon_keypress(key);
309 }
310 }
312 static int prev_x, prev_y;
313 static bool bnstate[32];
315 void mouse(int bn, int state, int x, int y)
316 {
317 prev_x = x;
318 prev_y = y;
319 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
320 }
322 void motion(int x, int y)
323 {
324 int dx = x - prev_x;
325 int dy = y - prev_y;
326 prev_x = x;
327 prev_y = y;
329 if(stereo_shift_pressed) {
330 if(dy != 0) {
331 stereo_focus_dist += dy * 0.01;
332 stereo_eye_sep = stereo_focus_dist / 30.0;
333 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
334 glutPostRedisplay();
335 }
336 return;
337 }
339 if(bnstate[0]) {
340 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
341 glutPostRedisplay();
342 }
343 if(bnstate[2]) {
344 cam.input_zoom(dy * 0.1);
345 glutPostRedisplay();
346 }
347 }