VR debug
[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         screen->start();
56         return 0;
57 }
58
59 void game_cleanup()
60 {
61 #ifdef BUILD_VR
62         if(opt.flags & OPT_VR) {
63                 goatvr_shutdown();
64         }
65 #endif
66         cleanup_screens();
67 }
68
69 static void update(float dt)
70 {
71         screen->update(dt);
72 }
73
74 void game_display(void)
75 {
76         static long prev_msec;
77         float dt = (float)(time_msec - prev_msec) / 1000.0f;
78         prev_msec = time_msec;
79
80         update(dt);
81
82 #ifdef BUILD_VR
83         if(opt.flags & OPT_VR) {
84                 int i;
85                 goatvr_draw_start();
86                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
87
88                 for(i=0; i<2; i++) {
89                         /* for each eye */
90                         goatvr_draw_eye(i);
91
92                         cgm_mcopy(proj_matrix, goatvr_projection_matrix(i, 0.5, 500.0));
93                         glMatrixMode(GL_PROJECTION);
94                         glLoadMatrixf(proj_matrix);
95
96                         cgm_mcopy(view_matrix, goatvr_view_matrix(i));
97                         glMatrixMode(GL_MODELVIEW);
98                         glLoadMatrixf(view_matrix);
99
100                         screen->draw();
101                         print_framerate();
102                         draw_osd();
103                 }
104
105                 goatvr_draw_done();
106
107                 if(should_swap) {
108                         game_swap_buffers();
109                 }
110
111         } else
112 #endif  /* BUILD_VR */
113         {
114                 /* non-VR mode */
115                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
116
117                 cgm_mperspective(proj_matrix, cgm_deg_to_rad(40.0), win_aspect, 0.5, 500.0);
118                 glMatrixMode(GL_PROJECTION);
119                 glLoadMatrixf(proj_matrix);
120
121                 cgm_midentity(view_matrix);
122                 glMatrixMode(GL_MODELVIEW);
123                 glLoadMatrixf(view_matrix);
124
125                 screen->draw();
126                 print_framerate();
127                 draw_osd();
128
129                 game_swap_buffers();
130         }
131
132         assert(glGetError() == GL_NO_ERROR);
133
134         calc_framerate();
135 }
136
137 void game_reshape(int x, int y)
138 {
139         glViewport(0, 0, x, y);
140 #ifdef BUILD_VR
141         goatvr_set_fb_size(x, y, 1.0f);
142 #endif
143
144         reshape_screens(x, y);
145 }
146
147 void game_keyboard(int key, int pressed)
148 {
149         unsigned int mod = game_get_modifiers();
150
151         if(pressed) {
152                 switch(key) {
153                 case 27:
154                         game_quit();
155                         return;
156
157                 case '\n':
158                 case '\r':
159                         if(mod & MOD_ALT) {
160                                 game_toggle_fullscreen();
161                                 return;
162                         }
163                         break;
164
165                 case KEY_HOME:
166 #ifdef BUILD_VR
167                         if(opt.flags & OPT_VR) {
168                                 goatvr_recenter();
169                         }
170 #endif
171                         break;
172
173                 default:
174                         break;
175                 }
176         }
177
178         screen->keyboard(key, pressed);
179 }
180
181 void game_mouse_button(int bn, int pressed, int x, int y)
182 {
183         screen->mouse(bn, pressed, x, y);
184 }
185
186 void game_mouse_motion(int x, int y)
187 {
188         screen->motion(x, y);
189 }
190
191 void game_mouse_wheel(int dir)
192 {
193         screen->wheel(dir);
194 }
195
196
197 void game_gamepad_axis(int axis, float val)
198 {
199         joy_axis[axis] = val;
200 }
201
202 void game_gamepad_button(int bn, int pressed)
203 {
204         if(pressed) {
205                 joy_bnstate |= (1 << bn);
206         } else {
207                 joy_bnstate &= ~(1 << bn);
208         }
209 }
210
211 static void calc_framerate(void)
212 {
213         static unsigned long nframes;
214         static long prev_upd;
215
216         long elapsed = time_msec - prev_upd;
217         if(elapsed >= 1000) {
218                 framerate = nframes * 10000 / elapsed;
219                 nframes = 1;
220                 prev_upd = time_msec;
221         } else {
222                 ++nframes;
223         }
224 }
225
226 static void print_framerate(void)
227 {
228         print_text(9 * win_width / 10, 20, 1, 1, 0, "fps: %d.%d", framerate / 10, framerate % 10);
229 }