initial commit
[liquidmodel] / 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
17
18 int main(int argc, char **argv)
19 {
20         glutInit(&argc, argv);
21
22         load_config("demo.cfg");
23         if(parse_args(argc, argv) == -1) {
24                 return 1;
25         }
26
27         glutInitWindowSize(1280, 800);
28         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
29         glutCreateWindow("Mindlapse");
30
31         glutDisplayFunc(display);
32         glutIdleFunc(glutPostRedisplay);
33         glutReshapeFunc(demo_reshape);
34         glutKeyboardFunc(keypress);
35         glutSpecialFunc(skeypress);
36         glutMouseFunc(mouse);
37         glutMotionFunc(demo_motion);
38         glutPassiveMotionFunc(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
53         glutMainLoop();
54         return 0;
55 }
56
57 void swap_buffers(void)
58 {
59         glutSwapBuffers();
60 }
61
62 static void display(void)
63 {
64         sys_time = glutGet(GLUT_ELAPSED_TIME);
65
66         demo_display();
67
68         glutSwapBuffers();
69         assert(glGetError() == GL_NO_ERROR);
70 }
71
72 static void keypress(unsigned char key, int x, int y)
73 {
74         switch(key) {
75         case 27:
76                 glutExit();
77                 break;
78
79         case 'f':
80         case 'F':
81                 opt.fullscreen ^= 1;
82                 if(opt.fullscreen) {
83                         prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
84                         prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
85                         glutFullScreen();
86                 } else {
87                         glutReshapeWindow(prev_xsz, prev_ysz);
88                 }
89                 break;
90
91         default:
92                 demo_keyboard(key, 1);
93         }
94 }
95
96 static void skeypress(int key, int x, int y)
97 {
98         if((key = translate_key(key))) {
99                 demo_keyboard(key, 1);
100         }
101 }
102
103 static void mouse(int bn, int st, int x, int y)
104 {
105         int bidx = bn - GLUT_LEFT_BUTTON;
106         int press = st == GLUT_DOWN;
107
108         demo_mouse(bidx, press, x, y);
109 }
110
111 static int translate_key(int key)
112 {
113         if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
114                 return key - GLUT_KEY_F1 + KEY_F1;
115         }
116         switch(key) {
117         case GLUT_KEY_LEFT:
118                 return KEY_LEFT;
119         case GLUT_KEY_RIGHT:
120                 return KEY_RIGHT;
121         case GLUT_KEY_UP:
122                 return KEY_UP;
123         case GLUT_KEY_DOWN:
124                 return KEY_DOWN;
125         case GLUT_KEY_PAGE_UP:
126                 return KEY_PGUP;
127         case GLUT_KEY_PAGE_DOWN:
128                 return KEY_PGDOWN;
129         default:
130                 break;
131         }
132         return 0;
133 }