dungeon_crawler

view prototype/src/main.cc @ 25:527fede30057

- fixed sphere rendering (PointLight::draw) - added config argument options - fixed macosx compilation
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 02:16:08 +0300
parents e122ba214ee1
children 21999ef6636b
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 // load a tileset
90 tileset = new TileSet;
91 printf("loading tileset: %s\n", cfg.tileset_file);
92 if(!tileset->load(datafile_path(cfg.tileset_file))) {
93 return false;
94 }
95 set_active_tileset(tileset);
97 level = new Level;
98 printf("loading level: %s\n", cfg.level_file);
99 if(!level->load(datafile_path(cfg.level_file))) {
100 return false;
101 }
103 cam.input_move(0, 0.5, 0);
105 return true;
106 }
108 void cleanup()
109 {
110 delete level;
111 delete tileset;
113 destroy_renderer();
114 }
116 void idle()
117 {
118 glutPostRedisplay();
119 }
121 void disp()
122 {
123 update(glutGet(GLUT_ELAPSED_TIME));
125 if(cfg.stereo) {
126 glDrawBuffer(GL_BACK_LEFT);
128 glMatrixMode(GL_PROJECTION);
129 glLoadIdentity();
130 proj_matrix(-1);
131 glMatrixMode(GL_MODELVIEW);
132 glLoadIdentity();
133 view_matrix(-1);
135 draw();
137 glDrawBuffer(GL_BACK_RIGHT);
139 glMatrixMode(GL_PROJECTION);
140 glLoadIdentity();
141 proj_matrix(1);
142 glMatrixMode(GL_MODELVIEW);
143 glLoadIdentity();
144 view_matrix(1);
146 draw();
147 } else {
148 glMatrixMode(GL_PROJECTION);
149 glLoadIdentity();
150 proj_matrix(0);
151 glMatrixMode(GL_MODELVIEW);
152 glLoadIdentity();
153 view_matrix(0);
155 draw();
156 }
158 glutSwapBuffers();
159 assert(glGetError() == GL_NO_ERROR);
161 usleep(10000);
162 }
164 void draw()
165 {
166 render_deferred(level);
168 if(show_con) {
169 draw_cmdcon();
170 }
171 }
173 void view_matrix(int eye)
174 {
175 float offs = stereo_eye_sep * eye * 0.5;
176 glTranslatef(-offs, 0, 0);
177 cam.use_inverse();
178 }
180 void proj_matrix(int eye)
181 {
182 static const float fov = M_PI / 4.0;
183 static const float near_clip = 0.1;
184 static const float far_clip = 100.0;
186 float top = near_clip * tan(fov * 0.5);
187 float right = top * (float)cfg.width / (float)cfg.height;
189 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
190 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
191 }
194 void update(unsigned long msec)
195 {
196 static unsigned long last_upd;
198 if(last_upd == 0) {
199 last_upd = msec;
200 }
201 float dt = (float)(msec - last_upd) / 1000.0;
203 float offs = 2.5 * dt;
204 float dx = 0, dy = 0;
206 // handle key input
207 if(keystate['w'] || keystate['W']) {
208 dy -= offs;
209 }
210 if(keystate['s'] || keystate['S']) {
211 dy += offs;
212 }
213 if(keystate['d'] || keystate['D']) {
214 dx += offs;
215 }
216 if(keystate['a'] || keystate['A']) {
217 dx -= offs;
218 }
220 cam.input_move(dx, 0, dy);
222 last_upd = msec;
223 }
225 void reshape(int x, int y)
226 {
227 glViewport(0, 0, x, y);
228 cfg.width = x;
229 cfg.height = y;
230 }
232 static bool stereo_shift_pressed;
234 void keyb(unsigned char key, int x, int y)
235 {
236 switch(key) {
237 case 27:
238 exit(0);
240 case '`':
241 show_con = true;
242 glutKeyboardFunc(keyb_con);
243 glutKeyboardUpFunc(0);
244 glutPostRedisplay();
245 break;
247 case 'z':
248 stereo_shift_pressed = true;
249 break;
251 case '\n':
252 case '\r':
253 {
254 static bool fullscr;
255 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
256 fullscr = !fullscr;
257 if(fullscr) {
258 glutFullScreen();
259 } else {
260 glutPositionWindow(20, 20);
261 }
262 }
263 }
264 break;
266 default:
267 break;
268 }
270 keystate[key] = true;
271 }
273 void key_release(unsigned char key, int x, int y)
274 {
275 switch(key) {
276 case 'z':
277 stereo_shift_pressed = false;
278 break;
280 default:
281 break;
282 }
284 keystate[key] = false;
285 }
287 void keyb_con(unsigned char key, int x, int y)
288 {
289 if(key == '`' || key == 27) {
290 show_con = false;
291 glutKeyboardFunc(keyb);
292 glutKeyboardUpFunc(key_release);
293 glutPostRedisplay();
294 } else {
295 cmdcon_keypress(key);
296 }
297 }
299 static int prev_x, prev_y;
300 static bool bnstate[32];
302 void mouse(int bn, int state, int x, int y)
303 {
304 prev_x = x;
305 prev_y = y;
306 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
307 }
309 void motion(int x, int y)
310 {
311 int dx = x - prev_x;
312 int dy = y - prev_y;
313 prev_x = x;
314 prev_y = y;
316 if(stereo_shift_pressed) {
317 if(dy != 0) {
318 stereo_focus_dist += dy * 0.01;
319 stereo_eye_sep = stereo_focus_dist / 30.0;
320 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
321 glutPostRedisplay();
322 }
323 return;
324 }
326 if(bnstate[0]) {
327 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
328 glutPostRedisplay();
329 }
330 if(bnstate[2]) {
331 cam.input_zoom(dy * 0.1);
332 glutPostRedisplay();
333 }
334 }