dungeon_crawler

view prototype/src/main.cc @ 24:e122ba214ee1

implementing the command console
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Aug 2012 18:03:11 +0300
parents fa8f89d06f6f
children 527fede30057
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("data");
82 add_data_path("sdr");
84 if(!init_renderer(xsz, ysz)) {
85 return false;
86 }
88 // load a tileset
89 tileset = new TileSet;
90 printf("loading tileset: %s\n", cfg.tileset_file);
91 if(!tileset->load(datafile_path(cfg.tileset_file))) {
92 return false;
93 }
94 set_active_tileset(tileset);
96 level = new Level;
97 printf("loading level: %s\n", cfg.level_file);
98 if(!level->load(datafile_path(cfg.level_file))) {
99 return false;
100 }
102 cam.input_move(0, 0.5, 0);
104 return true;
105 }
107 void cleanup()
108 {
109 delete level;
110 delete tileset;
112 destroy_renderer();
113 }
115 void idle()
116 {
117 glutPostRedisplay();
118 }
120 void disp()
121 {
122 update(glutGet(GLUT_ELAPSED_TIME));
124 if(cfg.stereo) {
125 glDrawBuffer(GL_BACK_LEFT);
127 glMatrixMode(GL_PROJECTION);
128 glLoadIdentity();
129 proj_matrix(-1);
130 glMatrixMode(GL_MODELVIEW);
131 glLoadIdentity();
132 view_matrix(-1);
134 draw();
136 glDrawBuffer(GL_BACK_RIGHT);
138 glMatrixMode(GL_PROJECTION);
139 glLoadIdentity();
140 proj_matrix(1);
141 glMatrixMode(GL_MODELVIEW);
142 glLoadIdentity();
143 view_matrix(1);
145 draw();
146 } else {
147 glMatrixMode(GL_PROJECTION);
148 glLoadIdentity();
149 proj_matrix(0);
150 glMatrixMode(GL_MODELVIEW);
151 glLoadIdentity();
152 view_matrix(0);
154 draw();
155 }
157 glutSwapBuffers();
158 assert(glGetError() == GL_NO_ERROR);
160 usleep(10000);
161 }
163 void draw()
164 {
165 render_deferred(level);
167 if(show_con) {
168 draw_cmdcon();
169 }
170 }
172 void view_matrix(int eye)
173 {
174 float offs = stereo_eye_sep * eye * 0.5;
175 glTranslatef(-offs, 0, 0);
176 cam.use_inverse();
177 }
179 void proj_matrix(int eye)
180 {
181 static const float fov = M_PI / 4.0;
182 static const float near_clip = 0.1;
183 static const float far_clip = 100.0;
185 float top = near_clip * tan(fov * 0.5);
186 float right = top * (float)cfg.width / (float)cfg.height;
188 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
189 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
190 }
193 void update(unsigned long msec)
194 {
195 static unsigned long last_upd;
197 if(last_upd == 0) {
198 last_upd = msec;
199 }
200 float dt = (float)(msec - last_upd) / 1000.0;
202 float offs = 2.5 * dt;
203 float dx = 0, dy = 0;
205 // handle key input
206 if(keystate['w'] || keystate['W']) {
207 dy -= offs;
208 }
209 if(keystate['s'] || keystate['S']) {
210 dy += offs;
211 }
212 if(keystate['d'] || keystate['D']) {
213 dx += offs;
214 }
215 if(keystate['a'] || keystate['A']) {
216 dx -= offs;
217 }
219 cam.input_move(dx, 0, dy);
221 last_upd = msec;
222 }
224 void reshape(int x, int y)
225 {
226 glViewport(0, 0, x, y);
227 cfg.width = x;
228 cfg.height = y;
229 }
231 static bool stereo_shift_pressed;
233 void keyb(unsigned char key, int x, int y)
234 {
235 switch(key) {
236 case 27:
237 exit(0);
239 case '`':
240 show_con = true;
241 glutKeyboardFunc(keyb_con);
242 glutKeyboardUpFunc(0);
243 glutPostRedisplay();
244 break;
246 case 'z':
247 stereo_shift_pressed = true;
248 break;
250 case '\n':
251 case '\r':
252 {
253 static bool fullscr;
254 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
255 fullscr = !fullscr;
256 if(fullscr) {
257 glutFullScreen();
258 } else {
259 glutPositionWindow(20, 20);
260 }
261 }
262 }
263 break;
265 default:
266 break;
267 }
269 keystate[key] = true;
270 }
272 void key_release(unsigned char key, int x, int y)
273 {
274 switch(key) {
275 case 'z':
276 stereo_shift_pressed = false;
277 break;
279 default:
280 break;
281 }
283 keystate[key] = false;
284 }
286 void keyb_con(unsigned char key, int x, int y)
287 {
288 if(key == '`' || key == 27) {
289 show_con = false;
290 glutKeyboardFunc(keyb);
291 glutKeyboardUpFunc(key_release);
292 glutPostRedisplay();
293 } else {
294 cmdcon_keypress(key);
295 }
296 }
298 static int prev_x, prev_y;
299 static bool bnstate[32];
301 void mouse(int bn, int state, int x, int y)
302 {
303 prev_x = x;
304 prev_y = y;
305 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
306 }
308 void motion(int x, int y)
309 {
310 int dx = x - prev_x;
311 int dy = y - prev_y;
312 prev_x = x;
313 prev_y = y;
315 if(stereo_shift_pressed) {
316 if(dy != 0) {
317 stereo_focus_dist += dy * 0.01;
318 stereo_eye_sep = stereo_focus_dist / 30.0;
319 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
320 glutPostRedisplay();
321 }
322 return;
323 }
325 if(bnstate[0]) {
326 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
327 glutPostRedisplay();
328 }
329 if(bnstate[2]) {
330 cam.input_zoom(dy * 0.1);
331 glutPostRedisplay();
332 }
333 }