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