]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - vid_shared.c
trying to fix many forward references in input code
[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
102 void (GLAPIENTRY *qglDrawRangeElements)(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
103 void (GLAPIENTRY *qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
104 void (GLAPIENTRY *qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
105 //void (GLAPIENTRY *qglNormalPointer)(GLenum type, GLsizei stride, const GLvoid *ptr);
106 void (GLAPIENTRY *qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
107 void (GLAPIENTRY *qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
108 void (GLAPIENTRY *qglArrayElement)(GLint i);
109
110 void (GLAPIENTRY *qglColor4ub)(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha);
111 void (GLAPIENTRY *qglTexCoord2f)(GLfloat s, GLfloat t);
112 void (GLAPIENTRY *qglVertex2f)(GLfloat x, GLfloat y);
113 void (GLAPIENTRY *qglVertex3f)(GLfloat x, GLfloat y, GLfloat z);
114 void (GLAPIENTRY *qglBegin)(GLenum mode);
115 void (GLAPIENTRY *qglEnd)(void);
116
117 void (GLAPIENTRY *qglMatrixMode)(GLenum mode);
118 void (GLAPIENTRY *qglOrtho)(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
119 void (GLAPIENTRY *qglFrustum)(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
120 void (GLAPIENTRY *qglViewport)(GLint x, GLint y, GLsizei width, GLsizei height);
121 //void (GLAPIENTRY *qglPushMatrix)(void);
122 //void (GLAPIENTRY *qglPopMatrix)(void);
123 void (GLAPIENTRY *qglLoadIdentity)(void);
124 void (GLAPIENTRY *qglLoadMatrixd)(const GLdouble *m);
125 void (GLAPIENTRY *qglLoadMatrixf)(const GLfloat *m);
126 //void (GLAPIENTRY *qglMultMatrixd)(const GLdouble *m);
127 //void (GLAPIENTRY *qglMultMatrixf)(const GLfloat *m);
128 //void (GLAPIENTRY *qglRotated)(GLdouble angle, GLdouble x, GLdouble y, GLdouble z);
129 void (GLAPIENTRY *qglRotatef)(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
130 //void (GLAPIENTRY *qglScaled)(GLdouble x, GLdouble y, GLdouble z);
131 //void (GLAPIENTRY *qglScalef)(GLfloat x, GLfloat y, GLfloat z);
132 //void (GLAPIENTRY *qglTranslated)(GLdouble x, GLdouble y, GLdouble z);
133 void (GLAPIENTRY *qglTranslatef)(GLfloat x, GLfloat y, GLfloat z);
134
135 void (GLAPIENTRY *qglReadPixels)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels);
136
137 void (GLAPIENTRY *qglStencilFunc)(GLenum func, GLint ref, GLuint mask);
138 void (GLAPIENTRY *qglStencilMask)(GLuint mask);
139 void (GLAPIENTRY *qglStencilOp)(GLenum fail, GLenum zfail, GLenum zpass);
140 void (GLAPIENTRY *qglClearStencil)(GLint s);
141
142 //void (GLAPIENTRY *qglTexEnvf)(GLenum target, GLenum pname, GLfloat param);
143 void (GLAPIENTRY *qglTexEnvi)(GLenum target, GLenum pname, GLint param);
144
145 //void (GLAPIENTRY *qglTexParameterf)(GLenum target, GLenum pname, GLfloat param);
146 void (GLAPIENTRY *qglTexParameteri)(GLenum target, GLenum pname, GLint param);
147
148 void (GLAPIENTRY *qglBindTexture)(GLenum target, GLuint texture);
149 void (GLAPIENTRY *qglTexImage2D)(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
150 void (GLAPIENTRY *qglTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels);
151 void (GLAPIENTRY *qglDeleteTextures)(GLsizei n, const GLuint *textures);
152 void (GLAPIENTRY *qglPixelStoref)(GLenum pname, GLfloat param);
153 void (GLAPIENTRY *qglPixelStorei)(GLenum pname, GLint param);
154
155 void (GLAPIENTRY *qglDrawRangeElementsEXT)(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
156
157 //void (GLAPIENTRY *qglColorTableEXT)(int, int, int, int, int, const void *);
158
159 int GL_CheckExtension(const char *name, const gl_extensionfunctionlist_t *funcs, const char *disableparm, int silent)
160 {
161         int failed = false;
162         const gl_extensionfunctionlist_t *func;
163
164         Con_Printf("checking for %s...  ", name);
165
166         for (func = funcs;func && func->name;func++)
167                 *func->funcvariable = NULL;
168
169         if (disableparm && COM_CheckParm(disableparm))
170         {
171                 Con_Printf("disabled by commandline\n");
172                 return false;
173         }
174
175         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)))
176         {
177                 for (func = funcs;func && func->name != NULL;func++)
178                 {
179                         // functions are cleared before all the extensions are evaluated
180                         if (!(*func->funcvariable = (void *) GL_GetProcAddress(func->name)))
181                         {
182                                 if (!silent)
183                                         Con_Printf("missing function \"%s\" - broken driver!\n", func->name);
184                                 failed = true;
185                         }
186                 }
187                 // delay the return so it prints all missing functions
188                 if (failed)
189                         return false;
190                 Con_Printf("enabled\n");
191                 return true;
192         }
193         else
194         {
195                 Con_Printf("not detected\n");
196                 return false;
197         }
198 }
199
200 static gl_extensionfunctionlist_t opengl110funcs[] =
201 {
202         {"glClearColor", (void **) &qglClearColor},
203         {"glClear", (void **) &qglClear},
204 //      {"glAlphaFunc", (void **) &qglAlphaFunc},
205         {"glBlendFunc", (void **) &qglBlendFunc},
206         {"glCullFace", (void **) &qglCullFace},
207         {"glDrawBuffer", (void **) &qglDrawBuffer},
208         {"glReadBuffer", (void **) &qglReadBuffer},
209         {"glEnable", (void **) &qglEnable},
210         {"glDisable", (void **) &qglDisable},
211 //      {"glIsEnabled", (void **) &qglIsEnabled},
212         {"glEnableClientState", (void **) &qglEnableClientState},
213         {"glDisableClientState", (void **) &qglDisableClientState},
214         {"glGetBooleanv", (void **) &qglGetBooleanv},
215         {"glGetDoublev", (void **) &qglGetDoublev},
216         {"glGetFloatv", (void **) &qglGetFloatv},
217         {"glGetIntegerv", (void **) &qglGetIntegerv},
218         {"glGetError", (void **) &qglGetError},
219         {"glGetString", (void **) &qglGetString},
220         {"glFinish", (void **) &qglFinish},
221         {"glFlush", (void **) &qglFlush},
222         {"glClearDepth", (void **) &qglClearDepth},
223         {"glDepthFunc", (void **) &qglDepthFunc},
224         {"glDepthMask", (void **) &qglDepthMask},
225         {"glDepthRange", (void **) &qglDepthRange},
226         {"glDrawElements", (void **) &qglDrawElements},
227         {"glVertexPointer", (void **) &qglVertexPointer},
228 //      {"glNormalPointer", (void **) &qglNormalPointer},
229         {"glColorPointer", (void **) &qglColorPointer},
230         {"glTexCoordPointer", (void **) &qglTexCoordPointer},
231         {"glArrayElement", (void **) &qglArrayElement},
232         {"glColor4ub", (void **) &qglColor4ub},
233         {"glTexCoord2f", (void **) &qglTexCoord2f},
234         {"glVertex2f", (void **) &qglVertex2f},
235         {"glVertex3f", (void **) &qglVertex3f},
236         {"glBegin", (void **) &qglBegin},
237         {"glEnd", (void **) &qglEnd},
238         {"glMatrixMode", (void **) &qglMatrixMode},
239         {"glOrtho", (void **) &qglOrtho},
240         {"glFrustum", (void **) &qglFrustum},
241         {"glViewport", (void **) &qglViewport},
242 //      {"glPushMatrix", (void **) &qglPushMatrix},
243 //      {"glPopMatrix", (void **) &qglPopMatrix},
244         {"glLoadIdentity", (void **) &qglLoadIdentity},
245         {"glLoadMatrixd", (void **) &qglLoadMatrixd},
246         {"glLoadMatrixf", (void **) &qglLoadMatrixf},
247 //      {"glMultMatrixd", (void **) &qglMultMatrixd},
248 //      {"glMultMatrixf", (void **) &qglMultMatrixf},
249 //      {"glRotated", (void **) &qglRotated},
250         {"glRotatef", (void **) &qglRotatef},
251 //      {"glScaled", (void **) &qglScaled},
252 //      {"glScalef", (void **) &qglScalef},
253 //      {"glTranslated", (void **) &qglTranslated},
254         {"glTranslatef", (void **) &qglTranslatef},
255         {"glReadPixels", (void **) &qglReadPixels},
256         {"glStencilFunc", (void **) &qglStencilFunc},
257         {"glStencilMask", (void **) &qglStencilMask},
258         {"glStencilOp", (void **) &qglStencilOp},
259         {"glClearStencil", (void **) &qglClearStencil},
260 //      {"glTexEnvf", (void **) &qglTexEnvf},
261         {"glTexEnvi", (void **) &qglTexEnvi},
262 //      {"glTexParameterf", (void **) &qglTexParameterf},
263         {"glTexParameteri", (void **) &qglTexParameteri},
264         {"glBindTexture", (void **) &qglBindTexture},
265         {"glTexImage2D", (void **) &qglTexImage2D},
266         {"glTexSubImage2D", (void **) &qglTexSubImage2D},
267         {"glDeleteTextures", (void **) &qglDeleteTextures},
268         {"glPixelStoref", (void **) &qglPixelStoref},
269         {"glPixelStorei", (void **) &qglPixelStorei},
270         {NULL, NULL}
271 };
272
273 static gl_extensionfunctionlist_t drawrangeelementsfuncs[] =
274 {
275         {"glDrawRangeElements", (void **) &qglDrawRangeElements},
276         {NULL, NULL}
277 };
278
279 static gl_extensionfunctionlist_t drawrangeelementsextfuncs[] =
280 {
281         {"glDrawRangeElementsEXT", (void **) &qglDrawRangeElementsEXT},
282         {NULL, NULL}
283 };
284
285 static gl_extensionfunctionlist_t multitexturefuncs[] =
286 {
287         {"glMultiTexCoord2fARB", (void **) &qglMultiTexCoord2f},
288         {"glActiveTextureARB", (void **) &qglActiveTexture},
289         {"glClientActiveTextureARB", (void **) &qglClientActiveTexture},
290         {NULL, NULL}
291 };
292
293 static gl_extensionfunctionlist_t compiledvertexarrayfuncs[] =
294 {
295         {"glLockArraysEXT", (void **) &qglLockArraysEXT},
296         {"glUnlockArraysEXT", (void **) &qglUnlockArraysEXT},
297         {NULL, NULL}
298 };
299
300 void VID_CheckExtensions(void)
301 {
302         gl_combine_extension = false;
303         gl_supportslockarrays = false;
304         gl_textureunits = 1;
305
306         if (!GL_CheckExtension("OpenGL 1.1.0", opengl110funcs, NULL, false))
307                 Sys_Error("OpenGL 1.1.0 functions not found\n");
308
309         Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
310         Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
311         Con_Printf ("GL_VERSION: %s\n", gl_version);
312         Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
313         Con_Printf ("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
314
315         Con_Printf("Checking OpenGL extensions...\n");
316
317         if (!GL_CheckExtension("glDrawRangeElements", drawrangeelementsfuncs, "-nodrawrangeelements", true))
318                 GL_CheckExtension("GL_EXT_draw_range_elements", drawrangeelementsextfuncs, "-nodrawrangeelements", false);
319
320         if (GL_CheckExtension("GL_ARB_multitexture", multitexturefuncs, "-nomtex", false))
321         {
322                 qglGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
323                 if (gl_textureunits > 1)
324                         gl_combine_extension = GL_CheckExtension("GL_ARB_texture_env_combine", NULL, "-nocombine", false) || GL_CheckExtension("GL_EXT_texture_env_combine", NULL, "-nocombine", false);
325                 else
326                 {
327                         Con_Printf("GL_ARB_multitexture with less than 2 units? - BROKEN DRIVER!\n");
328                         gl_textureunits = 1; // for sanity sake, make sure it's not 0
329                 }
330         }
331
332         gl_supportslockarrays = GL_CheckExtension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva", false);
333
334         // we don't care if it's an extension or not, they are identical functions, so keep it simple in the rendering code
335         if (qglDrawRangeElements == NULL)
336                 qglDrawRangeElements = qglDrawRangeElementsEXT;
337 }
338
339 void Force_CenterView_f (void)
340 {
341         cl.viewangles[PITCH] = 0;
342 }
343
344 void IN_PreMove(void)
345 {
346 }
347
348 void CL_AdjustAngles(void);
349 void IN_PostMove(void)
350 {
351         // clamp after the move as well to prevent messed up rendering angles
352         CL_AdjustAngles();
353 }
354
355 void IN_Mouse(usercmd_t *cmd, float mx, float my)
356 {
357         int mouselook = (in_mlook.state & 1) || freelook.integer;
358         float mouse_x, mouse_y;
359         static float old_mouse_x = 0, old_mouse_y = 0;
360
361         if (m_filter.integer)
362         {
363                 mouse_x = (mx + old_mouse_x) * 0.5;
364                 mouse_y = (my + old_mouse_y) * 0.5;
365         }
366         else
367         {
368                 mouse_x = mx;
369                 mouse_y = my;
370         }
371
372         old_mouse_x = mx;
373         old_mouse_y = my;
374
375         // LordHavoc: viewzoom affects mouse sensitivity for sniping
376         mouse_x *= sensitivity.value * cl.viewzoom;
377         mouse_y *= sensitivity.value * cl.viewzoom;
378
379         // Add mouse X/Y movement to cmd
380         if ((in_strafe.state & 1) || (lookstrafe.integer && mouselook))
381                 cmd->sidemove += m_side.value * mouse_x;
382         else
383                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
384
385         if (mouselook)
386                 V_StopPitchDrift();
387
388         if (mouselook && !(in_strafe.state & 1))
389                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
390         else
391         {
392                 if ((in_strafe.state & 1) && noclip_anglehack)
393                         cmd->upmove -= m_forward.value * mouse_y;
394                 else
395                         cmd->forwardmove -= m_forward.value * mouse_y;
396         }
397 }
398
399 void VID_Shared_Init(void)
400 {
401         int i;
402
403         Cvar_RegisterVariable(&vid_fullscreen);
404         Cvar_RegisterVariable(&vid_width);
405         Cvar_RegisterVariable(&vid_height);
406         Cvar_RegisterVariable(&vid_bitsperpixel);
407         Cvar_RegisterVariable(&vid_stencil);
408         Cvar_RegisterVariable(&vid_mouse);
409         Cvar_RegisterVariable(&gl_combine);
410         Cvar_RegisterVariable(&in_pitch_min);
411         Cvar_RegisterVariable(&in_pitch_max);
412         Cvar_RegisterVariable(&m_filter);
413         Cmd_AddCommand("force_centerview", Force_CenterView_f);
414         Cmd_AddCommand("vid_restart", VID_Restart_f);
415
416 // interpret command-line parameters
417         if ((i = COM_CheckParm("-window")) != 0)
418                 Cvar_SetValueQuick(&vid_fullscreen, false);
419         if ((i = COM_CheckParm("-fullscreen")) != 0)
420                 Cvar_SetValueQuick(&vid_fullscreen, true);
421         if ((i = COM_CheckParm("-width")) != 0)
422                 Cvar_SetQuick(&vid_width, com_argv[i+1]);
423         if ((i = COM_CheckParm("-height")) != 0)
424                 Cvar_SetQuick(&vid_height, com_argv[i+1]);
425         if ((i = COM_CheckParm("-bpp")) != 0)
426                 Cvar_SetQuick(&vid_bitsperpixel, com_argv[i+1]);
427         if ((i = COM_CheckParm("-nostencil")) != 0)
428                 Cvar_SetValueQuick(&vid_stencil, 0);
429         if ((i = COM_CheckParm("-stencil")) != 0)
430                 Cvar_SetValueQuick(&vid_stencil, 1);
431 }
432
433 int current_vid_fullscreen;
434 int current_vid_width;
435 int current_vid_height;
436 int current_vid_bitsperpixel;
437 int current_vid_stencil;
438 extern int VID_InitMode (int fullscreen, int width, int height, int bpp, int stencil);
439 int VID_Mode(int fullscreen, int width, int height, int bpp, int stencil)
440 {
441         Con_Printf("Video: %s %dx%dx%d %s\n", fullscreen ? "fullscreen" : "window", width, height, bpp, stencil ? "with stencil" : "without stencil");
442         if (VID_InitMode(fullscreen, width, height, bpp, stencil))
443         {
444                 current_vid_fullscreen = fullscreen;
445                 current_vid_width = width;
446                 current_vid_height = height;
447                 current_vid_bitsperpixel = bpp;
448                 current_vid_stencil = stencil;
449                 Cvar_SetValueQuick(&vid_fullscreen, fullscreen);
450                 Cvar_SetValueQuick(&vid_width, width);
451                 Cvar_SetValueQuick(&vid_height, height);
452                 Cvar_SetValueQuick(&vid_bitsperpixel, bpp);
453                 Cvar_SetValueQuick(&vid_stencil, stencil);
454                 return true;
455         }
456         else
457                 return false;
458 }
459
460 static void VID_OpenSystems(void)
461 {
462         R_Modules_Start();
463         S_Open();
464         CDAudio_Open();
465 }
466
467 static void VID_CloseSystems(void)
468 {
469         CDAudio_Close();
470         S_Close();
471         R_Modules_Shutdown();
472 }
473
474 void VID_Restart_f(void)
475 {
476         Con_Printf("VID_Restart: changing from %s %dx%dx%dbpp %s, to %s %dx%dx%dbpp %s.\n",
477                 current_vid_fullscreen ? "fullscreen" : "window", current_vid_width, current_vid_height, current_vid_bitsperpixel, current_vid_stencil ? "with stencil" : "without stencil",
478                 vid_fullscreen.integer ? "fullscreen" : "window", vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, vid_stencil.integer ? "with stencil" : "without stencil");
479         VID_Close();
480         if (!VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, vid_stencil.integer))
481         {
482                 Con_Printf("Video mode change failed\n");
483                 if (!VID_Mode(current_vid_fullscreen, current_vid_width, current_vid_height, current_vid_bitsperpixel, current_vid_stencil))
484                         Sys_Error("Unable to restore to last working video mode\n");
485         }
486         VID_OpenSystems();
487 }
488
489 void VID_Open(void)
490 {
491         Con_Printf("Starting video system\n");
492         if (!VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, vid_stencil.integer))
493         {
494                 Con_Printf("Desired video mode fail, trying fallbacks...\n");
495                 if (!vid_stencil.integer || !VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer, false))
496                 {
497                         if (vid_fullscreen.integer)
498                         {
499                                 if (!VID_Mode(true, 640, 480, 16, false))
500                                         if (!VID_Mode(false, 640, 480, 16, false))
501                                                 Sys_Error("Video modes failed\n");
502                         }
503                         else
504                                 Sys_Error("Windowed video failed\n");
505                 }
506         }
507         VID_OpenSystems();
508 }
509
510 void VID_Close(void)
511 {
512         VID_CloseSystems();
513         VID_Shutdown();
514 }