dungeon_crawler

view prototype/src/main.cc @ 17:d98240a13793

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