conworlds

view src/glut/main_glut.cc @ 21:2da585428507

added both glut and sdl2 versions just for fun...
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 27 Aug 2014 04:16:22 +0300
parents src/glut/main.cc@782ff06817fb
children
line source
1 #ifdef USE_GLUT
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include "opengl.h"
6 #ifdef __APPLE__
7 #include <GLUT/glut.h>
8 #else
9 #include <GL/glut.h>
10 #endif
11 #include "game.h"
12 #include "gameopt.h"
13 #include "vr/vr.h"
15 static bool init();
16 static void cleanup();
18 static void display();
19 static void idle();
20 static void reshape(int x, int y);
21 static void keyb(unsigned char key, int x, int y);
22 static void keyb_up(unsigned char key, int x, int y);
23 static void mouse(int bn, int st, int x, int y);
24 static void motion(int x, int y);
25 static void sball_motion(int x, int y, int z);
26 static void sball_rotate(int x, int y, int z);
28 static bool fullscreen_pending;
30 int main(int argc, char **argv)
31 {
32 glutInitWindowSize(1024, 600);
33 glutInit(&argc, argv);
35 if(!parse_args(argc, argv)) {
36 return 1;
37 }
39 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0));
40 glutCreateWindow("LD48 #30 - connected worlds");
42 glutDisplayFunc(display);
43 glutIdleFunc(idle);
44 glutReshapeFunc(reshape);
45 glutKeyboardFunc(keyb);
46 glutKeyboardUpFunc(keyb_up);
47 glutMouseFunc(mouse);
48 glutMotionFunc(motion);
49 glutSpaceballMotionFunc(sball_motion);
50 glutSpaceballRotateFunc(sball_rotate);
52 if(!init()) {
53 return 1;
54 }
55 atexit(cleanup);
57 glutMainLoop();
58 return 0;
59 }
61 static bool init()
62 {
63 glewInit();
65 if(!game_init()) {
66 return false;
67 }
69 if(opt.vr) {
70 int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH);
71 int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT);
72 if(win_xsz && win_ysz) {
73 glutReshapeWindow(win_xsz, win_ysz);
74 }
75 }
76 return true;
77 }
79 static void cleanup()
80 {
81 game_cleanup();
82 }
84 static void display()
85 {
86 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
88 game_update(msec);
89 game_render();
90 }
92 static void idle()
93 {
94 if(fullscreen_pending) {
95 glutFullScreen();
96 }
98 glutPostRedisplay();
99 }
101 static void reshape(int x, int y)
102 {
103 game_reshape(x, y);
104 }
106 static void keyb(unsigned char key, int x, int y)
107 {
108 static bool fullscr;
109 static int prev_xpos, prev_ypos;
110 static int prev_xsz, prev_ysz;
112 switch(key) {
113 case 'f':
114 fullscr = !fullscr;
115 if(fullscr) {
116 int xoffs, yoffs;
118 prev_xpos = glutGet(GLUT_WINDOW_X);
119 prev_ypos = glutGet(GLUT_WINDOW_Y);
120 prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
121 prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
123 xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
124 yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
125 if(xoffs || yoffs) {
126 printf("repositioning: %d,%d\n", xoffs, yoffs);
127 glutPositionWindow(xoffs, yoffs);
128 }
129 fullscreen_pending = true;
130 } else {
131 fullscreen_pending = false;
132 glutPositionWindow(prev_xpos, prev_ypos);
133 glutReshapeWindow(prev_xsz, prev_ysz);
134 }
135 break;
136 }
137 game_keyboard(key, true);
138 }
140 static void keyb_up(unsigned char key, int x, int y)
141 {
142 game_keyboard(key, false);
143 }
145 static void mouse(int bn, int st, int x, int y)
146 {
147 switch(bn) {
148 case GLUT_RIGHT_BUTTON + 1:
149 if(st == GLUT_DOWN) {
150 game_mwheel(1);
151 }
152 break;
154 case GLUT_RIGHT_BUTTON + 2:
155 if(st == GLUT_DOWN) {
156 game_mwheel(-1);
157 }
158 break;
160 default:
161 game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
162 }
163 }
165 static void motion(int x, int y)
166 {
167 game_motion(x, y);
168 }
170 #define SBALL_MOVE_SCALE 0.00025
171 #define SBALL_ROT_SCALE 0.01
173 static void sball_motion(int x, int y, int z)
174 {
175 game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE);
176 }
178 static void sball_rotate(int x, int y, int z)
179 {
180 game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE);
181 }
183 #endif // USE_GLUT