dungeon_crawler

annotate prototype/src/main.cc @ 18:5c41e6fcb300

- commandline arguments - stereoscopic rendering - FBO fixed
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Aug 2012 03:17:48 +0300
parents d98240a13793
children fa8f89d06f6f
rev   line source
nuclear@1 1 #include <stdio.h>
nuclear@1 2 #include <stdlib.h>
nuclear@1 3 #include <assert.h>
nuclear@7 4 #include <unistd.h>
nuclear@1 5 #include "opengl.h"
nuclear@1 6 #include "level.h"
nuclear@1 7 #include "camera.h"
nuclear@5 8 #include "datapath.h"
nuclear@5 9 #include "tileset.h"
nuclear@15 10 #include "renderer.h"
nuclear@18 11 #include "cfg.h"
nuclear@1 12
nuclear@17 13 bool init(int xsz, int ysz);
nuclear@5 14 void cleanup();
nuclear@7 15 void idle();
nuclear@1 16 void disp();
nuclear@15 17 void draw();
nuclear@18 18 void view_matrix(int eye);
nuclear@18 19 void proj_matrix(int eye);
nuclear@7 20 void update(unsigned long msec);
nuclear@1 21 void reshape(int x, int y);
nuclear@1 22 void keyb(unsigned char key, int x, int y);
nuclear@7 23 void key_release(unsigned char key, int x, int y);
nuclear@1 24 void mouse(int bn, int state, int x, int y);
nuclear@1 25 void motion(int x, int y);
nuclear@1 26
nuclear@5 27 static TileSet *tileset;
nuclear@1 28 static Level *level;
nuclear@7 29
nuclear@7 30 static FpsCamera cam;
nuclear@7 31 static bool keystate[256];
nuclear@1 32
nuclear@18 33 static float stereo_focus_dist = 0.25;
nuclear@18 34 static float stereo_eye_sep = stereo_focus_dist / 30.0;
nuclear@18 35
nuclear@5 36
nuclear@1 37 int main(int argc, char **argv)
nuclear@1 38 {
nuclear@1 39 glutInit(&argc, argv);
nuclear@5 40
nuclear@18 41 if(!cfg.parse_args(argc, argv)) {
nuclear@18 42 return 1;
nuclear@5 43 }
nuclear@5 44
nuclear@18 45 glutInitWindowSize(cfg.width, cfg.height);
nuclear@18 46 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
nuclear@18 47 glutCreateWindow("dungeon crawler prototype");
nuclear@1 48
nuclear@7 49 glutIdleFunc(idle);
nuclear@1 50 glutDisplayFunc(disp);
nuclear@1 51 glutReshapeFunc(reshape);
nuclear@1 52 glutKeyboardFunc(keyb);
nuclear@7 53 glutKeyboardUpFunc(key_release);
nuclear@1 54 glutMouseFunc(mouse);
nuclear@1 55 glutMotionFunc(motion);
nuclear@1 56
nuclear@1 57 glewInit();
nuclear@1 58
nuclear@18 59 if(!init(cfg.width, cfg.height)) {
nuclear@5 60 return 1;
nuclear@5 61 }
nuclear@5 62
nuclear@5 63 glutMainLoop();
nuclear@5 64 }
nuclear@5 65
nuclear@17 66 bool init(int xsz, int ysz)
nuclear@5 67 {
nuclear@1 68 glEnable(GL_LIGHTING);
nuclear@1 69 glEnable(GL_LIGHT0);
nuclear@1 70 float ldir[] = {-1, 1, 2, 0};
nuclear@1 71 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@1 72 glEnable(GL_NORMALIZE);
nuclear@1 73
nuclear@1 74 glEnable(GL_DEPTH_TEST);
nuclear@1 75 glEnable(GL_CULL_FACE);
nuclear@1 76 glEnable(GL_MULTISAMPLE);
nuclear@1 77
nuclear@5 78 add_data_path("data");
nuclear@16 79 add_data_path("sdr");
nuclear@5 80
nuclear@17 81 if(!init_renderer(xsz, ysz)) {
nuclear@15 82 return false;
nuclear@15 83 }
nuclear@15 84
nuclear@5 85 // load a tileset
nuclear@5 86 tileset = new TileSet;
nuclear@18 87 printf("loading tileset: %s\n", cfg.tileset_file);
nuclear@18 88 if(!tileset->load(datafile_path(cfg.tileset_file))) {
nuclear@5 89 return false;
nuclear@5 90 }
nuclear@5 91 set_active_tileset(tileset);
nuclear@5 92
nuclear@1 93 level = new Level;
nuclear@18 94 printf("loading level: %s\n", cfg.level_file);
nuclear@18 95 if(!level->load(datafile_path(cfg.level_file))) {
nuclear@5 96 return false;
nuclear@1 97 }
nuclear@1 98
nuclear@7 99 cam.input_move(0, 0.5, 0);
nuclear@7 100
nuclear@5 101 return true;
nuclear@1 102 }
nuclear@1 103
nuclear@15 104 void cleanup()
nuclear@15 105 {
nuclear@15 106 delete level;
nuclear@15 107 delete tileset;
nuclear@15 108
nuclear@15 109 destroy_renderer();
nuclear@15 110 }
nuclear@15 111
nuclear@7 112 void idle()
nuclear@7 113 {
nuclear@7 114 glutPostRedisplay();
nuclear@7 115 }
nuclear@7 116
nuclear@1 117 void disp()
nuclear@1 118 {
nuclear@7 119 update(glutGet(GLUT_ELAPSED_TIME));
nuclear@7 120
nuclear@18 121 if(cfg.stereo) {
nuclear@18 122 glDrawBuffer(GL_BACK_LEFT);
nuclear@1 123
nuclear@18 124 glMatrixMode(GL_PROJECTION);
nuclear@18 125 glLoadIdentity();
nuclear@18 126 proj_matrix(-1);
nuclear@18 127 glMatrixMode(GL_MODELVIEW);
nuclear@18 128 glLoadIdentity();
nuclear@18 129 view_matrix(-1);
nuclear@1 130
nuclear@18 131 render_deferred(draw);
nuclear@18 132
nuclear@18 133 glDrawBuffer(GL_BACK_RIGHT);
nuclear@18 134
nuclear@18 135 glMatrixMode(GL_PROJECTION);
nuclear@18 136 glLoadIdentity();
nuclear@18 137 proj_matrix(1);
nuclear@18 138 glMatrixMode(GL_MODELVIEW);
nuclear@18 139 glLoadIdentity();
nuclear@18 140 view_matrix(1);
nuclear@18 141
nuclear@18 142 render_deferred(draw);
nuclear@18 143
nuclear@18 144 } else {
nuclear@18 145 glMatrixMode(GL_PROJECTION);
nuclear@18 146 glLoadIdentity();
nuclear@18 147 proj_matrix(0);
nuclear@18 148 glMatrixMode(GL_MODELVIEW);
nuclear@18 149 glLoadIdentity();
nuclear@18 150 view_matrix(0);
nuclear@18 151
nuclear@18 152 render_deferred(draw);
nuclear@18 153 }
nuclear@1 154
nuclear@1 155 glutSwapBuffers();
nuclear@1 156 assert(glGetError() == GL_NO_ERROR);
nuclear@7 157
nuclear@7 158 usleep(10000);
nuclear@7 159 }
nuclear@7 160
nuclear@15 161 void draw()
nuclear@15 162 {
nuclear@18 163 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@15 164 level->draw();
nuclear@15 165 }
nuclear@15 166
nuclear@18 167 void view_matrix(int eye)
nuclear@18 168 {
nuclear@18 169 float offs = stereo_eye_sep * eye * 0.5;
nuclear@18 170 glTranslatef(-offs, 0, 0);
nuclear@18 171 cam.use_inverse();
nuclear@18 172 }
nuclear@18 173
nuclear@18 174 void proj_matrix(int eye)
nuclear@18 175 {
nuclear@18 176 static const float fov = M_PI / 4.0;
nuclear@18 177 static const float near_clip = 0.1;
nuclear@18 178 static const float far_clip = 100.0;
nuclear@18 179
nuclear@18 180 float top = near_clip * tan(fov * 0.5);
nuclear@18 181 float right = top * (float)cfg.width / (float)cfg.height;
nuclear@18 182
nuclear@18 183 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
nuclear@18 184 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
nuclear@18 185 }
nuclear@18 186
nuclear@18 187
nuclear@7 188 void update(unsigned long msec)
nuclear@7 189 {
nuclear@7 190 static unsigned long last_upd;
nuclear@7 191
nuclear@7 192 if(last_upd == 0) {
nuclear@7 193 last_upd = msec;
nuclear@7 194 }
nuclear@7 195 float dt = (float)(msec - last_upd) / 1000.0;
nuclear@7 196
nuclear@9 197 float offs = 2.5 * dt;
nuclear@7 198 float dx = 0, dy = 0;
nuclear@7 199
nuclear@7 200 // handle key input
nuclear@7 201 if(keystate['w'] || keystate['W']) {
nuclear@7 202 dy -= offs;
nuclear@7 203 }
nuclear@7 204 if(keystate['s'] || keystate['S']) {
nuclear@7 205 dy += offs;
nuclear@7 206 }
nuclear@7 207 if(keystate['d'] || keystate['D']) {
nuclear@7 208 dx += offs;
nuclear@7 209 }
nuclear@7 210 if(keystate['a'] || keystate['A']) {
nuclear@7 211 dx -= offs;
nuclear@7 212 }
nuclear@7 213
nuclear@7 214 cam.input_move(dx, 0, dy);
nuclear@7 215
nuclear@7 216 last_upd = msec;
nuclear@1 217 }
nuclear@1 218
nuclear@1 219 void reshape(int x, int y)
nuclear@1 220 {
nuclear@1 221 glViewport(0, 0, x, y);
nuclear@18 222 cfg.width = x;
nuclear@18 223 cfg.height = y;
nuclear@1 224 }
nuclear@1 225
nuclear@18 226 static bool stereo_shift_pressed;
nuclear@18 227
nuclear@1 228 void keyb(unsigned char key, int x, int y)
nuclear@1 229 {
nuclear@1 230 switch(key) {
nuclear@1 231 case 27:
nuclear@1 232 exit(0);
nuclear@18 233
nuclear@18 234 case 'z':
nuclear@18 235 stereo_shift_pressed = true;
nuclear@18 236 break;
nuclear@18 237
nuclear@18 238 case '\n':
nuclear@18 239 case '\r':
nuclear@18 240 {
nuclear@18 241 static bool fullscr;
nuclear@18 242 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
nuclear@18 243 fullscr = !fullscr;
nuclear@18 244 if(fullscr) {
nuclear@18 245 glutFullScreen();
nuclear@18 246 } else {
nuclear@18 247 glutPositionWindow(20, 20);
nuclear@18 248 }
nuclear@18 249 }
nuclear@18 250 }
nuclear@18 251 break;
nuclear@18 252
nuclear@18 253 default:
nuclear@18 254 break;
nuclear@1 255 }
nuclear@7 256
nuclear@7 257 keystate[key] = true;
nuclear@7 258 }
nuclear@7 259
nuclear@7 260 void key_release(unsigned char key, int x, int y)
nuclear@7 261 {
nuclear@18 262 switch(key) {
nuclear@18 263 case 'z':
nuclear@18 264 stereo_shift_pressed = false;
nuclear@18 265 break;
nuclear@18 266
nuclear@18 267 default:
nuclear@18 268 break;
nuclear@18 269 }
nuclear@18 270
nuclear@7 271 keystate[key] = false;
nuclear@1 272 }
nuclear@1 273
nuclear@1 274 static int prev_x, prev_y;
nuclear@1 275 static bool bnstate[32];
nuclear@1 276
nuclear@1 277 void mouse(int bn, int state, int x, int y)
nuclear@1 278 {
nuclear@1 279 prev_x = x;
nuclear@1 280 prev_y = y;
nuclear@1 281 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
nuclear@1 282 }
nuclear@1 283
nuclear@1 284 void motion(int x, int y)
nuclear@1 285 {
nuclear@1 286 int dx = x - prev_x;
nuclear@1 287 int dy = y - prev_y;
nuclear@1 288 prev_x = x;
nuclear@1 289 prev_y = y;
nuclear@1 290
nuclear@18 291 if(stereo_shift_pressed) {
nuclear@18 292 if(dy != 0) {
nuclear@18 293 stereo_focus_dist += dy * 0.01;
nuclear@18 294 stereo_eye_sep = stereo_focus_dist / 30.0;
nuclear@18 295 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
nuclear@18 296 glutPostRedisplay();
nuclear@18 297 }
nuclear@18 298 return;
nuclear@18 299 }
nuclear@18 300
nuclear@1 301 if(bnstate[0]) {
nuclear@7 302 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
nuclear@1 303 glutPostRedisplay();
nuclear@1 304 }
nuclear@1 305 if(bnstate[2]) {
nuclear@1 306 cam.input_zoom(dy * 0.1);
nuclear@1 307 glutPostRedisplay();
nuclear@1 308 }
nuclear@1 309 }