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