- added libdrawtext
[demo_prior] / src / demo.c
1 #include <stdio.h>
2 #include "opengl.h"
3 #include "demo.h"
4 #include "part.h"
5 #include "post.h"
6 #include "sdr.h"
7 #include "opt.h"
8 #include "drawtext.h"
9 #include "imtk.h"
10 #include "osd.h"
11
12 void reg_whitted(void);
13
14 int win_width, win_height;
15 float win_aspect;
16 long time_msec;
17
18 struct dtx_font *fnt_ui;
19 int fnt_ui_size;
20
21 static int reshape_pending;
22 static unsigned int sdr_gamma;
23
24 int demo_init(void)
25 {
26         int i;
27
28         if(init_opengl() == -1) {
29                 return -1;
30         }
31
32         glEnable(GL_DEPTH_TEST);
33         glEnable(GL_CULL_FACE);
34         if(opt.srgb) {
35                 glEnable(GL_FRAMEBUFFER_SRGB);
36         }
37         if(opt.msaa) {
38                 glEnable(GL_MULTISAMPLE);
39         }
40
41         if(opt.vsync) {
42                 gl_swap_interval(1);
43         } else {
44                 gl_swap_interval(0);
45         }
46
47         if(!(fnt_ui = dtx_open_font_glyphmap("data/ui.glyphmap"))) {
48                 fprintf(stderr, "failed to open ui font\n");
49                 return -1;
50         }
51         fnt_ui_size = dtx_get_glyphmap_ptsize(dtx_get_glyphmap(fnt_ui, 0));
52         dtx_prepare_range(fnt_ui, fnt_ui_size, 32, 127);
53
54         post_init();
55
56         if(!(sdr_gamma = create_program_load("sdr/post.v.glsl", "sdr/gamma.p.glsl"))) {
57                 fprintf(stderr, "Warning: failed to load the gamma correction shader\n");
58         }
59
60         reg_whitted();
61
62         for(i=0; i<num_parts; i++) {
63                 if(parts[i]->init() == -1) {
64                         fprintf(stderr, "part %s init failed\n", parts[i]->name);
65                         return -1;
66                 }
67         }
68
69         switch_part(parts[0]);
70         return 0;
71 }
72
73 void demo_cleanup(void)
74 {
75         int i;
76
77         if(sdr_gamma) {
78                 glDeleteProgram(sdr_gamma);
79         }
80         post_cleanup();
81
82         for(i=0; i<num_parts; i++) {
83                 parts[i]->destroy();
84         }
85
86         dtx_close_font(fnt_ui);
87 }
88
89 void demo_display(void)
90 {
91         long part_time;
92
93         if(reshape_pending) {
94                 post_reshape(win_width, win_height);
95                 reshape_pending = 0;
96         }
97
98         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
99
100         if(!cur_part) return;
101
102         part_time = time_msec - cur_part->start_time;
103         if(part_time < cur_part->in_time) {
104                 float t = (float)part_time / cur_part->in_time;
105
106                 if(prev_part) {
107                         long prev_part_time = time_msec - prev_part->start_time;
108                         if(prev_part->draw_out) {
109                                 prev_part->draw_out(prev_part_time, t);
110                         } else {
111                                 prev_part->draw(prev_part_time);
112                         }
113                 }
114
115                 if(cur_part->draw_in) {
116                         cur_part->draw_in(part_time, t);
117                 } else {
118                         cur_part->draw(part_time);
119                 }
120         } else {
121                 prev_part = 0;
122                 cur_part->draw(part_time);
123         }
124
125         /* no-srgb gamma correction fallback */
126         if(!opt.srgb) {
127                 glBindTexture(GL_TEXTURE_2D, post_fbtex[0].id);
128                 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, win_width, win_height);
129
130                 glUseProgram(sdr_gamma);
131                 overlay_tex(post_fbtex, 1.0);
132         }
133
134         {
135                 static long frames, prev_upd, fps;
136                 long dt;
137
138                 frames++;
139
140                 dt = time_msec - prev_upd;
141                 if(dt >= 750) {
142                         fps = (frames * 1000 << 8 + 128) / dt;
143                         frames = 0;
144                         prev_upd = time_msec;
145                 }
146
147                 print_text(win_width - 80, 20, 1.0, 0.8, 0.1, "fps: %ld.%ld", fps >> 8, ((fps & 0xff) * 10) >> 8);
148         }
149
150         draw_osd();
151 }
152
153 void demo_reshape(int x, int y)
154 {
155         reshape_pending = 1;
156
157         win_width = x;
158         win_height = y;
159         win_aspect = (float)x / (float)y;
160
161         glViewport(0, 0, x, y);
162
163         glMatrixMode(GL_PROJECTION);
164         glLoadIdentity();
165         gluPerspective(50.0, win_aspect, 0.5, 500.0);
166
167         imtk_set_viewport(x, y);
168 }
169
170 void demo_keyboard(int key, int st)
171 {
172         if(st && key == 27) {
173                 demo_quit();
174                 return;
175         }
176
177         if(cur_part && cur_part->keyboard) {
178                 cur_part->keyboard(key, st);
179         }
180 }
181
182
183 void demo_mbutton(int bn, int st, int x, int y)
184 {
185         if(cur_part && cur_part->mbutton) {
186                 cur_part->mbutton(bn, st, x, y);
187         }
188 }
189
190 void demo_mmotion(int x, int y)
191 {
192         if(cur_part && cur_part->mmotion) {
193                 cur_part->mmotion(x, y);
194         }
195 }
196
197 void demo_sball_motion(int x, int y, int z)
198 {
199 }
200
201 void demo_sball_rotate(int x, int y, int z)
202 {
203 }
204
205 void demo_sball_button(int bn, int st)
206 {
207 }