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 void mouse(int bn, int st, int x, int y);
20 static void motion(int x, int y);
21 static unsigned int next_pow2(unsigned int x);
22 static void set_fullscreen(int fs);
23 static void set_vsync(int vsync);
25 static unsigned int num_pressed;
26 static unsigned char keystate[256];
28 static unsigned long start_time;
29 static unsigned int modkeys;
31 static uint16_t bnstate, bnmask;
33 static float win_aspect;
34 static unsigned int tex;
36 static int tex_xsz, tex_ysz;
37 static uint32_t convbuf[240 * 160];
44 static void (*glx_swap_interval_ext)();
45 static void (*glx_swap_interval_sgi)();
49 static PROC wgl_swap_interval_ext;
52 int main(int argc, char **argv)
54 glutInit(&argc, argv);
55 glutInitWindowSize(960, 640);
56 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
57 glutCreateWindow("GBAjam22 PC build");
59 glutDisplayFunc(display);
61 glutReshapeFunc(reshape);
62 glutKeyboardFunc(keydown);
63 glutKeyboardUpFunc(keyup);
64 glutSpecialFunc(skeydown);
65 glutSpecialUpFunc(skeyup);
67 glutMotionFunc(motion);
69 glEnable(GL_TEXTURE_2D);
70 glEnable(GL_CULL_FACE);
73 xdpy = glXGetCurrentDisplay();
74 xwin = glXGetCurrentDrawable();
76 if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
77 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
81 wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
84 glGenTextures(1, &tex);
85 glBindTexture(GL_TEXTURE_2D, tex);
87 tex_xsz = next_pow2(240);
88 tex_ysz = next_pow2(160);
89 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
94 glMatrixMode(GL_TEXTURE);
96 glScalef(240.0f / tex_xsz, 160.0f / tex_ysz, 1);
99 interrupt(INTR_VBLANK, vblank);
103 if(init_screens() == -1) {
104 fprintf(stderr, "failed to initialize screens");
108 if(change_screen(find_screen("game")) == -1) {
109 fprintf(stderr, "failed to find game screen");
117 void select_input(uint16_t bmask)
123 uint16_t get_input(void)
125 uint16_t s = bnstate;
130 #define PACK_RGB32(r, g, b) \
131 ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff) | 0xff000000)
133 #define UNPACK_R16(c) (((c) << 3) & 0xf8)
134 #define UNPACK_G16(c) (((c) >> 2) & 0xf8)
135 #define UNPACK_B16(c) (((c) >> 7) & 0xf8)
137 void present(int buf)
139 int i, npix = 240 * 160;
140 uint32_t *dptr = convbuf;
141 uint8_t *sptr = (uint8_t*)(buf ? gba_vram_lfb1 : gba_vram_lfb0);
143 for(i=0; i<npix; i++) {
145 int r = UNPACK_R16(gba_bgpal[idx]);
146 int g = UNPACK_G16(gba_bgpal[idx]);
147 int b = UNPACK_B16(gba_bgpal[idx]);
148 *dptr++ = PACK_RGB32(b, g, r);
151 glBindTexture(GL_TEXTURE_2D, tex);
152 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 240, 160, GL_RGBA,
153 GL_UNSIGNED_BYTE, convbuf);
155 glMatrixMode(GL_MODELVIEW);
157 if(win_aspect >= 1.5f) {
158 glScalef(1.5f / win_aspect, 1, 1);
160 glScalef(1, win_aspect / 1.5f, 1);
163 glClear(GL_COLOR_BUFFER_BIT);
177 assert(glGetError() == GL_NO_ERROR);
180 static void display(void)
187 static void vblank(void)
191 if(curscr && curscr->vblank) {
196 static void idle(void)
201 static void reshape(int x, int y)
203 win_aspect = (float)x / (float)y;
204 glViewport(0, 0, x, y);
207 static int bnmap(int key)
237 static void keydown(unsigned char key, int x, int y)
249 static void keyup(unsigned char key, int x, int y)
257 static void skeydown(int key, int x, int y)
265 static void skeyup(int key, int x, int y)
273 static int mbstate[3];
274 static int prev_x, prev_y;
276 static void mouse(int bn, int st, int x, int y)
278 int bidx = bn - GLUT_LEFT_BUTTON;
279 int press = st == GLUT_DOWN ? 1 : 0;
282 mbstate[bidx] = press;
288 static void motion(int x, int y)
297 if(!(dx | dy)) return;
300 view_dtheta -= dx * 0x100;
301 view_dphi -= dy * 0x100;
304 view_zoom += dy * 0x100;
305 if(view_zoom < 0) view_zoom = 0;
309 static unsigned int next_pow2(unsigned int x)
320 static void set_fullscreen(int fs)
322 static int win_x, win_y;
326 win_x = glutGet(GLUT_WINDOW_WIDTH);
327 win_y = glutGet(GLUT_WINDOW_HEIGHT);
332 glutReshapeWindow(win_x, win_y);
339 static void set_vsync(int vsync)
341 vsync = vsync ? 1 : 0;
342 if(glx_swap_interval_ext) {
343 glx_swap_interval_ext(xdpy, xwin, vsync);
344 } else if(glx_swap_interval_sgi) {
345 glx_swap_interval_sgi(vsync);
350 static void set_vsync(int vsync)
352 if(wgl_swap_interval_ext) {
353 wgl_swap_interval_ext(vsync ? 1 : 0);