- added glutIgnoreKeyRepeat in miniglut
[vrlugburz] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "miniglut.h"
4 #include "opengl.h"
5 #include "game.h"
6
7 static void display(void);
8 static void idle(void);
9 static void keypress(unsigned char key, int x, int y);
10 static void keyrelease(unsigned char key, int x, int y);
11 static void skeypress(int key, int x, int y);
12 static void skeyrelease(int key, int x, int y);
13 static void mbutton(int bn, int st, int x, int y);
14 static int skey_translate(int key);
15
16 static int quit;
17 static long start_time;
18
19 int main(int argc, char **argv)
20 {
21         glutInit(&argc, argv);
22         glutInitWindowSize(1280, 720);
23         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_SRGB);
24         glutCreateWindow("lugburz VR");
25
26         glutDisplayFunc(display);
27         glutIdleFunc(idle);
28         glutReshapeFunc(game_reshape);
29         glutKeyboardFunc(keypress);
30         glutKeyboardUpFunc(keyrelease);
31         glutSpecialFunc(skeypress);
32         glutSpecialUpFunc(skeyrelease);
33         glutMouseFunc(mbutton);
34         glutMotionFunc(game_mmotion);
35         glutPassiveMotionFunc(game_mmotion);
36
37         glutIgnoreKeyRepeat(1);
38
39         if(game_init() == -1) {
40                 return 1;
41         }
42
43         start_time = glutGet(GLUT_ELAPSED_TIME);
44         while(!quit) {
45                 glutMainLoopEvent();
46         }
47
48         game_shutdown();
49         return 0;
50 }
51
52 void game_quit(void)
53 {
54         quit = 1;
55 }
56
57 void game_swap_buffers(void)
58 {
59         glutSwapBuffers();
60 }
61
62 static void display(void)
63 {
64         time_msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
65         game_display();
66 }
67
68 static void idle(void)
69 {
70         glutPostRedisplay();
71 }
72
73 static void keypress(unsigned char key, int x, int y)
74 {
75         game_keyboard(key, 1);
76 }
77
78 static void keyrelease(unsigned char key, int x, int y)
79 {
80         game_keyboard(key, 0);
81 }
82
83 static void skeypress(int key, int x, int y)
84 {
85         if((key = skey_translate(key)) >= 0) {
86                 game_keyboard(key, 1);
87         }
88 }
89
90 static void skeyrelease(int key, int x, int y)
91 {
92         if((key = skey_translate(key)) >= 0) {
93                 game_keyboard(key, 0);
94         }
95 }
96
97 static void mbutton(int bn, int st, int x, int y)
98 {
99         int bidx = bn - GLUT_LEFT_BUTTON;
100         int press = st == GLUT_DOWN;
101         game_mbutton(bidx, press, x, y);
102 }
103
104 static int skey_translate(int key)
105 {
106         switch(key) {
107         case GLUT_KEY_LEFT:
108                 return KEY_LEFT;
109         case GLUT_KEY_RIGHT:
110                 return KEY_RIGHT;
111         case GLUT_KEY_UP:
112                 return KEY_UP;
113         case GLUT_KEY_DOWN:
114                 return KEY_DOWN;
115         case GLUT_KEY_PAGE_UP:
116                 return KEY_PGUP;
117         case GLUT_KEY_PAGE_DOWN:
118                 return KEY_PGDOWN;
119         default:
120                 return -1;
121         }
122 }