fixed minor issues
[dosdemo] / src / glut / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <assert.h>
5 #include "miniglut.h"
6 #include "demo.h"
7 #include "gfx.h"
8 #include "gfxutil.h"
9 #include "timer.h"
10 #include "audio.h"
11 #include "cfgopt.h"
12 #include "cgmath/cgmath.h"
13 #include "util.h"
14
15 static void display(void);
16 static void idle(void);
17 static void reshape(int x, int y);
18 static void keydown(unsigned char key, int x, int y);
19 static void keyup(unsigned char key, int x, int y);
20 static void skeydown(int key, int x, int y);
21 static void skeyup(int key, int x, int y);
22 static int translate_special(int skey);
23 static void mouse_button(int bn, int st, int x, int y);
24 static void mouse_motion(int x, int y);
25 static void sball_motion(int x, int y, int z);
26 static void sball_rotate(int x, int y, int z);
27 static void sball_button(int bn, int st);
28 static void recalc_sball_matrix(float *xform);
29 static unsigned int next_pow2(unsigned int x);
30 static void set_fullscreen(int fs);
31 static void set_vsync(int vsync);
32
33 int have_joy;
34 unsigned int joy_bnstate, joy_bndiff, joy_bnpress;
35
36 #define MODE(w, h)      \
37         {0, w, h, 16, w * 2, 5, 6, 5, 11, 5, 0, 0xf800, 0x7e0, 0x1f, 0xbadf00d, 2, 0}
38 static struct video_mode vmodes[] = {
39         MODE(320, 240), MODE(400, 300), MODE(512, 384), MODE(640, 480),
40         MODE(800, 600), MODE(1024, 768), MODE(1280, 960), MODE(1280, 1024),
41         MODE(1920, 1080), MODE(1600, 1200), MODE(1920, 1200)
42 };
43 static struct video_mode *cur_vmode;
44
45 static unsigned int num_pressed;
46 static unsigned char keystate[256];
47
48 static unsigned long start_time;
49 static unsigned int modkeys;
50
51 static int win_width, win_height;
52 static float win_aspect;
53 static unsigned int tex;
54
55 #ifdef __unix__
56 #include <GL/glx.h>
57 static Display *xdpy;
58 static Window xwin;
59
60 static void (*glx_swap_interval_ext)();
61 static void (*glx_swap_interval_sgi)();
62 #endif
63 #ifdef _WIN32
64 #include <windows.h>
65 static PROC wgl_swap_interval_ext;
66 #endif
67
68 static int use_sball;
69 static cgm_vec3 pos = {0, 0, 0};
70 static cgm_quat rot = {0, 0, 0, 1};
71
72
73 int main(int argc, char **argv)
74 {
75         glutInit(&argc, argv);
76
77         if(glutGet(GLUT_SCREEN_HEIGHT) <= 1024) {
78                 glutInitWindowSize(640, 480);
79         } else {
80                 glutInitWindowSize(1280, 960);
81         }
82         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
83         glutCreateWindow("Mindlapse");
84
85         glutDisplayFunc(display);
86         glutIdleFunc(idle);
87         glutReshapeFunc(reshape);
88         glutKeyboardFunc(keydown);
89         glutKeyboardUpFunc(keyup);
90         glutSpecialFunc(skeydown);
91         glutSpecialUpFunc(skeyup);
92         glutMouseFunc(mouse_button);
93         glutMotionFunc(mouse_motion);
94         glutPassiveMotionFunc(mouse_motion);
95         glutSpaceballMotionFunc(sball_motion);
96         glutSpaceballRotateFunc(sball_rotate);
97         glutSpaceballButtonFunc(sball_button);
98
99         glutSetCursor(GLUT_CURSOR_NONE);
100
101         glEnable(GL_TEXTURE_2D);
102         glEnable(GL_CULL_FACE);
103
104
105         if(!set_video_mode(match_video_mode(FB_WIDTH, FB_HEIGHT, FB_BPP), 1)) {
106                 return 1;
107         }
108
109 #ifdef __unix__
110         xdpy = glXGetCurrentDisplay();
111         xwin = glXGetCurrentDrawable();
112
113         if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
114                 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
115         }
116 #endif
117 #ifdef _WIN32
118         wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
119 #endif
120
121         if(au_init() == -1) {
122                 return 1;
123         }
124         time_msec = 0;
125         if(demo_init(argc, argv) == -1) {
126                 return 1;
127         }
128         atexit(demo_cleanup);
129
130         if(opt.fullscreen) {
131                 set_fullscreen(opt.fullscreen);
132         }
133
134         reset_timer();
135
136         glutMainLoop();
137         return 0;
138 }
139
140 void demo_quit(void)
141 {
142         exit(0);
143 }
144
145 struct video_mode *video_modes(void)
146 {
147         return vmodes;
148 }
149
150 int num_video_modes(void)
151 {
152         return sizeof vmodes / sizeof *vmodes;
153 }
154
155 struct video_mode *get_video_mode(int idx)
156 {
157         if(idx == VMODE_CURRENT) {
158                 return cur_vmode;
159         }
160         return vmodes + idx;
161 }
162
163 int match_video_mode(int xsz, int ysz, int bpp)
164 {
165         struct video_mode *vm = vmodes;
166         int i, count = num_video_modes();
167
168         for(i=0; i<count; i++) {
169                 if(vm->xsz == xsz && vm->ysz == ysz && vm->bpp == bpp) {
170                         return i;
171                 }
172                 vm++;
173         }
174         return -1;
175 }
176
177 static int tex_xsz, tex_ysz;
178 static uint32_t *convbuf;
179 static int convbuf_size;
180
181 void *set_video_mode(int idx, int nbuf)
182 {
183         struct video_mode *vm = vmodes + idx;
184
185         if(cur_vmode == vm) {
186                 return vmem;
187         }
188
189         glGenTextures(1, &tex);
190         glBindTexture(GL_TEXTURE_2D, tex);
191
192         tex_xsz = next_pow2(vm->xsz);
193         tex_ysz = next_pow2(vm->ysz);
194         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
195                         GL_UNSIGNED_BYTE, 0);
196         if(opt.scaler == SCALER_LINEAR) {
197                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
198                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
199         } else {
200                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
201                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
202         }
203
204         glMatrixMode(GL_TEXTURE);
205         glLoadIdentity();
206         glScalef((float)vm->xsz / tex_xsz, (float)vm->ysz / tex_ysz, 1);
207
208         if(vm->xsz * vm->ysz > convbuf_size) {
209                 convbuf_size = vm->xsz * vm->ysz;
210                 free(convbuf);
211                 convbuf = malloc(convbuf_size * sizeof *convbuf);
212         }
213
214         if(demo_resizefb(vm->xsz, vm->ysz, vm->bpp) == -1) {
215                 fprintf(stderr, "failed to allocate virtual framebuffer\n");
216                 return 0;
217         }
218         vmem = fb_pixels;
219
220         cur_vmode = vm;
221         return vmem;
222 }
223
224 void wait_vsync(void)
225 {
226 }
227
228 void blit_frame(void *pixels, int vsync)
229 {
230         int i;
231         uint32_t *dptr = convbuf;
232         uint16_t *sptr = pixels;
233         static int prev_vsync = -1;
234
235         if(vsync != prev_vsync) {
236                 set_vsync(vsync);
237                 prev_vsync = vsync;
238         }
239
240         demo_post_draw(pixels);
241
242         for(i=0; i<FB_WIDTH * FB_HEIGHT; i++) {
243                 int r = UNPACK_R16(*sptr);
244                 int g = UNPACK_G16(*sptr);
245                 int b = UNPACK_B16(*sptr);
246                 *dptr++ = PACK_RGB32(b, g, r);
247                 sptr++;
248         }
249
250         glBindTexture(GL_TEXTURE_2D, tex);
251         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, FB_WIDTH, FB_HEIGHT, GL_RGBA,
252                         GL_UNSIGNED_BYTE, convbuf);
253
254         glMatrixMode(GL_MODELVIEW);
255         glLoadIdentity();
256         if(win_aspect >= FB_ASPECT) {
257                 glScalef(FB_ASPECT / win_aspect, 1, 1);
258         } else {
259                 glScalef(1, win_aspect / FB_ASPECT, 1);
260         }
261
262         glClear(GL_COLOR_BUFFER_BIT);
263
264         glBegin(GL_QUADS);
265         glTexCoord2f(0, 1);
266         glVertex2f(-1, -1);
267         glTexCoord2f(1, 1);
268         glVertex2f(1, -1);
269         glTexCoord2f(1, 0);
270         glVertex2f(1, 1);
271         glTexCoord2f(0, 0);
272         glVertex2f(-1, 1);
273         glEnd();
274
275         glutSwapBuffers();
276         assert(glGetError() == GL_NO_ERROR);
277 }
278
279 int kb_isdown(int key)
280 {
281         switch(key) {
282         case KB_ANY:
283                 return num_pressed;
284
285         case KB_ALT:
286                 return keystate[KB_LALT] + keystate[KB_RALT];
287
288         case KB_CTRL:
289                 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
290         }
291
292         if(isalpha(key)) {
293                 key = tolower(key);
294         }
295         return keystate[key];
296 }
297
298 /* timer */
299 void init_timer(int res_hz)
300 {
301 }
302
303 void reset_timer(void)
304 {
305         start_time = glutGet(GLUT_ELAPSED_TIME);
306 }
307
308 unsigned long get_msec(void)
309 {
310         return glutGet(GLUT_ELAPSED_TIME) - start_time;
311 }
312
313 #ifdef _WIN32
314 #include <windows.h>
315
316 void sleep_msec(unsigned long msec)
317 {
318         Sleep(msec);
319 }
320
321 #else
322 #include <unistd.h>
323
324 void sleep_msec(unsigned long msec)
325 {
326         usleep(msec * 1000);
327 }
328 #endif
329
330 static void display(void)
331 {
332         recalc_sball_matrix(sball_matrix);
333
334         time_msec = get_msec();
335         demo_draw();
336 }
337
338 static void idle(void)
339 {
340         glutPostRedisplay();
341 }
342
343 static void reshape(int x, int y)
344 {
345         win_width = x;
346         win_height = y;
347         win_aspect = (float)x / (float)y;
348         glViewport(0, 0, x, y);
349 }
350
351 static void keydown(unsigned char key, int x, int y)
352 {
353         modkeys = glutGetModifiers();
354
355         if((key == '\n' || key == '\r') && (modkeys & GLUT_ACTIVE_ALT)) {
356                 opt.fullscreen ^= 1;
357                 set_fullscreen(opt.fullscreen);
358                 return;
359         }
360         keystate[key] = 1;
361         demo_keyboard(key, 1);
362 }
363
364 static void keyup(unsigned char key, int x, int y)
365 {
366         keystate[key] = 0;
367         demo_keyboard(key, 0);
368 }
369
370 static void skeydown(int key, int x, int y)
371 {
372         if(key == GLUT_KEY_F5) {
373                 opt.scaler = (opt.scaler + 1) % NUM_SCALERS;
374
375                 if(opt.scaler == SCALER_LINEAR) {
376                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
377                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
378                 } else {
379                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
380                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
381                 }
382         }
383         key = translate_special(key);
384         keystate[key] = 1;
385         demo_keyboard(key, 1);
386 }
387
388 static void skeyup(int key, int x, int y)
389 {
390         key = translate_special(key);
391         keystate[key] = 0;
392         demo_keyboard(key, 0);
393 }
394
395 static int translate_special(int skey)
396 {
397         switch(skey) {
398         case 127:
399                 return 127;
400         case GLUT_KEY_LEFT:
401                 return KB_LEFT;
402         case GLUT_KEY_RIGHT:
403                 return KB_RIGHT;
404         case GLUT_KEY_UP:
405                 return KB_UP;
406         case GLUT_KEY_DOWN:
407                 return KB_DOWN;
408         case GLUT_KEY_PAGE_UP:
409                 return KB_PGUP;
410         case GLUT_KEY_PAGE_DOWN:
411                 return KB_PGDN;
412         case GLUT_KEY_HOME:
413                 return KB_HOME;
414         case GLUT_KEY_END:
415                 return KB_END;
416         default:
417                 if(skey >= GLUT_KEY_F1 && skey <= GLUT_KEY_F12) {
418                         return KB_F1 + skey - GLUT_KEY_F1;
419                 }
420         }
421         return 0;
422 }
423
424 static void map_mouse_pos(int *xp, int *yp)
425 {
426         int x = *xp;
427         int y = *yp;
428
429         /* TODO */
430         *xp = x * FB_WIDTH / win_width;
431         *yp = y * FB_HEIGHT / win_height;
432 }
433
434 static void mouse_button(int bn, int st, int x, int y)
435 {
436         int bit;
437
438         map_mouse_pos(&x, &y);
439         mouse_x = x;
440         mouse_y = y;
441
442         switch(bn) {
443         case GLUT_LEFT_BUTTON:
444                 bit = 0;
445                 break;
446         case GLUT_RIGHT_BUTTON:
447                 bit = 1;
448                 break;
449         case GLUT_MIDDLE_BUTTON:
450                 bit = 2;
451                 break;
452         }
453
454         if(st == GLUT_DOWN) {
455                 mouse_bmask |= 1 << bit;
456         } else {
457                 mouse_bmask &= ~(1 << bit);
458         }
459 }
460
461 static void mouse_motion(int x, int y)
462 {
463         map_mouse_pos(&x, &y);
464         mouse_x = x;
465         mouse_y = y;
466 }
467
468 static void sball_motion(int x, int y, int z)
469 {
470         pos.x += x * 0.001f;
471         pos.y += y * 0.001f;
472         pos.z -= z * 0.001f;
473
474 }
475
476 static void sball_rotate(int rx, int ry, int rz)
477 {
478         if(rx | ry | rz) {
479                 float s = (float)rsqrt(rx * rx + ry * ry + rz * rz);
480                 cgm_qrotate(&rot, 0.001f / s, rx * s, ry * s, -rz * s);
481         }
482 }
483
484 static void sball_button(int bn, int st)
485 {
486         pos.x = pos.y = pos.z = 0;
487         rot.x = rot.y = rot.z = 0;
488         rot.w = 1;
489 }
490
491 static void recalc_sball_matrix(float *xform)
492 {
493         cgm_mrotation_quat(xform, &rot);
494         xform[12] = pos.x;
495         xform[13] = pos.y;
496         xform[14] = pos.z;
497 }
498
499
500 static unsigned int next_pow2(unsigned int x)
501 {
502         x--;
503         x |= x >> 1;
504         x |= x >> 2;
505         x |= x >> 4;
506         x |= x >> 8;
507         x |= x >> 16;
508         return x + 1;
509 }
510
511 static void set_fullscreen(int fs)
512 {
513         static int win_x, win_y;
514
515         if(fs) {
516                 win_x = glutGet(GLUT_WINDOW_WIDTH);
517                 win_y = glutGet(GLUT_WINDOW_HEIGHT);
518                 glutFullScreen();
519         } else {
520                 glutReshapeWindow(win_x, win_y);
521         }
522 }
523
524 #ifdef __unix__
525 static void set_vsync(int vsync)
526 {
527         vsync = vsync ? 1 : 0;
528         if(glx_swap_interval_ext) {
529                 glx_swap_interval_ext(xdpy, xwin, vsync);
530         } else if(glx_swap_interval_sgi) {
531                 glx_swap_interval_sgi(vsync);
532         }
533 }
534 #endif
535 #ifdef WIN32
536 static void set_vsync(int vsync)
537 {
538         if(wgl_swap_interval_ext) {
539                 wgl_swap_interval_ext(vsync ? 1 : 0);
540         }
541 }
542 #endif