vrshoot
view src/main_glut.cc @ 1:e7ca128b8713
looks nice :)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Feb 2014 00:35:22 +0200 |
parents | |
children |
line source
1 #include <stdlib.h>
2 #include <ctype.h>
3 #include <assert.h>
4 #include "opengl.h"
6 #include "game.h"
7 #include "input.h"
8 #include "screen.h"
9 #include "logger.h"
11 /* called by any other part of the game when a redisplay is needed
12 * (mostly useful for tools which do not redisplay continuously)
13 */
14 void request_redisplay();
16 void swap_buffers();
18 static void display(void);
19 static void reshape(int x, int y);
20 static void keydown(unsigned char key, int x, int y);
21 static void keyup(unsigned char key, int x, int y);
22 static void skeydown(int key, int x, int y);
23 static void skeyup(int key, int x, int y);
24 static void mouse(int bn, int state, int x, int y);
25 static void motion(int x, int y);
26 static void passive_motion(int x, int y);
28 static long redisp_interval;
30 int main(int argc, char **argv)
31 {
32 glutInit(&argc, argv);
33 glutInitWindowSize(800, 600);
34 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
35 glutCreateWindow("Xmass inferno");
37 glutDisplayFunc(display);
38 glutReshapeFunc(reshape);
39 glutKeyboardFunc(keydown);
40 glutKeyboardUpFunc(keyup);
41 glutSpecialFunc(skeydown);
42 glutSpecialUpFunc(skeyup);
43 glutMouseFunc(mouse);
44 glutMotionFunc(motion);
45 glutPassiveMotionFunc(passive_motion);
47 game_set_args(argc, argv);
48 if(!game_init()) {
49 return 1;
50 }
51 atexit(game_cleanup);
53 glutMainLoop();
54 return 0;
55 }
57 void request_redisplay()
58 {
59 glutPostRedisplay();
60 }
62 void swap_buffers()
63 {
64 glUseProgram(0);
65 glutSwapBuffers();
66 }
68 static void display()
69 {
70 long ival = current_screen()->redisplay_interval();
71 if(ival && !redisp_interval) {
72 debug_log("switching to auto-redisplay (interval: %ld)\n", redisp_interval);
73 glutIdleFunc(request_redisplay);
74 }
75 if(!ival && redisp_interval) {
76 debug_log("switching to manual redisplay\n");
77 glutIdleFunc(0);
78 }
79 redisp_interval = ival;
81 game_display();
82 // TODO implement framerate limiter
83 }
86 static void reshape(int x, int y)
87 {
88 game_reshape(x, y);
89 }
91 /* GLUT doesn't have any callbacks for modifier keys, so we fake it by calling
92 * this function all over the place, to generate fake events whenever a modifier
93 * key is detected to have changed state.
94 * The limitation is that this will only detect modkey changes whenever a regular
95 * key is pressed or released, or when a mouse button is pressed or released.
96 */
97 static void handle_modkeys()
98 {
99 static int modstate;
101 int mod = glutGetModifiers();
102 if(mod != modstate) {
103 int diff = mod ^ modstate;
105 if(diff & GLUT_ACTIVE_SHIFT) {
106 bool pressed = mod & GLUT_ACTIVE_SHIFT;
107 game_key(KEY_LSHIFT, pressed);
108 }
109 if(diff & GLUT_ACTIVE_CTRL) {
110 bool pressed = mod & GLUT_ACTIVE_CTRL;
111 game_key(KEY_LCTRL, pressed);
112 }
113 if(diff & GLUT_ACTIVE_ALT) {
114 bool pressed = mod & GLUT_ACTIVE_ALT;
115 game_key(KEY_LALT, pressed);
116 }
117 }
118 modstate = mod;
119 }
121 static int translate_special_key(int key)
122 {
123 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
124 return (key - GLUT_KEY_F1) + KEY_F1;
125 }
126 if(key >= GLUT_KEY_LEFT && key <= GLUT_KEY_PAGE_DOWN) {
127 return (key - GLUT_KEY_LEFT) + KEY_LEFT;
128 }
130 switch(key) {
131 case GLUT_KEY_HOME:
132 return KEY_HOME;
133 case GLUT_KEY_END:
134 return KEY_END;
135 case GLUT_KEY_INSERT:
136 return KEY_INSERT;
138 default:
139 break;
140 }
141 return 0;
142 }
144 static void keydown(unsigned char c, int x, int y)
145 {
146 int key = c;
147 if(key == 127) {
148 key = KEY_DELETE;
149 }
151 handle_modkeys();
152 game_key(key, true);
153 }
155 static void keyup(unsigned char c, int x, int y)
156 {
157 int key = c;
158 if(key == 127) {
159 key = KEY_DELETE;
160 }
162 handle_modkeys();
163 game_key(key, false);
164 }
166 static void skeydown(int key, int x, int y)
167 {
168 handle_modkeys();
170 if((key = translate_special_key(key)) > 0) {
171 game_key(key, true);
172 }
173 }
175 static void skeyup(int key, int x, int y)
176 {
177 handle_modkeys();
179 if((key = translate_special_key(key)) > 0) {
180 game_key(key, false);
181 }
182 }
184 static void mouse(int bn, int state, int x, int y)
185 {
186 handle_modkeys();
188 int bnidx = bn - GLUT_LEFT_BUTTON;
189 bool bnstate = state == GLUT_DOWN;
191 current_screen()->button(bnidx, bnstate, x, y);
192 set_mouse_state(x, y, bnidx, bnstate); // this MUST be after the event dispatch
193 }
195 static void motion(int x, int y)
196 {
197 set_mouse_state(x, y);
198 current_screen()->motion(x, y, false);
199 }
201 static void passive_motion(int x, int y)
202 {
203 set_mouse_state(x, y);
204 current_screen()->motion(x, y, true);
205 }