conworlds

view src/main.cc @ 10:e3f0ca1d008a

added preliminary OpenHMD module
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 20:11:15 +0300
parents bd8202d6d28d
children 5dc4e2b8f6f5
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 game_keyboard(key, true, x, y);
88 }
90 static void keyb_up(unsigned char key, int x, int y)
91 {
92 game_keyboard(key, false, x, y);
93 }
95 static void mouse(int bn, int st, int x, int y)
96 {
97 switch(bn) {
98 case GLUT_RIGHT_BUTTON + 1:
99 if(st == GLUT_DOWN) {
100 game_mwheel(1);
101 }
102 break;
104 case GLUT_RIGHT_BUTTON + 2:
105 if(st == GLUT_DOWN) {
106 game_mwheel(-1);
107 }
108 break;
110 default:
111 game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
112 }
113 }
115 static void motion(int x, int y)
116 {
117 game_motion(x, y);
118 }
120 #define SBALL_MOVE_SCALE 0.00025
121 #define SBALL_ROT_SCALE 0.01
123 static void sball_motion(int x, int y, int z)
124 {
125 game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE);
126 }
128 static void sball_rotate(int x, int y, int z)
129 {
130 game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE);
131 }