options/config
[demo_prior] / src / main.c
1 #include <stdlib.h>
2 #include "miniglut.h"
3 #include "demo.h"
4 #include "opt.h"
5
6 static void display(void);
7 static void idle(void);
8 static void keydown(unsigned char key, int x, int y);
9 static void keyup(unsigned char key, int x, int y);
10 static void mouse(int bn, int st, int x, int y);
11
12 static long time_start;
13
14 int main(int argc, char **argv)
15 {
16         unsigned int glut_flags = GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH;
17
18         glutInit(&argc, argv);
19
20         read_cfg("demo.cfg");
21         if(parse_args(argc, argv) == -1) {
22                 return 1;
23         }
24         if(opt.srgb) glut_flags |= GLUT_SRGB;
25         if(opt.msaa) glut_flags |= GLUT_MULTISAMPLE;
26
27         glutInitWindowSize(opt.width, opt.height);
28         glutInitDisplayMode(glut_flags);
29         glutCreateWindow("Prior Art / Mindlapse");
30
31         glutDisplayFunc(display);
32         glutIdleFunc(idle);
33         glutReshapeFunc(demo_reshape);
34         glutKeyboardFunc(keydown);
35         glutKeyboardUpFunc(keyup);
36         glutMouseFunc(mouse);
37         glutMotionFunc(demo_mmotion);
38         glutSpaceballMotionFunc(demo_sball_motion);
39         glutSpaceballRotateFunc(demo_sball_rotate);
40         glutSpaceballButtonFunc(demo_sball_button);
41
42         if(opt.fullscr) {
43                 glutFullScreen();
44         }
45
46         if(demo_init() == -1) {
47                 return 1;
48         }
49         atexit(demo_cleanup);
50
51         time_start = glutGet(GLUT_ELAPSED_TIME);
52
53         glutMainLoop();
54         return 0;
55 }
56
57 void demo_quit(void)
58 {
59         glutExit();
60 }
61
62 static void display(void)
63 {
64         time_msec = glutGet(GLUT_ELAPSED_TIME) - time_start;
65
66         demo_display();
67
68         glutSwapBuffers();
69 }
70
71 static void idle(void)
72 {
73         glutPostRedisplay();
74 }
75
76 #define KEYCOMBO_FS \
77         ((key == '\r' || key == '\n') && (glutGetModifiers() & GLUT_ACTIVE_ALT))
78
79
80 static void keydown(unsigned char key, int x, int y)
81 {
82         static int fullscr = -1, prev_x, prev_y;
83
84         if(KEYCOMBO_FS) {
85                 if(fullscr == -1) fullscr = opt.fullscr;
86                 fullscr ^= 1;
87                 if(fullscr) {
88                         prev_x = glutGet(GLUT_WINDOW_X);
89                         prev_y = glutGet(GLUT_WINDOW_Y);
90                         glutFullScreen();
91                 } else {
92                         glutPositionWindow(prev_x, prev_y);
93                 }
94         }
95
96         demo_keyboard(key, 1);
97 }
98
99 static void keyup(unsigned char key, int x, int y)
100 {
101         demo_keyboard(key, 0);
102 }
103
104 static void mouse(int bn, int st, int x, int y)
105 {
106         demo_mbutton(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
107 }