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