2 * Copyright (C) 2003 Robert Kooima
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.
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.
22 /*---------------------------------------------------------------------------*/
24 int video_init(const char *title, const char *icon)
26 SDL_QuitSubSystem(SDL_INIT_VIDEO);
28 if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
30 fprintf(stderr, "%s\n", SDL_GetError());
34 /* This has to happen before mode setting... */
38 /* Initialize the video. */
40 if (!video_mode(config_get_d(CONFIG_FULLSCREEN),
41 config_get_d(CONFIG_WIDTH),
42 config_get_d(CONFIG_HEIGHT)))
44 fprintf(stderr, "%s\n", SDL_GetError());
48 /* ...and this has to happen after it. */
52 SDL_WM_SetCaption(title, title);
57 /*---------------------------------------------------------------------------*/
59 PFNGLACTIVETEXTUREARBPROC glActiveTexture_;
61 int check_extension(const char *needle)
63 const GLubyte *haystack, *c;
65 /* Search for the given string in the OpenGL extension strings. */
67 for (haystack = glGetString(GL_EXTENSIONS); *haystack; haystack++)
69 for (c = (const GLubyte *) needle; *c && *haystack; c++, haystack++)
73 if ((*c == 0) && (*haystack == ' ' || *haystack == '\0'))
80 int video_mode(int f, int w, int h)
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;
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);
94 /* Require 16-bit double buffer with 16-bit depth buffer. */
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);
102 /* Try to set the currently specified mode. */
104 if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (f ? SDL_FULLSCREEN : 0)))
106 config_set_d(CONFIG_FULLSCREEN, f);
107 config_set_d(CONFIG_WIDTH, w);
108 config_set_d(CONFIG_HEIGHT, h);
110 glViewport(0, 0, w, h);
111 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
113 glEnable(GL_NORMALIZE);
114 glEnable(GL_CULL_FACE);
115 glEnable(GL_DEPTH_TEST);
116 glEnable(GL_TEXTURE_2D);
117 glEnable(GL_LIGHTING);
120 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,
121 GL_SEPARATE_SPECULAR_COLOR);
123 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
124 glDepthFunc(GL_LEQUAL);
126 /* If GL supports multisample, and SDL got a multisample buffer... */
128 #ifdef GL_ARB_multisample
129 if (check_extension("ARB_multisample"))
131 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
133 glEnable(GL_MULTISAMPLE_ARB);
137 if (check_extension("ARB_multitexture"))
142 PFNGLACTIVETEXTUREARBPROC fn;
145 cast.ob = SDL_GL_GetProcAddress("glActiveTextureARB");
146 glActiveTexture_ = cast.fn;
149 /* Attempt manual swap control if SDL's is broken. */
151 if (vsync && SDL_GL_GetAttribute(SDL_GL_SWAP_CONTROL, &vsync) == -1)
157 /* If the mode failed, try it without stereo. */
161 config_set_d(CONFIG_STEREO, 0);
162 return video_mode(f, w, h);
165 /* If the mode failed, try decreasing the level of multisampling. */
169 config_set_d(CONFIG_MULTISAMPLE, samples / 2);
170 return video_mode(f, w, h);
173 /* If that mode failed, try it without reflections. */
177 config_set_d(CONFIG_REFLECTION, 0);
178 return video_mode(f, w, h);
181 /* If THAT mode failed, punt. */
186 /*---------------------------------------------------------------------------*/
191 static int ticks = 0;
192 static int frames = 0;
199 void video_swap(void)
203 SDL_GL_SwapBuffers();
205 /* Accumulate time passed and frames rendered. */
207 dt = (int) SDL_GetTicks() - last;
213 /* Average over 250ms. */
217 /* Round the frames-per-second value to the nearest integer. */
219 double k = 1000.0 * frames / ticks;
223 /* Compute frame time and frames-per-second stats. */
225 fps = (int) ((c - k < k - f) ? c : f);
226 ms = (float) ticks / (float) frames;
228 /* Reset the counters for the next update. */
233 /* Output statistics if configured. */
235 if (config_get_d(CONFIG_STATS))
236 fprintf(stdout, "%4d %8.4f\n", fps, ms);
240 /*---------------------------------------------------------------------------*/
242 static int grabbed = 0;
244 void video_set_grab(int w)
248 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
250 SDL_WarpMouse(config_get_d(CONFIG_WIDTH) / 2,
251 config_get_d(CONFIG_HEIGHT) / 2);
253 SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
256 SDL_WM_GrabInput(SDL_GRAB_ON);
257 SDL_ShowCursor(SDL_DISABLE);
262 void video_clr_grab(void)
264 SDL_WM_GrabInput(SDL_GRAB_OFF);
265 SDL_ShowCursor(SDL_ENABLE);
269 int video_get_grab(void)
274 /*---------------------------------------------------------------------------*/
276 void video_push_persp(float fov, float n, float f)
280 GLfloat r = fov / 2 * V_PI / 180;
282 GLfloat c = cos(r) / s;
284 GLfloat a = ((GLfloat) config_get_d(CONFIG_WIDTH) /
285 (GLfloat) config_get_d(CONFIG_HEIGHT));
287 glMatrixMode(GL_PROJECTION);
302 m[2][2] = -(f + n) / (f - n);
306 m[3][2] = -2.0f * n * f / (f - n);
309 glMultMatrixf(&m[0][0]);
311 glMatrixMode(GL_MODELVIEW);
314 void video_push_ortho(void)
316 GLfloat w = (GLfloat) config_get_d(CONFIG_WIDTH);
317 GLfloat h = (GLfloat) config_get_d(CONFIG_HEIGHT);
319 glMatrixMode(GL_PROJECTION);
323 glOrtho(0.0, w, 0.0, h, -1.0, +1.0);
325 glMatrixMode(GL_MODELVIEW);
328 void video_pop_matrix(void)
330 glMatrixMode(GL_PROJECTION);
334 glMatrixMode(GL_MODELVIEW);
337 void video_clear(void)
339 if (config_get_d(CONFIG_REFLECTION))
340 glClear(GL_DEPTH_BUFFER_BIT |
341 GL_STENCIL_BUFFER_BIT);
343 glClear(GL_DEPTH_BUFFER_BIT);
346 /*---------------------------------------------------------------------------*/