9 static void display(void);
10 static void idle(void);
11 static void reshape(int x, int y);
12 static void keydown(unsigned char key, int x, int y);
13 static void keyup(unsigned char key, int x, int y);
14 static void skeydown(int key, int x, int y);
15 static void skeyup(int key, int x, int y);
16 static int translate_special(int skey);
17 static unsigned int next_pow2(unsigned int x);
18 static void set_fullscreen(int fs);
19 static void set_vsync(int vsync);
21 static unsigned int num_pressed;
22 static unsigned char keystate[256];
24 static unsigned long start_time;
25 static unsigned int modkeys;
27 static float win_aspect;
28 static unsigned int tex;
30 static int tex_xsz, tex_ysz;
31 static uint32_t convbuf[240 * 160];
38 static void (*glx_swap_interval_ext)();
39 static void (*glx_swap_interval_sgi)();
43 static PROC wgl_swap_interval_ext;
46 int main(int argc, char **argv)
48 glutInit(&argc, argv);
49 glutInitWindowSize(800, 600);
50 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
51 glutCreateWindow("GBAjam22 PC build");
53 glutDisplayFunc(display);
55 glutReshapeFunc(reshape);
56 glutKeyboardFunc(keydown);
57 glutKeyboardUpFunc(keyup);
58 glutSpecialFunc(skeydown);
59 glutSpecialUpFunc(skeyup);
61 glutSetCursor(GLUT_CURSOR_NONE);
63 glEnable(GL_TEXTURE_2D);
64 glEnable(GL_CULL_FACE);
67 xdpy = glXGetCurrentDisplay();
68 xwin = glXGetCurrentDrawable();
70 if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
71 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
75 wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
78 glGenTextures(1, &tex);
79 glBindTexture(GL_TEXTURE_2D, tex);
81 tex_xsz = next_pow2(240);
82 tex_ysz = next_pow2(160);
83 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
88 glMatrixMode(GL_TEXTURE);
90 glScalef(240.0f / tex_xsz, 160.0f / tex_ysz, 1);
96 void blit_frame(void *pixels, int vsync)
98 int i, npix = fb_width * fb_height;
99 uint32_t *dptr = convbuf;
100 uint16_t *sptr = pixels;
102 for(i=0; i<npix; i++) {
103 int r = UNPACK_R16(*sptr);
104 int g = UNPACK_G16(*sptr);
105 int b = UNPACK_B16(*sptr);
106 *dptr++ = PACK_RGB32(b, g, r);
110 glBindTexture(GL_TEXTURE_2D, tex);
111 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, fb_width, fb_height, GL_RGBA,
112 GL_UNSIGNED_BYTE, convbuf);
114 glMatrixMode(GL_MODELVIEW);
116 if(win_aspect >= 1.5f) {
117 glScalef(1.5f / win_aspect, 1, 1);
119 glScalef(1, win_aspect / 1.5f, 1);
122 glClear(GL_COLOR_BUFFER_BIT);
136 assert(glGetError() == GL_NO_ERROR);
139 int kb_isdown(int key)
146 return keystate[KB_LALT] + keystate[KB_RALT];
149 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
155 return keystate[key];
158 static void display(void)
162 time_msec = get_msec();
166 static void idle(void)
171 static void reshape(int x, int y)
173 win_aspect = (float)x / (float)y;
174 glViewport(0, 0, x, y);
177 static void keydown(unsigned char key, int x, int y)
179 modkeys = glutGetModifiers();
185 static void keyup(unsigned char key, int x, int y)
191 static void skeydown(int key, int x, int y)
193 key = translate_special(key);
198 static void skeyup(int key, int x, int y)
200 key = translate_special(key);
205 static int translate_special(int skey)
210 /* case GLUT_KEY_LEFT:
218 case GLUT_KEY_PAGE_UP:
220 case GLUT_KEY_PAGE_DOWN:
227 if(skey >= GLUT_KEY_F1 && skey <= GLUT_KEY_F12) {
228 return KB_F1 + skey - GLUT_KEY_F1;
235 static unsigned int next_pow2(unsigned int x)
246 static void set_fullscreen(int fs)
248 static int win_x, win_y;
252 win_x = glutGet(GLUT_WINDOW_WIDTH);
253 win_y = glutGet(GLUT_WINDOW_HEIGHT);
258 glutReshapeWindow(win_x, win_y);
265 static void set_vsync(int vsync)
267 vsync = vsync ? 1 : 0;
268 if(glx_swap_interval_ext) {
269 glx_swap_interval_ext(xdpy, xwin, vsync);
270 } else if(glx_swap_interval_sgi) {
271 glx_swap_interval_sgi(vsync);
276 static void set_vsync(int vsync)
278 if(wgl_swap_interval_ext) {
279 wgl_swap_interval_ext(vsync ? 1 : 0);