dungeon_crawler

annotate prototype/src/main.cc @ 15:3a3236a4833c

adding shaders and renderer abstraction
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 19 Aug 2012 23:09:30 +0300
parents b10ba85f75e0
children 91180ee7b7d9
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@1 11
nuclear@5 12 bool init();
nuclear@5 13 void cleanup();
nuclear@7 14 void idle();
nuclear@1 15 void disp();
nuclear@15 16 void draw();
nuclear@7 17 void update(unsigned long msec);
nuclear@1 18 void reshape(int x, int y);
nuclear@1 19 void keyb(unsigned char key, int x, int y);
nuclear@7 20 void key_release(unsigned char key, int x, int y);
nuclear@1 21 void mouse(int bn, int state, int x, int y);
nuclear@1 22 void motion(int x, int y);
nuclear@1 23
nuclear@5 24 static TileSet *tileset;
nuclear@1 25 static Level *level;
nuclear@7 26
nuclear@7 27 static FpsCamera cam;
nuclear@7 28 static bool keystate[256];
nuclear@1 29
nuclear@5 30 static const char *level_file = "0.level";
nuclear@5 31
nuclear@1 32 int main(int argc, char **argv)
nuclear@1 33 {
nuclear@1 34 glutInit(&argc, argv);
nuclear@5 35
nuclear@5 36 if(argc > 1) {
nuclear@5 37 level_file = argv[1];
nuclear@5 38 }
nuclear@5 39
nuclear@1 40 glutInitWindowSize(800, 600);
nuclear@1 41 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
nuclear@1 42 glutCreateWindow("prototype");
nuclear@1 43
nuclear@7 44 glutIdleFunc(idle);
nuclear@1 45 glutDisplayFunc(disp);
nuclear@1 46 glutReshapeFunc(reshape);
nuclear@1 47 glutKeyboardFunc(keyb);
nuclear@7 48 glutKeyboardUpFunc(key_release);
nuclear@1 49 glutMouseFunc(mouse);
nuclear@1 50 glutMotionFunc(motion);
nuclear@1 51
nuclear@1 52 glewInit();
nuclear@1 53
nuclear@5 54 if(!init()) {
nuclear@5 55 return 1;
nuclear@5 56 }
nuclear@5 57
nuclear@5 58 glutMainLoop();
nuclear@5 59 }
nuclear@5 60
nuclear@5 61 bool init()
nuclear@5 62 {
nuclear@1 63 glEnable(GL_LIGHTING);
nuclear@1 64 glEnable(GL_LIGHT0);
nuclear@1 65 float ldir[] = {-1, 1, 2, 0};
nuclear@1 66 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@1 67 glEnable(GL_NORMALIZE);
nuclear@1 68
nuclear@1 69 glEnable(GL_DEPTH_TEST);
nuclear@1 70 glEnable(GL_CULL_FACE);
nuclear@1 71 glEnable(GL_MULTISAMPLE);
nuclear@1 72
nuclear@5 73 add_data_path("data");
nuclear@5 74
nuclear@15 75 if(!init_renderer()) {
nuclear@15 76 return false;
nuclear@15 77 }
nuclear@15 78
nuclear@5 79 // load a tileset
nuclear@5 80 tileset = new TileSet;
nuclear@5 81 if(!tileset->load(datafile_path("default.tileset"))) {
nuclear@5 82 return false;
nuclear@5 83 }
nuclear@5 84 set_active_tileset(tileset);
nuclear@5 85
nuclear@1 86 level = new Level;
nuclear@7 87 printf("loading level: %s\n", level_file);
nuclear@5 88 if(!level->load(datafile_path(level_file))) {
nuclear@5 89 return false;
nuclear@1 90 }
nuclear@1 91
nuclear@7 92 cam.input_move(0, 0.5, 0);
nuclear@7 93
nuclear@5 94 return true;
nuclear@1 95 }
nuclear@1 96
nuclear@15 97 void cleanup()
nuclear@15 98 {
nuclear@15 99 delete level;
nuclear@15 100 delete tileset;
nuclear@15 101
nuclear@15 102 destroy_renderer();
nuclear@15 103 }
nuclear@15 104
nuclear@7 105 void idle()
nuclear@7 106 {
nuclear@7 107 glutPostRedisplay();
nuclear@7 108 }
nuclear@7 109
nuclear@1 110 void disp()
nuclear@1 111 {
nuclear@7 112 update(glutGet(GLUT_ELAPSED_TIME));
nuclear@7 113
nuclear@1 114 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@1 115
nuclear@1 116 glMatrixMode(GL_MODELVIEW);
nuclear@1 117 glLoadIdentity();
nuclear@7 118 cam.use_inverse();
nuclear@1 119
nuclear@15 120 render_deferred(draw);
nuclear@1 121
nuclear@1 122 glutSwapBuffers();
nuclear@1 123 assert(glGetError() == GL_NO_ERROR);
nuclear@7 124
nuclear@7 125 usleep(10000);
nuclear@7 126 }
nuclear@7 127
nuclear@15 128 void draw()
nuclear@15 129 {
nuclear@15 130 level->draw();
nuclear@15 131 }
nuclear@15 132
nuclear@7 133 void update(unsigned long msec)
nuclear@7 134 {
nuclear@7 135 static unsigned long last_upd;
nuclear@7 136
nuclear@7 137 if(last_upd == 0) {
nuclear@7 138 last_upd = msec;
nuclear@7 139 }
nuclear@7 140 float dt = (float)(msec - last_upd) / 1000.0;
nuclear@7 141
nuclear@9 142 float offs = 2.5 * dt;
nuclear@7 143 float dx = 0, dy = 0;
nuclear@7 144
nuclear@7 145 // handle key input
nuclear@7 146 if(keystate['w'] || keystate['W']) {
nuclear@7 147 dy -= offs;
nuclear@7 148 }
nuclear@7 149 if(keystate['s'] || keystate['S']) {
nuclear@7 150 dy += offs;
nuclear@7 151 }
nuclear@7 152 if(keystate['d'] || keystate['D']) {
nuclear@7 153 dx += offs;
nuclear@7 154 }
nuclear@7 155 if(keystate['a'] || keystate['A']) {
nuclear@7 156 dx -= offs;
nuclear@7 157 }
nuclear@7 158
nuclear@7 159 cam.input_move(dx, 0, dy);
nuclear@7 160
nuclear@7 161 last_upd = msec;
nuclear@1 162 }
nuclear@1 163
nuclear@1 164 void reshape(int x, int y)
nuclear@1 165 {
nuclear@1 166 glViewport(0, 0, x, y);
nuclear@1 167 glMatrixMode(GL_PROJECTION);
nuclear@1 168 glLoadIdentity();
nuclear@7 169 gluPerspective(45.0, (float)x / (float)y, 0.25, 100.0);
nuclear@1 170 }
nuclear@1 171
nuclear@1 172 void keyb(unsigned char key, int x, int y)
nuclear@1 173 {
nuclear@1 174 switch(key) {
nuclear@1 175 case 27:
nuclear@1 176 exit(0);
nuclear@1 177 }
nuclear@7 178
nuclear@7 179 keystate[key] = true;
nuclear@7 180 }
nuclear@7 181
nuclear@7 182 void key_release(unsigned char key, int x, int y)
nuclear@7 183 {
nuclear@7 184 keystate[key] = false;
nuclear@1 185 }
nuclear@1 186
nuclear@1 187 static int prev_x, prev_y;
nuclear@1 188 static bool bnstate[32];
nuclear@1 189
nuclear@1 190 void mouse(int bn, int state, int x, int y)
nuclear@1 191 {
nuclear@1 192 prev_x = x;
nuclear@1 193 prev_y = y;
nuclear@1 194 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
nuclear@1 195 }
nuclear@1 196
nuclear@1 197 void motion(int x, int y)
nuclear@1 198 {
nuclear@1 199 int dx = x - prev_x;
nuclear@1 200 int dy = y - prev_y;
nuclear@1 201 prev_x = x;
nuclear@1 202 prev_y = y;
nuclear@1 203
nuclear@1 204 if(bnstate[0]) {
nuclear@7 205 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
nuclear@1 206 glutPostRedisplay();
nuclear@1 207 }
nuclear@1 208 if(bnstate[2]) {
nuclear@1 209 cam.input_zoom(dy * 0.1);
nuclear@1 210 glutPostRedisplay();
nuclear@1 211 }
nuclear@1 212 }