renamed byte to qbyte throughout engine to eliminate a mingw conflict
[divverent/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
15 cvar_t vid_mode = {0, "vid_mode", "0"};
16 cvar_t vid_mouse = {CVAR_SAVE, "vid_mouse", "1"};
17 cvar_t vid_fullscreen = {0, "vid_fullscreen", "1"};
18 cvar_t gl_combine = {0, "gl_combine", "1"};
19
20 cvar_t in_pitch_min = {0, "in_pitch_min", "-90"};
21 cvar_t in_pitch_max = {0, "in_pitch_max", "90"};
22
23 // GL_ARB_multitexture
24 void (GLAPIENTRY *qglMultiTexCoord2f) (GLenum, GLfloat, GLfloat);
25 void (GLAPIENTRY *qglActiveTexture) (GLenum);
26 void (GLAPIENTRY *qglClientActiveTexture) (GLenum);
27
28 // GL_EXT_compiled_vertex_array
29 void (GLAPIENTRY *qglLockArraysEXT) (GLint first, GLint count);
30 void (GLAPIENTRY *qglUnlockArraysEXT) (void);
31
32 typedef struct
33 {
34         char *name;
35         void **funcvariable;
36 }
37 gl_extensionfunctionlist_t;
38
39 typedef struct
40 {
41         char *name;
42         gl_extensionfunctionlist_t *funcs;
43         int *enablevariable;
44         char *disableparm;
45 }
46 gl_extensioninfo_t;
47
48 static gl_extensionfunctionlist_t multitexturefuncs[] =
49 {
50         {"glMultiTexCoord2fARB", (void **) &qglMultiTexCoord2f},
51         {"glActiveTextureARB", (void **) &qglActiveTexture},
52         {"glClientActiveTextureARB", (void **) &qglClientActiveTexture},
53         {NULL, NULL}
54 };
55
56 static gl_extensionfunctionlist_t compiledvertexarrayfuncs[] =
57 {
58         {"glLockArraysEXT", (void **) &qglLockArraysEXT},
59         {"glUnlockArraysEXT", (void **) &qglUnlockArraysEXT},
60         {NULL, NULL}
61 };
62
63 #ifndef WIN32
64 #include <dlfcn.h>
65 #endif
66
67 #ifndef WIN32
68 static void *prjobj = NULL;
69 #endif
70
71 static void gl_getfuncs_begin(void)
72 {
73 #ifndef WIN32
74         if (prjobj)
75                 dlclose(prjobj);
76
77         prjobj = dlopen(NULL, RTLD_LAZY);
78         if (prjobj == NULL)
79         {
80                 Con_Printf("Unable to open symbol list for main program.\n");
81                 return;
82         }
83 #endif
84 }
85
86 static void gl_getfuncs_end(void)
87 {
88 #ifndef WIN32
89         if (prjobj)
90         {
91                 dlclose(prjobj);
92                 prjobj = NULL;
93         }
94 #endif
95 }
96
97 static void *gl_getfuncaddress(char *name)
98 {
99 #ifdef WIN32
100         return (void *) wglGetProcAddress(name);
101 #else
102         return (void *) dlsym(prjobj, name);
103 #endif
104 }
105
106 static int gl_checkextension(char *name, gl_extensionfunctionlist_t *funcs, char *disableparm)
107 {
108         gl_extensionfunctionlist_t *func;
109
110         Con_Printf("checking for %s...  ", name);
111
112         for (func = funcs;func && func->name;func++)
113                 *func->funcvariable = NULL;
114
115         if (disableparm && COM_CheckParm(disableparm))
116         {
117                 Con_Printf("disabled by commandline\n");
118                 return false;
119         }
120
121         if (strstr(gl_extensions, name))
122         {
123                 for (func = funcs;func && func->name != NULL;func++)
124                 {
125                         if (!(*func->funcvariable = (void *) gl_getfuncaddress(func->name)))
126                         {
127                                 Con_Printf("missing function \"%s\"!\n", func->name);
128                                 return false;
129                         }
130                 }
131                 Con_Printf("enabled\n");
132                 return true;
133         }
134         else
135         {
136                 Con_Printf("not detected\n");
137                 return false;
138         }
139 }
140
141 void VID_CheckExtensions(void)
142 {
143         Con_Printf("Checking OpenGL extensions...\n");
144
145         gl_getfuncs_begin();
146
147         gl_combine_extension = false;
148         gl_supportslockarrays = false;
149         gl_textureunits = 1;
150
151         if (gl_checkextension("GL_ARB_multitexture", multitexturefuncs, "-nomtex"))
152         {
153                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
154                 if (gl_textureunits > 1)
155                         gl_combine_extension = gl_checkextension("GL_ARB_texture_env_combine", NULL, "-nocombine") || gl_checkextension("GL_EXT_texture_env_combine", NULL, "-nocombine");
156                 else
157                         gl_textureunits = 1; // for sanity sake, make sure it's not 0
158         }
159
160         gl_supportslockarrays = gl_checkextension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva");
161
162         gl_getfuncs_end();
163 }
164
165 void Force_CenterView_f (void)
166 {
167         cl.viewangles[PITCH] = 0;
168 }
169
170 void VID_InitCvars(void)
171 {
172         Cvar_RegisterVariable(&vid_mode);
173         Cvar_RegisterVariable(&vid_mouse);
174         Cvar_RegisterVariable(&vid_fullscreen);
175         Cvar_RegisterVariable(&gl_combine);
176         Cvar_RegisterVariable(&in_pitch_min);
177         Cvar_RegisterVariable(&in_pitch_max);
178         Cmd_AddCommand("force_centerview", Force_CenterView_f);
179 }