conworlds

view src/main.cc @ 16:7a2041ddb7e7

fixed line endings
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 24 Aug 2014 18:42:40 +0300
parents 778ed91cb7fd
children c814f77d177e
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "opengl.h"
4 #include "game.h"
5 #include "gameopt.h"
6 #include "vr/vr.h"
8 static bool init();
9 static void cleanup();
11 static void display();
12 static void idle();
13 static void reshape(int x, int y);
14 static void keyb(unsigned char key, int x, int y);
15 static void keyb_up(unsigned char key, int x, int y);
16 static void mouse(int bn, int st, int x, int y);
17 static void motion(int x, int y);
18 static void sball_motion(int x, int y, int z);
19 static void sball_rotate(int x, int y, int z);
21 static bool fullscreen_pending;
23 int main(int argc, char **argv)
24 {
25 glutInitWindowSize(1024, 600);
26 glutInit(&argc, argv);
28 if(!parse_args(argc, argv)) {
29 return 1;
30 }
32 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0));
33 glutCreateWindow("LD48 #30 - connected worlds");
35 glutDisplayFunc(display);
36 glutIdleFunc(idle);
37 glutReshapeFunc(reshape);
38 glutKeyboardFunc(keyb);
39 glutKeyboardUpFunc(keyb_up);
40 glutMouseFunc(mouse);
41 glutMotionFunc(motion);
42 glutSpaceballMotionFunc(sball_motion);
43 glutSpaceballRotateFunc(sball_rotate);
45 if(!init()) {
46 return 1;
47 }
48 atexit(cleanup);
50 glutMainLoop();
51 return 0;
52 }
54 static bool init()
55 {
56 glewInit();
58 if(!game_init()) {
59 return false;
60 }
62 if(opt.vr) {
63 int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH);
64 int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT);
65 if(win_xsz && win_ysz) {
66 glutReshapeWindow(win_xsz, win_ysz);
67 }
68 }
69 return true;
70 }
72 static void cleanup()
73 {
74 game_cleanup();
75 }
77 static void display()
78 {
79 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
81 game_update(msec);
82 game_render();
83 }
85 static void idle()
86 {
87 if(fullscreen_pending) {
88 glutFullScreen();
89 }
91 glutPostRedisplay();
92 }
94 static void reshape(int x, int y)
95 {
96 game_reshape(x, y);
97 }
99 static void keyb(unsigned char key, int x, int y)
100 {
101 static bool fullscr;
102 static int prev_xpos, prev_ypos;
103 static int prev_xsz, prev_ysz;
105 switch(key) {
106 case 'f':
107 fullscr = !fullscr;
108 if(fullscr) {
109 int xoffs, yoffs;
111 prev_xpos = glutGet(GLUT_WINDOW_X);
112 prev_ypos = glutGet(GLUT_WINDOW_Y);
113 prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
114 prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
116 xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
117 yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
118 if(xoffs || yoffs) {
119 printf("repositioning: %d,%d\n", xoffs, yoffs);
120 glutPositionWindow(xoffs, yoffs);
121 }
122 fullscreen_pending = true;
123 } else {
124 fullscreen_pending = false;
125 glutPositionWindow(prev_xpos, prev_ypos);
126 glutReshapeWindow(prev_xsz, prev_ysz);
127 }
128 break;
129 }
130 game_keyboard(key, true, x, y);
131 }
133 static void keyb_up(unsigned char key, int x, int y)
134 {
135 game_keyboard(key, false, x, y);
136 }
138 static void mouse(int bn, int st, int x, int y)
139 {
140 switch(bn) {
141 case GLUT_RIGHT_BUTTON + 1:
142 if(st == GLUT_DOWN) {
143 game_mwheel(1);
144 }
145 break;
147 case GLUT_RIGHT_BUTTON + 2:
148 if(st == GLUT_DOWN) {
149 game_mwheel(-1);
150 }
151 break;
153 default:
154 game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
155 }
156 }
158 static void motion(int x, int y)
159 {
160 game_motion(x, y);
161 }
163 #define SBALL_MOVE_SCALE 0.00025
164 #define SBALL_ROT_SCALE 0.01
166 static void sball_motion(int x, int y, int z)
167 {
168 game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE);
169 }
171 static void sball_rotate(int x, int y, int z)
172 {
173 game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE);
174 }