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);
33 glutCreateWindow("ludum dare 42");
35 glutDisplayFunc(display);
37 glutReshapeFunc(reshape);
38 glutKeyboardFunc(keydown);
39 glutKeyboardUpFunc(keyup);
40 glutSpecialFunc(skeydown);
41 glutSpecialUpFunc(skeyup);
43 glutMotionFunc(motion);
44 glutPassiveMotionFunc(motion);
45 glutMouseWheelFunc(wheel);
52 prev_time = glutGet(GLUT_ELAPSED_TIME);
63 bool game_keystate(int key)
67 return keystate[idx] & (1 << bit);
70 bool game_bnstate(int bn)
75 unsigned int game_modkeys()
80 void draw_text(float x, float y, float r, float g, float b, const char *fmt, ...)
82 char buf[256], *text = buf;
86 vsprintf(buf, fmt, ap);
89 glMatrixMode(GL_MODELVIEW);
92 glMatrixMode(GL_PROJECTION);
95 glOrtho(0, win_width, 0, win_height, -1, 1);
97 glDisable(GL_LIGHTING);
102 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *text++);
105 glEnable(GL_LIGHTING);
108 glMatrixMode(GL_MODELVIEW);
112 static void display()
114 frame_time = glutGet(GLUT_ELAPSED_TIME);
115 frame_dt = (frame_time - prev_time) / 1000.0f;
120 static long frames, fps, prev_fps_upd;
121 draw_text(1, 1, 1, 1, 0, "fps: %ld", fps);
123 if(frame_time - prev_fps_upd >= 1000) {
126 prev_fps_upd = frame_time;
138 static void reshape(int x, int y)
140 glViewport(0, 0, x, y);
143 win_aspect = (float)x / (float)y;
148 static void keydown(unsigned char key, int x, int y)
150 modkeys = glutGetModifiers();
151 keystate[key / 32] |= (1 << (key % 32));
152 game_keyboard(key, true);
155 static void keyup(unsigned char key, int x, int y)
157 modkeys = glutGetModifiers();
158 keystate[key / 32] &= ~(1 << (key % 32));
159 game_keyboard(key, false);
162 static int conv_skey(int key)
164 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
165 return KEY_F1 + (key - GLUT_KEY_F1);
177 case GLUT_KEY_PAGE_UP:
179 case GLUT_KEY_PAGE_DOWN:
185 case GLUT_KEY_INSERT:
194 static void skeydown(int key, int x, int y)
196 modkeys = glutGetModifiers();
197 keystate[key / 32] |= (1 << (key % 32));
198 game_keyboard(conv_skey(key), true);
201 static void skeyup(int key, int x, int y)
203 modkeys = glutGetModifiers();
204 keystate[key / 32] &= ~(1 << (key % 32));
205 game_keyboard(conv_skey(key), false);
208 static void mouse(int bn, int st, int x, int y)
210 int idx = bn - GLUT_LEFT_BUTTON;
211 bool pressed = st == GLUT_DOWN;
213 modkeys = glutGetModifiers();
218 } else if(idx == 4) {
224 bnstate[idx] = pressed;
226 game_mbutton(idx, pressed, x, y);
229 static void motion(int x, int y)
234 static void wheel(int wheel, int dir, int x, int y)
236 game_mwheel(dir, x, y);