From 284dd7a5e650c0edea896734f0179a3fde95defc Mon Sep 17 00:00:00 2001
From: lordhavoc <lordhavoc@d7cf8633-e32d-0410-b094-e92efae38249>
Date: Thu, 28 Feb 2002 01:38:47 +0000
Subject: [PATCH] cleaned up mouse input system fixed a bug with svc_setangle
 on pitch (it was immediately getting clamped in some cases, which made it
 quite useless)

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@1589 d7cf8633-e32d-0410-b094-e92efae38249
---
 cl_input.c   | 19 ++++++-----------
 cl_main.c    |  4 ++++
 in_svgalib.c | 54 +++++-------------------------------------------
 in_win.c     | 58 +++-------------------------------------------------
 input.h      |  8 ++++++++
 vid.h        |  3 ---
 vid_glx.c    | 51 ++++-----------------------------------------
 vid_shared.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 85 insertions(+), 167 deletions(-)

diff --git a/cl_input.c b/cl_input.c
index 030ffa29..661b05a4 100644
--- a/cl_input.c
+++ b/cl_input.c
@@ -261,7 +261,6 @@ void CL_AdjustAngles (void)
 	{
 		cl.viewangles[YAW] -= speed*cl_yawspeed.value*CL_KeyState (&in_right);
 		cl.viewangles[YAW] += speed*cl_yawspeed.value*CL_KeyState (&in_left);
-		cl.viewangles[YAW] = ANGLEMOD(cl.viewangles[YAW]);
 	}
 	if (in_klook.state & 1)
 	{
@@ -279,18 +278,12 @@ void CL_AdjustAngles (void)
 	if (up || down)
 		V_StopPitchDrift ();
 
-	// LordHavoc: changed from 80 to 90 (straight up)
-	if (cl.viewangles[PITCH] > 90)
-		cl.viewangles[PITCH] = 90;
-	// LordHavoc: changed from -70 to -90 (straight down)
-	if (cl.viewangles[PITCH] < -90)
-		cl.viewangles[PITCH] = -90;
-
-	if (cl.viewangles[ROLL] > 50)
-		cl.viewangles[ROLL] = 50;
-	if (cl.viewangles[ROLL] < -50)
-		cl.viewangles[ROLL] = -50;
-		
+	cl.viewangles[YAW] = ANGLEMOD(cl.viewangles[YAW]);
+	cl.viewangles[PITCH] = ANGLEMOD(cl.viewangles[PITCH]);
+	cl.viewangles[ROLL] = ANGLEMOD(cl.viewangles[ROLL]);
+
+	cl.viewangles[PITCH] = bound (in_pitch_min.value, cl.viewangles[PITCH], in_pitch_max.value);
+	cl.viewangles[ROLL] = bound(-50, cl.viewangles[ROLL], 50);
 }
 
 /*
diff --git a/cl_main.c b/cl_main.c
index 13037820..30b27560 100644
--- a/cl_main.c
+++ b/cl_main.c
@@ -831,9 +831,13 @@ void CL_SendCmd (void)
 	// get basic movement from keyboard
 		CL_BaseMove (&cmd);
 
+		IN_PreMove(); // OS independent code
+
 	// allow mice or other external controllers to add to the move
 		IN_Move (&cmd);
 
+		IN_PostMove(); // OS independent code
+
 	// send the unreliable message
 		CL_SendMove (&cmd);
 	}
diff --git a/in_svgalib.c b/in_svgalib.c
index bf31dbe1..fe0719db 100644
--- a/in_svgalib.c
+++ b/in_svgalib.c
@@ -63,8 +63,6 @@ static int	mx, my, uimx, uimy;
 static void IN_init_kb(void);
 static void IN_init_mouse(void);
 
-cvar_t m_filter = {CVAR_SAVE, "m_filter","0"};
-
 static void keyhandler(int scancode, int state)
 {
 	int sc;
@@ -234,8 +232,6 @@ static void IN_init_mouse(void)
 	char *mousedev;
 	int mouserate = MOUSE_DEFAULTSAMPLERATE;
 
-	Cvar_RegisterVariable (&m_filter);
-
 	mouse_buttons = 3;
 
 	mtype = vga_getmousetype();
@@ -322,7 +318,6 @@ void IN_Commands(void)
 
 void IN_Move(usercmd_t *cmd)
 {
-	int mouselook = (in_mlook.state & 1) || freelook.integer;
 	if (!UseMouse)
 		return;
 
@@ -331,52 +326,13 @@ void IN_Move(usercmd_t *cmd)
 		;
 
 	if (key_dest != key_game)
-	{
 		ui_mouseupdaterelative(uimx, uimy);
-		uimx = uimy = 0;
-		return;
-	}
-	uimx = uimy = 0;
-
-	if (m_filter.integer)
-	{
-		mouse_x = (mx + old_mouse_x) * 0.5;
-		mouse_y = (my + old_mouse_y) * 0.5;
-	}
 	else
-	{
-		mouse_x = mx;
-		mouse_y = my;
-	}
-	old_mouse_x = mx;
-	old_mouse_y = my;
-	/* Clear for next update */
-	mx = my = 0;
-
-	// LordHavoc: viewzoom affects mouse sensitivity for sniping
-	mouse_x *= sensitivity.value * cl.viewzoom;
-	mouse_y *= sensitivity.value * cl.viewzoom;
-
-	/* Add mouse X/Y movement to cmd */
-	if ( (in_strafe.state & 1) || (lookstrafe.integer && mouselook))
-		cmd->sidemove += m_side.value * mouse_x;
-	else
-		cl.viewangles[YAW] -= m_yaw.value * mouse_x;
-
-	if (mouselook)
-		V_StopPitchDrift();
-
-	// LordHavoc: changed limits on pitch from -70 to 80, to -90 to 90
-	if (mouselook && !(in_strafe.state & 1))
-		cl.viewangles[PITCH] += m_pitch.value * mouse_y;
-	else
-	{
-		if ((in_strafe.state & 1) && noclip_anglehack)
-			cmd->upmove -= m_forward.value * mouse_y;
-		else
-			cmd->forwardmove -= m_forward.value * mouse_y;
-	}
-	cl.viewangles[PITCH] = bound (in_pitch_min.value, cl.viewangles[PITCH], in_pitch_max.value);
+		IN_Mouse(cmd, mx, my);
+	mx = 0;
+	my = 0;
+	uimx = 0;
+	uimy = 0;
 }
 
 void IN_HandlePause (qboolean pause)
diff --git a/in_win.c b/in_win.c
index 94c34373..35e01381 100644
--- a/in_win.c
+++ b/in_win.c
@@ -33,8 +33,6 @@ HRESULT (WINAPI *pDirectInputCreate)(HINSTANCE hinst, DWORD dwVersion,
 	LPDIRECTINPUT * lplpDirectInput, LPUNKNOWN punkOuter);
 
 // mouse variables
-cvar_t	m_filter = {CVAR_SAVE, "m_filter","0"};
-
 int			mouse_buttons;
 int			mouse_oldbuttonstate;
 POINT		current_pos;
@@ -438,9 +436,6 @@ IN_Init
 */
 void IN_Init (void)
 {
-	// mouse variables
-	Cvar_RegisterVariable (&m_filter);
-
 	// joystick variables
 	Cvar_RegisterVariable (&in_joystick);
 	Cvar_RegisterVariable (&joy_name);
@@ -633,52 +628,11 @@ void IN_MouseMove (usercmd_t *cmd)
 		my_accum = 0;
 	}
 
-//if (mx ||  my)
-//	Con_DPrintf("mx=%d, my=%d\n", mx, my);
+	IN_Mouse(cmd, mx, my);
 
-	if (m_filter.integer)
-	{
-		mouse_x = (mx + old_mouse_x) * 0.5;
-		mouse_y = (my + old_mouse_y) * 0.5;
-	}
-	else
-	{
-		mouse_x = mx;
-		mouse_y = my;
-	}
-
-	old_mouse_x = mx;
-	old_mouse_y = my;
-
-	// LordHavoc: viewzoom affects mouse sensitivity for sniping
-	mouse_x *= sensitivity.value * cl.viewzoom;
-	mouse_y *= sensitivity.value * cl.viewzoom;
-
-// add mouse X/Y movement to cmd
-	if ( (in_strafe.state & 1) || (lookstrafe.integer && mouselook))
-		cmd->sidemove += m_side.value * mouse_x;
-	else
-		cl.viewangles[YAW] -= m_yaw.value * mouse_x;
-
-	if (mouselook)
-		V_StopPitchDrift ();
-	
-	// LordHavoc: changed limits on pitch from -70 to 80, to -90 to 90
-	if (mouselook && !(in_strafe.state & 1))
-		cl.viewangles[PITCH] += m_pitch.value * mouse_y;
-	else
-	{
-		if ((in_strafe.state & 1) && noclip_anglehack)
-			cmd->upmove -= m_forward.value * mouse_y;
-		else
-			cmd->forwardmove -= m_forward.value * mouse_y;
-	}
-
-// if the mouse has moved, force it to the center, so there's room to move
-	if (mx || my)
-	{
+	// if the mouse has moved, force it to the center, so there's room to move
+	if (!dinput && (mx || my))
 		SetCursorPos (window_center_x, window_center_y);
-	}
 }
 
 
@@ -1182,10 +1136,4 @@ void IN_JoyMove (usercmd_t *cmd)
 			break;
 		}
 	}
-
-	// bounds check pitch
-	if (cl.viewangles[PITCH] > 80.0)
-		cl.viewangles[PITCH] = 80.0;
-	if (cl.viewangles[PITCH] < -70.0)
-		cl.viewangles[PITCH] = -70.0;
 }
diff --git a/input.h b/input.h
index c3daa170..a44a5eec 100644
--- a/input.h
+++ b/input.h
@@ -19,6 +19,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 // input.h -- external (non-keyboard) input devices
 
+extern cvar_t in_pitch_min;
+extern cvar_t in_pitch_max;
+
 void IN_Init (void);
 
 void IN_Shutdown (void);
@@ -29,6 +32,11 @@ void IN_Commands (void);
 void IN_Move (usercmd_t *cmd);
 // add additional movement on top of the keyboard move cmd
 
+void IN_PreMove(void);
+void IN_PostMove(void);
+
+void IN_Mouse(usercmd_t *cmd, float mx, float my);
+
 void IN_ClearStates (void);
 // restores all button and position states to defaults
 
diff --git a/vid.h b/vid.h
index 73395282..2df5d6fc 100644
--- a/vid.h
+++ b/vid.h
@@ -40,9 +40,6 @@ extern cvar_t vid_mode;
 extern cvar_t vid_mouse;
 extern cvar_t vid_fullscreen;
 
-extern cvar_t in_pitch_min;
-extern cvar_t in_pitch_max;
-
 void VID_InitCvars(void);
 
 void GL_Init (void);
diff --git a/vid_glx.c b/vid_glx.c
index 5fa9867b..c4282cf3 100644
--- a/vid_glx.c
+++ b/vid_glx.c
@@ -60,7 +60,6 @@ static int p_mouse_x, p_mouse_y;
 
 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
-cvar_t m_filter = {0, "m_filter", "0"};
 
 qboolean vidmode_ext = false;
 
@@ -635,7 +634,6 @@ void VID_Init(void)
 
 	Cvar_RegisterVariable (&vid_dga);
 	Cvar_RegisterVariable (&vid_dga_mouseaccel);
-	Cvar_RegisterVariable (&m_filter);
 
 // interpret command-line params
 
@@ -830,53 +828,12 @@ void IN_Commands (void)
 {
 }
 
-/*
-===========
-IN_Move
-===========
-*/
-void IN_MouseMove (usercmd_t *cmd)
-{
-	if (!mouse_avail)
-		return;
-
-	if (m_filter.integer)
-	{
-		mouse_x = (mouse_x + old_mouse_x) * 0.5;
-		mouse_y = (mouse_y + old_mouse_y) * 0.5;
-
-		old_mouse_x = mouse_x;
-		old_mouse_y = mouse_y;
-	}
-
-	// LordHavoc: viewzoom affects mouse sensitivity for sniping
-	mouse_x *= sensitivity.value * cl.viewzoom;
-	mouse_y *= sensitivity.value * cl.viewzoom;
-
-	if (in_strafe.state & 1)
-		cmd->sidemove += m_side.value * mouse_x;
-	else
-		cl.viewangles[YAW] -= m_yaw.value * mouse_x;
-
-	//if (freelook)
-		V_StopPitchDrift ();
-
-	if (/*freelook && */!(in_strafe.state & 1))
-		cl.viewangles[PITCH] += m_pitch.value * mouse_y;
-	else
-	{
-		if ((in_strafe.state & 1) && noclip_anglehack)
-			cmd->upmove -= m_forward.value * mouse_y;
-		else
-			cmd->forwardmove -= m_forward.value * mouse_y;
-	}
-	mouse_x = mouse_y = 0.0;
-}
-
 void IN_Move (usercmd_t *cmd)
 {
-	IN_MouseMove(cmd);
-	cl.viewangles[PITCH] = bound (in_pitch_min.value, cl.viewangles[PITCH], in_pitch_max.value);
+	if (mouseavail)
+		In_Mouse(cmd, mouse_x, mouse_y);
+	mouse_x = 0;
+	mouse_y = 0;
 }
 
 
diff --git a/vid_shared.c b/vid_shared.c
index 79451a54..6730f55f 100644
--- a/vid_shared.c
+++ b/vid_shared.c
@@ -20,6 +20,8 @@ cvar_t gl_combine = {0, "gl_combine", "1"};
 cvar_t in_pitch_min = {0, "in_pitch_min", "-90"};
 cvar_t in_pitch_max = {0, "in_pitch_max", "90"};
 
+cvar_t m_filter = {CVAR_SAVE, "m_filter","0"};
+
 // GL_ARB_multitexture
 void (GLAPIENTRY *qglMultiTexCoord2f) (GLenum, GLfloat, GLfloat);
 void (GLAPIENTRY *qglActiveTexture) (GLenum);
@@ -167,6 +169,58 @@ void Force_CenterView_f (void)
 	cl.viewangles[PITCH] = 0;
 }
 
+void IN_PreMove(void)
+{
+}
+
+void IN_PostMove(void)
+{
+}
+
+void IN_Mouse(usercmd_t *cmd, float mx, float my)
+{
+	int mouselook = (in_mlook.state & 1) || freelook.integer;
+	float mouse_x, mouse_y;
+	static float old_mouse_x = 0, old_mouse_y = 0;
+
+	if (m_filter.integer)
+	{
+		mouse_x = (mx + old_mouse_x) * 0.5;
+		mouse_y = (my + old_mouse_y) * 0.5;
+	}
+	else
+	{
+		mouse_x = mx;
+		mouse_y = my;
+	}
+
+	old_mouse_x = mx;
+	old_mouse_y = my;
+
+	// LordHavoc: viewzoom affects mouse sensitivity for sniping
+	mouse_x *= sensitivity.value * cl.viewzoom;
+	mouse_y *= sensitivity.value * cl.viewzoom;
+
+	// Add mouse X/Y movement to cmd
+	if ( (in_strafe.state & 1) || (lookstrafe.integer && mouselook))
+		cmd->sidemove += m_side.value * mouse_x;
+	else
+		cl.viewangles[YAW] -= m_yaw.value * mouse_x;
+
+	if (mouselook)
+		V_StopPitchDrift();
+
+	if (mouselook && !(in_strafe.state & 1))
+		cl.viewangles[PITCH] += m_pitch.value * mouse_y;
+	else
+	{
+		if ((in_strafe.state & 1) && noclip_anglehack)
+			cmd->upmove -= m_forward.value * mouse_y;
+		else
+			cmd->forwardmove -= m_forward.value * mouse_y;
+	}
+}
+
 void VID_InitCvars(void)
 {
 	Cvar_RegisterVariable(&vid_mode);
@@ -175,5 +229,6 @@ void VID_InitCvars(void)
 	Cvar_RegisterVariable(&gl_combine);
 	Cvar_RegisterVariable(&in_pitch_min);
 	Cvar_RegisterVariable(&in_pitch_max);
+	Cvar_RegisterVariable(&m_filter);
 	Cmd_AddCommand("force_centerview", Force_CenterView_f);
 }
-- 
2.39.5