conworlds

view src/main.cc @ 7:bd8202d6d28d

some progress...
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 16:55:16 +0300
parents 3c36bc28c6c2
children c2eecf764daa
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();
74 if(!vr_swap_buffers()) {
75 glutSwapBuffers();
76 }
77 }
79 static void idle()
80 {
81 glutPostRedisplay();
82 }
84 static void reshape(int x, int y)
85 {
86 game_reshape(x, y);
87 }
89 static void keyb(unsigned char key, int x, int y)
90 {
91 game_keyboard(key, true, x, y);
92 }
94 static void keyb_up(unsigned char key, int x, int y)
95 {
96 game_keyboard(key, false, x, y);
97 }
99 static void mouse(int bn, int st, int x, int y)
100 {
101 switch(bn) {
102 case GLUT_RIGHT_BUTTON + 1:
103 if(st == GLUT_DOWN) {
104 game_mwheel(1);
105 }
106 break;
108 case GLUT_RIGHT_BUTTON + 2:
109 if(st == GLUT_DOWN) {
110 game_mwheel(-1);
111 }
112 break;
114 default:
115 game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
116 }
117 }
119 static void motion(int x, int y)
120 {
121 game_motion(x, y);
122 }
124 #define SBALL_MOVE_SCALE 0.00025
125 #define SBALL_ROT_SCALE 0.01
127 static void sball_motion(int x, int y, int z)
128 {
129 game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE);
130 }
132 static void sball_rotate(int x, int y, int z)
133 {
134 game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE);
135 }