placing white holes, and changed to n-body gravitational pull instead of
[ld42_outofspace] / src / main.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <stdarg.h>
5 #include <GL/freeglut.h>
6 #include "game.h"
7
8 #define KEYST_SZ        65536 / 32
9
10 void draw_text(float x, float y, float r, float g, float b, const char *fmt, ...);
11
12 static void display();
13 static void idle();
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);
22
23 static long prev_time;
24 static uint32_t keystate[KEYST_SZ];
25 static bool bnstate[16];
26 static unsigned int modkeys;
27
28 int main(int argc, char **argv)
29 {
30         glutInit(&argc, argv);
31         glutInitWindowSize(1024, 600);
32         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_SRGB | GLUT_MULTISAMPLE);
33
34         glutInitContextVersion(3, 0);
35         glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
36         glutInitContextFlags(GLUT_DEBUG);
37
38         glutCreateWindow("ludum dare 42");
39
40         glutDisplayFunc(display);
41         glutIdleFunc(idle);
42         glutReshapeFunc(reshape);
43         glutKeyboardFunc(keydown);
44         glutKeyboardUpFunc(keyup);
45         glutSpecialFunc(skeydown);
46         glutSpecialUpFunc(skeyup);
47         glutMouseFunc(mouse);
48         glutMotionFunc(motion);
49         glutPassiveMotionFunc(motion);
50         glutMouseWheelFunc(wheel);
51
52         if(!game_init()) {
53                 return 1;
54         }
55         atexit(game_cleanup);
56
57         prev_time = glutGet(GLUT_ELAPSED_TIME);
58
59         glutMainLoop();
60         return 0;
61 }
62
63 void game_quit()
64 {
65         exit(0);
66 }
67
68 bool game_keystate(int key)
69 {
70         int idx = key / 32;
71         int bit = key % 32;
72         return keystate[idx] & (1 << bit);
73 }
74
75 bool game_bnstate(int bn)
76 {
77         return bnstate[bn];
78 }
79
80 unsigned int game_modkeys()
81 {
82         return modkeys;
83 }
84
85 void draw_text(float x, float y, float r, float g, float b, const char *fmt, ...)
86 {
87         char buf[256], *text = buf;
88         va_list ap;
89
90         va_start(ap, fmt);
91         vsprintf(buf, fmt, ap);
92         va_end(ap);
93
94         glMatrixMode(GL_MODELVIEW);
95         glPushMatrix();
96         glLoadIdentity();
97         glMatrixMode(GL_PROJECTION);
98         glPushMatrix();
99         glLoadIdentity();
100         glOrtho(0, win_width, 0, win_height, -1, 1);
101
102         glPushAttrib(GL_ENABLE_BIT);
103         glDisable(GL_LIGHTING);
104
105         glRasterPos2f(1, 1);
106         glColor3f(r, g, b);
107         while(*text) {
108                 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *text++);
109         }
110
111         glPopAttrib();
112
113         glPopMatrix();
114         glMatrixMode(GL_MODELVIEW);
115         glPopMatrix();
116 }
117
118 static void display()
119 {
120         frame_time = glutGet(GLUT_ELAPSED_TIME);
121         frame_dt = (frame_time - prev_time) / 1000.0f;
122
123         game_draw();
124
125
126         static long frames, fps, prev_fps_upd;
127         draw_text(1, 1, 1, 1, 0, "fps: %ld", fps);
128
129         if(frame_time - prev_fps_upd >= 1000) {
130                 fps = frames;
131                 frames = 0;
132                 prev_fps_upd = frame_time;
133         }
134         frames++;
135
136         glutSwapBuffers();
137 }
138
139 static void idle()
140 {
141         glutPostRedisplay();
142 }
143
144 static void reshape(int x, int y)
145 {
146         glViewport(0, 0, x, y);
147         win_width = x;
148         win_height = y;
149         win_aspect = (float)x / (float)y;
150
151         game_reshape(x, y);
152 }
153
154 static void keydown(unsigned char key, int x, int y)
155 {
156         modkeys = glutGetModifiers();
157         keystate[key / 32] |= (1 << (key % 32));
158         game_keyboard(key, true);
159 }
160
161 static void keyup(unsigned char key, int x, int y)
162 {
163         modkeys = glutGetModifiers();
164         keystate[key / 32] &= ~(1 << (key % 32));
165         game_keyboard(key, false);
166 }
167
168 static int conv_skey(int key)
169 {
170         if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
171                 return KEY_F1 + (key - GLUT_KEY_F1);
172         }
173
174         switch(key) {
175         case GLUT_KEY_LEFT:
176                 return KEY_LEFT;
177         case GLUT_KEY_UP:
178                 return KEY_UP;
179         case GLUT_KEY_RIGHT:
180                 return KEY_RIGHT;
181         case GLUT_KEY_DOWN:
182                 return KEY_DOWN;
183         case GLUT_KEY_PAGE_UP:
184                 return KEY_PGUP;
185         case GLUT_KEY_PAGE_DOWN:
186                 return KEY_PGDOWN;
187         case GLUT_KEY_HOME:
188                 return KEY_HOME;
189         case GLUT_KEY_END:
190                 return KEY_END;
191         case GLUT_KEY_INSERT:
192                 return KEY_INSERT;
193         default:
194                 break;
195         }
196
197         return 0;
198 }
199
200 static void skeydown(int key, int x, int y)
201 {
202         modkeys = glutGetModifiers();
203         keystate[key / 32] |= (1 << (key % 32));
204         game_keyboard(conv_skey(key), true);
205 }
206
207 static void skeyup(int key, int x, int y)
208 {
209         modkeys = glutGetModifiers();
210         keystate[key / 32] &= ~(1 << (key % 32));
211         game_keyboard(conv_skey(key), false);
212 }
213
214 static void mouse(int bn, int st, int x, int y)
215 {
216         int idx = bn - GLUT_LEFT_BUTTON;
217         bool pressed = st == GLUT_DOWN;
218
219         modkeys = glutGetModifiers();
220
221         if(idx == 3) {
222                 wheel(0, 1, x, y);
223                 return;
224         } else if(idx == 4) {
225                 wheel(0, -1, x, y);
226                 return;
227         }
228
229         if(idx < 16) {
230                 bnstate[idx] = pressed;
231         }
232         game_mbutton(idx, pressed, x, y);
233 }
234
235 static void motion(int x, int y)
236 {
237         game_mmotion(x, y);
238 }
239
240 static void wheel(int wheel, int dir, int x, int y)
241 {
242         game_mwheel(dir, x, y);
243 }