11 static void display(void);
12 static void vblank(void);
13 static void idle(void);
14 static void reshape(int x, int y);
15 static void keydown(unsigned char key, int x, int y);
16 static void keyup(unsigned char key, int x, int y);
17 static void skeydown(int key, int x, int y);
18 static void skeyup(int key, int x, int y);
19 static int translate_special(int skey);
20 static unsigned int next_pow2(unsigned int x);
21 static void set_fullscreen(int fs);
22 static void set_vsync(int vsync);
24 static unsigned int num_pressed;
25 static unsigned char keystate[256];
27 static unsigned long start_time;
28 static unsigned int modkeys;
30 static uint16_t bnstate, bnmask;
32 static float win_aspect;
33 static unsigned int tex;
35 static int tex_xsz, tex_ysz;
36 static uint32_t convbuf[240 * 160];
43 static void (*glx_swap_interval_ext)();
44 static void (*glx_swap_interval_sgi)();
48 static PROC wgl_swap_interval_ext;
51 int main(int argc, char **argv)
53 glutInit(&argc, argv);
54 glutInitWindowSize(800, 600);
55 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
56 glutCreateWindow("GBAjam22 PC build");
58 glutDisplayFunc(display);
60 glutReshapeFunc(reshape);
61 glutKeyboardFunc(keydown);
62 glutKeyboardUpFunc(keyup);
63 glutSpecialFunc(skeydown);
64 glutSpecialUpFunc(skeyup);
66 glutSetCursor(GLUT_CURSOR_NONE);
68 glEnable(GL_TEXTURE_2D);
69 glEnable(GL_CULL_FACE);
72 xdpy = glXGetCurrentDisplay();
73 xwin = glXGetCurrentDrawable();
75 if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
76 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
80 wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
83 glGenTextures(1, &tex);
84 glBindTexture(GL_TEXTURE_2D, tex);
86 tex_xsz = next_pow2(240);
87 tex_ysz = next_pow2(160);
88 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
93 glMatrixMode(GL_TEXTURE);
95 glScalef(240.0f / tex_xsz, 160.0f / tex_ysz, 1);
98 interrupt(INTR_VBLANK, vblank);
102 if(init_screens() == -1) {
103 fprintf(stderr, "failed to initialize screens");
107 if(change_screen(find_screen("game")) == -1) {
108 fprintf(stderr, "failed to find game screen");
116 void select_input(uint16_t bmask)
122 uint16_t get_input(void)
124 uint16_t s = bnstate;
129 #define PACK_RGB32(r, g, b) \
130 ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff) | 0xff000000)
132 #define UNPACK_R16(c) (((c) >> 9) & 0xf8)
133 #define UNPACK_G16(c) (((c) >> 3) & 0xf8)
134 #define UNPACK_B16(c) (((c) << 3) & 0xf8)
136 void present(int buf)
138 int i, npix = 240 * 160;
139 uint32_t *dptr = convbuf;
140 uint8_t *sptr = buf ? gba_vram_lfb1 : gba_vram_lfb0;
142 for(i=0; i<npix; i++) {
144 int r = UNPACK_R16(gba_bgpal[idx]);
145 int g = UNPACK_G16(gba_bgpal[idx]);
146 int b = UNPACK_B16(gba_bgpal[idx]);
147 *dptr++ = PACK_RGB32(b, g, r);
150 glBindTexture(GL_TEXTURE_2D, tex);
151 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 240, 160, GL_RGBA,
152 GL_UNSIGNED_BYTE, convbuf);
154 glMatrixMode(GL_MODELVIEW);
156 if(win_aspect >= 1.5f) {
157 glScalef(1.5f / win_aspect, 1, 1);
159 glScalef(1, win_aspect / 1.5f, 1);
162 glClear(GL_COLOR_BUFFER_BIT);
176 assert(glGetError() == GL_NO_ERROR);
179 static void display(void)
186 static void vblank(void)
190 if(curscr && curscr->vblank) {
195 static void idle(void)
200 static void reshape(int x, int y)
202 win_aspect = (float)x / (float)y;
203 glViewport(0, 0, x, y);
206 static int bnmap(int key)
236 static void keydown(unsigned char key, int x, int y)
248 static void keyup(unsigned char key, int x, int y)
256 static void skeydown(int key, int x, int y)
264 static void skeyup(int key, int x, int y)
272 static unsigned int next_pow2(unsigned int x)
283 static void set_fullscreen(int fs)
285 static int win_x, win_y;
289 win_x = glutGet(GLUT_WINDOW_WIDTH);
290 win_y = glutGet(GLUT_WINDOW_HEIGHT);
295 glutReshapeWindow(win_x, win_y);
302 static void set_vsync(int vsync)
304 vsync = vsync ? 1 : 0;
305 if(glx_swap_interval_ext) {
306 glx_swap_interval_ext(xdpy, xwin, vsync);
307 } else if(glx_swap_interval_sgi) {
308 glx_swap_interval_sgi(vsync);
313 static void set_vsync(int vsync)
315 if(wgl_swap_interval_ext) {
316 wgl_swap_interval_ext(vsync ? 1 : 0);