]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - vid_shared.c
bmodel shadow volumes
[xonotic/darkplaces.git] / vid_shared.c
1
2 #include "quakedef.h"
3
4 // global video state
5 viddef_t vid;
6
7 // LordHavoc: these are only set in wgl
8 qboolean isG200 = false; // LordHavoc: the Matrox G200 can't do per pixel alpha, and it uses a D3D driver for GL... ugh...
9 qboolean isRagePro = false; // LordHavoc: the ATI Rage Pro has limitations with per pixel alpha (the color scaler does not apply to per pixel alpha images...), although not as bad as a G200.
10
11 // LordHavoc: GL_ARB_multitexture support
12 int gl_textureunits = 0;
13 // LordHavoc: GL_ARB_texture_env_combine or GL_EXT_texture_env_combine support
14 int gl_combine_extension = false;
15 // LordHavoc: GL_EXT_compiled_vertex_array support
16 int gl_supportslockarrays = false;
17 // LordHavoc: GLX_SGI_video_sync and WGL_EXT_swap_control
18 int gl_videosyncavailable = false;
19 // indicates that stencil is available
20 int gl_stencil = false;
21
22 // LordHavoc: if window is hidden, don't update screen
23 int vid_hidden = true;
24 // LordHavoc: if window is not the active window, don't hog as much CPU time,
25 // let go of the mouse, turn off sound, and restore system gamma ramps...
26 int vid_activewindow = true;
27
28 cvar_t vid_fullscreen = {CVAR_SAVE, "vid_fullscreen", "1"};
29 cvar_t vid_width = {CVAR_SAVE, "vid_width", "640"};
30 cvar_t vid_height = {CVAR_SAVE, "vid_height", "480"};
31 cvar_t vid_bitsperpixel = {CVAR_SAVE, "vid_bitsperpixel", "16"};
32 cvar_t vid_stencil = {CVAR_SAVE, "vid_stencil", "0"};
33
34 cvar_t vid_mouse = {CVAR_SAVE, "vid_mouse", "1"};
35 cvar_t gl_combine = {0, "gl_combine", "1"};
36
37 cvar_t in_pitch_min = {0, "in_pitch_min", "-90"};
38 cvar_t in_pitch_max = {0, "in_pitch_max", "90"};
39
40 cvar_t m_filter = {CVAR_SAVE, "m_filter","0"};
41
42 // brand of graphics chip
43 const char *gl_vendor;
44 // graphics chip model and other information
45 const char *gl_renderer;
46 // begins with 1.0.0, 1.1.0, 1.2.0, 1.2.1, 1.3.0, 1.3.1, or 1.4.0
47 const char *gl_version;
48 // extensions list, space separated
49 const char *gl_extensions;
50 // WGL, GLX, or AGL
51 const char *gl_platform;
52 // another extensions list, containing platform-specific extensions that are
53 // not in the main list
54 const char *gl_platformextensions;
55 // name of driver library (opengl32.dll, libGL.so.1, or whatever)
56 char gl_driver[256];
57
58 // GL_ARB_multitexture
59 void (GLAPIENTRY *qglMultiTexCoord2f) (GLenum, GLfloat, GLfloat);
60 void (GLAPIENTRY *qglActiveTexture) (GLenum);
61 void (GLAPIENTRY *qglClientActiveTexture) (GLenum);
62
63 // GL_EXT_compiled_vertex_array
64 void (GLAPIENTRY *qglLockArraysEXT) (GLint first, GLint count);
65 void (GLAPIENTRY *qglUnlockArraysEXT) (void);
66
67
68 // general GL functions
69
70 void (GLAPIENTRY *qglClearColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
71
72 void (GLAPIENTRY *qglClear)(GLbitfield mask);
73
74 //void (GLAPIENTRY *qglAlphaFunc)(GLenum func, GLclampf ref);
75 void (GLAPIENTRY *qglBlendFunc)(GLenum sfactor, GLenum dfactor);
76 void (GLAPIENTRY *qglCullFace)(GLenum mode);
77
78 void (GLAPIENTRY *qglDrawBuffer)(GLenum mode);
79 void (GLAPIENTRY *qglReadBuffer)(GLenum mode);
80 void (GLAPIENTRY *qglEnable)(GLenum cap);
81 void (GLAPIENTRY *qglDisable)(GLenum cap);
82 //GLboolean GLAPIENTRY *qglIsEnabled)(GLenum cap);
83
84 void (GLAPIENTRY *qglEnableClientState)(GLenum cap);
85 void (GLAPIENTRY *qglDisableClientState)(GLenum cap);
86
87 void (GLAPIENTRY *qglGetBooleanv)(GLenum pname, GLboolean *params);
88 void (GLAPIENTRY *qglGetDoublev)(GLenum pname, GLdouble *params);
89 void (GLAPIENTRY *qglGetFloatv)(GLenum pname, GLfloat *params);
90 void (GLAPIENTRY *qglGetIntegerv)(GLenum pname, GLint *params);
91
92 GLenum (GLAPIENTRY *qglGetError)(void);
93 const GLubyte* (GLAPIENTRY *qglGetString)(GLenum name);
94 void (GLAPIENTRY *qglFinish)(void);
95 void (GLAPIENTRY *qglFlush)(void);
96
97 void (GLAPIENTRY *qglClearDepth)(GLclampd depth);
98 void (GLAPIENTRY *qglDepthFunc)(GLenum func);
99 void (GLAPIENTRY *qglDepthMask)(GLboolean flag);
100 void (GLAPIENTRY *qglDepthRange)(GLclampd near_val, GLclampd far_val);
101 void (GLAPIENTRY *qglColorMask)(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
102
103 void (GLAPIENTRY *qglDrawRangeElements)(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
104 void (GLAPIENTRY *qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
105 void (GLAPIENTRY *qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
106 //void (GLAPIENTRY *qglNormalPointer)(GLenum type, GLsizei stride, const GLvoid *ptr);
107 void (GLAPIENTRY *qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
108 void (GLAPIENTRY *qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
109 void (GLAPIENTRY *qglArrayElement)(GLint i);
110
111 void (GLAPIENTRY *qglColor4f)(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
112 void (GLAPIENTRY *qglTexCoord2f)(GLfloat s, GLfloat t);
113 void (GLAPIENTRY *qglVertex2f)(GLfloat x, GLfloat y);
114 void (GLAPIENTRY *qglVertex3f)(GLfloat x, GLfloat y, GLfloat z);
115 void (GLAPIENTRY *qglBegin)(GLenum mode);
116 void (GLAPIENTRY *qglEnd)(void);
117
118 void (GLAPIENTRY *qglMatrixMode)(GLenum mode);
119 void (GLAPIENTRY *qglOrtho)(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
120 void (GLAPIENTRY *qglFrustum)(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
121 void (GLAPIENTRY *qglViewport)(GLint x, GLint y, GLsizei width, GLsizei height);
122 //void (GLAPIENTRY *qglPushMatrix)(void);
123 //void (GLAPIENTRY *qglPopMatrix)(void);
124 void (GLAPIENTRY *qglLoadIdentity)(void);
125 void (GLAPIENTRY *qglLoadMatrixd)(const GLdouble *m);
126 void (GLAPIENTRY *qglLoadMatrixf)(const GLfloat *m);
127 //void (GLAPIENTRY *qglMultMatrixd)(const GLdouble *m);
128 //void (GLAPIENTRY *qglMultMatrixf)(const GLfloat *m);
129 //void (GLAPIENTRY *qglRotated)(GLdouble angle, GLdouble x, GLdouble y, GLdouble z);
130 void (GLAPIENTRY *qglRotatef)(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
131 //void (GLAPIENTRY *qglScaled)(GLdouble x, GLdouble y, GLdouble z);
132 //void (GLAPIENTRY *qglScalef)(GLfloat x, GLfloat y, GLfloat z);
133 //void (GLAPIENTRY *qglTranslated)(GLdouble x, GLdouble y, GLdouble z);
134 void (GLAPIENTRY *qglTranslatef)(GLfloat x, GLfloat y, GLfloat z);
135
136 void (GLAPIENTRY *qglReadPixels)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels);
137
138 void (GLAPIENTRY *qglStencilFunc)(GLenum func, GLint ref, GLuint mask);
139 void (GLAPIENTRY *qglStencilMask)(GLuint mask);
140 void (GLAPIENTRY *qglStencilOp)(GLenum fail, GLenum zfail, GLenum zpass);
141 void (GLAPIENTRY *qglClearStencil)(GLint s);
142
143 //void (GLAPIENTRY *qglTexEnvf)(GLenum target, GLenum pname, GLfloat param);
144 void (GLAPIENTRY *qglTexEnvi)(GLenum target, GLenum pname, GLint param);
145
146 //void (GLAPIENTRY *qglTexParameterf)(GLenum target, GLenum pname, GLfloat param);
147 void (GLAPIENTRY *qglTexParameteri)(GLenum target, GLenum pname, GLint param);
148
149 void (GLAPIENTRY *qglBindTexture)(GLenum target, GLuint texture);
150 void (GLAPIENTRY *qglTexImage2D)(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
151 void (GLAPIENTRY *qglTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels);
152 void (GLAPIENTRY *qglDeleteTextures)(GLsizei n, const GLuint *textures);
153 void (GLAPIENTRY *qglPixelStoref)(GLenum pname, GLfloat param);
154 void (GLAPIENTRY *qglPixelStorei)(GLenum pname, GLint param);
155
156 void (GLAPIENTRY *qglDrawRangeElementsEXT)(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
157
158 //void (GLAPIENTRY *qglColorTableEXT)(int, int, int, int, int, const void *);
159
160 int GL_CheckExtension(const char *name, const gl_extensionfunctionlist_t *funcs, const char *disableparm, int silent)
161 {
162         int failed = false;
163         const gl_extensionfunctionlist_t *func;
164
165         Con_Printf("checking for %s...  ", name);
166
167         for (func = funcs;func && func->name;func++)
168                 *func->funcvariable = NULL;
169
170         if (disableparm && COM_CheckParm(disableparm))
171         {
172                 Con_Printf("disabled by commandline\n");
173                 return false;
174         }
175
176         if (strstr(gl_extensions, name) || strstr(gl_platformextensions, name) || (strncmp(name, "GL_", 3) && strncmp(name, "WGL_", 4) && strncmp(name, "GLX_", 4) && strncmp(name, "AGL_", 4)))
177         {
178                 for (func = funcs;func && func->name != NULL;func++)
179                 {
180                         // functions are cleared before all the extensions are evaluated
181                         if (!(*func->funcvariable = (void *) GL_GetProcAddress(func->name)))
182                         {
183                                 if (!silent)
184                                         Con_Printf("missing function \"%s\" - broken driver!\n", func->name);
185                                 failed = true;
186                         }
187                 }
188                 // delay the return so it prints all missing functions
189                 if (failed)
190                         return false;
191                 Con_Printf("enabled\n");
192                 return true;
193         }
194         else
195         {
196                 Con_Printf("not detected\n");
197                 return false;
198         }
199 }
200
201 static gl_extensionfunctionlist_t opengl110funcs[] =
202 {
203         {"glClearColor", (void **) &qglClearColor},
204         {"glClear", (void **) &qglClear},
205 //      {"glAlphaFunc", (void **) &qglAlphaFunc},
206         {"glBlendFunc", (void **) &qglBlendFunc},
207         {"glCullFace", (void **) &qglCullFace},
208         {"glDrawBuffer", (void **) &qglDrawBuffer},
209         {"glReadBuffer", (void **) &qglReadBuffer},
210         {"glEnable", (void **) &qglEnable},
211         {"glDisable", (void **) &qglDisable},
212 //      {"glIsEnabled", (void **) &qglIsEnabled},
213         {"glEnableClientState", (void **) &qglEnableClientState},
214         {"glDisableClientState", (void **) &qglDisableClientState},
215         {"glGetBooleanv", (void **) &qglGetBooleanv},
216         {"glGetDoublev", (void **) &qglGetDoublev},
217         {"glGetFloatv", (void **) &qglGetFloatv},
218         {"glGetIntegerv", (void **) &qglGetIntegerv},
219         {"glGetError", (void **) &qglGetError},
220         {"glGetString", (void **) &qglGetString},
221         {"glFinish", (void **) &qglFinish},
222         {"glFlush", (void **) &qglFlush},
223         {"glClearDepth", (void **) &qglClearDepth},
224         {"glDepthFunc", (void **) &qglDepthFunc},
225         {"glDepthMask", (void **) &qglDepthMask},
226         {"glDepthRange", (void **) &qglDepthRange},
227         {"glDrawElements", (void **) &qglDrawElements},
228         {"glColorMask", (void **) &qglColorMask},
229         {"glVertexPointer", (void **) &qglVertexPointer},
230 //      {"glNormalPointer", (void **) &qglNormalPointer},
231         {"glColorPointer", (void **) &qglColorPointer},
232         {"glTexCoordPointer", (void **) &qglTexCoordPointer},
233         {"glArrayElement", (void **) &qglArrayElement},
234         {"glColor4f", (void **) &qglColor4f},
235         {"glTexCoord2f", (void **) &qglTexCoord2f},
236         {"glVertex2f", (void **) &qglVertex2f},
237         {"glVertex3f", (void **) &qglVertex3f},
238         {"glBegin", (void **) &qglBegin},
239         {"glEnd", (void **) &qglEnd},
240         {"glMatrixMode", (void **) &qglMatrixMode},
241         {"glOrtho", (void **) &qglOrtho},
242         {"glFrustum", (void **) &qglFrustum},
243         {"glViewport", (void **) &qglViewport},
244 //      {"glPushMatrix", (void **) &qglPushMatrix},
245 //      {"glPopMatrix", (void **) &qglPopMatrix},
246         {"glLoadIdentity", (void **) &qglLoadIdentity},
247         {"glLoadMatrixd", (void **) &qglLoadMatrixd},
248         {"glLoadMatrixf", (void **) &qglLoadMatrixf},
249 //      {"glMultMatrixd", (void **) &qglMultMatrixd},
250 //      {"glMultMatrixf", (void **) &qglMultMatrixf},
251 //      {"glRotated", (void **) &qglRotated},
252         {"glRotatef", (void **) &qglRotatef},
253 //      {"glScaled", (void **) &qglScaled},
254 //      {"glScalef", (void **) &qglScalef},
255 //      {"glTranslated", (void **) &qglTranslated},
256         {"glTranslatef", (void **) &qglTranslatef},
257         {"glReadPixels", (void **) &qglReadPixels},
258         {"glStencilFunc", (void **) &qglStencilFunc},
259         {"glStencilMask", (void **) &qglStencilMask},
260         {"glStencilOp", (void **) &qglStencilOp},
261         {"glClearStencil", (void **) &qglClearStencil},
262 //      {"glTexEnvf", (void **) &qglTexEnvf},
263         {"glTexEnvi", (void **) &qglTexEnvi},
264 //      {"glTexParameterf", (void **) &qglTexParameterf},
265         {"glTexParameteri", (void **) &qglTexParameteri},
266         {"glBindTexture", (void **) &qglBindTexture},
267         {"glTexImage2D", (void **) &qglTexImage2D},
268         {"glTexSubImage2D", (void **) &qglTexSubImage2D},
269         {"glDeleteTextures", (void **) &qglDeleteTextures},
270         {"glPixelStoref", (void **) &qglPixelStoref},
271         {"glPixelStorei", (void **) &qglPixelStorei},
272         {NULL, NULL}
273 };
274
275 static gl_extensionfunctionlist_t drawrangeelementsfuncs[] =
276 {
277         {"glDrawRangeElements", (void **) &qglDrawRangeElements},
278         {NULL, NULL}
279 };
280
281 static gl_extensionfunctionlist_t drawrangeelementsextfuncs[] =
282 {
283         {"glDrawRangeElementsEXT", (void **) &qglDrawRangeElementsEXT},
284         {NULL, NULL}
285 };
286
287 static gl_extensionfunctionlist_t multitexturefuncs[] =
288 {
289         {"glMultiTexCoord2fARB", (void **) &qglMultiTexCoord2f},
290         {"glActiveTextureARB", (void **) &qglActiveTexture},
291         {"glClientActiveTextureARB", (void **) &qglClientActiveTexture},
292         {NULL, NULL}
293 };
294
295 static gl_extensionfunctionlist_t compiledvertexarrayfuncs[] =
296 {
297         {"glLockArraysEXT", (void **) &qglLockArraysEXT},
298         {"glUnlockArraysEXT", (void **) &qglUnlockArraysEXT},
299         {NULL, NULL}
300 };
301
302 void VID_CheckExtensions(void)
303 {
304         gl_combine_extension = false;
305         gl_supportslockarrays = false;
306         gl_textureunits = 1;
307
308         if (!GL_CheckExtension("OpenGL 1.1.0", opengl110funcs, NULL, false))
309                 Sys_Error("OpenGL 1.1.0 functions not found\n");
310
311         Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
312         Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
313         Con_Printf ("GL_VERSION: %s\n", gl_version);
314         Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
315         Con_Printf ("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
316
317         Con_Printf("Checking OpenGL extensions...\n");
318
319         if (!GL_CheckExtension("glDrawRangeElements", drawrangeelementsfuncs, "-nodrawrangeelements", true))
320                 GL_CheckExtension("GL_EXT_draw_range_elements", drawrangeelementsextfuncs, "-nodrawrangeelements", false);
321
322         if (GL_CheckExtension("GL_ARB_multitexture", multitexturefuncs, "-nomtex", false))
323         {
324                 qglGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
325                 if (gl_textureunits > 1)
326                         gl_combine_extension = GL_CheckExtension("GL_ARB_texture_env_combine", NULL, "-nocombine", false) || GL_CheckExtension("GL_EXT_texture_env_combine", NULL, "-nocombine", false);
327                 else
328                 {
329                         Con_Printf("GL_ARB_multitexture with less than 2 units? - BROKEN DRIVER!\n");
330                         gl_textureunits = 1; // for sanity sake, make sure it's not 0
331                 }
332         }
333
334         gl_supportslockarrays = GL_CheckExtension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva", false);
335
336         // we don't care if it's an extension or not, they are identical functions, so keep it simple in the rendering code
337         if (qglDrawRangeElements == NULL)
338                 qglDrawRangeElements = qglDrawRangeElementsEXT;
339 }
340
341 void Force_CenterView_f (void)
342 {
343         cl.viewangles[PITCH] = 0;
344 }
345
346 void IN_PreMove(void)
347 {
348 }
349
350 void CL_AdjustAngles(void);
351 void IN_PostMove(void)
352 {
353         // clamp after the move as well to prevent messed up rendering angles
354         CL_AdjustAngles();
355 }
356
357 void IN_Mouse(usercmd_t *cmd, float mx, float my)
358 {
359         int mouselook = (in_mlook.state & 1) || freelook.integer;
360         float mouse_x, mouse_y;
361         static float old_mouse_x = 0, old_mouse_y = 0;
362
363         if (m_filter.integer)
364         {
365                 mouse_x = (mx + old_mouse_x) * 0.5;
366                 mouse_y = (my + old_mouse_y) * 0.5;
367         }
368         else
369         {
370                 mouse_x = mx;
371                 mouse_y = my;
372         }
373
374         old_mouse_x = mx;
375         old_mouse_y = my;
376
377         // LordHavoc: viewzoom affects mouse sensitivity for sniping
378         mouse_x *= sensitivity.value * cl.viewzoom;
379         mouse_y *= sensitivity.value * cl.viewzoom;
380
381         // Add mouse X/Y movement to cmd
382         if ((in_strafe.state & 1) || (lookstrafe.integer && mouselook))
383                 cmd->sidemove += m_side.value * mouse_x;
384         else
385                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
386
387         if (mouselook)
388                 V_StopPitchDrift();
389
390         if (mouselook && !(in_strafe.state & 1))
391                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
392         else
393         {
394                 if ((in_strafe.state & 1) && noclip_anglehack)
395                         cmd->upmove -= m_forward.value * mouse_y;
396                 else
397                         cmd->forwardmove -= m_forward.value * mouse_y;
398         }
399 }
400
401 void VID_Shared_Init(void)
402 {
403         int i;
404
405         Cvar_RegisterVariable(&vid_fullscreen);
406         Cvar_RegisterVariable(&vid_width);
407         Cvar_RegisterVariable(&vid_height);
408         Cvar_RegisterVariable(&vid_bitsperpixel);
409         Cvar_RegisterVariable(&vid_stencil);
410         Cvar_RegisterVariable(&vid_mouse);
411         Cvar_RegisterVariable(&gl_combine);
412         Cvar_RegisterVariable(&in_pitch_min);
413         Cvar_RegisterVariable(&in_pitch_max);
414         Cvar_RegisterVariable(&m_filter);
415         Cmd_AddCommand("force_centerview", Force_CenterView_f);
416         Cmd_AddCommand("vid_restart", VID_Restart_f);
417
418 // interpret command-line parameters
419         if ((i = COM_CheckParm("-window")) != 0)
420                 Cvar_SetValueQuick(&vid_fullscreen, false);
421         if ((i = COM_CheckParm("-fullscreen")) != 0)
422                 Cvar_SetValueQuick(&vid_fullscreen, true);
423         if ((i = COM_CheckParm("-width")) != 0)
424                 Cvar_SetQuick(&vid_width, com_argv[i+1]);
425         if ((i = COM_CheckParm("-height")) != 0)
426                 Cvar_SetQuick(&vid_height, com_argv[i+1]);
427         if ((i = COM_CheckParm("-bpp")) != 0)
428                 Cvar_SetQuick(&vid_bitsperpixel, com_argv[i+1]);
429         if ((i = COM_CheckParm("-nostencil")) != 0)
430                 Cvar_SetValueQuick(&vid_stencil, 0);
431         if ((i = COM_CheckParm("-stencil")) != 0)
432                 Cvar_SetValueQuick(&vid_stencil, 1);
433 }
434
435 int current_vid_fullscreen;
436 int current_vid_width;
437 int current_vid_height;
438 int current_vid_bitsperpixel;
439 int current_vid_stencil;
440 extern int VID_InitMode (int fullscreen, int width, int height, int bpp, int stencil);
441 int VID_Mode(int fullscreen, int width, int height, int bpp, int stencil)
442 {
443         Con_Printf("Video: %s %dx%dx%d %s\n", fullscreen ? "fullscreen" : "window", width, height, bpp, stencil ? "with stencil" : "without stencil");
444         if (VID_InitMode(fullscreen, width, height, bpp, stencil))
445         {
446                 current_vid_fullscreen = fullscreen;
447                 current_vid_width = width;
448                 current_vid_height = height;
449                 current_vid_bitsperpixel = bpp;
450                 current_vid_stencil = stencil;
451                 Cvar_SetValueQuick(&vid_fullscreen, fullscreen);
452                 Cvar_SetValueQuick(&vid_width, width);
453                 Cvar_SetValueQuick(&vid_height, height);
454                 Cvar_SetValueQuick(&vid_bitsperpixel, bpp);
455                 Cvar_SetValueQuick(&vid_stencil, stencil);
456                 return true;
457         }
458         else
459                 return false;
460 }
461
462 static void VID_OpenSystems(void)
463 {
464         R_Modules_Start();
465         S_Open();
466         CDAudio_Open();
467 }
468
469 static void VID_CloseSystems(void)
470 {
471         CDAudio_Close();
472         S_Close();
473         R_Modules_Shutdown();
474 }
475
476 void VID_Restart_f(void)
477 {
478         Con_Printf("VID_Restart: changing from %s %dx%dx%dbpp %s, to %s %dx%dx%dbpp %s.\n",
479                 current_vid_fullscreen ? "fullscreen" : "window", current_vid_width, current_vid_height, current_vid_bitsperpixel, current_vid_stencil ? "with stencil" : "without stencil",
480                 vid_fullscreen.integer ? "fullscreen" : "window", vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, vid_stencil.integer ? "with stencil" : "without stencil");
481         VID_Close();
482         if (!VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, vid_stencil.integer))
483         {
484                 Con_Printf("Video mode change failed\n");
485                 if (!VID_Mode(current_vid_fullscreen, current_vid_width, current_vid_height, current_vid_bitsperpixel, current_vid_stencil))
486                         Sys_Error("Unable to restore to last working video mode\n");
487         }
488         VID_OpenSystems();
489 }
490
491 void VID_Open(void)
492 {
493         Con_Printf("Starting video system\n");
494         if (!VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, vid_stencil.integer))
495         {
496                 Con_Printf("Desired video mode fail, trying fallbacks...\n");
497                 if (!vid_stencil.integer || !VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, false))
498                 {
499                         if (vid_fullscreen.integer)
500                         {
501                                 if (!VID_Mode(true, 640, 480, 16, false))
502                                         if (!VID_Mode(false, 640, 480, 16, false))
503                                                 Sys_Error("Video modes failed\n");
504                         }
505                         else
506                                 Sys_Error("Windowed video failed\n");
507                 }
508         }
509         VID_OpenSystems();
510 }
511
512 void VID_Close(void)
513 {
514         VID_CloseSystems();
515         VID_Shutdown();
516 }