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