dungeon_crawler

annotate prototype/src/main.cc @ 35:d0e93b4d9ec9

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