build-time config option for VR mode
[vrtris] / src / game.c
1 #include <assert.h>
2 #ifdef BUILD_VR
3 #include <goatvr.h>
4 #endif
5 #include <cgmath/cgmath.h>
6 #include "opengl.h"
7 #include "game.h"
8 #include "screen.h"
9 #include "osd.h"
10 #include "opt.h"
11
12 #define DEFSCR  "game"
13
14 static void calc_framerate(void);
15 static void print_framerate(void);
16
17 #ifdef BUILD_VR
18 static int should_swap;
19 #endif
20 static unsigned long framerate;
21
22
23 int game_init(int argc, char **argv)
24 {
25         if(init_opengl() == -1) {
26                 return -1;
27         }
28
29         if(init_options(argc, argv, "vrtris.conf") == -1) {
30                 return -1;
31         }
32
33         if(init_screens() == -1) {
34                 return -1;
35         }
36
37 #ifdef BUILD_VR
38         if(opt.flags & OPT_VR) {
39                 if(goatvr_init() == -1) {
40                         return -1;
41                 }
42                 goatvr_set_origin_mode(GOATVR_HEAD);
43                 goatvr_set_units_scale(10.0f);
44
45                 goatvr_startvr();
46                 should_swap = goatvr_should_swap();
47         }
48 #endif  /* BUILD_VR */
49
50         glEnable(GL_DEPTH_TEST);
51         glEnable(GL_CULL_FACE);
52         glEnable(GL_LIGHTING);
53         glEnable(GL_LIGHT0);
54
55         return 0;
56 }
57
58 void game_cleanup()
59 {
60 #ifdef BUILD_VR
61         if(opt.flags & OPT_VR) {
62                 goatvr_shutdown();
63         }
64 #endif
65         cleanup_screens();
66 }
67
68 static void update(float dt)
69 {
70         screen->update(dt);
71 }
72
73 void game_display(void)
74 {
75         static long prev_msec;
76         float dt = (float)(time_msec - prev_msec) / 1000.0f;
77         prev_msec = time_msec;
78
79         update(dt);
80
81 #ifdef BUILD_VR
82         if(opt.flags & OPT_VR) {
83                 int i;
84                 goatvr_draw_start();
85                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
86
87                 for(i=0; i<2; i++) {
88                         /* for each eye */
89                         goatvr_draw_eye(i);
90
91                         cgm_mcopy(proj_matrix, goatvr_projection_matrix(i, 0.5, 500.0));
92                         glMatrixMode(GL_PROJECTION);
93                         glLoadMatrixf(proj_matrix);
94
95                         cgm_mcopy(view_matrix, goatvr_view_matrix(i));
96                         glMatrixMode(GL_MODELVIEW);
97                         glLoadMatrixf(view_matrix);
98
99                         screen->draw();
100                         print_framerate();
101                         draw_osd();
102                 }
103
104                 goatvr_draw_done();
105
106                 if(should_swap) {
107                         game_swap_buffers();
108                 }
109
110         } else
111 #endif  /* BUILD_VR */
112         {
113                 /* non-VR mode */
114                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
115
116                 cgm_mperspective(proj_matrix, cgm_deg_to_rad(40.0), win_aspect, 0.5, 500.0);
117                 glMatrixMode(GL_PROJECTION);
118                 glLoadMatrixf(proj_matrix);
119
120                 cgm_midentity(view_matrix);
121                 glMatrixMode(GL_MODELVIEW);
122                 glLoadMatrixf(view_matrix);
123
124                 screen->draw();
125                 print_framerate();
126                 draw_osd();
127
128                 game_swap_buffers();
129         }
130
131         assert(glGetError() == GL_NO_ERROR);
132
133         calc_framerate();
134 }
135
136 void game_reshape(int x, int y)
137 {
138         glViewport(0, 0, x, y);
139 #ifdef BUILD_VR
140         goatvr_set_fb_size(x, y, 1.0f);
141 #endif
142
143         reshape_screens(x, y);
144 }
145
146 void game_keyboard(int key, int pressed)
147 {
148         unsigned int mod = game_get_modifiers();
149
150         if(pressed) {
151                 switch(key) {
152                 case 27:
153                         game_quit();
154                         return;
155
156                 case '\n':
157                 case '\r':
158                         if(mod & MOD_ALT) {
159                                 game_toggle_fullscreen();
160                                 return;
161                         }
162                         break;
163
164                 case KEY_HOME:
165 #ifdef BUILD_VR
166                         if(opt.flags & OPT_VR) {
167                                 goatvr_recenter();
168                         }
169 #endif
170                         break;
171
172                 default:
173                         break;
174                 }
175         }
176
177         screen->keyboard(key, pressed);
178 }
179
180 void game_mouse_button(int bn, int pressed, int x, int y)
181 {
182         screen->mouse(bn, pressed, x, y);
183 }
184
185 void game_mouse_motion(int x, int y)
186 {
187         screen->motion(x, y);
188 }
189
190 void game_mouse_wheel(int dir)
191 {
192         screen->wheel(dir);
193 }
194
195
196 void game_gamepad_axis(int axis, float val)
197 {
198         joy_axis[axis] = val;
199 }
200
201 void game_gamepad_button(int bn, int pressed)
202 {
203         if(pressed) {
204                 joy_bnstate |= (1 << bn);
205         } else {
206                 joy_bnstate &= ~(1 << bn);
207         }
208 }
209
210 static void calc_framerate(void)
211 {
212         static unsigned long nframes;
213         static long prev_upd;
214
215         long elapsed = time_msec - prev_upd;
216         if(elapsed >= 1000) {
217                 framerate = nframes * 10000 / elapsed;
218                 nframes = 1;
219                 prev_upd = time_msec;
220         } else {
221                 ++nframes;
222         }
223 }
224
225 static void print_framerate(void)
226 {
227         print_text(9 * win_width / 10, 20, 1, 1, 0, "fps: %d.%d", framerate / 10, framerate % 10);
228 }