Clamping the mouse pointer input in the Message loop

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Clamping the mouse pointer input in the Message loop

Post by NaturalDemon » November 6th, 2012, 11:59 am

in certain casses ... the mouse data is bigger than the window .... and also when mouse button is down and dragging outside the window.

spend about 2 days .... trying to find the cause and restrict it to the last value inside the window.

it seams i can't prevent a int of 800 and bigger from the mouse input slipping in.


even my Enemy subclass ... witch is threaded and consist of a simple pixel following the mouse ...
is clampled.

i tried both methods ....
min & max.

aswel ...

Code: Select all

// adjust X to fit in Window
int correctX(int xParam)
{
	char correctX[64];
	// X is outside on the left side of the window, increase
	if(xParam < 0)
	{
		do
		{
			xParam++;
			sprintf_s(correctX, "correctX increase:   GET_X_PARAM:   %d\n", (char)xParam);
		}
		while(xParam < 0);	
		
		fputs (correctX,pFile);
		return xParam;
	}
	// X is outside on the left side of the window, dencrease
	else if(xParam > (windowSizeX -1))
	{
		do
		{
			xParam--;
			sprintf_s(correctX, "correctX decrease:   GET_X_PARAM:   %d\n", (char)xParam);
		}
		while(xParam > (windowSizeX -1));
		
		fputs (correctX,pFile);
		return xParam;
	}
	else
	{
		sprintf_s(correctX, "correctX:   GET_X_PARAM:   %d\n", (char)xParam);
		fputs (correctX,pFile);
		return xParam;
	}
	
}
// adjust Y to fit in Window
int correctY(int yParam)
{
	char correctY [64];
	// Y is outside and above the top side of the window, increase
	if(yParam > (windowSizeY -1))
	{
		do
		{
			yParam--;
			sprintf_s(correctY, "correctY decrease:   GET_Y_PARAM: %d\n", (char)yParam);
		}
		while(yParam > (windowSizeY -1));
		
		fputs (correctY,pFile);
		return yParam;
	}
	else if(yParam < 0)
	{
		do
		{
			yParam++;
			sprintf_s(correctY, "correctY increase:   GET_Y_PARAM: %d\n", (char)yParam);
		}
		while(yParam < 0);
		
		fputs (correctY,pFile);
		return yParam;
	}
	else
	{
		sprintf_s(correctY, "correctY:   GET_Y_PARAM:   %d\n", (char)yParam);
		fputs (correctY,pFile);
		return yParam;
	}
}


nothing helps.

i spend a morning on Raw mouse using WM_INPUT ... but it's relative to the previous mouse coordnite and not relativ to the window ... and most probably only suitable for full screen app.s

Code: Select all

/****************************************************************************************** 
 *	Chili DirectX Framework Version 12.10.21											  *	
 *	Windows.cpp																			  *
 *	Copyright 2012 PlanetChili.net														  *
 *																						  *
 *	This file is part of The Chili DirectX Framework.									  *
 *																						  *
 *	The Chili DirectX Framework is free software: you can redistribute it and/or modify	  *
 *	it under the terms of the GNU General Public License as published by				  *
 *	the Free Software Foundation, either version 3 of the License, or					  *
 *	(at your option) any later version.													  *
 *																						  *
 *	The Chili DirectX Framework is distributed in the hope that it will be useful,		  *
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of						  *
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the						  *
 *	GNU General Public License for more details.										  *
 *																						  *
 *	You should have received a copy of the GNU General Public License					  *
 *	along with The Chili DirectX Framework.  If not, see <http://www.gnu.org/licenses/>.  *
 ******************************************************************************************/
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include "D3DGraphics.h"
#include "Game.h"
#include "resource.h"
#include "Mouse.h"

static KeyboardServer kServ;
static MouseServer mServ;

//#define WINDOWSIZEX 800
//#define WINDOWSIZEY 600

#define SCREENWIDTH 800
#define SCREENHEIGHT 600

#ifndef HID_USAGE_PAGE_GENERIC
#define HID_USAGE_PAGE_GENERIC         ((USHORT) 0x01)
#endif
#ifndef HID_USAGE_GENERIC_MOUSE
#define HID_USAGE_GENERIC_MOUSE        ((USHORT) 0x02)
#endif

    /*RAWINPUTDEVICE Rid[1];
    Rid[0].usUsagePage = HID_USAGE_PAGE_GENERIC; 
    Rid[0].usUsage = HID_USAGE_GENERIC_MOUSE; 
    Rid[0].dwFlags = RIDEV_INPUTSINK;   
    Rid[0].hwndTarget = hWnd;
    RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]);*/

int windowSizeX = SCREENWIDTH; 
int windowSizeY = SCREENHEIGHT;

//short GET_X_PARAM = 0;
//short GET_Y_PARAM = 0;
int SaveX = 0;
int SaveY = 0;

int xCurrentPosition = 0;
int yCurrentPosition = 0;


/// open file for writting
FILE* pFile = fopen("log.txt","w");

// adjust X to fit in Window
int correctX(int xParam)
{
	char correctX[64];
	// X is outside on the left side of the window, increase
	if(xParam < 0)
	{
		do
		{
			xParam++;
			sprintf_s(correctX, "correctX increase:   GET_X_PARAM:   %d\n", (char)xParam);
		}
		while(xParam < 0);	
		
		fputs (correctX,pFile);
		return xParam;
	}
	// X is outside on the left side of the window, dencrease
	else if(xParam > (windowSizeX -1))
	{
		do
		{
			xParam--;
			sprintf_s(correctX, "correctX decrease:   GET_X_PARAM:   %d\n", (char)xParam);
		}
		while(xParam > (windowSizeX -1));
		
		fputs (correctX,pFile);
		return xParam;
	}
	else
	{
		sprintf_s(correctX, "correctX:   GET_X_PARAM:   %d\n", (char)xParam);
		fputs (correctX,pFile);
		return xParam;
	}
	
}
// adjust Y to fit in Window
int correctY(int yParam)
{
	char correctY [64];
	// Y is outside and above the top side of the window, increase
	if(yParam > (windowSizeY -1))
	{
		do
		{
			yParam--;
			sprintf_s(correctY, "correctY decrease:   GET_Y_PARAM: %d\n", (char)yParam);
		}
		while(yParam > (windowSizeY -1));
		
		fputs (correctY,pFile);
		return yParam;
	}
	else if(yParam < 0)
	{
		do
		{
			yParam++;
			sprintf_s(correctY, "correctY increase:   GET_Y_PARAM: %d\n", (char)yParam);
		}
		while(yParam < 0);
		
		fputs (correctY,pFile);
		return yParam;
	}
	else
	{
		sprintf_s(correctY, "correctY:   GET_Y_PARAM:   %d\n", (char)yParam);
		fputs (correctY,pFile);
		return yParam;
	}
}



LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	/*point.a = (LONG )windowSizeX;
	point.b = (LONG )windowSizeY;*/

	

    switch( msg )
    {
		

        case WM_DESTROY:
            PostQuitMessage( 0 );
            break;

		// ************ KEYBOARD MESSAGES ************ //
		case WM_KEYDOWN:
			kServ.OnKeyPressed( wParam );
			break;
		case WM_KEYUP:
   			kServ.OnKeyReleased( wParam );
			break;
		case WM_CHAR:
			kServ.OnChar( wParam );
			break;
		// ************ END KEYBOARD MESSAGES ************ //

		// ************ MOUSE MESSAGES ************ //
		case WM_MOUSEMOVE:
			{

				//char buffer2[ 128 ];
				//sprintf(buffer2, "INPUT GET_X_PARAM: %d GET_Y_PARAM: %d\n", (char)LOWORD( lParam ),  (char)HIWORD( lParam ));
				//fputs (buffer2,pFile);

				int GET_X_PARAM = (short)LOWORD( lParam );
				int GET_Y_PARAM = (short)HIWORD( lParam );

				//int GET_X_PARAM;// = correctX((short)LOWORD( lParam ));
				//int GET_Y_PARAM ;//= correctY((short)HIWORD( lParam ));

				//GET_X_PARAM = max( 0,GET_X_PARAM ); // dramatic slow
				//GET_X_PARAM = min( (SCREENWIDTH-1),GET_X_PARAM ); // dramatic slow
				//GET_Y_PARAM = max( 0,GET_Y_PARAM ); // dramatic slow
				//GET_Y_PARAM = min( (SCREENHEIGHT-1),GET_Y_PARAM ); // dramatic slow

//				//char buffer2[ 128 ];
//				sprintf(buffer2, "INPUT after correction: GET_X_PARAM: %d GET_Y_PARAM: %d\n", (char)GET_X_PARAM,  (char)GET_Y_PARAM);
//				fputs (buffer2,pFile);
//;
//				fputs ("-------------------------------------------------------------------------------------\n",pFile);

				/*GET_X_PARAM = (short)LOWORD( lParam );
				GET_Y_PARAM = (short)HIWORD( lParam );*/
				if( GET_X_PARAM >= 0 && GET_X_PARAM < SCREENWIDTH && GET_Y_PARAM >= 0 && GET_Y_PARAM < SCREENHEIGHT )
				{
					fputs ("inside screen boundry \n",pFile);

					mServ.OnMouseMove( GET_X_PARAM,GET_Y_PARAM );
					if( !mServ.IsInWindow() )
					{
						SetCapture( hWnd );
						mServ.OnMouseEnter();
					}
				}
				else
				{
					if( wParam & (MK_LBUTTON | MK_RBUTTON) )
					{
						GET_X_PARAM = max( 0,GET_X_PARAM );
						GET_X_PARAM = min( SCREENWIDTH-1,GET_X_PARAM );
						GET_Y_PARAM = max( 0,GET_Y_PARAM );
						GET_Y_PARAM = min( SCREENHEIGHT-1,GET_Y_PARAM );
						mServ.OnMouseMove( GET_X_PARAM,GET_Y_PARAM );
					}
					else
					{
						ReleaseCapture();
						mServ.OnMouseLeave();
						mServ.OnLeftReleased();
						mServ.OnRightReleased();
					}
				}
			}
			break;
		case WM_LBUTTONDOWN:
			mServ.OnLeftPressed();
			//mServ.OnMouseMove( GET_X_PARAM,GET_Y_PARAM );
			fputs ("WM_LBUTTONDOWN\n",pFile);
			break;
		case WM_RBUTTONDOWN:
			mServ.OnRightPressed();
			//mServ.OnMouseMove( GET_X_PARAM,GET_Y_PARAM );
			fputs ("WM_RBUTTONDOWN\n",pFile);
			break;
		case WM_LBUTTONUP:
			mServ.OnLeftReleased();
			fputs ("WM_LBUTTONUP\n",pFile);
			break;
		case WM_RBUTTONUP:
			mServ.OnRightReleased();
			fputs ("WM_RBUTTONUP\n",pFile);
			break;
		case WM_NCHITTEST:

		break;
		// ************ END MOUSE MESSAGES ************ //
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}


int WINAPI wWinMain( HINSTANCE hInst,HINSTANCE,LPWSTR,INT )
{
	// windows .... check for running process
	// http://www.cplusplus.com/forum/windows/12137/

	

	WNDCLASSEX wc = { sizeof( WNDCLASSEX ),CS_CLASSDC,MsgProc,0,0,
                      GetModuleHandle( NULL ),NULL,NULL,NULL,NULL,
                      L"Chili DirectX Framework Window",NULL };
    wc.hIconSm = (HICON)LoadImage( hInst,MAKEINTRESOURCE( IDI_APPICON16 ),IMAGE_ICON,16,16,0 );
	wc.hIcon   = (HICON)LoadImage( hInst,MAKEINTRESOURCE( IDI_APPICON32 ),IMAGE_ICON,32,32,0 );
	wc.hCursor = LoadCursor( NULL,IDC_ARROW );
    RegisterClassEx( &wc );
	
	RECT wr;
	wr.left = 650;
	wr.right = windowSizeX + wr.left;
	wr.top = 150;
	wr.bottom = windowSizeY + wr.top;
	AdjustWindowRect( &wr,WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,FALSE );
    HWND hWnd = CreateWindowW( L"Chili DirectX Framework Window",L"Chili DirectX Framework",
                              WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,wr.left,wr.top,wr.right-wr.left,wr.bottom-wr.top,
                              NULL,NULL,wc.hInstance,NULL );

    ShowWindow( hWnd,SW_SHOWDEFAULT );
    UpdateWindow( hWnd );

	Game theGame( hWnd,kServ,mServ, windowSizeX, windowSizeY );
	
    MSG msg;
    ZeroMemory( &msg,sizeof( msg ) );
    while( msg.message != WM_QUIT )
    {
        if( PeekMessage( &msg,NULL,0,0,PM_REMOVE ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        else
		{
			theGame.Go();
		}
    }

	fclose (pFile);

    UnregisterClass( L"Chili DirectX Framework Window",wc.hInstance );
    return 0;
}

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Clamping the mouse pointer input in the Message loop

Post by LuX » November 6th, 2012, 4:38 pm

Those are some mad functions to correct the position. How about just check if the value is less than 0 -> make it 0, else check if the value is more than 799 -> set it 799.
ʕ •ᴥ•ʔ

NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Re: Clamping the mouse pointer input in the Message loop

Post by NaturalDemon » November 6th, 2012, 8:11 pm

that's what min & max do.
if you hover over them in the IDE.
it says
max/min == (a<b)?a:b) or (a>b)?a:b

but if you build them in the message loop it wil cause very severe performance isseus.

like i said .. i spend abbout 2 day and i tried pretty much all the ifno on mouse handling on the msdn...

this copy here is a reduced back to chii version ... but i have a backup ... from everything i tried.
i tried to find the right combo messages and used to a txt file to output .... WM_MOUSEMOVE and WM_NCMOUSEMOVE both fire .. alternating.

maybwe i'm over seeing something.

even with min/max in the message loop aswel in my class ... i still get bigger than 799 values and that's very odd.

my method/functions work ... but not always ...

Code: Select all

-------------------------------------------------------------------------------------
INPUT GET_X_PARAM: 13 GET_Y_PARAM: 66
correctX:   GET_X_PARAM:   13
correctY:   GET_Y_PARAM:   66
INPUT after correction: GET_X_PARAM: 13 GET_Y_PARAM: 66
-------------------------------------------------------------------------------------
INPUT GET_X_PARAM: 15 GET_Y_PARAM: 66
correctX:   GET_X_PARAM:   15
correctY:   GET_Y_PARAM:   66
INPUT after correction: GET_X_PARAM: 15 GET_Y_PARAM: 66
-------------------------------------------------------------------------------------
INPUT GET_X_PARAM: 17 GET_Y_PARAM: 66
correctX:   GET_X_PARAM:   17
correctY:   GET_Y_PARAM:   66
INPUT after correction: GET_X_PARAM: 17 GET_Y_PARAM: 66
-------------------------------------------------------------------------------------
INPUT GET_X_PARAM: 19 GET_Y_PARAM: 66
correctX:   GET_X_PARAM:   19
correctY:   GET_Y_PARAM:   66
INPUT after correction: GET_X_PARAM: 19 GET_Y_PARAM: 66
-------------------------------------------------------------------------------------
INPUT GET_X_PARAM: 21 GET_Y_PARAM: 66
correctX:   GET_X_PARAM:   21
correctY:   GET_Y_PARAM:   66
INPUT after correction: GET_X_PARAM: 21 GET_Y_PARAM: 66
-------------------------------------------------------------------------------------
INPUT GET_X_PARAM: 24 GET_Y_PARAM: 67
correctX:   GET_X_PARAM:   24
correctY:   GET_Y_PARAM:   67
INPUT after correction: GET_X_PARAM: 24 GET_Y_PARAM: 67
-------------------------------------------------------------------------------------
INPUT GET_X_PARAM: 28 GET_Y_PARAM: 68
correctX:   GET_X_PARAM:   28
correctY:   GET_Y_PARAM:   68
INPUT after correction: GET_X_PARAM: 28 GET_Y_PARAM: 68
-------------------------------------------------------------------------------------
INPUT GET_X_PARAM: 34 GET_Y_PARAM: 68
correctX decrease:   GET_X_PARAM:   31
correctY:   GET_Y_PARAM:   68
INPUT after correction: GET_X_PARAM: 31 GET_Y_PARAM: 68
-------------------------------------------------------------------------------------

the last line says, ...
decrease ... while the original value is 34 and gets deducted to 31.
why?

Post Reply