ok now it works nicely in VR
[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
44                 goatvr_startvr();
45                 should_swap = goatvr_should_swap();
46         }
47 #endif  /* BUILD_VR */
48
49         glEnable(GL_DEPTH_TEST);
50         glEnable(GL_CULL_FACE);
51         glEnable(GL_LIGHTING);
52         glEnable(GL_LIGHT0);
53
54         screen->start();
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 #ifdef BUILD_VR
80         if(opt.flags & OPT_VR) {
81                 int i;
82                 goatvr_draw_start();
83                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
84
85                 update(dt);
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                 update(dt);
117
118                 cgm_mperspective(proj_matrix, cgm_deg_to_rad(40.0), win_aspect, 0.5, 500.0);
119                 glMatrixMode(GL_PROJECTION);
120                 glLoadMatrixf(proj_matrix);
121
122                 cgm_midentity(view_matrix);
123                 glMatrixMode(GL_MODELVIEW);
124                 glLoadMatrixf(view_matrix);
125
126                 screen->draw();
127                 print_framerate();
128                 draw_osd();
129
130                 game_swap_buffers();
131         }
132
133         assert(glGetError() == GL_NO_ERROR);
134
135         calc_framerate();
136 }
137
138 void game_reshape(int x, int y)
139 {
140         glViewport(0, 0, x, y);
141 #ifdef BUILD_VR
142         goatvr_set_fb_size(x, y, 1.0f);
143 #endif
144
145         reshape_screens(x, y);
146 }
147
148 void game_keyboard(int key, int pressed)
149 {
150         unsigned int mod = game_get_modifiers();
151
152         if(pressed) {
153                 switch(key) {
154                 case 27:
155                         game_quit();
156                         return;
157
158                 case '\n':
159                 case '\r':
160                         if(mod & MOD_ALT) {
161                                 game_toggle_fullscreen();
162                                 return;
163                         }
164                         break;
165
166                 case KEY_HOME:
167 #ifdef BUILD_VR
168                         if(opt.flags & OPT_VR) {
169                                 goatvr_recenter();
170                         }
171 #endif
172                         break;
173
174                 default:
175                         break;
176                 }
177         }
178
179         screen->keyboard(key, pressed);
180 }
181
182 void game_mouse_button(int bn, int pressed, int x, int y)
183 {
184         screen->mouse(bn, pressed, x, y);
185 }
186
187 void game_mouse_motion(int x, int y)
188 {
189         screen->motion(x, y);
190 }
191
192 void game_mouse_wheel(int dir)
193 {
194         screen->wheel(dir);
195 }
196
197
198 void game_gamepad_axis(int axis, float val)
199 {
200         joy_axis[axis] = val;
201 }
202
203 void game_gamepad_button(int bn, int pressed)
204 {
205         if(pressed) {
206                 joy_bnstate |= (1 << bn);
207         } else {
208                 joy_bnstate &= ~(1 << bn);
209         }
210 }
211
212 static void calc_framerate(void)
213 {
214         static unsigned long nframes;
215         static long prev_upd;
216
217         long elapsed = time_msec - prev_upd;
218         if(elapsed >= 1000) {
219                 framerate = nframes * 10000 / elapsed;
220                 nframes = 1;
221                 prev_upd = time_msec;
222         } else {
223                 ++nframes;
224         }
225 }
226
227 static void print_framerate(void)
228 {
229         print_text(9 * win_width / 10, 20, 1, 1, 0, "fps: %d.%d", framerate / 10, framerate % 10);
230 }