12 #include "cgmath/cgmath.h"
16 static void display(void);
17 static void idle(void);
18 static void reshape(int x, int y);
19 static void keydown(unsigned char key, int x, int y);
20 static void keyup(unsigned char key, int x, int y);
21 static void skeydown(int key, int x, int y);
22 static void skeyup(int key, int x, int y);
23 static int translate_special(int skey);
24 static void mouse_button(int bn, int st, int x, int y);
25 static void mouse_motion(int x, int y);
26 static void sball_motion(int x, int y, int z);
27 static void sball_rotate(int x, int y, int z);
28 static void sball_button(int bn, int st);
29 static void recalc_sball_matrix(float *xform);
30 static unsigned int next_pow2(unsigned int x);
31 static void set_fullscreen(int fs);
32 static void set_vsync(int vsync);
35 unsigned int joy_bnstate, joy_bndiff, joy_bnpress;
38 {0, w, h, 16, w * 2, 5, 6, 5, 11, 5, 0, 0xf800, 0x7e0, 0x1f, 0xbadf00d, 2, 0}
39 static struct video_mode vmodes[] = {
40 MODE(320, 240), MODE(400, 300), MODE(512, 384), MODE(640, 480),
41 MODE(800, 600), MODE(1024, 768), MODE(1280, 960), MODE(1280, 1024),
42 MODE(1920, 1080), MODE(1600, 1200), MODE(1920, 1200)
44 static struct video_mode *cur_vmode;
46 static unsigned int num_pressed;
47 static unsigned char keystate[256];
49 static unsigned long start_time;
50 static unsigned int modkeys;
52 static int win_width, win_height;
53 static float win_aspect;
54 static unsigned int tex;
61 static void (*glx_swap_interval_ext)();
62 static void (*glx_swap_interval_sgi)();
66 static PROC wgl_swap_interval_ext;
70 static cgm_vec3 pos = {0, 0, 0};
71 static cgm_quat rot = {0, 0, 0, 1};
74 int main(int argc, char **argv)
76 glutInit(&argc, argv);
78 if(glutGet(GLUT_SCREEN_HEIGHT) <= 1024) {
79 glutInitWindowSize(640, 480);
81 glutInitWindowSize(1280, 960);
83 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
84 glutCreateWindow("Mindlapse");
86 glutDisplayFunc(display);
88 glutReshapeFunc(reshape);
89 glutKeyboardFunc(keydown);
90 glutKeyboardUpFunc(keyup);
91 glutSpecialFunc(skeydown);
92 glutSpecialUpFunc(skeyup);
93 glutMouseFunc(mouse_button);
94 glutMotionFunc(mouse_motion);
95 glutPassiveMotionFunc(mouse_motion);
96 glutSpaceballMotionFunc(sball_motion);
97 glutSpaceballRotateFunc(sball_rotate);
98 glutSpaceballButtonFunc(sball_button);
100 glutSetCursor(GLUT_CURSOR_NONE);
102 glEnable(GL_TEXTURE_2D);
103 glEnable(GL_CULL_FACE);
105 if(read_cpuid(&cpuid) == 0) {
109 if(!set_video_mode(match_video_mode(FB_WIDTH, FB_HEIGHT, FB_BPP), 1)) {
114 xdpy = glXGetCurrentDisplay();
115 xwin = glXGetCurrentDrawable();
117 if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
118 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
122 wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
125 if(au_init() == -1) {
129 if(demo_init1(argc, argv) == -1) {
134 set_fullscreen(opt.fullscreen);
135 reshape(glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT));
137 reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
140 if(demo_init2() == -1) {
143 atexit(demo_cleanup);
156 struct video_mode *video_modes(void)
161 int num_video_modes(void)
163 return sizeof vmodes / sizeof *vmodes;
166 struct video_mode *get_video_mode(int idx)
168 if(idx == VMODE_CURRENT) {
174 int match_video_mode(int xsz, int ysz, int bpp)
176 struct video_mode *vm = vmodes;
177 int i, count = num_video_modes();
179 for(i=0; i<count; i++) {
180 if(vm->xsz == xsz && vm->ysz == ysz && vm->bpp == bpp) {
188 static int tex_xsz, tex_ysz;
189 static uint32_t *convbuf;
190 static int convbuf_size;
192 void *set_video_mode(int idx, int nbuf)
194 struct video_mode *vm = vmodes + idx;
196 if(cur_vmode == vm) {
200 glGenTextures(1, &tex);
201 glBindTexture(GL_TEXTURE_2D, tex);
203 tex_xsz = next_pow2(vm->xsz);
204 tex_ysz = next_pow2(vm->ysz);
205 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
206 GL_UNSIGNED_BYTE, 0);
207 if(opt.scaler == SCALER_LINEAR) {
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
209 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
211 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
212 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
215 glMatrixMode(GL_TEXTURE);
217 glScalef((float)vm->xsz / tex_xsz, (float)vm->ysz / tex_ysz, 1);
219 if(vm->xsz * vm->ysz > convbuf_size) {
220 convbuf_size = vm->xsz * vm->ysz;
222 convbuf = malloc(convbuf_size * sizeof *convbuf);
225 if(demo_resizefb(vm->xsz, vm->ysz, vm->bpp) == -1) {
226 fprintf(stderr, "failed to allocate virtual framebuffer\n");
235 void wait_vsync(void)
239 void blit_frame(void *pixels, int vsync)
242 uint32_t *dptr = convbuf;
243 uint16_t *sptr = pixels;
244 static int prev_vsync = -1;
246 if(vsync != prev_vsync) {
251 demo_post_draw(pixels);
253 for(i=0; i<FB_WIDTH * FB_HEIGHT; i++) {
254 int r = UNPACK_R16(*sptr);
255 int g = UNPACK_G16(*sptr);
256 int b = UNPACK_B16(*sptr);
257 *dptr++ = PACK_RGB32(b, g, r);
261 glBindTexture(GL_TEXTURE_2D, tex);
262 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, FB_WIDTH, FB_HEIGHT, GL_RGBA,
263 GL_UNSIGNED_BYTE, convbuf);
265 glMatrixMode(GL_MODELVIEW);
267 if(win_aspect >= FB_ASPECT) {
268 glScalef(FB_ASPECT / win_aspect, 1, 1);
270 glScalef(1, win_aspect / FB_ASPECT, 1);
273 glClear(GL_COLOR_BUFFER_BIT);
287 assert(glGetError() == GL_NO_ERROR);
290 int kb_isdown(int key)
297 return keystate[KB_LALT] + keystate[KB_RALT];
300 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
306 return keystate[key];
310 void init_timer(int res_hz)
314 void reset_timer(void)
316 start_time = glutGet(GLUT_ELAPSED_TIME);
319 unsigned long get_msec(void)
321 return glutGet(GLUT_ELAPSED_TIME) - start_time;
327 void sleep_msec(unsigned long msec)
335 void sleep_msec(unsigned long msec)
341 static void display(void)
343 recalc_sball_matrix(sball_matrix);
345 time_msec = get_msec();
349 static void idle(void)
354 static void reshape(int x, int y)
358 win_aspect = (float)x / (float)y;
359 glViewport(0, 0, x, y);
362 static void keydown(unsigned char key, int x, int y)
364 modkeys = glutGetModifiers();
366 if((key == '\n' || key == '\r') && (modkeys & GLUT_ACTIVE_ALT)) {
368 set_fullscreen(opt.fullscreen);
372 demo_keyboard(key, 1);
375 static void keyup(unsigned char key, int x, int y)
378 demo_keyboard(key, 0);
381 static void skeydown(int key, int x, int y)
383 if(key == GLUT_KEY_F5) {
384 opt.scaler = (opt.scaler + 1) % NUM_SCALERS;
386 if(opt.scaler == SCALER_LINEAR) {
387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
388 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
390 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
391 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
394 key = translate_special(key);
396 demo_keyboard(key, 1);
399 static void skeyup(int key, int x, int y)
401 key = translate_special(key);
403 demo_keyboard(key, 0);
406 static int translate_special(int skey)
419 case GLUT_KEY_PAGE_UP:
421 case GLUT_KEY_PAGE_DOWN:
428 if(skey >= GLUT_KEY_F1 && skey <= GLUT_KEY_F12) {
429 return KB_F1 + skey - GLUT_KEY_F1;
435 static void map_mouse_pos(int *xp, int *yp)
441 *xp = x * FB_WIDTH / win_width;
442 *yp = y * FB_HEIGHT / win_height;
445 static void mouse_button(int bn, int st, int x, int y)
449 map_mouse_pos(&x, &y);
454 case GLUT_LEFT_BUTTON:
457 case GLUT_RIGHT_BUTTON:
460 case GLUT_MIDDLE_BUTTON:
465 if(st == GLUT_DOWN) {
466 mouse_bmask |= 1 << bit;
468 mouse_bmask &= ~(1 << bit);
472 static void mouse_motion(int x, int y)
474 map_mouse_pos(&x, &y);
479 static void sball_motion(int x, int y, int z)
487 static void sball_rotate(int rx, int ry, int rz)
490 float s = (float)rsqrt(rx * rx + ry * ry + rz * rz);
491 cgm_qrotate(&rot, 0.001f / s, rx * s, ry * s, -rz * s);
495 static void sball_button(int bn, int st)
497 pos.x = pos.y = pos.z = 0;
498 rot.x = rot.y = rot.z = 0;
502 static void recalc_sball_matrix(float *xform)
504 cgm_mrotation_quat(xform, &rot);
511 static unsigned int next_pow2(unsigned int x)
522 static void set_fullscreen(int fs)
524 static int win_x, win_y;
527 win_x = glutGet(GLUT_WINDOW_WIDTH);
528 win_y = glutGet(GLUT_WINDOW_HEIGHT);
531 glutReshapeWindow(win_x, win_y);
536 static void set_vsync(int vsync)
538 vsync = vsync ? 1 : 0;
539 if(glx_swap_interval_ext) {
540 glx_swap_interval_ext(xdpy, xwin, vsync);
541 } else if(glx_swap_interval_sgi) {
542 glx_swap_interval_sgi(vsync);
547 static void set_vsync(int vsync)
549 if(wgl_swap_interval_ext) {
550 wgl_swap_interval_ext(vsync ? 1 : 0);