added optcfg and fixed camera xform
[cyberay] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <cgmath/cgmath.h>
5 #include "miniglut.h"
6 #include "game.h"
7 #include "level.h"
8 #include "rt.h"
9
10 enum {
11         KEY_F1          = GLUT_KEY_F1 | 0x100,
12         KEY_F2          = GLUT_KEY_F2 | 0x100,
13         KEY_F3          = GLUT_KEY_F3 | 0x100,
14         KEY_F4          = GLUT_KEY_F4 | 0x100,
15         KEY_F5          = GLUT_KEY_F5 | 0x100,
16         KEY_F6          = GLUT_KEY_F6 | 0x100,
17         KEY_F7          = GLUT_KEY_F7 | 0x100,
18         KEY_F8          = GLUT_KEY_F8 | 0x100,
19         KEY_F9          = GLUT_KEY_F9 | 0x100,
20         KEY_F10         = GLUT_KEY_F10 | 0x100,
21         KEY_F11         = GLUT_KEY_F11 | 0x100,
22         KEY_F12         = GLUT_KEY_F12 | 0x100,
23         KEY_LEFT        = GLUT_KEY_LEFT | 0x100,
24         KEY_UP          = GLUT_KEY_UP | 0x100,
25         KEY_RIGHT       = GLUT_KEY_RIGHT | 0x100,
26         KEY_DOWN        = GLUT_KEY_DOWN | 0x100,
27         KEY_PGUP        = GLUT_KEY_PAGE_UP | 0x100,
28         KEY_PGDN        = GLUT_KEY_PAGE_DOWN | 0x100,
29         KEY_HOME        = GLUT_KEY_HOME | 0x100,
30         KEY_END         = GLUT_KEY_END | 0x100,
31         KEY_INS         = GLUT_KEY_INSERT | 0x100
32 };
33
34 enum { INP_FWD, INP_BACK, INP_RIGHT, INP_LEFT, INP_FIRE, NUM_INPUTS };
35
36 static int init(void);
37 static void cleanup(void);
38 static void display(void);
39 static void idle(void);
40 static void reshape(int x, int y);
41 static void keydown(unsigned char key, int x, int y);
42 static void keyup(unsigned char key, int x, int y);
43 static void skeydown(int key, int x, int y);
44 static void skeyup(int key, int x, int y);
45 static void mouse(int bn, int st, int x, int y);
46 static void motion(int x, int y);
47 static unsigned int nextpow2(unsigned int x);
48
49 static long start_time;
50
51 static float cam_theta, cam_phi;
52 static cgm_vec3 cam_pos = {0, 1.6, 0};
53
54 static int mouse_x, mouse_y;
55 static int bnstate[8];
56
57 static int inpstate[NUM_INPUTS];
58
59 static int keymap[NUM_INPUTS][2] = {
60         {'w', KEY_UP},
61         {'s', KEY_DOWN},
62         {'d', KEY_RIGHT},
63         {'a', KEY_LEFT},
64         {' ', 0}
65 };
66
67 static unsigned int tex;
68 static int tex_width, tex_height;
69 static int tex_intfmt;
70 static float tex_xform[16];
71
72
73 int main(int argc, char **argv)
74 {
75         glutInit(&argc, argv);
76
77         if(init_options(argc, argv) == -1) {
78                 return 1;
79         }
80
81         glutInitWindowSize(opt.width, opt.height);
82         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
83         glutCreateWindow("cyberay");
84
85         glutDisplayFunc(display);
86         glutIdleFunc(idle);
87         glutReshapeFunc(reshape);
88         glutKeyboardFunc(keydown);
89         glutKeyboardUpFunc(keyup);
90         glutSpecialFunc(skeydown);
91         glutSpecialUpFunc(skeyup);
92         glutMouseFunc(mouse);
93         glutMotionFunc(motion);
94         glutPassiveMotionFunc(motion);
95
96         if(init() == -1) {
97                 return 1;
98         }
99         atexit(cleanup);
100
101         glutMainLoop();
102         return 0;
103 }
104
105 static int init(void)
106 {
107         if(!(tpool = tpool_create(0))) {
108                 fprintf(stderr, "failed to create thread pool\n");
109                 return -1;
110         }
111
112         glEnable(GL_CULL_FACE);
113
114
115         glGenTextures(1, &tex);
116         glBindTexture(GL_TEXTURE_2D, tex);
117         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
118         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
119
120         tex_intfmt = GL_RGB16F;
121
122         if(load_level(&lvl, "data/test.lvl") == -1) {
123                 return -1;
124         }
125
126         start_time = glutGet(GLUT_ELAPSED_TIME);
127         return 0;
128 }
129
130 static void cleanup(void)
131 {
132         destroy_level(&lvl);
133
134         glDeleteTextures(1, &tex);
135
136         tpool_destroy(tpool);
137 }
138
139 #define WALK_SPEED 3.0f
140 static void update(void)
141 {
142         static unsigned int prev_upd;
143         unsigned int msec;
144         float dt, vfwd, vright;
145
146         msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
147         dt = (float)(msec - prev_upd) / 1000.0f;
148         prev_upd = msec;
149
150         vfwd = vright = 0;
151
152         if(inpstate[INP_FWD]) {
153                 vfwd -= WALK_SPEED * dt;
154         }
155         if(inpstate[INP_BACK]) {
156                 vfwd += WALK_SPEED * dt;
157         }
158         if(inpstate[INP_RIGHT]) {
159                 vright += WALK_SPEED * dt;
160         }
161         if(inpstate[INP_LEFT]) {
162                 vright -= WALK_SPEED * dt;
163         }
164
165         cam_pos.x += cos(cam_theta) * vright + sin(cam_theta) * vfwd;
166         cam_pos.z += -sin(cam_theta) * vright + cos(cam_theta) * vfwd;
167
168         cgm_midentity(view_xform);
169         cgm_mrotate_x(view_xform, cam_phi);
170         cgm_mrotate_y(view_xform, cam_theta);
171         cgm_mtranslate(view_xform, cam_pos.x, cam_pos.y, cam_pos.z);
172 }
173
174 static void display(void)
175 {
176         update();
177
178         render();
179         glBindTexture(GL_TEXTURE_2D, tex);
180         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, fb.width, fb.height, GL_RGB, GL_FLOAT, fb.pixels);
181         glEnable(GL_TEXTURE_2D);
182
183         glBegin(GL_QUADS);
184         glTexCoord2f(0, 1);
185         glVertex2f(-1, -1);
186         glTexCoord2f(1, 1);
187         glVertex2f(1, -1);
188         glTexCoord2f(1, 0);
189         glVertex2f(1, 1);
190         glTexCoord2f(0, 0);
191         glVertex2f(-1, 1);
192         glEnd();
193
194         glutSwapBuffers();
195         assert(glGetError() == GL_NO_ERROR);
196 }
197
198 static void idle(void)
199 {
200         glutPostRedisplay();
201 }
202
203 static void reshape(int x, int y)
204 {
205         glViewport(0, 0, x, y);
206
207         if(x > tex_width || y > tex_height) {
208                 tex_width = nextpow2(x);
209                 tex_height = nextpow2(y);
210
211                 glBindTexture(GL_TEXTURE_2D, tex);
212                 glTexImage2D(GL_TEXTURE_2D, 0, tex_intfmt, tex_width, tex_height, 0, GL_RGB, GL_FLOAT, 0);
213         }
214         fbsize(x, y);
215         cgm_mscaling(tex_xform, (float)x / tex_width, (float)y / tex_height, 1.0f);
216
217         glMatrixMode(GL_TEXTURE);
218         glLoadMatrixf(tex_xform);
219 }
220
221 static void keyb(int key, int press)
222 {
223         int i;
224
225         for(i=0; i<NUM_INPUTS; i++) {
226                 if(keymap[i][0] == key || keymap[i][1] == key) {
227                         inpstate[i] = press;
228                 }
229         }
230 }
231
232 static void keydown(unsigned char key, int x, int y)
233 {
234         if(key == 27) exit(0);
235         keyb(key, 1);
236 }
237
238 static void keyup(unsigned char key, int x, int y)
239 {
240         keyb(key, 0);
241 }
242
243 static void skeydown(int key, int x, int y)
244 {
245         keyb(key | 0x100, 1);
246 }
247
248 static void skeyup(int key, int x, int y)
249 {
250         keyb(key | 0x100, 0);
251 }
252
253 static void mouse(int bn, int st, int x, int y)
254 {
255         mouse_x = x;
256         mouse_y = y;
257         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN ? 1 : 0;
258 }
259
260 static void motion(int x, int y)
261 {
262         int dx = x - mouse_x;
263         int dy = y - mouse_y;
264         mouse_x = x;
265         mouse_y = y;
266
267         if(!(dx | dy)) return;
268
269         if(bnstate[0]) {
270                 cam_theta -= dx * 0.01;
271                 cam_phi -= dy * 0.01;
272
273                 if(cam_phi < -M_PI) cam_phi = -M_PI;
274                 if(cam_phi > M_PI) cam_phi = M_PI;
275         }
276 }
277
278 static unsigned int nextpow2(unsigned int x)
279 {
280         x--;
281         x |= x >> 1;
282         x |= x >> 2;
283         x |= x >> 4;
284         x |= x >> 8;
285         x |= x >> 16;
286         return x + 1;
287 }