5 #include <GL/freeglut.h>
8 #define KEYST_SZ 65536 / 32
10 void draw_text(float x, float y, float r, float g, float b, const char *fmt, ...);
12 static void display();
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 void wheel(int wheel, int dir, int x, int y);
23 static long prev_time;
24 static uint32_t keystate[KEYST_SZ];
25 static bool bnstate[16];
26 static unsigned int modkeys;
28 int main(int argc, char **argv)
30 glutInit(&argc, argv);
31 glutInitWindowSize(1024, 600);
32 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_SRGB | GLUT_MULTISAMPLE);
34 glutInitContextVersion(3, 0);
35 glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
36 glutInitContextFlags(GLUT_DEBUG);
38 glutCreateWindow("ludum dare 42");
40 glutDisplayFunc(display);
42 glutReshapeFunc(reshape);
43 glutKeyboardFunc(keydown);
44 glutKeyboardUpFunc(keyup);
45 glutSpecialFunc(skeydown);
46 glutSpecialUpFunc(skeyup);
48 glutMotionFunc(motion);
49 glutPassiveMotionFunc(motion);
50 glutMouseWheelFunc(wheel);
57 prev_time = glutGet(GLUT_ELAPSED_TIME);
68 bool game_keystate(int key)
72 return keystate[idx] & (1 << bit);
75 bool game_bnstate(int bn)
80 unsigned int game_modkeys()
85 void draw_text(float x, float y, float r, float g, float b, const char *fmt, ...)
87 char buf[256], *text = buf;
91 vsprintf(buf, fmt, ap);
94 glMatrixMode(GL_MODELVIEW);
97 glMatrixMode(GL_PROJECTION);
100 glOrtho(0, win_width, 0, win_height, -1, 1);
102 glDisable(GL_LIGHTING);
107 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *text++);
110 glEnable(GL_LIGHTING);
113 glMatrixMode(GL_MODELVIEW);
117 static void display()
119 frame_time = glutGet(GLUT_ELAPSED_TIME);
120 frame_dt = (frame_time - prev_time) / 1000.0f;
125 static long frames, fps, prev_fps_upd;
126 draw_text(1, 1, 1, 1, 0, "fps: %ld", fps);
128 if(frame_time - prev_fps_upd >= 1000) {
131 prev_fps_upd = frame_time;
143 static void reshape(int x, int y)
145 glViewport(0, 0, x, y);
148 win_aspect = (float)x / (float)y;
153 static void keydown(unsigned char key, int x, int y)
155 modkeys = glutGetModifiers();
156 keystate[key / 32] |= (1 << (key % 32));
157 game_keyboard(key, true);
160 static void keyup(unsigned char key, int x, int y)
162 modkeys = glutGetModifiers();
163 keystate[key / 32] &= ~(1 << (key % 32));
164 game_keyboard(key, false);
167 static int conv_skey(int key)
169 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
170 return KEY_F1 + (key - GLUT_KEY_F1);
182 case GLUT_KEY_PAGE_UP:
184 case GLUT_KEY_PAGE_DOWN:
190 case GLUT_KEY_INSERT:
199 static void skeydown(int key, int x, int y)
201 modkeys = glutGetModifiers();
202 keystate[key / 32] |= (1 << (key % 32));
203 game_keyboard(conv_skey(key), true);
206 static void skeyup(int key, int x, int y)
208 modkeys = glutGetModifiers();
209 keystate[key / 32] &= ~(1 << (key % 32));
210 game_keyboard(conv_skey(key), false);
213 static void mouse(int bn, int st, int x, int y)
215 int idx = bn - GLUT_LEFT_BUTTON;
216 bool pressed = st == GLUT_DOWN;
218 modkeys = glutGetModifiers();
223 } else if(idx == 4) {
229 bnstate[idx] = pressed;
231 game_mbutton(idx, pressed, x, y);
234 static void motion(int x, int y)
239 static void wheel(int wheel, int dir, int x, int y)
241 game_mwheel(dir, x, y);