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.
21 /*---------------------------------------------------------------------------*/
23 int video_init(const char *title, const char *icon)
25 SDL_QuitSubSystem(SDL_INIT_VIDEO);
27 if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
29 fprintf(stderr, "%s\n", SDL_GetError());
33 /* This has to happen before mode setting... */
37 /* Initialize the video. */
39 if (!video_mode(config_get_d(CONFIG_FULLSCREEN),
40 config_get_d(CONFIG_WIDTH),
41 config_get_d(CONFIG_HEIGHT)))
43 fprintf(stderr, "%s\n", SDL_GetError());
47 /* ...and this has to happen after it. */
51 SDL_WM_SetCaption(title, title);
53 SDL_ShowCursor(SDL_DISABLE);
58 /*---------------------------------------------------------------------------*/
60 int check_extension(const char *needle)
62 const GLubyte *haystack, *c;
64 /* Search for the given string in the OpenGL extension strings. */
66 for (haystack = glGetString(GL_EXTENSIONS); *haystack; haystack++)
68 for (c = (const GLubyte *) needle; *c && *haystack; c++, haystack++)
72 if ((*c == 0) && (*haystack == ' ' || *haystack == '\0'))
79 int video_mode(int f, int w, int h)
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;
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);
93 /* Require 16-bit double buffer with 16-bit depth buffer. */
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);
101 /* Try to set the currently specified mode. */
103 if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (f ? SDL_FULLSCREEN : 0)))
105 config_set_d(CONFIG_FULLSCREEN, f);
106 config_set_d(CONFIG_WIDTH, w);
107 config_set_d(CONFIG_HEIGHT, h);
109 glViewport(0, 0, w, h);
110 glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
112 glEnable(GL_NORMALIZE);
113 glEnable(GL_CULL_FACE);
114 glEnable(GL_DEPTH_TEST);
115 glEnable(GL_TEXTURE_2D);
116 glEnable(GL_LIGHTING);
119 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,
120 GL_SEPARATE_SPECULAR_COLOR);
121 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
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 glReadBuffer(GL_FRONT);
142 /* If the mode failed, try it without stereo. */
146 config_set_d(CONFIG_STEREO, 0);
147 return video_mode(f, w, h);
150 /* If the mode failed, try decreasing the level of multisampling. */
154 config_set_d(CONFIG_MULTISAMPLE, samples / 2);
155 return video_mode(f, w, h);
158 /* If that mode failed, try it without reflections. */
162 config_set_d(CONFIG_REFLECTION, 0);
163 return video_mode(f, w, h);
166 /* If THAT mode failed, punt. */
171 /*---------------------------------------------------------------------------*/
176 static int ticks = 0;
177 static int frames = 0;
184 void video_swap(void)
188 SDL_GL_SwapBuffers();
190 /* Accumulate time passed and frames rendered. */
192 dt = (int) SDL_GetTicks() - last;
198 /* Average over 250ms. */
202 /* Round the frames-per-second value to the nearest integer. */
204 double k = 1000.0 * frames / ticks;
208 /* Compute frame time and frames-per-second stats. */
210 fps = (int) ((c - k < k - f) ? c : f);
211 ms = (float) ticks / (float) frames;
213 /* Reset the counters for the next update. */
218 /* Output statistics if configured. */
220 if (config_get_d(CONFIG_STATS))
221 fprintf(stdout, "%4d %8.4f\n", fps, ms);
225 /*---------------------------------------------------------------------------*/
227 static int grabbed = 0;
229 void video_set_grab(int w)
233 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
235 SDL_WarpMouse(config_get_d(CONFIG_WIDTH) / 2,
236 config_get_d(CONFIG_HEIGHT) / 2);
238 SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
241 SDL_WM_GrabInput(SDL_GRAB_ON);
246 void video_clr_grab(void)
248 SDL_WM_GrabInput(SDL_GRAB_OFF);
252 int video_get_grab(void)
257 /*---------------------------------------------------------------------------*/
259 void video_push_persp(float fov, float n, float f)
263 GLdouble r = fov / 2 * V_PI / 180;
265 GLdouble c = cos(r) / s;
267 GLdouble a = ((GLdouble) config_get_d(CONFIG_WIDTH) /
268 (GLdouble) config_get_d(CONFIG_HEIGHT));
270 glMatrixMode(GL_PROJECTION);
285 m[2][2] = -(f + n) / (f - n);
289 m[3][2] = -2.0 * n * f / (f - n);
292 glMultMatrixd(&m[0][0]);
294 glMatrixMode(GL_MODELVIEW);
297 void video_push_ortho(void)
299 GLdouble w = (GLdouble) config_get_d(CONFIG_WIDTH);
300 GLdouble h = (GLdouble) config_get_d(CONFIG_HEIGHT);
302 glMatrixMode(GL_PROJECTION);
306 glOrtho(0.0, w, 0.0, h, -1.0, +1.0);
308 glMatrixMode(GL_MODELVIEW);
311 void video_pop_matrix(void)
313 glMatrixMode(GL_PROJECTION);
317 glMatrixMode(GL_MODELVIEW);
320 void video_clear(void)
322 if (config_get_d(CONFIG_REFLECTION))
323 glClear(GL_COLOR_BUFFER_BIT |
324 GL_DEPTH_BUFFER_BIT |
325 GL_STENCIL_BUFFER_BIT);
327 glClear(GL_COLOR_BUFFER_BIT |
328 GL_DEPTH_BUFFER_BIT);
331 /*---------------------------------------------------------------------------*/