initial commit
[shapestoy] / 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
39         if(opt.fullscreen) {
40                 prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
41                 prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
42                 glutFullScreen();
43         }
44
45         if(demo_init() == -1) {
46                 return 1;
47         }
48         atexit(demo_cleanup);
49
50         start_time = glutGet(GLUT_ELAPSED_TIME);
51
52         glutMainLoop();
53         return 0;
54 }
55
56 void swap_buffers(void)
57 {
58         glutSwapBuffers();
59 }
60
61 static void display(void)
62 {
63         sys_time = glutGet(GLUT_ELAPSED_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 }