dungeon_crawler

view prototype/src/main.cc @ 43:3fef65352b0b

- added mipmap generation
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 30 Aug 2012 05:58:09 +0300
parents acfe0c0110fc
children f3030df27110
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 "renderer_deferred.h"
12 #include "cmdcon.h"
13 #include "cfg.h"
14 #include "timer.h"
16 bool init(int xsz, int ysz);
17 void cleanup();
18 void idle();
19 void disp();
20 void draw();
21 void view_matrix(int eye);
22 void proj_matrix(int eye);
23 void update(unsigned long msec);
24 void reshape(int x, int y);
25 void keyb(unsigned char key, int x, int y);
26 void key_release(unsigned char key, int x, int y);
27 void keyb_con(unsigned char key, int x, int y);
28 void mouse(int bn, int state, int x, int y);
29 void motion(int x, int y);
31 static TileSet *tileset;
32 static Level *level;
34 static FpsCamera cam;
35 static bool keystate[256];
37 static float stereo_focus_dist = 0.25;
38 static float stereo_eye_sep = stereo_focus_dist / 30.0;
40 static bool show_con;
42 int main(int argc, char **argv)
43 {
44 glutInit(&argc, argv);
46 if(!cfg.parse_args(argc, argv)) {
47 return 1;
48 }
50 glutInitWindowSize(cfg.width, cfg.height);
51 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
52 glutCreateWindow("dungeon crawler prototype");
54 glutIdleFunc(idle);
55 glutDisplayFunc(disp);
56 glutReshapeFunc(reshape);
57 glutKeyboardFunc(keyb);
58 glutKeyboardUpFunc(key_release);
59 glutMouseFunc(mouse);
60 glutMotionFunc(motion);
62 glewInit();
64 if(!init(cfg.width, cfg.height)) {
65 return 1;
66 }
67 atexit(cleanup);
69 glutMainLoop();
70 }
72 bool init(int xsz, int ysz)
73 {
74 // backup light for the forward crappy renderer
75 glEnable(GL_LIGHTING);
76 glEnable(GL_LIGHT0);
78 float ldir[] = {0, 0, -0.5, 1};
79 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
80 float lcol[] = {1, 1, 1, 1};
81 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
82 glLightfv(GL_LIGHT0, GL_SPECULAR, lcol);
84 glEnable(GL_DEPTH_TEST);
85 glEnable(GL_CULL_FACE);
86 glEnable(GL_MULTISAMPLE);
88 add_data_path(".");
89 add_data_path("data");
90 add_data_path("sdr");
92 rend = new DeferredRenderer();
93 if(!cfg.use_deferred || !rend->init(xsz, ysz)) {
94 printf("falling back to crappy renderer...\n");
96 rend = new FwdRenderer();
97 if(!rend->init(xsz, ysz)) {
98 fprintf(stderr, "failed to create renderer\n");
99 return false;
100 }
101 }
103 if(!init_cmdcon()) {
104 return false;
105 }
107 // load a tileset
108 tileset = new TileSet;
109 printf("loading tileset: %s\n", cfg.tileset_file);
110 if(!tileset->load(datafile_path(cfg.tileset_file))) {
111 return false;
112 }
113 set_active_tileset(tileset);
115 level = new Level;
116 printf("loading level: %s\n", cfg.level_file);
117 if(!level->load(datafile_path(cfg.level_file))) {
118 return false;
119 }
121 cam.input_move(0, 0.5, 0);
123 return true;
124 }
126 void cleanup()
127 {
128 delete level;
129 delete tileset;
130 delete rend;
132 cleanup_cmdcon();
133 }
135 void idle()
136 {
137 glutPostRedisplay();
138 }
140 void disp()
141 {
142 update(get_time_msec());
144 if(cfg.stereo) {
145 glDrawBuffer(GL_BACK_LEFT);
147 glMatrixMode(GL_PROJECTION);
148 glLoadIdentity();
149 proj_matrix(-1);
150 glMatrixMode(GL_MODELVIEW);
151 glLoadIdentity();
152 view_matrix(-1);
154 draw();
156 glDrawBuffer(GL_BACK_RIGHT);
158 glMatrixMode(GL_PROJECTION);
159 glLoadIdentity();
160 proj_matrix(1);
161 glMatrixMode(GL_MODELVIEW);
162 glLoadIdentity();
163 view_matrix(1);
165 draw();
166 } else {
167 glMatrixMode(GL_PROJECTION);
168 glLoadIdentity();
169 proj_matrix(0);
170 glMatrixMode(GL_MODELVIEW);
171 glLoadIdentity();
172 view_matrix(0);
174 draw();
175 }
177 glutSwapBuffers();
178 assert(glGetError() == GL_NO_ERROR);
179 }
181 void draw()
182 {
183 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
185 rend->render(level);
187 if(show_con) {
188 draw_cmdcon();
189 }
190 }
192 void view_matrix(int eye)
193 {
194 float offs = stereo_eye_sep * eye * 0.5;
195 glTranslatef(-offs, 0, 0);
196 cam.use_inverse();
197 }
199 void proj_matrix(int eye)
200 {
201 static const float fov = M_PI / 4.0;
202 static const float near_clip = 0.1;
203 static const float far_clip = 100.0;
205 float top = near_clip * tan(fov * 0.5);
206 float right = top * (float)cfg.width / (float)cfg.height;
208 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
209 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
210 }
213 void update(unsigned long msec)
214 {
215 static unsigned long last_upd;
217 if(last_upd == 0) {
218 last_upd = msec;
219 }
220 float dt = (float)(msec - last_upd) / 1000.0;
222 float offs = 2.5 * dt;
223 float dx = 0, dy = 0;
225 // handle key input
226 if(keystate['w'] || keystate['W']) {
227 dy -= offs;
228 }
229 if(keystate['s'] || keystate['S']) {
230 dy += offs;
231 }
232 if(keystate['d'] || keystate['D']) {
233 dx += offs;
234 }
235 if(keystate['a'] || keystate['A']) {
236 dx -= offs;
237 }
239 cam.input_move(dx, 0, dy);
241 level->update(msec, dt);
243 last_upd = msec;
244 }
246 void reshape(int x, int y)
247 {
248 glViewport(0, 0, x, y);
249 cfg.width = x;
250 cfg.height = y;
252 rend->resize(x, y);
253 }
255 static bool stereo_shift_pressed;
257 void keyb(unsigned char key, int x, int y)
258 {
259 switch(key) {
260 case 27:
261 exit(0);
263 case '`':
264 show_con = true;
265 glutKeyboardFunc(keyb_con);
266 glutKeyboardUpFunc(0);
267 glutPostRedisplay();
268 break;
270 case 'z':
271 stereo_shift_pressed = true;
272 break;
274 case '\n':
275 case '\r':
276 {
277 static bool fullscr;
278 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
279 fullscr = !fullscr;
280 if(fullscr) {
281 glutFullScreen();
282 } else {
283 glutPositionWindow(20, 20);
284 }
285 }
286 }
287 break;
289 default:
290 break;
291 }
293 keystate[key] = true;
294 }
296 void key_release(unsigned char key, int x, int y)
297 {
298 switch(key) {
299 case 'z':
300 stereo_shift_pressed = false;
301 break;
303 default:
304 break;
305 }
307 keystate[key] = false;
308 }
310 void keyb_con(unsigned char key, int x, int y)
311 {
312 if(key == '`' || key == 27) {
313 show_con = false;
314 glutKeyboardFunc(keyb);
315 glutKeyboardUpFunc(key_release);
316 glutPostRedisplay();
317 } else {
318 cmdcon_keypress(key);
319 }
320 }
322 static int prev_x, prev_y;
323 static bool bnstate[32];
325 void mouse(int bn, int state, int x, int y)
326 {
327 prev_x = x;
328 prev_y = y;
329 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
330 }
332 void motion(int x, int y)
333 {
334 int dx = x - prev_x;
335 int dy = y - prev_y;
336 prev_x = x;
337 prev_y = y;
339 if(stereo_shift_pressed) {
340 if(dy != 0) {
341 stereo_focus_dist += dy * 0.01;
342 stereo_eye_sep = stereo_focus_dist / 30.0;
343 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
344 glutPostRedisplay();
345 }
346 return;
347 }
349 if(bnstate[0]) {
350 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
351 glutPostRedisplay();
352 }
353 if(bnstate[2]) {
354 cam.input_zoom(dy * 0.1);
355 glutPostRedisplay();
356 }
357 }