12 #include "cgmath/cgmath.h"
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);
34 unsigned int joy_bnstate, joy_bndiff, joy_bnpress;
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)
43 static struct video_mode *cur_vmode;
45 static unsigned int num_pressed;
46 static unsigned char keystate[256];
48 static unsigned long start_time;
49 static unsigned int modkeys;
51 static int win_width, win_height;
52 static float win_aspect;
53 static unsigned int tex;
60 static void (*glx_swap_interval_ext)();
61 static void (*glx_swap_interval_sgi)();
65 static PROC wgl_swap_interval_ext;
69 static cgm_vec3 pos = {0, 0, 0};
70 static cgm_quat rot = {0, 0, 0, 1};
73 int main(int argc, char **argv)
75 glutInit(&argc, argv);
77 if(glutGet(GLUT_SCREEN_HEIGHT) <= 1024) {
78 glutInitWindowSize(640, 480);
80 glutInitWindowSize(1280, 960);
82 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
83 glutCreateWindow("Mindlapse");
85 glutDisplayFunc(display);
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);
99 glutSetCursor(GLUT_CURSOR_NONE);
101 glEnable(GL_TEXTURE_2D);
102 glEnable(GL_CULL_FACE);
105 if(!set_video_mode(match_video_mode(FB_WIDTH, FB_HEIGHT, FB_BPP), 1)) {
110 xdpy = glXGetCurrentDisplay();
111 xwin = glXGetCurrentDrawable();
113 if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
114 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
118 wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
121 if(au_init() == -1) {
125 if(demo_init1(argc, argv) == -1) {
130 set_fullscreen(opt.fullscreen);
131 reshape(glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT));
133 reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
136 if(demo_init2() == -1) {
139 atexit(demo_cleanup);
152 struct video_mode *video_modes(void)
157 int num_video_modes(void)
159 return sizeof vmodes / sizeof *vmodes;
162 struct video_mode *get_video_mode(int idx)
164 if(idx == VMODE_CURRENT) {
170 int match_video_mode(int xsz, int ysz, int bpp)
172 struct video_mode *vm = vmodes;
173 int i, count = num_video_modes();
175 for(i=0; i<count; i++) {
176 if(vm->xsz == xsz && vm->ysz == ysz && vm->bpp == bpp) {
184 static int tex_xsz, tex_ysz;
185 static uint32_t *convbuf;
186 static int convbuf_size;
188 void *set_video_mode(int idx, int nbuf)
190 struct video_mode *vm = vmodes + idx;
192 if(cur_vmode == vm) {
196 glGenTextures(1, &tex);
197 glBindTexture(GL_TEXTURE_2D, tex);
199 tex_xsz = next_pow2(vm->xsz);
200 tex_ysz = next_pow2(vm->ysz);
201 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
202 GL_UNSIGNED_BYTE, 0);
203 if(opt.scaler == SCALER_LINEAR) {
204 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
205 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
207 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
211 glMatrixMode(GL_TEXTURE);
213 glScalef((float)vm->xsz / tex_xsz, (float)vm->ysz / tex_ysz, 1);
215 if(vm->xsz * vm->ysz > convbuf_size) {
216 convbuf_size = vm->xsz * vm->ysz;
218 convbuf = malloc(convbuf_size * sizeof *convbuf);
221 if(demo_resizefb(vm->xsz, vm->ysz, vm->bpp) == -1) {
222 fprintf(stderr, "failed to allocate virtual framebuffer\n");
231 void wait_vsync(void)
235 void blit_frame(void *pixels, int vsync)
238 uint32_t *dptr = convbuf;
239 uint16_t *sptr = pixels;
240 static int prev_vsync = -1;
242 if(vsync != prev_vsync) {
247 demo_post_draw(pixels);
249 for(i=0; i<FB_WIDTH * FB_HEIGHT; i++) {
250 int r = UNPACK_R16(*sptr);
251 int g = UNPACK_G16(*sptr);
252 int b = UNPACK_B16(*sptr);
253 *dptr++ = PACK_RGB32(b, g, r);
257 glBindTexture(GL_TEXTURE_2D, tex);
258 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, FB_WIDTH, FB_HEIGHT, GL_RGBA,
259 GL_UNSIGNED_BYTE, convbuf);
261 glMatrixMode(GL_MODELVIEW);
263 if(win_aspect >= FB_ASPECT) {
264 glScalef(FB_ASPECT / win_aspect, 1, 1);
266 glScalef(1, win_aspect / FB_ASPECT, 1);
269 glClear(GL_COLOR_BUFFER_BIT);
283 assert(glGetError() == GL_NO_ERROR);
286 int kb_isdown(int key)
293 return keystate[KB_LALT] + keystate[KB_RALT];
296 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
302 return keystate[key];
306 void init_timer(int res_hz)
310 void reset_timer(void)
312 start_time = glutGet(GLUT_ELAPSED_TIME);
315 unsigned long get_msec(void)
317 return glutGet(GLUT_ELAPSED_TIME) - start_time;
323 void sleep_msec(unsigned long msec)
331 void sleep_msec(unsigned long msec)
337 static void display(void)
339 recalc_sball_matrix(sball_matrix);
341 time_msec = get_msec();
345 static void idle(void)
350 static void reshape(int x, int y)
354 win_aspect = (float)x / (float)y;
355 glViewport(0, 0, x, y);
358 static void keydown(unsigned char key, int x, int y)
360 modkeys = glutGetModifiers();
362 if((key == '\n' || key == '\r') && (modkeys & GLUT_ACTIVE_ALT)) {
364 set_fullscreen(opt.fullscreen);
368 demo_keyboard(key, 1);
371 static void keyup(unsigned char key, int x, int y)
374 demo_keyboard(key, 0);
377 static void skeydown(int key, int x, int y)
379 if(key == GLUT_KEY_F5) {
380 opt.scaler = (opt.scaler + 1) % NUM_SCALERS;
382 if(opt.scaler == SCALER_LINEAR) {
383 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
384 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
386 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
390 key = translate_special(key);
392 demo_keyboard(key, 1);
395 static void skeyup(int key, int x, int y)
397 key = translate_special(key);
399 demo_keyboard(key, 0);
402 static int translate_special(int skey)
415 case GLUT_KEY_PAGE_UP:
417 case GLUT_KEY_PAGE_DOWN:
424 if(skey >= GLUT_KEY_F1 && skey <= GLUT_KEY_F12) {
425 return KB_F1 + skey - GLUT_KEY_F1;
431 static void map_mouse_pos(int *xp, int *yp)
437 *xp = x * FB_WIDTH / win_width;
438 *yp = y * FB_HEIGHT / win_height;
441 static void mouse_button(int bn, int st, int x, int y)
445 map_mouse_pos(&x, &y);
450 case GLUT_LEFT_BUTTON:
453 case GLUT_RIGHT_BUTTON:
456 case GLUT_MIDDLE_BUTTON:
461 if(st == GLUT_DOWN) {
462 mouse_bmask |= 1 << bit;
464 mouse_bmask &= ~(1 << bit);
468 static void mouse_motion(int x, int y)
470 map_mouse_pos(&x, &y);
475 static void sball_motion(int x, int y, int z)
483 static void sball_rotate(int rx, int ry, int rz)
486 float s = (float)rsqrt(rx * rx + ry * ry + rz * rz);
487 cgm_qrotate(&rot, 0.001f / s, rx * s, ry * s, -rz * s);
491 static void sball_button(int bn, int st)
493 pos.x = pos.y = pos.z = 0;
494 rot.x = rot.y = rot.z = 0;
498 static void recalc_sball_matrix(float *xform)
500 cgm_mrotation_quat(xform, &rot);
507 static unsigned int next_pow2(unsigned int x)
518 static void set_fullscreen(int fs)
520 static int win_x, win_y;
523 win_x = glutGet(GLUT_WINDOW_WIDTH);
524 win_y = glutGet(GLUT_WINDOW_HEIGHT);
527 glutReshapeWindow(win_x, win_y);
532 static void set_vsync(int vsync)
534 vsync = vsync ? 1 : 0;
535 if(glx_swap_interval_ext) {
536 glx_swap_interval_ext(xdpy, xwin, vsync);
537 } else if(glx_swap_interval_sgi) {
538 glx_swap_interval_sgi(vsync);
543 static void set_vsync(int vsync)
545 if(wgl_swap_interval_ext) {
546 wgl_swap_interval_ext(vsync ? 1 : 0);