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 reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
123 if(au_init() == -1) {
127 if(demo_init(argc, argv) == -1) {
130 atexit(demo_cleanup);
133 set_fullscreen(opt.fullscreen);
147 struct video_mode *video_modes(void)
152 int num_video_modes(void)
154 return sizeof vmodes / sizeof *vmodes;
157 struct video_mode *get_video_mode(int idx)
159 if(idx == VMODE_CURRENT) {
165 int match_video_mode(int xsz, int ysz, int bpp)
167 struct video_mode *vm = vmodes;
168 int i, count = num_video_modes();
170 for(i=0; i<count; i++) {
171 if(vm->xsz == xsz && vm->ysz == ysz && vm->bpp == bpp) {
179 static int tex_xsz, tex_ysz;
180 static uint32_t *convbuf;
181 static int convbuf_size;
183 void *set_video_mode(int idx, int nbuf)
185 struct video_mode *vm = vmodes + idx;
187 if(cur_vmode == vm) {
191 glGenTextures(1, &tex);
192 glBindTexture(GL_TEXTURE_2D, tex);
194 tex_xsz = next_pow2(vm->xsz);
195 tex_ysz = next_pow2(vm->ysz);
196 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
197 GL_UNSIGNED_BYTE, 0);
198 if(opt.scaler == SCALER_LINEAR) {
199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
202 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
203 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
206 glMatrixMode(GL_TEXTURE);
208 glScalef((float)vm->xsz / tex_xsz, (float)vm->ysz / tex_ysz, 1);
210 if(vm->xsz * vm->ysz > convbuf_size) {
211 convbuf_size = vm->xsz * vm->ysz;
213 convbuf = malloc(convbuf_size * sizeof *convbuf);
216 if(demo_resizefb(vm->xsz, vm->ysz, vm->bpp) == -1) {
217 fprintf(stderr, "failed to allocate virtual framebuffer\n");
226 void wait_vsync(void)
230 void blit_frame(void *pixels, int vsync)
233 uint32_t *dptr = convbuf;
234 uint16_t *sptr = pixels;
235 static int prev_vsync = -1;
237 if(vsync != prev_vsync) {
242 demo_post_draw(pixels);
244 for(i=0; i<FB_WIDTH * FB_HEIGHT; i++) {
245 int r = UNPACK_R16(*sptr);
246 int g = UNPACK_G16(*sptr);
247 int b = UNPACK_B16(*sptr);
248 *dptr++ = PACK_RGB32(b, g, r);
252 glBindTexture(GL_TEXTURE_2D, tex);
253 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, FB_WIDTH, FB_HEIGHT, GL_RGBA,
254 GL_UNSIGNED_BYTE, convbuf);
256 glMatrixMode(GL_MODELVIEW);
258 if(win_aspect >= FB_ASPECT) {
259 glScalef(FB_ASPECT / win_aspect, 1, 1);
261 glScalef(1, win_aspect / FB_ASPECT, 1);
264 glClear(GL_COLOR_BUFFER_BIT);
278 assert(glGetError() == GL_NO_ERROR);
281 int kb_isdown(int key)
288 return keystate[KB_LALT] + keystate[KB_RALT];
291 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
297 return keystate[key];
301 void init_timer(int res_hz)
305 void reset_timer(void)
307 start_time = glutGet(GLUT_ELAPSED_TIME);
310 unsigned long get_msec(void)
312 return glutGet(GLUT_ELAPSED_TIME) - start_time;
318 void sleep_msec(unsigned long msec)
326 void sleep_msec(unsigned long msec)
332 static void display(void)
334 recalc_sball_matrix(sball_matrix);
336 time_msec = get_msec();
340 static void idle(void)
345 static void reshape(int x, int y)
349 win_aspect = (float)x / (float)y;
350 glViewport(0, 0, x, y);
353 static void keydown(unsigned char key, int x, int y)
355 modkeys = glutGetModifiers();
357 if((key == '\n' || key == '\r') && (modkeys & GLUT_ACTIVE_ALT)) {
359 set_fullscreen(opt.fullscreen);
363 demo_keyboard(key, 1);
366 static void keyup(unsigned char key, int x, int y)
369 demo_keyboard(key, 0);
372 static void skeydown(int key, int x, int y)
374 if(key == GLUT_KEY_F5) {
375 opt.scaler = (opt.scaler + 1) % NUM_SCALERS;
377 if(opt.scaler == SCALER_LINEAR) {
378 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
379 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
382 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
385 key = translate_special(key);
387 demo_keyboard(key, 1);
390 static void skeyup(int key, int x, int y)
392 key = translate_special(key);
394 demo_keyboard(key, 0);
397 static int translate_special(int skey)
410 case GLUT_KEY_PAGE_UP:
412 case GLUT_KEY_PAGE_DOWN:
419 if(skey >= GLUT_KEY_F1 && skey <= GLUT_KEY_F12) {
420 return KB_F1 + skey - GLUT_KEY_F1;
426 static void map_mouse_pos(int *xp, int *yp)
432 *xp = x * FB_WIDTH / win_width;
433 *yp = y * FB_HEIGHT / win_height;
436 static void mouse_button(int bn, int st, int x, int y)
440 map_mouse_pos(&x, &y);
445 case GLUT_LEFT_BUTTON:
448 case GLUT_RIGHT_BUTTON:
451 case GLUT_MIDDLE_BUTTON:
456 if(st == GLUT_DOWN) {
457 mouse_bmask |= 1 << bit;
459 mouse_bmask &= ~(1 << bit);
463 static void mouse_motion(int x, int y)
465 map_mouse_pos(&x, &y);
470 static void sball_motion(int x, int y, int z)
478 static void sball_rotate(int rx, int ry, int rz)
481 float s = (float)rsqrt(rx * rx + ry * ry + rz * rz);
482 cgm_qrotate(&rot, 0.001f / s, rx * s, ry * s, -rz * s);
486 static void sball_button(int bn, int st)
488 pos.x = pos.y = pos.z = 0;
489 rot.x = rot.y = rot.z = 0;
493 static void recalc_sball_matrix(float *xform)
495 cgm_mrotation_quat(xform, &rot);
502 static unsigned int next_pow2(unsigned int x)
513 static void set_fullscreen(int fs)
515 static int win_x, win_y;
518 win_x = glutGet(GLUT_WINDOW_WIDTH);
519 win_y = glutGet(GLUT_WINDOW_HEIGHT);
522 glutReshapeWindow(win_x, win_y);
527 static void set_vsync(int vsync)
529 vsync = vsync ? 1 : 0;
530 if(glx_swap_interval_ext) {
531 glx_swap_interval_ext(xdpy, xwin, vsync);
532 } else if(glx_swap_interval_sgi) {
533 glx_swap_interval_sgi(vsync);
538 static void set_vsync(int vsync)
540 if(wgl_swap_interval_ext) {
541 wgl_swap_interval_ext(vsync ? 1 : 0);