dungeon_crawler

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