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 int video_mode(int f, int w, int h)
61 int stereo = config_get_d(CONFIG_STEREO) ? 1 : 0;
62 int stencil = config_get_d(CONFIG_REFLECTION) ? 1 : 0;
63 int buffers = config_get_d(CONFIG_MULTISAMPLE) ? 1 : 0;
64 int samples = config_get_d(CONFIG_MULTISAMPLE);
65 int vsync = config_get_d(CONFIG_VSYNC) ? 1 : 0;
67 SDL_GL_SetAttribute(SDL_GL_STEREO, stereo);
68 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, stencil);
69 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, buffers);
70 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples);
71 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, vsync);
73 /* Require 16-bit double buffer with 16-bit depth buffer. */
75 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
76 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
77 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
78 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
79 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
81 /* Try to set the currently specified mode. */
83 if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (f ? SDL_FULLSCREEN : 0)))
85 config_set_d(CONFIG_FULLSCREEN, f);
86 config_set_d(CONFIG_WIDTH, w);
87 config_set_d(CONFIG_HEIGHT, h);
92 glViewport(0, 0, w, h);
93 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
95 glEnable(GL_NORMALIZE);
96 glEnable(GL_CULL_FACE);
97 glEnable(GL_DEPTH_TEST);
98 glEnable(GL_TEXTURE_2D);
99 glEnable(GL_LIGHTING);
103 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,
104 GL_SEPARATE_SPECULAR_COLOR);
107 glPixelStorei(GL_PACK_ALIGNMENT, 1);
108 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
110 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
111 glDepthFunc(GL_LEQUAL);
113 /* If GL supports multisample, and SDL got a multisample buffer... */
115 if (glext_check("ARB_multisample"))
117 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
118 if (buffers) glEnable(GL_MULTISAMPLE);
121 /* Attempt manual swap control if SDL's is broken. */
123 if (vsync && SDL_GL_GetAttribute(SDL_GL_SWAP_CONTROL, &vsync) == -1)
129 /* If the mode failed, try it without stereo. */
133 config_set_d(CONFIG_STEREO, 0);
134 return video_mode(f, w, h);
137 /* If the mode failed, try decreasing the level of multisampling. */
141 config_set_d(CONFIG_MULTISAMPLE, samples / 2);
142 return video_mode(f, w, h);
145 /* If that mode failed, try it without reflections. */
149 config_set_d(CONFIG_REFLECTION, 0);
150 return video_mode(f, w, h);
153 /* If THAT mode failed, punt. */
158 /*---------------------------------------------------------------------------*/
163 static int ticks = 0;
164 static int frames = 0;
171 void video_swap(void)
175 SDL_GL_SwapBuffers();
177 /* Accumulate time passed and frames rendered. */
179 dt = (int) SDL_GetTicks() - last;
185 /* Average over 250ms. */
189 /* Round the frames-per-second value to the nearest integer. */
191 double k = 1000.0 * frames / ticks;
195 /* Compute frame time and frames-per-second stats. */
197 fps = (int) ((c - k < k - f) ? c : f);
198 ms = (float) ticks / (float) frames;
200 /* Reset the counters for the next update. */
205 /* Output statistics if configured. */
207 if (config_get_d(CONFIG_STATS))
208 fprintf(stdout, "%4d %8.4f\n", fps, ms);
212 /*---------------------------------------------------------------------------*/
214 static int grabbed = 0;
216 void video_set_grab(int w)
220 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
222 SDL_WarpMouse(config_get_d(CONFIG_WIDTH) / 2,
223 config_get_d(CONFIG_HEIGHT) / 2);
225 SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
228 SDL_WM_GrabInput(SDL_GRAB_ON);
229 SDL_ShowCursor(SDL_DISABLE);
234 void video_clr_grab(void)
236 SDL_WM_GrabInput(SDL_GRAB_OFF);
237 SDL_ShowCursor(SDL_ENABLE);
241 int video_get_grab(void)
246 /*---------------------------------------------------------------------------*/
248 void video_push_persp(float fov, float n, float f)
252 GLfloat r = fov / 2 * V_PI / 180;
254 GLfloat c = cos(r) / s;
256 GLfloat a = ((GLfloat) config_get_d(CONFIG_WIDTH) /
257 (GLfloat) config_get_d(CONFIG_HEIGHT));
259 glMatrixMode(GL_PROJECTION);
274 m[2][2] = -(f + n) / (f - n);
278 m[3][2] = -2.0f * n * f / (f - n);
281 glMultMatrixf(&m[0][0]);
283 glMatrixMode(GL_MODELVIEW);
286 void video_push_ortho(void)
288 GLfloat w = (GLfloat) config_get_d(CONFIG_WIDTH);
289 GLfloat h = (GLfloat) config_get_d(CONFIG_HEIGHT);
291 glMatrixMode(GL_PROJECTION);
295 glOrtho_(0.0, w, 0.0, h, -1.0, +1.0);
297 glMatrixMode(GL_MODELVIEW);
300 void video_pop_matrix(void)
302 glMatrixMode(GL_PROJECTION);
306 glMatrixMode(GL_MODELVIEW);
309 void video_clear(void)
311 if (config_get_d(CONFIG_REFLECTION))
312 glClear(GL_DEPTH_BUFFER_BIT |
313 GL_STENCIL_BUFFER_BIT);
315 glClear(GL_DEPTH_BUFFER_BIT);
318 /*---------------------------------------------------------------------------*/