fixed: was potentially storing stack-allocated name strings in the trackmap
[andemo] / src / pc / main_glut.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
8 static void display(void);
9 static void keypress(unsigned char key, int x, int y);
10 static void skeypress(int key, int x, int y);
11 static void mouse(int bn, int st, int x, int y);
12 static int translate_key(int key);
13
14 static long start_time;
15
16
17 int main(int argc, char **argv)
18 {
19         glutInit(&argc, argv);
20         glutInitWindowSize(1280, 800);
21         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
22         glutCreateWindow("Mindlapse");
23
24         glutDisplayFunc(display);
25         glutIdleFunc(glutPostRedisplay);
26         glutReshapeFunc(demo_reshape);
27         glutKeyboardFunc(keypress);
28         glutSpecialFunc(skeypress);
29         glutMouseFunc(mouse);
30         glutMotionFunc(demo_motion);
31
32         if(demo_init() == -1) {
33                 return 1;
34         }
35         atexit(demo_cleanup);
36
37         start_time = glutGet(GLUT_ELAPSED_TIME);
38         glutMainLoop();
39         return 0;
40 }
41
42 void swap_buffers(void)
43 {
44         glutSwapBuffers();
45 }
46
47 static void display(void)
48 {
49         time_msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
50
51         demo_display();
52
53         glutSwapBuffers();
54         assert(glGetError() == GL_NO_ERROR);
55 }
56
57 static void keypress(unsigned char key, int x, int y)
58 {
59         if(key == 27) exit(0);
60
61         demo_keyboard(key, 1);
62 }
63
64 static void skeypress(int key, int x, int y)
65 {
66         if((key = translate_key(key))) {
67                 demo_keyboard(key, 1);
68         }
69 }
70
71 static void mouse(int bn, int st, int x, int y)
72 {
73         int bidx = bn - GLUT_LEFT_BUTTON;
74         int press = st == GLUT_DOWN;
75
76         demo_mouse(bidx, press, x, y);
77 }
78
79 static int translate_key(int key)
80 {
81         if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
82                 return key - GLUT_KEY_F1 + KEY_F1;
83         }
84         switch(key) {
85         case GLUT_KEY_LEFT:
86                 return KEY_LEFT;
87         case GLUT_KEY_RIGHT:
88                 return KEY_RIGHT;
89         case GLUT_KEY_UP:
90                 return KEY_UP;
91         case GLUT_KEY_DOWN:
92                 return KEY_DOWN;
93         case GLUT_KEY_PAGE_UP:
94                 return KEY_PGUP;
95         case GLUT_KEY_PAGE_DOWN:
96                 return KEY_PGDOWN;
97         default:
98                 break;
99         }
100         return 0;
101 }