5 #include <cgmath/cgmath.h>
14 static void calc_framerate(void);
15 static void print_framerate(void);
18 static int should_swap;
20 static unsigned long framerate;
23 int game_init(int argc, char **argv)
25 if(init_opengl() == -1) {
29 if(init_options(argc, argv, "vrtris.conf") == -1) {
33 if(init_screens() == -1) {
38 if(opt.flags & OPT_VR) {
39 if(goatvr_init() == -1) {
42 goatvr_set_origin_mode(GOATVR_HEAD);
43 goatvr_set_units_scale(10.0f);
46 should_swap = goatvr_should_swap();
50 glEnable(GL_DEPTH_TEST);
51 glEnable(GL_CULL_FACE);
52 glEnable(GL_LIGHTING);
62 if(opt.flags & OPT_VR) {
69 static void update(float dt)
74 void game_display(void)
76 static long prev_msec;
77 float dt = (float)(time_msec - prev_msec) / 1000.0f;
78 prev_msec = time_msec;
83 if(opt.flags & OPT_VR) {
86 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92 cgm_mcopy(proj_matrix, goatvr_projection_matrix(i, 0.5, 500.0));
93 glMatrixMode(GL_PROJECTION);
94 glLoadMatrixf(proj_matrix);
96 cgm_mcopy(view_matrix, goatvr_view_matrix(i));
97 glMatrixMode(GL_MODELVIEW);
98 glLoadMatrixf(view_matrix);
112 #endif /* BUILD_VR */
115 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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);
121 cgm_midentity(view_matrix);
122 glMatrixMode(GL_MODELVIEW);
123 glLoadMatrixf(view_matrix);
132 assert(glGetError() == GL_NO_ERROR);
137 void game_reshape(int x, int y)
139 glViewport(0, 0, x, y);
141 goatvr_set_fb_size(x, y, 1.0f);
144 reshape_screens(x, y);
147 void game_keyboard(int key, int pressed)
149 unsigned int mod = game_get_modifiers();
160 game_toggle_fullscreen();
167 if(opt.flags & OPT_VR) {
178 screen->keyboard(key, pressed);
181 void game_mouse_button(int bn, int pressed, int x, int y)
183 screen->mouse(bn, pressed, x, y);
186 void game_mouse_motion(int x, int y)
188 screen->motion(x, y);
191 void game_mouse_wheel(int dir)
197 void game_gamepad_axis(int axis, float val)
199 joy_axis[axis] = val;
202 void game_gamepad_button(int bn, int pressed)
205 joy_bnstate |= (1 << bn);
207 joy_bnstate &= ~(1 << bn);
211 static void calc_framerate(void)
213 static unsigned long nframes;
214 static long prev_upd;
216 long elapsed = time_msec - prev_upd;
217 if(elapsed >= 1000) {
218 framerate = nframes * 10000 / elapsed;
220 prev_upd = time_msec;
226 static void print_framerate(void)
228 print_text(9 * win_width / 10, 20, 1, 1, 0, "fps: %d.%d", framerate / 10, framerate % 10);