part system
[demo_prior] / src / demo.c
1 #include <stdio.h>
2 #include "opengl.h"
3 #include "demo.h"
4 #include "part.h"
5
6 void reg_whitted(void);
7
8 int win_width, win_height;
9 float win_aspect;
10 long time_msec;
11
12
13 int demo_init(void)
14 {
15         int i;
16
17         if(init_opengl() == -1) {
18                 return -1;
19         }
20
21         glEnable(GL_DEPTH_TEST);
22         glEnable(GL_CULL_FACE);
23         glEnable(GL_FRAMEBUFFER_SRGB);
24         glEnable(GL_MULTISAMPLE);
25
26         reg_whitted();
27
28         for(i=0; i<num_parts; i++) {
29                 if(parts[i]->init() == -1) {
30                         fprintf(stderr, "part %s init failed\n", parts[i]->name);
31                         return -1;
32                 }
33         }
34
35         switch_part(parts[0]);
36         return 0;
37 }
38
39 void demo_cleanup(void)
40 {
41         int i;
42
43         for(i=0; i<num_parts; i++) {
44                 parts[i]->destroy();
45         }
46 }
47
48 void demo_display(void)
49 {
50         long part_time;
51
52         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
53
54         if(!cur_part) return;
55
56         part_time = time_msec - cur_part->start_time;
57         if(part_time < cur_part->in_time) {
58                 float t = (float)part_time / cur_part->in_time;
59
60                 if(prev_part) {
61                         long prev_part_time = time_msec - prev_part->start_time;
62                         if(prev_part->draw_out) {
63                                 prev_part->draw_out(prev_part_time, t);
64                         } else {
65                                 prev_part->draw(prev_part_time);
66                         }
67                 }
68
69                 if(cur_part->draw_in) {
70                         cur_part->draw_in(part_time, t);
71                 } else {
72                         cur_part->draw(part_time);
73                 }
74         } else {
75                 prev_part = 0;
76                 cur_part->draw(part_time);
77         }
78 }
79
80 void demo_reshape(int x, int y)
81 {
82         win_width = x;
83         win_height = y;
84         win_aspect = (float)x / (float)y;
85
86         glViewport(0, 0, x, y);
87
88         glMatrixMode(GL_PROJECTION);
89         glLoadIdentity();
90         gluPerspective(50.0, win_aspect, 0.5, 500.0);
91 }
92
93 void demo_keyboard(int key, int st)
94 {
95         if(st && key == 27) {
96                 demo_quit();
97                 return;
98         }
99
100         if(cur_part && cur_part->keyboard) {
101                 cur_part->keyboard(key, st);
102         }
103 }
104
105
106 void demo_mbutton(int bn, int st, int x, int y)
107 {
108         if(cur_part && cur_part->mbutton) {
109                 cur_part->mbutton(bn, st, x, y);
110         }
111 }
112
113 void demo_mmotion(int x, int y)
114 {
115         if(cur_part && cur_part->mmotion) {
116                 cur_part->mmotion(x, y);
117         }
118 }
119
120 void demo_sball_motion(int x, int y, int z)
121 {
122 }
123
124 void demo_sball_rotate(int x, int y, int z)
125 {
126 }
127
128 void demo_sball_button(int bn, int st)
129 {
130 }