cleaned up mouse input system
[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 cvar_t m_filter = {CVAR_SAVE, "m_filter","0"};
24
25 // GL_ARB_multitexture
26 void (GLAPIENTRY *qglMultiTexCoord2f) (GLenum, GLfloat, GLfloat);
27 void (GLAPIENTRY *qglActiveTexture) (GLenum);
28 void (GLAPIENTRY *qglClientActiveTexture) (GLenum);
29
30 // GL_EXT_compiled_vertex_array
31 void (GLAPIENTRY *qglLockArraysEXT) (GLint first, GLint count);
32 void (GLAPIENTRY *qglUnlockArraysEXT) (void);
33
34 typedef struct
35 {
36         char *name;
37         void **funcvariable;
38 }
39 gl_extensionfunctionlist_t;
40
41 typedef struct
42 {
43         char *name;
44         gl_extensionfunctionlist_t *funcs;
45         int *enablevariable;
46         char *disableparm;
47 }
48 gl_extensioninfo_t;
49
50 static gl_extensionfunctionlist_t multitexturefuncs[] =
51 {
52         {"glMultiTexCoord2fARB", (void **) &qglMultiTexCoord2f},
53         {"glActiveTextureARB", (void **) &qglActiveTexture},
54         {"glClientActiveTextureARB", (void **) &qglClientActiveTexture},
55         {NULL, NULL}
56 };
57
58 static gl_extensionfunctionlist_t compiledvertexarrayfuncs[] =
59 {
60         {"glLockArraysEXT", (void **) &qglLockArraysEXT},
61         {"glUnlockArraysEXT", (void **) &qglUnlockArraysEXT},
62         {NULL, NULL}
63 };
64
65 #ifndef WIN32
66 #include <dlfcn.h>
67 #endif
68
69 #ifndef WIN32
70 static void *prjobj = NULL;
71 #endif
72
73 static void gl_getfuncs_begin(void)
74 {
75 #ifndef WIN32
76         if (prjobj)
77                 dlclose(prjobj);
78
79         prjobj = dlopen(NULL, RTLD_LAZY);
80         if (prjobj == NULL)
81         {
82                 Con_Printf("Unable to open symbol list for main program.\n");
83                 return;
84         }
85 #endif
86 }
87
88 static void gl_getfuncs_end(void)
89 {
90 #ifndef WIN32
91         if (prjobj)
92         {
93                 dlclose(prjobj);
94                 prjobj = NULL;
95         }
96 #endif
97 }
98
99 static void *gl_getfuncaddress(char *name)
100 {
101 #ifdef WIN32
102         return (void *) wglGetProcAddress(name);
103 #else
104         return (void *) dlsym(prjobj, name);
105 #endif
106 }
107
108 static int gl_checkextension(char *name, gl_extensionfunctionlist_t *funcs, char *disableparm)
109 {
110         gl_extensionfunctionlist_t *func;
111
112         Con_Printf("checking for %s...  ", name);
113
114         for (func = funcs;func && func->name;func++)
115                 *func->funcvariable = NULL;
116
117         if (disableparm && COM_CheckParm(disableparm))
118         {
119                 Con_Printf("disabled by commandline\n");
120                 return false;
121         }
122
123         if (strstr(gl_extensions, name))
124         {
125                 for (func = funcs;func && func->name != NULL;func++)
126                 {
127                         if (!(*func->funcvariable = (void *) gl_getfuncaddress(func->name)))
128                         {
129                                 Con_Printf("missing function \"%s\"!\n", func->name);
130                                 return false;
131                         }
132                 }
133                 Con_Printf("enabled\n");
134                 return true;
135         }
136         else
137         {
138                 Con_Printf("not detected\n");
139                 return false;
140         }
141 }
142
143 void VID_CheckExtensions(void)
144 {
145         Con_Printf("Checking OpenGL extensions...\n");
146
147         gl_getfuncs_begin();
148
149         gl_combine_extension = false;
150         gl_supportslockarrays = false;
151         gl_textureunits = 1;
152
153         if (gl_checkextension("GL_ARB_multitexture", multitexturefuncs, "-nomtex"))
154         {
155                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
156                 if (gl_textureunits > 1)
157                         gl_combine_extension = gl_checkextension("GL_ARB_texture_env_combine", NULL, "-nocombine") || gl_checkextension("GL_EXT_texture_env_combine", NULL, "-nocombine");
158                 else
159                         gl_textureunits = 1; // for sanity sake, make sure it's not 0
160         }
161
162         gl_supportslockarrays = gl_checkextension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva");
163
164         gl_getfuncs_end();
165 }
166
167 void Force_CenterView_f (void)
168 {
169         cl.viewangles[PITCH] = 0;
170 }
171
172 void IN_PreMove(void)
173 {
174 }
175
176 void IN_PostMove(void)
177 {
178 }
179
180 void IN_Mouse(usercmd_t *cmd, float mx, float my)
181 {
182         int mouselook = (in_mlook.state & 1) || freelook.integer;
183         float mouse_x, mouse_y;
184         static float old_mouse_x = 0, old_mouse_y = 0;
185
186         if (m_filter.integer)
187         {
188                 mouse_x = (mx + old_mouse_x) * 0.5;
189                 mouse_y = (my + old_mouse_y) * 0.5;
190         }
191         else
192         {
193                 mouse_x = mx;
194                 mouse_y = my;
195         }
196
197         old_mouse_x = mx;
198         old_mouse_y = my;
199
200         // LordHavoc: viewzoom affects mouse sensitivity for sniping
201         mouse_x *= sensitivity.value * cl.viewzoom;
202         mouse_y *= sensitivity.value * cl.viewzoom;
203
204         // Add mouse X/Y movement to cmd
205         if ( (in_strafe.state & 1) || (lookstrafe.integer && mouselook))
206                 cmd->sidemove += m_side.value * mouse_x;
207         else
208                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
209
210         if (mouselook)
211                 V_StopPitchDrift();
212
213         if (mouselook && !(in_strafe.state & 1))
214                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
215         else
216         {
217                 if ((in_strafe.state & 1) && noclip_anglehack)
218                         cmd->upmove -= m_forward.value * mouse_y;
219                 else
220                         cmd->forwardmove -= m_forward.value * mouse_y;
221         }
222 }
223
224 void VID_InitCvars(void)
225 {
226         Cvar_RegisterVariable(&vid_mode);
227         Cvar_RegisterVariable(&vid_mouse);
228         Cvar_RegisterVariable(&vid_fullscreen);
229         Cvar_RegisterVariable(&gl_combine);
230         Cvar_RegisterVariable(&in_pitch_min);
231         Cvar_RegisterVariable(&in_pitch_max);
232         Cvar_RegisterVariable(&m_filter);
233         Cmd_AddCommand("force_centerview", Force_CenterView_f);
234 }