foo
[deeprace] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "miniglut.h"
5 #include "game.h"
6
7 static void idle(void);
8 static void reshape(int x, int y);
9 static void keydown(unsigned char key, int x, int y);
10 static void keyup(unsigned char key, int x, int y);
11 static void skeydown(int key, int x, int y);
12 static void skeyup(int key, int x, int y);
13 static void mouse(int bn, int st, int x, int y);
14 static void motion(int x, int y);
15 static void sball_motion(int x, int y, int z);
16 static void sball_rotate(int x, int y, int z);
17 static void sball_button(int bn, int st);
18 static int translate_skey(int key);
19
20 static int warping;
21
22 int main(int argc, char **argv)
23 {
24         glutInit(&argc, argv);
25         glutInitWindowSize(640, 480);
26         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
27         glutCreateWindow("deeprace");
28
29         glutDisplayFunc(game_display);
30         glutIdleFunc(idle);
31         glutReshapeFunc(reshape);
32         glutKeyboardFunc(keydown);
33         glutKeyboardUpFunc(keyup);
34         glutSpecialFunc(skeydown);
35         glutSpecialUpFunc(skeyup);
36         glutMouseFunc(mouse);
37         glutMotionFunc(motion);
38         glutPassiveMotionFunc(motion);
39         glutSpaceballMotionFunc(sball_motion);
40         glutSpaceballRotateFunc(sball_rotate);
41         glutSpaceballButtonFunc(sball_button);
42
43         if(game_init() == -1) {
44                 return 1;
45         }
46         atexit(game_shutdown);
47         glutMainLoop();
48         return 0;
49 }
50
51 void game_swap_buffers(void)
52 {
53         glutSwapBuffers();
54         assert(glGetError() == GL_NO_ERROR);
55 }
56
57 void game_quit(void)
58 {
59         exit(0);
60 }
61
62 void game_fullscreen(int fs)
63 {
64         static int prev_w, prev_h;
65         static int prev_grab;
66
67         if(fs == -1) {
68                 fs = !fullscr;
69         }
70
71         if(fs == fullscr) return;
72
73         if(fs) {
74                 prev_w = glutGet(GLUT_WINDOW_WIDTH);
75                 prev_h = glutGet(GLUT_WINDOW_HEIGHT);
76                 prev_grab = mouse_grabbed;
77                 game_grabmouse(1);
78                 glutFullScreen();
79         } else {
80                 glutReshapeWindow(prev_w, prev_h);
81                 if(!prev_grab) {
82                         game_grabmouse(0);
83                 }
84         }
85         fullscr = fs;
86 }
87
88 void game_grabmouse(int grab)
89 {
90         static int prev_x, prev_y;
91
92         if(grab == -1) {
93                 grab = !mouse_grabbed;
94         }
95
96         if(grab == mouse_grabbed) return;
97
98         if(grab) {
99                 warping = 1;
100                 prev_x = mouse_x;
101                 prev_y = mouse_y;
102                 glutWarpPointer(win_width / 2, win_height / 2);
103                 glutSetCursor(GLUT_CURSOR_NONE);
104         } else {
105                 warping = 1;
106                 glutWarpPointer(prev_x, prev_y);
107                 glutSetCursor(GLUT_CURSOR_INHERIT);
108         }
109         mouse_grabbed = grab;
110 }
111
112
113 static void idle(void)
114 {
115         glutPostRedisplay();
116 }
117
118 static void reshape(int x, int y)
119 {
120         if(fullscr) {
121                 warping = 1;
122                 glutWarpPointer(x / 2, y / 2);
123         }
124         game_reshape(x, y);
125 }
126
127 static void keydown(unsigned char key, int x, int y)
128 {
129         modkeys = glutGetModifiers();
130         game_keyboard(key, 1);
131 }
132
133 static void keyup(unsigned char key, int x, int y)
134 {
135         game_keyboard(key, 0);
136 }
137
138 static void skeydown(int key, int x, int y)
139 {
140         int k;
141         modkeys = glutGetModifiers();
142         if((k = translate_skey(key)) >= 0) {
143                 game_keyboard(k, 1);
144         }
145 }
146
147 static void skeyup(int key, int x, int y)
148 {
149         int k = translate_skey(key);
150         if(k >= 0) {
151                 game_keyboard(k, 0);
152         }
153 }
154
155 static void mouse(int bn, int st, int x, int y)
156 {
157         modkeys = glutGetModifiers();
158         game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
159 }
160
161 static void motion(int x, int y)
162 {
163         if(mouse_grabbed) {
164                 if(!warping) {
165                         game_motion(x, y);
166                         warping = 1;
167                         glutWarpPointer(win_width / 2, win_height / 2);
168                 } else {
169                         warping = 0;
170                 }
171         } else {
172                 game_motion(x, y);
173         }
174 }
175
176 static void sball_motion(int x, int y, int z)
177 {
178 }
179
180 static void sball_rotate(int x, int y, int z)
181 {
182 }
183
184 static void sball_button(int bn, int st)
185 {
186 }
187
188 static int translate_skey(int key)
189 {
190         switch(key) {
191         case GLUT_KEY_LEFT:
192                 return GKEY_LEFT;
193         case GLUT_KEY_UP:
194                 return GKEY_UP;
195         case GLUT_KEY_RIGHT:
196                 return GKEY_RIGHT;
197         case GLUT_KEY_DOWN:
198                 return GKEY_DOWN;
199         case GLUT_KEY_PAGE_UP:
200                 return GKEY_PGUP;
201         case GLUT_KEY_PAGE_DOWN:
202                 return GKEY_PGDOWN;
203         case GLUT_KEY_HOME:
204                 return GKEY_HOME;
205         case GLUT_KEY_END:
206                 return GKEY_END;
207         case GLUT_KEY_INSERT:
208                 return GKEY_INS;
209         default:
210                 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
211                         return key - GLUT_KEY_F1 + GKEY_F1;
212                 }
213         }
214
215         return -1;
216 }