b71cc9535e045c654d0548a01c3559c9c431d35e
[neverball] / share / video.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include "video.h"
16 #include "vec3.h"
17 #include "glext.h"
18 #include "config.h"
19 #include "syswm.h"
20 #include "sync.h"
21
22 /*---------------------------------------------------------------------------*/
23
24 int video_init(const char *title, const char *icon)
25 {
26     SDL_QuitSubSystem(SDL_INIT_VIDEO);
27
28     if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
29     {
30         fprintf(stderr, "%s\n", SDL_GetError());
31         return 0;
32     }
33
34     /* This has to happen before mode setting... */
35
36     set_SDL_icon(icon);
37
38     /* Initialize the video. */
39
40     if (!video_mode(config_get_d(CONFIG_FULLSCREEN),
41                     config_get_d(CONFIG_WIDTH),
42                     config_get_d(CONFIG_HEIGHT)))
43     {
44         fprintf(stderr, "%s\n", SDL_GetError());
45         return 0;
46     }
47
48     /* ...and this has to happen after it. */
49
50     set_EWMH_icon(icon);
51
52     SDL_WM_SetCaption(title, title);
53
54     return 1;
55 }
56
57 /*---------------------------------------------------------------------------*/
58
59 PFNGLACTIVETEXTUREARBPROC glActiveTextureARB_;
60
61 int check_extension(const char *needle)
62 {
63     const GLubyte *haystack, *c;
64
65     /* Search for the given string in the OpenGL extension strings. */
66
67     for (haystack = glGetString(GL_EXTENSIONS); *haystack; haystack++)
68     {
69         for (c = (const GLubyte *) needle; *c && *haystack; c++, haystack++)
70             if (*c != *haystack)
71                 break;
72
73         if ((*c == 0) && (*haystack == ' ' || *haystack == '\0'))
74             return 1;
75     }
76
77     return 0;
78 }
79
80 int video_mode(int f, int w, int h)
81 {
82     int stereo  = config_get_d(CONFIG_STEREO)      ? 1 : 0;
83     int stencil = config_get_d(CONFIG_REFLECTION)  ? 1 : 0;
84     int buffers = config_get_d(CONFIG_MULTISAMPLE) ? 1 : 0;
85     int samples = config_get_d(CONFIG_MULTISAMPLE);
86     int vsync   = config_get_d(CONFIG_VSYNC)       ? 1 : 0;
87
88     SDL_GL_SetAttribute(SDL_GL_STEREO,             stereo);
89     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,       stencil);
90     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, buffers);
91     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples);
92     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,       vsync);
93
94     /* Require 16-bit double buffer with 16-bit depth buffer. */
95
96     SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
97     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
98     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
99     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
100     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
101
102     /* Try to set the currently specified mode. */
103
104     if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (f ? SDL_FULLSCREEN : 0)))
105     {
106         config_set_d(CONFIG_FULLSCREEN, f);
107         config_set_d(CONFIG_WIDTH,      w);
108         config_set_d(CONFIG_HEIGHT,     h);
109
110         glViewport(0, 0, w, h);
111         glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
112
113         glEnable(GL_NORMALIZE);
114         glEnable(GL_CULL_FACE);
115         glEnable(GL_DEPTH_TEST);
116         glEnable(GL_TEXTURE_2D);
117         glEnable(GL_LIGHTING);
118         glEnable(GL_BLEND);
119
120         glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,
121                       GL_SEPARATE_SPECULAR_COLOR);
122         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
123
124         glPixelStorei(GL_PACK_ALIGNMENT, 1);
125         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
126
127         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
128         glDepthFunc(GL_LEQUAL);
129
130         /* If GL supports multisample, and SDL got a multisample buffer... */
131
132 #ifdef GL_ARB_multisample
133         if (check_extension("ARB_multisample"))
134         {
135             SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
136             if (buffers)
137                 glEnable(GL_MULTISAMPLE_ARB);
138         }
139 #endif
140
141         if (check_extension("ARB_multitexture"))
142         {
143             union
144             {
145                 void *ob;
146                 PFNGLACTIVETEXTUREARBPROC fn;
147             } cast;
148
149             cast.ob = SDL_GL_GetProcAddress("glActiveTextureARB");
150             glActiveTextureARB_ = cast.fn;
151         }
152
153         glReadBuffer(GL_FRONT);
154
155         /* Attempt manual swap control if SDL's is broken. */
156
157         if (vsync && SDL_GL_GetAttribute(SDL_GL_SWAP_CONTROL, &vsync) == -1)
158             sync_init();
159
160         return 1;
161     }
162
163     /* If the mode failed, try it without stereo. */
164
165     else if (stereo)
166     {
167         config_set_d(CONFIG_STEREO, 0);
168         return video_mode(f, w, h);
169     }
170
171     /* If the mode failed, try decreasing the level of multisampling. */
172
173     else if (buffers)
174     {
175         config_set_d(CONFIG_MULTISAMPLE, samples / 2);
176         return video_mode(f, w, h);
177     }
178
179     /* If that mode failed, try it without reflections. */
180
181     else if (stencil)
182     {
183         config_set_d(CONFIG_REFLECTION, 0);
184         return video_mode(f, w, h);
185     }
186
187     /* If THAT mode failed, punt. */
188
189     return 0;
190 }
191
192 /*---------------------------------------------------------------------------*/
193
194 static float ms     = 0;
195 static int   fps    = 0;
196 static int   last   = 0;
197 static int   ticks  = 0;
198 static int   frames = 0;
199
200 int  video_perf(void)
201 {
202     return fps;
203 }
204
205 void video_swap(void)
206 {
207     int dt;
208
209     SDL_GL_SwapBuffers();
210
211     /* Accumulate time passed and frames rendered. */
212
213     dt = (int) SDL_GetTicks() - last;
214
215     frames +=  1;
216     ticks  += dt;
217     last   += dt;
218
219     /* Average over 250ms. */
220
221     if (ticks > 1000)
222     {
223         /* Round the frames-per-second value to the nearest integer. */
224
225         double k = 1000.0 * frames / ticks;
226         double f = floor(k);
227         double c = ceil (k);
228
229         /* Compute frame time and frames-per-second stats. */
230
231         fps = (int) ((c - k < k - f) ? c : f);
232         ms  = (float) ticks / (float) frames;
233
234         /* Reset the counters for the next update. */
235
236         frames = 0;
237         ticks  = 0;
238
239         /* Output statistics if configured. */
240
241         if (config_get_d(CONFIG_STATS))
242             fprintf(stdout, "%4d %8.4f\n", fps, ms);
243     }
244 }
245
246 /*---------------------------------------------------------------------------*/
247
248 static int grabbed = 0;
249
250 void video_set_grab(int w)
251 {
252     if (w)
253     {
254         SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
255
256         SDL_WarpMouse(config_get_d(CONFIG_WIDTH)  / 2,
257                       config_get_d(CONFIG_HEIGHT) / 2);
258
259         SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
260     }
261
262     SDL_WM_GrabInput(SDL_GRAB_ON);
263     SDL_ShowCursor(SDL_DISABLE);
264
265     grabbed = 1;
266 }
267
268 void video_clr_grab(void)
269 {
270     SDL_WM_GrabInput(SDL_GRAB_OFF);
271     SDL_ShowCursor(SDL_ENABLE);
272     grabbed = 0;
273 }
274
275 int  video_get_grab(void)
276 {
277     return grabbed;
278 }
279
280 /*---------------------------------------------------------------------------*/
281
282 void video_push_persp(float fov, float n, float f)
283 {
284     GLdouble m[4][4];
285
286     GLdouble r = fov / 2 * V_PI / 180;
287     GLdouble s = sin(r);
288     GLdouble c = cos(r) / s;
289
290     GLdouble a = ((GLdouble) config_get_d(CONFIG_WIDTH) /
291                   (GLdouble) config_get_d(CONFIG_HEIGHT));
292
293     glMatrixMode(GL_PROJECTION);
294     {
295         glPushMatrix();
296         glLoadIdentity();
297
298         m[0][0] =  c/a;
299         m[0][1] =  0.0;
300         m[0][2] =  0.0;
301         m[0][3] =  0.0;
302         m[1][0] =  0.0;
303         m[1][1] =    c;
304         m[1][2] =  0.0;
305         m[1][3] =  0.0;
306         m[2][0] =  0.0;
307         m[2][1] =  0.0;
308         m[2][2] = -(f + n) / (f - n);
309         m[2][3] = -1.0;
310         m[3][0] =  0.0;
311         m[3][1] =  0.0;
312         m[3][2] = -2.0 * n * f / (f - n);
313         m[3][3] =  0.0;
314
315         glMultMatrixd(&m[0][0]);
316     }
317     glMatrixMode(GL_MODELVIEW);
318 }
319
320 void video_push_ortho(void)
321 {
322     GLdouble w = (GLdouble) config_get_d(CONFIG_WIDTH);
323     GLdouble h = (GLdouble) config_get_d(CONFIG_HEIGHT);
324
325     glMatrixMode(GL_PROJECTION);
326     {
327         glPushMatrix();
328         glLoadIdentity();
329         glOrtho(0.0, w, 0.0, h, -1.0, +1.0);
330     }
331     glMatrixMode(GL_MODELVIEW);
332 }
333
334 void video_pop_matrix(void)
335 {
336     glMatrixMode(GL_PROJECTION);
337     {
338         glPopMatrix();
339     }
340     glMatrixMode(GL_MODELVIEW);
341 }
342
343 void video_clear(void)
344 {
345     if (config_get_d(CONFIG_REFLECTION))
346         glClear(GL_COLOR_BUFFER_BIT |
347                 GL_DEPTH_BUFFER_BIT |
348                 GL_STENCIL_BUFFER_BIT);
349     else
350         glClear(GL_COLOR_BUFFER_BIT |
351                 GL_DEPTH_BUFFER_BIT);
352 }
353
354 /*---------------------------------------------------------------------------*/