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