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