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_init(argc, argv) == -1) {
128 atexit(demo_cleanup);
131 set_fullscreen(opt.fullscreen);
145 struct video_mode *video_modes(void)
150 int num_video_modes(void)
152 return sizeof vmodes / sizeof *vmodes;
155 struct video_mode *get_video_mode(int idx)
157 if(idx == VMODE_CURRENT) {
163 int match_video_mode(int xsz, int ysz, int bpp)
165 struct video_mode *vm = vmodes;
166 int i, count = num_video_modes();
168 for(i=0; i<count; i++) {
169 if(vm->xsz == xsz && vm->ysz == ysz && vm->bpp == bpp) {
177 static int tex_xsz, tex_ysz;
178 static uint32_t *convbuf;
179 static int convbuf_size;
181 void *set_video_mode(int idx, int nbuf)
183 struct video_mode *vm = vmodes + idx;
185 if(cur_vmode == vm) {
189 glGenTextures(1, &tex);
190 glBindTexture(GL_TEXTURE_2D, tex);
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);
200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
204 glMatrixMode(GL_TEXTURE);
206 glScalef((float)vm->xsz / tex_xsz, (float)vm->ysz / tex_ysz, 1);
208 if(vm->xsz * vm->ysz > convbuf_size) {
209 convbuf_size = vm->xsz * vm->ysz;
211 convbuf = malloc(convbuf_size * sizeof *convbuf);
214 if(demo_resizefb(vm->xsz, vm->ysz, vm->bpp) == -1) {
215 fprintf(stderr, "failed to allocate virtual framebuffer\n");
224 void wait_vsync(void)
228 void blit_frame(void *pixels, int vsync)
231 uint32_t *dptr = convbuf;
232 uint16_t *sptr = pixels;
233 static int prev_vsync = -1;
235 if(vsync != prev_vsync) {
240 demo_post_draw(pixels);
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);
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);
254 glMatrixMode(GL_MODELVIEW);
256 if(win_aspect >= FB_ASPECT) {
257 glScalef(FB_ASPECT / win_aspect, 1, 1);
259 glScalef(1, win_aspect / FB_ASPECT, 1);
262 glClear(GL_COLOR_BUFFER_BIT);
276 assert(glGetError() == GL_NO_ERROR);
279 int kb_isdown(int key)
286 return keystate[KB_LALT] + keystate[KB_RALT];
289 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
295 return keystate[key];
299 void init_timer(int res_hz)
303 void reset_timer(void)
305 start_time = glutGet(GLUT_ELAPSED_TIME);
308 unsigned long get_msec(void)
310 return glutGet(GLUT_ELAPSED_TIME) - start_time;
316 void sleep_msec(unsigned long msec)
324 void sleep_msec(unsigned long msec)
330 static void display(void)
332 recalc_sball_matrix(sball_matrix);
334 time_msec = get_msec();
338 static void idle(void)
343 static void reshape(int x, int y)
347 win_aspect = (float)x / (float)y;
348 glViewport(0, 0, x, y);
351 static void keydown(unsigned char key, int x, int y)
353 modkeys = glutGetModifiers();
355 if((key == '\n' || key == '\r') && (modkeys & GLUT_ACTIVE_ALT)) {
357 set_fullscreen(opt.fullscreen);
361 demo_keyboard(key, 1);
364 static void keyup(unsigned char key, int x, int y)
367 demo_keyboard(key, 0);
370 static void skeydown(int key, int x, int y)
372 if(key == GLUT_KEY_F5) {
373 opt.scaler = (opt.scaler + 1) % NUM_SCALERS;
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);
379 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
383 key = translate_special(key);
385 demo_keyboard(key, 1);
388 static void skeyup(int key, int x, int y)
390 key = translate_special(key);
392 demo_keyboard(key, 0);
395 static int translate_special(int skey)
408 case GLUT_KEY_PAGE_UP:
410 case GLUT_KEY_PAGE_DOWN:
417 if(skey >= GLUT_KEY_F1 && skey <= GLUT_KEY_F12) {
418 return KB_F1 + skey - GLUT_KEY_F1;
424 static void map_mouse_pos(int *xp, int *yp)
430 *xp = x * FB_WIDTH / win_width;
431 *yp = y * FB_HEIGHT / win_height;
434 static void mouse_button(int bn, int st, int x, int y)
438 map_mouse_pos(&x, &y);
443 case GLUT_LEFT_BUTTON:
446 case GLUT_RIGHT_BUTTON:
449 case GLUT_MIDDLE_BUTTON:
454 if(st == GLUT_DOWN) {
455 mouse_bmask |= 1 << bit;
457 mouse_bmask &= ~(1 << bit);
461 static void mouse_motion(int x, int y)
463 map_mouse_pos(&x, &y);
468 static void sball_motion(int x, int y, int z)
476 static void sball_rotate(int rx, int ry, int 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);
484 static void sball_button(int bn, int st)
486 pos.x = pos.y = pos.z = 0;
487 rot.x = rot.y = rot.z = 0;
491 static void recalc_sball_matrix(float *xform)
493 cgm_mrotation_quat(xform, &rot);
500 static unsigned int next_pow2(unsigned int x)
511 static void set_fullscreen(int fs)
513 static int win_x, win_y;
516 win_x = glutGet(GLUT_WINDOW_WIDTH);
517 win_y = glutGet(GLUT_WINDOW_HEIGHT);
520 glutReshapeWindow(win_x, win_y);
525 static void set_vsync(int vsync)
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);
536 static void set_vsync(int vsync)
538 if(wgl_swap_interval_ext) {
539 wgl_swap_interval_ext(vsync ? 1 : 0);