Restricting mouse pointer to Window – ShiVa Engine

Restricting mouse pointer to Window

You can access the Windows API from within plugins. This example shows how to restrict the mouse pointer to the area of the game window.
First, create a control variable and storage for the rectangle of the window:

bool oldrectStored = false;
RECT oldRect;
RECT newRect;

The following function captures the current window and assigns a clipping area to the mouse cursor. Since you most likely want to be able to move out of the restricted area again at some point, it is important to offer a reset to the original, full-screen cursor area:

int Callback_wnd_restrict ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
    S3DX_API_PROFILING_START( "wnd.restrict" ) ;
    // Input Parameters
    int iInputCount = 0 ;
    S3DX::AIVariable bRestrict = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
	if (bRestrict.GetBooleanValue()) {
		if (!oldrectStored) {
			if (GetClipCursor(&oldRect)) {
				oldrectStored = true;
			}
			else {
				S3DX::log.warning("Could not store old rect!");
			}
		}
		auto win = GetActiveWindow();
		if (!IsWindow(win)) {
			S3DX::log.warning("HWND invalid!");
		}
		else if (GetWindowRect(win, &newRect)) {
			if (!ClipCursor(&newRect)) {
				S3DX::log.warning("ClipCursor failed!");
			}
		}
		else {
			S3DX::log.warning("GetWindowRect failed!");
		}
	}
	else {
		if (oldrectStored) {
			if (!ClipCursor(&oldRect)) {
				S3DX::log.warning("ClipCursor reset failed!");
			}
		}
	}
    // Return output Parameters
    int iReturnCount = 0 ;
    S3DX_API_PROFILING_STOP( ) ;
    return iReturnCount;
}

This approach can be modified to alter other things such as window position, style, size, etc.




Need more answers?

  • slackBanner