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