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);
76 glutInitWindowSize(800, 600);
77 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
78 glutCreateWindow("Mindlapse");
80 glutDisplayFunc(display);
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);
94 glutSetCursor(GLUT_CURSOR_NONE);
96 glEnable(GL_TEXTURE_2D);
97 glEnable(GL_CULL_FACE);
100 if(!set_video_mode(match_video_mode(FB_WIDTH, FB_HEIGHT, FB_BPP), 1)) {
105 xdpy = glXGetCurrentDisplay();
106 xwin = glXGetCurrentDrawable();
108 if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
109 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
113 wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
116 if(au_init() == -1) {
120 if(demo_init(argc, argv) == -1) {
123 atexit(demo_cleanup);
126 set_fullscreen(opt.fullscreen);
140 struct video_mode *video_modes(void)
145 int num_video_modes(void)
147 return sizeof vmodes / sizeof *vmodes;
150 struct video_mode *get_video_mode(int idx)
152 if(idx == VMODE_CURRENT) {
158 int match_video_mode(int xsz, int ysz, int bpp)
160 struct video_mode *vm = vmodes;
161 int i, count = num_video_modes();
163 for(i=0; i<count; i++) {
164 if(vm->xsz == xsz && vm->ysz == ysz && vm->bpp == bpp) {
172 static int tex_xsz, tex_ysz;
173 static uint32_t *convbuf;
174 static int convbuf_size;
176 void *set_video_mode(int idx, int nbuf)
178 struct video_mode *vm = vmodes + idx;
180 if(cur_vmode == vm) {
184 glGenTextures(1, &tex);
185 glBindTexture(GL_TEXTURE_2D, tex);
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);
195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
199 glMatrixMode(GL_TEXTURE);
201 glScalef((float)vm->xsz / tex_xsz, (float)vm->ysz / tex_ysz, 1);
203 if(vm->xsz * vm->ysz > convbuf_size) {
204 convbuf_size = vm->xsz * vm->ysz;
206 convbuf = malloc(convbuf_size * sizeof *convbuf);
209 if(demo_resizefb(vm->xsz, vm->ysz, vm->bpp) == -1) {
210 fprintf(stderr, "failed to allocate virtual framebuffer\n");
219 void wait_vsync(void)
223 void blit_frame(void *pixels, int vsync)
226 uint32_t *dptr = convbuf;
227 uint16_t *sptr = pixels;
228 static int prev_vsync = -1;
230 if(vsync != prev_vsync) {
235 demo_post_draw(pixels);
237 for(i=0; i<FB_WIDTH * FB_HEIGHT; i++) {
238 int r = UNPACK_R16(*sptr);
239 int g = UNPACK_G16(*sptr);
240 int b = UNPACK_B16(*sptr);
241 *dptr++ = PACK_RGB32(b, g, r);
245 glBindTexture(GL_TEXTURE_2D, tex);
246 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, FB_WIDTH, FB_HEIGHT, GL_RGBA,
247 GL_UNSIGNED_BYTE, convbuf);
249 glMatrixMode(GL_MODELVIEW);
251 if(win_aspect >= FB_ASPECT) {
252 glScalef(FB_ASPECT / win_aspect, 1, 1);
254 glScalef(1, win_aspect / FB_ASPECT, 1);
257 glClear(GL_COLOR_BUFFER_BIT);
271 assert(glGetError() == GL_NO_ERROR);
274 int kb_isdown(int key)
281 return keystate[KB_LALT] + keystate[KB_RALT];
284 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
290 return keystate[key];
294 void init_timer(int res_hz)
298 void reset_timer(void)
300 start_time = glutGet(GLUT_ELAPSED_TIME);
303 unsigned long get_msec(void)
305 return glutGet(GLUT_ELAPSED_TIME) - start_time;
311 void sleep_msec(unsigned long msec)
319 void sleep_msec(unsigned long msec)
325 static void display(void)
327 recalc_sball_matrix(sball_matrix);
329 time_msec = get_msec();
333 static void idle(void)
338 static void reshape(int x, int y)
342 win_aspect = (float)x / (float)y;
343 glViewport(0, 0, x, y);
346 static void keydown(unsigned char key, int x, int y)
348 modkeys = glutGetModifiers();
350 if((key == '\n' || key == '\r') && (modkeys & GLUT_ACTIVE_ALT)) {
352 set_fullscreen(opt.fullscreen);
356 demo_keyboard(key, 1);
359 static void keyup(unsigned char key, int x, int y)
362 demo_keyboard(key, 0);
365 static void skeydown(int key, int x, int y)
367 if(key == GLUT_KEY_F5) {
368 opt.scaler = (opt.scaler + 1) % NUM_SCALERS;
370 if(opt.scaler == SCALER_LINEAR) {
371 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
372 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
374 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
375 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
378 key = translate_special(key);
380 demo_keyboard(key, 1);
383 static void skeyup(int key, int x, int y)
385 key = translate_special(key);
387 demo_keyboard(key, 0);
390 static int translate_special(int skey)
403 case GLUT_KEY_PAGE_UP:
405 case GLUT_KEY_PAGE_DOWN:
412 if(skey >= GLUT_KEY_F1 && skey <= GLUT_KEY_F12) {
413 return KB_F1 + skey - GLUT_KEY_F1;
419 static void map_mouse_pos(int *xp, int *yp)
425 *xp = x * FB_WIDTH / win_width;
426 *yp = y * FB_HEIGHT / win_height;
429 static void mouse_button(int bn, int st, int x, int y)
431 map_mouse_pos(&x, &y);
435 if(st == GLUT_DOWN) {
436 mouse_bmask |= 1 << bn;
438 mouse_bmask &= ~(1 << bn);
442 static void mouse_motion(int x, int y)
444 map_mouse_pos(&x, &y);
449 static void sball_motion(int x, int y, int z)
457 static void sball_rotate(int rx, int ry, int rz)
460 float s = (float)rsqrt(rx * rx + ry * ry + rz * rz);
461 cgm_qrotate(&rot, 0.001f / s, rx * s, ry * s, -rz * s);
465 static void sball_button(int bn, int st)
467 pos.x = pos.y = pos.z = 0;
468 rot.x = rot.y = rot.z = 0;
472 static void recalc_sball_matrix(float *xform)
474 cgm_mrotation_quat(xform, &rot);
481 static unsigned int next_pow2(unsigned int x)
492 static void set_fullscreen(int fs)
494 static int win_x, win_y;
497 win_x = glutGet(GLUT_WINDOW_WIDTH);
498 win_y = glutGet(GLUT_WINDOW_HEIGHT);
501 glutReshapeWindow(win_x, win_y);
506 static void set_vsync(int vsync)
508 vsync = vsync ? 1 : 0;
509 if(glx_swap_interval_ext) {
510 glx_swap_interval_ext(xdpy, xwin, vsync);
511 } else if(glx_swap_interval_sgi) {
512 glx_swap_interval_sgi(vsync);
517 static void set_vsync(int vsync)
519 if(wgl_swap_interval_ext) {
520 wgl_swap_interval_ext(vsync ? 1 : 0);