initial commit
[ld42_outofspace] / src / main.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <GL/freeglut.h>
5 #include "game.h"
6
7 #define KEYST_SZ        65536 / 32
8
9 static void display();
10 static void idle();
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 void mouse(int bn, int st, int x, int y);
17 static void motion(int x, int y);
18 static void wheel(int wheel, int dir, int x, int y);
19
20 static long prev_time;
21 static uint32_t keystate[KEYST_SZ];
22 static bool bnstate[16];
23 static unsigned int modkeys;
24
25 int main(int argc, char **argv)
26 {
27         glutInit(&argc, argv);
28         glutInitWindowSize(1024, 600);
29         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_SRGB | GLUT_MULTISAMPLE);
30         glutCreateWindow("ludum dare 42");
31
32         glutDisplayFunc(display);
33         glutIdleFunc(idle);
34         glutReshapeFunc(reshape);
35         glutKeyboardFunc(keydown);
36         glutKeyboardUpFunc(keyup);
37         glutSpecialFunc(skeydown);
38         glutSpecialUpFunc(skeyup);
39         glutMouseFunc(mouse);
40         glutMotionFunc(motion);
41         glutPassiveMotionFunc(motion);
42         glutMouseWheelFunc(wheel);
43
44         if(!game_init()) {
45                 return 1;
46         }
47         atexit(game_cleanup);
48
49         prev_time = glutGet(GLUT_ELAPSED_TIME);
50
51         glutMainLoop();
52         return 0;
53 }
54
55 void game_quit()
56 {
57         exit(0);
58 }
59
60 bool game_keystate(int key)
61 {
62         int idx = key / 32;
63         int bit = key % 32;
64         return keystate[idx] & (1 << bit);
65 }
66
67 bool game_bnstate(int bn)
68 {
69         return bnstate[bn];
70 }
71
72 unsigned int game_modkeys()
73 {
74         return modkeys;
75 }
76
77 static void display()
78 {
79         frame_time = glutGet(GLUT_ELAPSED_TIME);
80         frame_dt = (frame_time - prev_time) / 1000.0f;
81
82         game_draw();
83         glutSwapBuffers();
84 }
85
86 static void idle()
87 {
88         glutPostRedisplay();
89 }
90
91 static void reshape(int x, int y)
92 {
93         glViewport(0, 0, x, y);
94         win_width = x;
95         win_height = y;
96         win_aspect = (float)x / (float)y;
97
98         game_reshape(x, y);
99 }
100
101 static void keydown(unsigned char key, int x, int y)
102 {
103         modkeys = glutGetModifiers();
104         keystate[key / 32] |= (1 << (key % 32));
105         game_keyboard(key, true);
106 }
107
108 static void keyup(unsigned char key, int x, int y)
109 {
110         modkeys = glutGetModifiers();
111         keystate[key / 32] &= ~(1 << (key % 32));
112         game_keyboard(key, false);
113 }
114
115 static int conv_skey(int key)
116 {
117         if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
118                 return KEY_F1 + (key - GLUT_KEY_F1);
119         }
120
121         switch(key) {
122         case GLUT_KEY_LEFT:
123                 return KEY_LEFT;
124         case GLUT_KEY_UP:
125                 return KEY_UP;
126         case GLUT_KEY_RIGHT:
127                 return KEY_RIGHT;
128         case GLUT_KEY_DOWN:
129                 return KEY_DOWN;
130         case GLUT_KEY_PAGE_UP:
131                 return KEY_PGUP;
132         case GLUT_KEY_PAGE_DOWN:
133                 return KEY_PGDOWN;
134         case GLUT_KEY_HOME:
135                 return KEY_HOME;
136         case GLUT_KEY_END:
137                 return KEY_END;
138         case GLUT_KEY_INSERT:
139                 return KEY_INSERT;
140         default:
141                 break;
142         }
143
144         return 0;
145 }
146
147 static void skeydown(int key, int x, int y)
148 {
149         modkeys = glutGetModifiers();
150         keystate[key / 32] |= (1 << (key % 32));
151         game_keyboard(conv_skey(key), true);
152 }
153
154 static void skeyup(int key, int x, int y)
155 {
156         modkeys = glutGetModifiers();
157         keystate[key / 32] &= ~(1 << (key % 32));
158         game_keyboard(conv_skey(key), false);
159 }
160
161 static void mouse(int bn, int st, int x, int y)
162 {
163         int idx = bn - GLUT_LEFT_BUTTON;
164         bool pressed = st == GLUT_DOWN;
165
166         modkeys = glutGetModifiers();
167
168         if(idx == 3) {
169                 wheel(0, 1, x, y);
170                 return;
171         } else if(idx == 4) {
172                 wheel(0, -1, x, y);
173                 return;
174         }
175
176         if(idx < 16) {
177                 bnstate[idx] = pressed;
178         }
179         game_mbutton(idx, pressed, x, y);
180 }
181
182 static void motion(int x, int y)
183 {
184         game_mmotion(x, y);
185 }
186
187 static void wheel(int wheel, int dir, int x, int y)
188 {
189         game_mwheel(dir, x, y);
190 }