vrchess

view src/main.cc @ 11:5dc4e2b8f6f5

LibOVR is broken
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 23 Aug 2014 00:24:20 +0300
parents c2eecf764daa
children 778ed91cb7fd
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "opengl.h"
4 #include "game.h"
5 #include "vr/vr.h"
7 static bool init();
8 static void cleanup();
10 static void display();
11 static void idle();
12 static void reshape(int x, int y);
13 static void keyb(unsigned char key, int x, int y);
14 static void keyb_up(unsigned char key, int x, int y);
15 static void mouse(int bn, int st, int x, int y);
16 static void motion(int x, int y);
17 static void sball_motion(int x, int y, int z);
18 static void sball_rotate(int x, int y, int z);
20 int main(int argc, char **argv)
21 {
22 glutInitWindowSize(1024, 600);
23 glutInit(&argc, argv);
24 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
25 glutCreateWindow("VR Chess");
27 glutDisplayFunc(display);
28 glutIdleFunc(idle);
29 glutReshapeFunc(reshape);
30 glutKeyboardFunc(keyb);
31 glutKeyboardUpFunc(keyb_up);
32 glutMouseFunc(mouse);
33 glutMotionFunc(motion);
34 glutSpaceballMotionFunc(sball_motion);
35 glutSpaceballRotateFunc(sball_rotate);
37 if(!init()) {
38 return 1;
39 }
40 atexit(cleanup);
42 glutMainLoop();
43 return 0;
44 }
46 static bool init()
47 {
48 glewInit();
50 if(!game_init()) {
51 return false;
52 }
54 int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH);
55 int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT);
56 if(win_xsz && win_ysz) {
57 glutReshapeWindow(win_xsz, win_ysz);
58 }
59 return true;
60 }
62 static void cleanup()
63 {
64 game_cleanup();
65 }
67 static void display()
68 {
69 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
71 game_update(msec);
72 game_render();
73 }
75 static void idle()
76 {
77 glutPostRedisplay();
78 }
80 static void reshape(int x, int y)
81 {
82 game_reshape(x, y);
83 }
85 static void keyb(unsigned char key, int x, int y)
86 {
87 static bool fullscr;
88 static int prev_xsz, prev_ysz;
90 switch(key) {
91 case 'f':
92 fullscr = !fullscr;
93 if(fullscr) {
94 int xoffs, yoffs;
96 prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
97 prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
99 xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
100 yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
101 if(xoffs || yoffs) {
102 printf("repositioning: %d,%d\n", xoffs, yoffs);
103 glutPositionWindow(xoffs + 1, yoffs + 1);
104 }
105 glutFullScreen();
106 } else {
107 glutReshapeWindow(prev_xsz, prev_ysz);
108 }
109 break;
110 }
111 game_keyboard(key, true, x, y);
112 }
114 static void keyb_up(unsigned char key, int x, int y)
115 {
116 game_keyboard(key, false, x, y);
117 }
119 static void mouse(int bn, int st, int x, int y)
120 {
121 switch(bn) {
122 case GLUT_RIGHT_BUTTON + 1:
123 if(st == GLUT_DOWN) {
124 game_mwheel(1);
125 }
126 break;
128 case GLUT_RIGHT_BUTTON + 2:
129 if(st == GLUT_DOWN) {
130 game_mwheel(-1);
131 }
132 break;
134 default:
135 game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
136 }
137 }
139 static void motion(int x, int y)
140 {
141 game_motion(x, y);
142 }
144 #define SBALL_MOVE_SCALE 0.00025
145 #define SBALL_ROT_SCALE 0.01
147 static void sball_motion(int x, int y, int z)
148 {
149 game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE);
150 }
152 static void sball_rotate(int x, int y, int z)
153 {
154 game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE);
155 }