updated readme
[andemo] / src / pc / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "opengl.h"
5 #include "miniglut.h"
6 #include "demo.h"
7 #include "cfgopt.h"
8
9 static void display(void);
10 static void keypress(unsigned char key, int x, int y);
11 static void skeypress(int key, int x, int y);
12 static void mouse(int bn, int st, int x, int y);
13 static int translate_key(int key);
14
15 static int prev_xsz, prev_ysz;
16 static long start_time;
17
18
19 int main(int argc, char **argv)
20 {
21         glutInit(&argc, argv);
22
23         load_config("demo.cfg");
24         if(parse_args(argc, argv) == -1) {
25                 return 1;
26         }
27
28         glutInitWindowSize(1280, 800);
29         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
30         glutCreateWindow("Mindlapse");
31
32         glutDisplayFunc(display);
33         glutIdleFunc(glutPostRedisplay);
34         glutReshapeFunc(demo_reshape);
35         glutKeyboardFunc(keypress);
36         glutSpecialFunc(skeypress);
37         glutMouseFunc(mouse);
38         glutMotionFunc(demo_motion);
39
40         if(opt.fullscreen) {
41                 prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
42                 prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
43                 glutFullScreen();
44         }
45
46         if(demo_init() == -1) {
47                 return 1;
48         }
49         atexit(demo_cleanup);
50
51         start_time = glutGet(GLUT_ELAPSED_TIME);
52         glutMainLoop();
53         return 0;
54 }
55
56 void swap_buffers(void)
57 {
58         glutSwapBuffers();
59 }
60
61 static void display(void)
62 {
63         time_msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
64
65         demo_display();
66
67         glutSwapBuffers();
68         assert(glGetError() == GL_NO_ERROR);
69 }
70
71 static void keypress(unsigned char key, int x, int y)
72 {
73         switch(key) {
74         case 27:
75                 glutExit();
76                 break;
77
78         case 'f':
79         case 'F':
80                 opt.fullscreen ^= 1;
81                 if(opt.fullscreen) {
82                         prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
83                         prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
84                         glutFullScreen();
85                 } else {
86                         glutReshapeWindow(prev_xsz, prev_ysz);
87                 }
88                 break;
89
90         default:
91                 demo_keyboard(key, 1);
92         }
93 }
94
95 static void skeypress(int key, int x, int y)
96 {
97         if((key = translate_key(key))) {
98                 demo_keyboard(key, 1);
99         }
100 }
101
102 static void mouse(int bn, int st, int x, int y)
103 {
104         int bidx = bn - GLUT_LEFT_BUTTON;
105         int press = st == GLUT_DOWN;
106
107         demo_mouse(bidx, press, x, y);
108 }
109
110 static int translate_key(int key)
111 {
112         if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
113                 return key - GLUT_KEY_F1 + KEY_F1;
114         }
115         switch(key) {
116         case GLUT_KEY_LEFT:
117                 return KEY_LEFT;
118         case GLUT_KEY_RIGHT:
119                 return KEY_RIGHT;
120         case GLUT_KEY_UP:
121                 return KEY_UP;
122         case GLUT_KEY_DOWN:
123                 return KEY_DOWN;
124         case GLUT_KEY_PAGE_UP:
125                 return KEY_PGUP;
126         case GLUT_KEY_PAGE_DOWN:
127                 return KEY_PGDOWN;
128         default:
129                 break;
130         }
131         return 0;
132 }