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@5
|
81 add_data_path("data");
|
nuclear@16
|
82 add_data_path("sdr");
|
nuclear@5
|
83
|
nuclear@17
|
84 if(!init_renderer(xsz, ysz)) {
|
nuclear@15
|
85 return false;
|
nuclear@15
|
86 }
|
nuclear@15
|
87
|
nuclear@5
|
88 // load a tileset
|
nuclear@5
|
89 tileset = new TileSet;
|
nuclear@18
|
90 printf("loading tileset: %s\n", cfg.tileset_file);
|
nuclear@18
|
91 if(!tileset->load(datafile_path(cfg.tileset_file))) {
|
nuclear@5
|
92 return false;
|
nuclear@5
|
93 }
|
nuclear@5
|
94 set_active_tileset(tileset);
|
nuclear@5
|
95
|
nuclear@1
|
96 level = new Level;
|
nuclear@18
|
97 printf("loading level: %s\n", cfg.level_file);
|
nuclear@18
|
98 if(!level->load(datafile_path(cfg.level_file))) {
|
nuclear@5
|
99 return false;
|
nuclear@1
|
100 }
|
nuclear@1
|
101
|
nuclear@7
|
102 cam.input_move(0, 0.5, 0);
|
nuclear@7
|
103
|
nuclear@5
|
104 return true;
|
nuclear@1
|
105 }
|
nuclear@1
|
106
|
nuclear@15
|
107 void cleanup()
|
nuclear@15
|
108 {
|
nuclear@15
|
109 delete level;
|
nuclear@15
|
110 delete tileset;
|
nuclear@15
|
111
|
nuclear@15
|
112 destroy_renderer();
|
nuclear@15
|
113 }
|
nuclear@15
|
114
|
nuclear@7
|
115 void idle()
|
nuclear@7
|
116 {
|
nuclear@7
|
117 glutPostRedisplay();
|
nuclear@7
|
118 }
|
nuclear@7
|
119
|
nuclear@1
|
120 void disp()
|
nuclear@1
|
121 {
|
nuclear@7
|
122 update(glutGet(GLUT_ELAPSED_TIME));
|
nuclear@7
|
123
|
nuclear@18
|
124 if(cfg.stereo) {
|
nuclear@18
|
125 glDrawBuffer(GL_BACK_LEFT);
|
nuclear@1
|
126
|
nuclear@18
|
127 glMatrixMode(GL_PROJECTION);
|
nuclear@18
|
128 glLoadIdentity();
|
nuclear@18
|
129 proj_matrix(-1);
|
nuclear@18
|
130 glMatrixMode(GL_MODELVIEW);
|
nuclear@18
|
131 glLoadIdentity();
|
nuclear@18
|
132 view_matrix(-1);
|
nuclear@1
|
133
|
nuclear@24
|
134 draw();
|
nuclear@18
|
135
|
nuclear@18
|
136 glDrawBuffer(GL_BACK_RIGHT);
|
nuclear@18
|
137
|
nuclear@18
|
138 glMatrixMode(GL_PROJECTION);
|
nuclear@18
|
139 glLoadIdentity();
|
nuclear@18
|
140 proj_matrix(1);
|
nuclear@18
|
141 glMatrixMode(GL_MODELVIEW);
|
nuclear@18
|
142 glLoadIdentity();
|
nuclear@18
|
143 view_matrix(1);
|
nuclear@18
|
144
|
nuclear@24
|
145 draw();
|
nuclear@18
|
146 } else {
|
nuclear@18
|
147 glMatrixMode(GL_PROJECTION);
|
nuclear@18
|
148 glLoadIdentity();
|
nuclear@18
|
149 proj_matrix(0);
|
nuclear@18
|
150 glMatrixMode(GL_MODELVIEW);
|
nuclear@18
|
151 glLoadIdentity();
|
nuclear@18
|
152 view_matrix(0);
|
nuclear@18
|
153
|
nuclear@24
|
154 draw();
|
nuclear@18
|
155 }
|
nuclear@1
|
156
|
nuclear@1
|
157 glutSwapBuffers();
|
nuclear@1
|
158 assert(glGetError() == GL_NO_ERROR);
|
nuclear@7
|
159
|
nuclear@7
|
160 usleep(10000);
|
nuclear@7
|
161 }
|
nuclear@7
|
162
|
nuclear@24
|
163 void draw()
|
nuclear@24
|
164 {
|
nuclear@24
|
165 render_deferred(level);
|
nuclear@24
|
166
|
nuclear@24
|
167 if(show_con) {
|
nuclear@24
|
168 draw_cmdcon();
|
nuclear@24
|
169 }
|
nuclear@24
|
170 }
|
nuclear@24
|
171
|
nuclear@18
|
172 void view_matrix(int eye)
|
nuclear@18
|
173 {
|
nuclear@18
|
174 float offs = stereo_eye_sep * eye * 0.5;
|
nuclear@18
|
175 glTranslatef(-offs, 0, 0);
|
nuclear@18
|
176 cam.use_inverse();
|
nuclear@18
|
177 }
|
nuclear@18
|
178
|
nuclear@18
|
179 void proj_matrix(int eye)
|
nuclear@18
|
180 {
|
nuclear@18
|
181 static const float fov = M_PI / 4.0;
|
nuclear@18
|
182 static const float near_clip = 0.1;
|
nuclear@18
|
183 static const float far_clip = 100.0;
|
nuclear@18
|
184
|
nuclear@18
|
185 float top = near_clip * tan(fov * 0.5);
|
nuclear@18
|
186 float right = top * (float)cfg.width / (float)cfg.height;
|
nuclear@18
|
187
|
nuclear@18
|
188 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
|
nuclear@18
|
189 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
|
nuclear@18
|
190 }
|
nuclear@18
|
191
|
nuclear@18
|
192
|
nuclear@7
|
193 void update(unsigned long msec)
|
nuclear@7
|
194 {
|
nuclear@7
|
195 static unsigned long last_upd;
|
nuclear@7
|
196
|
nuclear@7
|
197 if(last_upd == 0) {
|
nuclear@7
|
198 last_upd = msec;
|
nuclear@7
|
199 }
|
nuclear@7
|
200 float dt = (float)(msec - last_upd) / 1000.0;
|
nuclear@7
|
201
|
nuclear@9
|
202 float offs = 2.5 * dt;
|
nuclear@7
|
203 float dx = 0, dy = 0;
|
nuclear@7
|
204
|
nuclear@7
|
205 // handle key input
|
nuclear@7
|
206 if(keystate['w'] || keystate['W']) {
|
nuclear@7
|
207 dy -= offs;
|
nuclear@7
|
208 }
|
nuclear@7
|
209 if(keystate['s'] || keystate['S']) {
|
nuclear@7
|
210 dy += offs;
|
nuclear@7
|
211 }
|
nuclear@7
|
212 if(keystate['d'] || keystate['D']) {
|
nuclear@7
|
213 dx += offs;
|
nuclear@7
|
214 }
|
nuclear@7
|
215 if(keystate['a'] || keystate['A']) {
|
nuclear@7
|
216 dx -= offs;
|
nuclear@7
|
217 }
|
nuclear@7
|
218
|
nuclear@7
|
219 cam.input_move(dx, 0, dy);
|
nuclear@7
|
220
|
nuclear@7
|
221 last_upd = msec;
|
nuclear@1
|
222 }
|
nuclear@1
|
223
|
nuclear@1
|
224 void reshape(int x, int y)
|
nuclear@1
|
225 {
|
nuclear@1
|
226 glViewport(0, 0, x, y);
|
nuclear@18
|
227 cfg.width = x;
|
nuclear@18
|
228 cfg.height = y;
|
nuclear@1
|
229 }
|
nuclear@1
|
230
|
nuclear@18
|
231 static bool stereo_shift_pressed;
|
nuclear@18
|
232
|
nuclear@1
|
233 void keyb(unsigned char key, int x, int y)
|
nuclear@1
|
234 {
|
nuclear@1
|
235 switch(key) {
|
nuclear@1
|
236 case 27:
|
nuclear@1
|
237 exit(0);
|
nuclear@18
|
238
|
nuclear@24
|
239 case '`':
|
nuclear@24
|
240 show_con = true;
|
nuclear@24
|
241 glutKeyboardFunc(keyb_con);
|
nuclear@24
|
242 glutKeyboardUpFunc(0);
|
nuclear@24
|
243 glutPostRedisplay();
|
nuclear@24
|
244 break;
|
nuclear@24
|
245
|
nuclear@18
|
246 case 'z':
|
nuclear@18
|
247 stereo_shift_pressed = true;
|
nuclear@18
|
248 break;
|
nuclear@18
|
249
|
nuclear@18
|
250 case '\n':
|
nuclear@18
|
251 case '\r':
|
nuclear@18
|
252 {
|
nuclear@18
|
253 static bool fullscr;
|
nuclear@18
|
254 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
|
nuclear@18
|
255 fullscr = !fullscr;
|
nuclear@18
|
256 if(fullscr) {
|
nuclear@18
|
257 glutFullScreen();
|
nuclear@18
|
258 } else {
|
nuclear@18
|
259 glutPositionWindow(20, 20);
|
nuclear@18
|
260 }
|
nuclear@18
|
261 }
|
nuclear@18
|
262 }
|
nuclear@18
|
263 break;
|
nuclear@18
|
264
|
nuclear@18
|
265 default:
|
nuclear@18
|
266 break;
|
nuclear@1
|
267 }
|
nuclear@7
|
268
|
nuclear@7
|
269 keystate[key] = true;
|
nuclear@7
|
270 }
|
nuclear@7
|
271
|
nuclear@7
|
272 void key_release(unsigned char key, int x, int y)
|
nuclear@7
|
273 {
|
nuclear@18
|
274 switch(key) {
|
nuclear@18
|
275 case 'z':
|
nuclear@18
|
276 stereo_shift_pressed = false;
|
nuclear@18
|
277 break;
|
nuclear@18
|
278
|
nuclear@18
|
279 default:
|
nuclear@18
|
280 break;
|
nuclear@18
|
281 }
|
nuclear@18
|
282
|
nuclear@7
|
283 keystate[key] = false;
|
nuclear@1
|
284 }
|
nuclear@1
|
285
|
nuclear@24
|
286 void keyb_con(unsigned char key, int x, int y)
|
nuclear@24
|
287 {
|
nuclear@24
|
288 if(key == '`' || key == 27) {
|
nuclear@24
|
289 show_con = false;
|
nuclear@24
|
290 glutKeyboardFunc(keyb);
|
nuclear@24
|
291 glutKeyboardUpFunc(key_release);
|
nuclear@24
|
292 glutPostRedisplay();
|
nuclear@24
|
293 } else {
|
nuclear@24
|
294 cmdcon_keypress(key);
|
nuclear@24
|
295 }
|
nuclear@24
|
296 }
|
nuclear@24
|
297
|
nuclear@1
|
298 static int prev_x, prev_y;
|
nuclear@1
|
299 static bool bnstate[32];
|
nuclear@1
|
300
|
nuclear@1
|
301 void mouse(int bn, int state, int x, int y)
|
nuclear@1
|
302 {
|
nuclear@1
|
303 prev_x = x;
|
nuclear@1
|
304 prev_y = y;
|
nuclear@1
|
305 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
|
nuclear@1
|
306 }
|
nuclear@1
|
307
|
nuclear@1
|
308 void motion(int x, int y)
|
nuclear@1
|
309 {
|
nuclear@1
|
310 int dx = x - prev_x;
|
nuclear@1
|
311 int dy = y - prev_y;
|
nuclear@1
|
312 prev_x = x;
|
nuclear@1
|
313 prev_y = y;
|
nuclear@1
|
314
|
nuclear@18
|
315 if(stereo_shift_pressed) {
|
nuclear@18
|
316 if(dy != 0) {
|
nuclear@18
|
317 stereo_focus_dist += dy * 0.01;
|
nuclear@18
|
318 stereo_eye_sep = stereo_focus_dist / 30.0;
|
nuclear@18
|
319 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
|
nuclear@18
|
320 glutPostRedisplay();
|
nuclear@18
|
321 }
|
nuclear@18
|
322 return;
|
nuclear@18
|
323 }
|
nuclear@18
|
324
|
nuclear@1
|
325 if(bnstate[0]) {
|
nuclear@7
|
326 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
|
nuclear@1
|
327 glutPostRedisplay();
|
nuclear@1
|
328 }
|
nuclear@1
|
329 if(bnstate[2]) {
|
nuclear@1
|
330 cam.input_zoom(dy * 0.1);
|
nuclear@1
|
331 glutPostRedisplay();
|
nuclear@1
|
332 }
|
nuclear@1
|
333 }
|