dungeon_crawler

view prototype/src/main.cc @ 26:21999ef6636b

lalala
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 04:07:51 +0300
parents 527fede30057
children 2fc004802739
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 //render_deferred(level);
174 if(show_con) {
175 draw_cmdcon();
176 }
177 }
179 void view_matrix(int eye)
180 {
181 float offs = stereo_eye_sep * eye * 0.5;
182 glTranslatef(-offs, 0, 0);
183 cam.use_inverse();
184 }
186 void proj_matrix(int eye)
187 {
188 static const float fov = M_PI / 4.0;
189 static const float near_clip = 0.1;
190 static const float far_clip = 100.0;
192 float top = near_clip * tan(fov * 0.5);
193 float right = top * (float)cfg.width / (float)cfg.height;
195 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
196 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
197 }
200 void update(unsigned long msec)
201 {
202 static unsigned long last_upd;
204 if(last_upd == 0) {
205 last_upd = msec;
206 }
207 float dt = (float)(msec - last_upd) / 1000.0;
209 float offs = 2.5 * dt;
210 float dx = 0, dy = 0;
212 // handle key input
213 if(keystate['w'] || keystate['W']) {
214 dy -= offs;
215 }
216 if(keystate['s'] || keystate['S']) {
217 dy += offs;
218 }
219 if(keystate['d'] || keystate['D']) {
220 dx += offs;
221 }
222 if(keystate['a'] || keystate['A']) {
223 dx -= offs;
224 }
226 cam.input_move(dx, 0, dy);
228 last_upd = msec;
229 }
231 void reshape(int x, int y)
232 {
233 glViewport(0, 0, x, y);
234 cfg.width = x;
235 cfg.height = y;
236 }
238 static bool stereo_shift_pressed;
240 void keyb(unsigned char key, int x, int y)
241 {
242 switch(key) {
243 case 27:
244 exit(0);
246 case '`':
247 show_con = true;
248 glutKeyboardFunc(keyb_con);
249 glutKeyboardUpFunc(0);
250 glutPostRedisplay();
251 break;
253 case 'z':
254 stereo_shift_pressed = true;
255 break;
257 case '\n':
258 case '\r':
259 {
260 static bool fullscr;
261 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
262 fullscr = !fullscr;
263 if(fullscr) {
264 glutFullScreen();
265 } else {
266 glutPositionWindow(20, 20);
267 }
268 }
269 }
270 break;
272 default:
273 break;
274 }
276 keystate[key] = true;
277 }
279 void key_release(unsigned char key, int x, int y)
280 {
281 switch(key) {
282 case 'z':
283 stereo_shift_pressed = false;
284 break;
286 default:
287 break;
288 }
290 keystate[key] = false;
291 }
293 void keyb_con(unsigned char key, int x, int y)
294 {
295 if(key == '`' || key == 27) {
296 show_con = false;
297 glutKeyboardFunc(keyb);
298 glutKeyboardUpFunc(key_release);
299 glutPostRedisplay();
300 } else {
301 cmdcon_keypress(key);
302 }
303 }
305 static int prev_x, prev_y;
306 static bool bnstate[32];
308 void mouse(int bn, int state, int x, int y)
309 {
310 prev_x = x;
311 prev_y = y;
312 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
313 }
315 void motion(int x, int y)
316 {
317 int dx = x - prev_x;
318 int dy = y - prev_y;
319 prev_x = x;
320 prev_y = y;
322 if(stereo_shift_pressed) {
323 if(dy != 0) {
324 stereo_focus_dist += dy * 0.01;
325 stereo_eye_sep = stereo_focus_dist / 30.0;
326 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
327 glutPostRedisplay();
328 }
329 return;
330 }
332 if(bnstate[0]) {
333 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
334 glutPostRedisplay();
335 }
336 if(bnstate[2]) {
337 cam.input_zoom(dy * 0.1);
338 glutPostRedisplay();
339 }
340 }