conworlds
view src/main.cc @ 12:778ed91cb7fd
fullscreen to rift now works in extended mode with freeglut
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 23 Aug 2014 12:03:29 +0300 |
parents | 5dc4e2b8f6f5 |
children | 283cdfa7dda2 |
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 static bool fullscreen_pending;
22 int main(int argc, char **argv)
23 {
24 glutInitWindowSize(1024, 600);
25 glutInit(&argc, argv);
26 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
27 glutCreateWindow("VR Chess");
29 glutDisplayFunc(display);
30 glutIdleFunc(idle);
31 glutReshapeFunc(reshape);
32 glutKeyboardFunc(keyb);
33 glutKeyboardUpFunc(keyb_up);
34 glutMouseFunc(mouse);
35 glutMotionFunc(motion);
36 glutSpaceballMotionFunc(sball_motion);
37 glutSpaceballRotateFunc(sball_rotate);
39 if(!init()) {
40 return 1;
41 }
42 atexit(cleanup);
44 glutMainLoop();
45 return 0;
46 }
48 static bool init()
49 {
50 glewInit();
52 if(!game_init()) {
53 return false;
54 }
56 int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH);
57 int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT);
58 if(win_xsz && win_ysz) {
59 glutReshapeWindow(win_xsz, win_ysz);
60 }
61 return true;
62 }
64 static void cleanup()
65 {
66 game_cleanup();
67 }
69 static void display()
70 {
71 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
73 game_update(msec);
74 game_render();
75 }
77 static void idle()
78 {
79 if(fullscreen_pending) {
80 glutFullScreen();
81 }
83 glutPostRedisplay();
84 }
86 static void reshape(int x, int y)
87 {
88 game_reshape(x, y);
89 }
91 static void keyb(unsigned char key, int x, int y)
92 {
93 static bool fullscr;
94 static int prev_xpos, prev_ypos;
95 static int prev_xsz, prev_ysz;
97 switch(key) {
98 case 'f':
99 fullscr = !fullscr;
100 if(fullscr) {
101 int xoffs, yoffs;
103 prev_xpos = glutGet(GLUT_WINDOW_X);
104 prev_ypos = glutGet(GLUT_WINDOW_Y);
105 prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
106 prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
108 xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
109 yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
110 if(xoffs || yoffs) {
111 printf("repositioning: %d,%d\n", xoffs, yoffs);
112 glutPositionWindow(xoffs, yoffs);
113 fullscreen_pending = true;
114 }
115 } else {
116 fullscreen_pending = false;
117 glutPositionWindow(prev_xpos, prev_ypos);
118 glutReshapeWindow(prev_xsz, prev_ysz);
119 }
120 break;
121 }
122 game_keyboard(key, true, x, y);
123 }
125 static void keyb_up(unsigned char key, int x, int y)
126 {
127 game_keyboard(key, false, x, y);
128 }
130 static void mouse(int bn, int st, int x, int y)
131 {
132 switch(bn) {
133 case GLUT_RIGHT_BUTTON + 1:
134 if(st == GLUT_DOWN) {
135 game_mwheel(1);
136 }
137 break;
139 case GLUT_RIGHT_BUTTON + 2:
140 if(st == GLUT_DOWN) {
141 game_mwheel(-1);
142 }
143 break;
145 default:
146 game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
147 }
148 }
150 static void motion(int x, int y)
151 {
152 game_motion(x, y);
153 }
155 #define SBALL_MOVE_SCALE 0.00025
156 #define SBALL_ROT_SCALE 0.01
158 static void sball_motion(int x, int y, int z)
159 {
160 game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE);
161 }
163 static void sball_rotate(int x, int y, int z)
164 {
165 game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE);
166 }