dungeon_crawler

annotate prototype/src/main.cc @ 38:862461b686f4

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