a831feb9d171c37d031b1ded10fd89c63e28adb0
[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 "level.h"
7
8 enum {
9         KEY_F1          = GLUT_KEY_F1 | 0x100,
10         KEY_F2          = GLUT_KEY_F2 | 0x100,
11         KEY_F3          = GLUT_KEY_F3 | 0x100,
12         KEY_F4          = GLUT_KEY_F4 | 0x100,
13         KEY_F5          = GLUT_KEY_F5 | 0x100,
14         KEY_F6          = GLUT_KEY_F6 | 0x100,
15         KEY_F7          = GLUT_KEY_F7 | 0x100,
16         KEY_F8          = GLUT_KEY_F8 | 0x100,
17         KEY_F9          = GLUT_KEY_F9 | 0x100,
18         KEY_F10         = GLUT_KEY_F10 | 0x100,
19         KEY_F11         = GLUT_KEY_F11 | 0x100,
20         KEY_F12         = GLUT_KEY_F12 | 0x100,
21         KEY_LEFT        = GLUT_KEY_LEFT | 0x100,
22         KEY_UP          = GLUT_KEY_UP | 0x100,
23         KEY_RIGHT       = GLUT_KEY_RIGHT | 0x100,
24         KEY_DOWN        = GLUT_KEY_DOWN | 0x100,
25         KEY_PGUP        = GLUT_KEY_PAGE_UP | 0x100,
26         KEY_PGDN        = GLUT_KEY_PAGE_DOWN | 0x100,
27         KEY_HOME        = GLUT_KEY_HOME | 0x100,
28         KEY_END         = GLUT_KEY_END | 0x100,
29         KEY_INS         = GLUT_KEY_INSERT | 0x100
30 };
31
32 enum { INP_FWD, INP_BACK, INP_RIGHT, INP_LEFT, INP_FIRE, NUM_INPUTS };
33
34 static int init(void);
35 static void cleanup(void);
36 static void display(void);
37 static void idle(void);
38 static void reshape(int x, int y);
39 static void keydown(unsigned char key, int x, int y);
40 static void keyup(unsigned char key, int x, int y);
41 static void skeydown(int key, int x, int y);
42 static void skeyup(int key, int x, int y);
43 static void mouse(int bn, int st, int x, int y);
44 static void motion(int x, int y);
45
46
47 static long start_time;
48
49 static float cam_theta, cam_phi;
50 static cgm_vec3 cam_pos = {0, -1.6, 0};
51 static float pxform[16];
52
53 static int mouse_x, mouse_y;
54 static int bnstate[8];
55
56 static int inpstate[NUM_INPUTS];
57
58 static int keymap[NUM_INPUTS][2] = {
59         {'w', KEY_UP},
60         {'s', KEY_DOWN},
61         {'d', KEY_RIGHT},
62         {'a', KEY_LEFT},
63         {' ', 0}
64 };
65
66 static struct level lvl;
67
68 int main(int argc, char **argv)
69 {
70         glutInit(&argc, argv);
71         glutInitWindowSize(1280, 800);
72         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
73         glutCreateWindow("cyberay");
74
75         glutDisplayFunc(display);
76         glutIdleFunc(idle);
77         glutReshapeFunc(reshape);
78         glutKeyboardFunc(keydown);
79         glutKeyboardUpFunc(keyup);
80         glutSpecialFunc(skeydown);
81         glutSpecialUpFunc(skeyup);
82         glutMouseFunc(mouse);
83         glutMotionFunc(motion);
84         glutPassiveMotionFunc(motion);
85
86         if(init() == -1) {
87                 return 1;
88         }
89         atexit(cleanup);
90
91         glutMainLoop();
92         return 0;
93 }
94
95 static int init(void)
96 {
97         glEnable(GL_CULL_FACE);
98
99         glEnable(GL_DEPTH_TEST);
100         glEnable(GL_LIGHTING);
101         glEnable(GL_LIGHT0);
102
103         if(load_level(&lvl, "data/test.lvl") == -1) {
104                 return -1;
105         }
106
107         start_time = glutGet(GLUT_ELAPSED_TIME);
108         return 0;
109 }
110
111 static void cleanup(void)
112 {
113         destroy_level(&lvl);
114 }
115
116 #define WALK_SPEED 3.0f
117 static void update(void)
118 {
119         static unsigned int prev_upd;
120         unsigned int msec;
121         float dt, vfwd, vright;
122
123         msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
124         dt = (float)(msec - prev_upd) / 1000.0f;
125         prev_upd = msec;
126
127         vfwd = vright = 0;
128
129         if(inpstate[INP_FWD]) {
130                 vfwd -= WALK_SPEED * dt;
131         }
132         if(inpstate[INP_BACK]) {
133                 vfwd += WALK_SPEED * dt;
134         }
135         if(inpstate[INP_RIGHT]) {
136                 vright -= WALK_SPEED * dt;
137         }
138         if(inpstate[INP_LEFT]) {
139                 vright += WALK_SPEED * dt;
140         }
141
142         cam_pos.x += cos(cam_theta) * vright + sin(cam_theta) * vfwd;
143         cam_pos.z += sin(cam_theta) * vright - cos(cam_theta) * vfwd;
144
145         cgm_midentity(pxform);
146         cgm_mtranslate(pxform, cam_pos.x, cam_pos.y, cam_pos.z);
147         cgm_mrotate_y(pxform, cam_theta);
148         cgm_mrotate_x(pxform, cam_phi);
149 }
150
151 static void display(void)
152 {
153         update();
154
155         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
156
157         glMatrixMode(GL_MODELVIEW);
158         glLoadMatrixf(pxform);
159
160         draw_level(&lvl);
161
162         glutSwapBuffers();
163         assert(glGetError() == GL_NO_ERROR);
164 }
165
166 static void idle(void)
167 {
168         glutPostRedisplay();
169 }
170
171 static void reshape(int x, int y)
172 {
173         float proj[16];
174
175         cgm_mperspective(proj, cgm_deg_to_rad(50.0f), (float)x / (float)y, 0.5, 500.0);
176
177         glMatrixMode(GL_PROJECTION);
178         glLoadMatrixf(proj);
179 }
180
181 static void keyb(int key, int press)
182 {
183         int i;
184
185         for(i=0; i<NUM_INPUTS; i++) {
186                 if(keymap[i][0] == key || keymap[i][1] == key) {
187                         inpstate[i] = press;
188                 }
189         }
190 }
191
192 static void keydown(unsigned char key, int x, int y)
193 {
194         if(key == 27) exit(0);
195         keyb(key, 1);
196 }
197
198 static void keyup(unsigned char key, int x, int y)
199 {
200         keyb(key, 0);
201 }
202
203 static void skeydown(int key, int x, int y)
204 {
205         keyb(key | 0x100, 1);
206 }
207
208 static void skeyup(int key, int x, int y)
209 {
210         keyb(key | 0x100, 0);
211 }
212
213 static void mouse(int bn, int st, int x, int y)
214 {
215         mouse_x = x;
216         mouse_y = y;
217         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN ? 1 : 0;
218 }
219
220 static void motion(int x, int y)
221 {
222         int dx = x - mouse_x;
223         int dy = y - mouse_y;
224         mouse_x = x;
225         mouse_y = y;
226
227         if(!(dx | dy)) return;
228
229         if(bnstate[0]) {
230                 cam_theta += dx * 0.01;
231                 cam_phi += dy * 0.01;
232
233                 if(cam_phi < -M_PI) cam_phi = -M_PI;
234                 if(cam_phi > M_PI) cam_phi = M_PI;
235         }
236 }