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 glActiveTextureARB_;
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.1f, 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);
122 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
124 glPixelStorei(GL_PACK_ALIGNMENT, 1);
125 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
127 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
128 glDepthFunc(GL_LEQUAL);
130 /* If GL supports multisample, and SDL got a multisample buffer... */
132 #ifdef GL_ARB_multisample
133 if (check_extension("ARB_multisample"))
135 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
137 glEnable(GL_MULTISAMPLE_ARB);
141 if (check_extension("ARB_multitexture"))
146 PFNGLACTIVETEXTUREARBPROC fn;
149 cast.ob = SDL_GL_GetProcAddress("glActiveTextureARB");
150 glActiveTextureARB_ = cast.fn;
153 glReadBuffer(GL_FRONT);
155 /* Attempt manual swap control if SDL's is broken. */
157 if (vsync && SDL_GL_GetAttribute(SDL_GL_SWAP_CONTROL, &vsync) == -1)
163 /* If the mode failed, try it without stereo. */
167 config_set_d(CONFIG_STEREO, 0);
168 return video_mode(f, w, h);
171 /* If the mode failed, try decreasing the level of multisampling. */
175 config_set_d(CONFIG_MULTISAMPLE, samples / 2);
176 return video_mode(f, w, h);
179 /* If that mode failed, try it without reflections. */
183 config_set_d(CONFIG_REFLECTION, 0);
184 return video_mode(f, w, h);
187 /* If THAT mode failed, punt. */
192 /*---------------------------------------------------------------------------*/
197 static int ticks = 0;
198 static int frames = 0;
205 void video_swap(void)
209 SDL_GL_SwapBuffers();
211 /* Accumulate time passed and frames rendered. */
213 dt = (int) SDL_GetTicks() - last;
219 /* Average over 250ms. */
223 /* Round the frames-per-second value to the nearest integer. */
225 double k = 1000.0 * frames / ticks;
229 /* Compute frame time and frames-per-second stats. */
231 fps = (int) ((c - k < k - f) ? c : f);
232 ms = (float) ticks / (float) frames;
234 /* Reset the counters for the next update. */
239 /* Output statistics if configured. */
241 if (config_get_d(CONFIG_STATS))
242 fprintf(stdout, "%4d %8.4f\n", fps, ms);
246 /*---------------------------------------------------------------------------*/
248 static int grabbed = 0;
250 void video_set_grab(int w)
254 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
256 SDL_WarpMouse(config_get_d(CONFIG_WIDTH) / 2,
257 config_get_d(CONFIG_HEIGHT) / 2);
259 SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
262 SDL_WM_GrabInput(SDL_GRAB_ON);
263 SDL_ShowCursor(SDL_DISABLE);
268 void video_clr_grab(void)
270 SDL_WM_GrabInput(SDL_GRAB_OFF);
271 SDL_ShowCursor(SDL_ENABLE);
275 int video_get_grab(void)
280 /*---------------------------------------------------------------------------*/
282 void video_push_persp(float fov, float n, float f)
286 GLdouble r = fov / 2 * V_PI / 180;
288 GLdouble c = cos(r) / s;
290 GLdouble a = ((GLdouble) config_get_d(CONFIG_WIDTH) /
291 (GLdouble) config_get_d(CONFIG_HEIGHT));
293 glMatrixMode(GL_PROJECTION);
308 m[2][2] = -(f + n) / (f - n);
312 m[3][2] = -2.0 * n * f / (f - n);
315 glMultMatrixd(&m[0][0]);
317 glMatrixMode(GL_MODELVIEW);
320 void video_push_ortho(void)
322 GLdouble w = (GLdouble) config_get_d(CONFIG_WIDTH);
323 GLdouble h = (GLdouble) config_get_d(CONFIG_HEIGHT);
325 glMatrixMode(GL_PROJECTION);
329 glOrtho(0.0, w, 0.0, h, -1.0, +1.0);
331 glMatrixMode(GL_MODELVIEW);
334 void video_pop_matrix(void)
336 glMatrixMode(GL_PROJECTION);
340 glMatrixMode(GL_MODELVIEW);
343 void video_clear(void)
345 if (config_get_d(CONFIG_REFLECTION))
346 glClear(GL_COLOR_BUFFER_BIT |
347 GL_DEPTH_BUFFER_BIT |
348 GL_STENCIL_BUFFER_BIT);
350 glClear(GL_COLOR_BUFFER_BIT |
351 GL_DEPTH_BUFFER_BIT);
354 /*---------------------------------------------------------------------------*/