Jump to content

  • Log In with Google      Sign In   
  • Create Account

Make cursor stay in window


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Infinity95   Members   -  Reputation: 272

Like
0Likes
Like

Posted 23 July 2013 - 03:44 AM

Hello guys. I'm having some trouble keeping the cursor in my game window. I have the following code which works quite fine:

if(GetFocus() == Game::window) {
		GetCursorPos(&this->cursorPos);
		ScreenToClient(Game::window, &this->cursorPos);
		POINT pCopy = this->cursorPos;
		if(this->cursorPos.x > windowSize.right) pCopy.x = windowSize.right;
		if(this->cursorPos.x < 0) pCopy.x = 0;
		if(this->cursorPos.y > windowSize.bottom) pCopy.y = windowSize.bottom;
		if(this->cursorPos.y < 0) pCopy.y = 0;
		if(pCopy.x != this->cursorPos.x || pCopy.y != this->cursorPos.y) {
			ClientToScreen(Game::window, &pCopy);
			SetCursorPos(pCopy.x, pCopy.y);
		}
	}

The problem now is that when i go out of the window too quickly with the mouse and click it loses focus. Is there any way to avoid this?


"Errare humanum est, sed in errare perseverare diabolicum."


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 9305

Like
1Likes
Like

Posted 23 July 2013 - 03:52 AM

I think what a lot of games do is actually disable the mouse cursor and instead use some D3D API to directly read velocity values from the mouse driver. That way the cursor isn't actually moving and you're getting accurate mouse info. Check out Raw Input maybe?

 

If that doesn't work/isn't applicable/overkill, maybe ClipCursor would do the trick? It tends to go away when the user clicks, though, so it wouldn't be foolproof, and you need to remember to disable it when you lose focus. I don't recommend ClipCursor, though. It's probably one of those evil solutions that only corporate managers find elegant.


Edited by Bacterius, 23 July 2013 - 03:54 AM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 Endurion   Crossbones+   -  Reputation: 3695

Like
0Likes
Like

Posted 23 July 2013 - 06:03 AM

Actually ClipCursor is the correct way to do that.

 

However, as Bacterius said, you have to make sure you undo ClipCursor if your app loses focus or gets closed.


Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#4 vipar   Members   -  Reputation: 170

Like
0Likes
Like

Posted 23 July 2013 - 07:16 AM

Usually games just focus the cursor in the middle of the screen or in 0,0 and then have their own cursor that reads the mouse hardware.

 

You might have noticed some times when alt-tabbing out of FPS games too many times in a row.


CEO of Dynamic Realities


#5 radioteeth   Prime Members   -  Reputation: 1153

Like
0Likes
Like

Posted 23 July 2013 - 10:27 AM

I make the cursor invisible, set the cursor position to the center of the screen each time I get it's position (each frame), and use the difference between actual position and screen center to determine how to displace a virtual cursor (for GUI purposes) and/or route the detected movement to a camera system. That is of course unless I've detected that the window has lost focus, then I re-enable the cursor's visibility and stop setting it to the center of the screen.



#6 Infinity95   Members   -  Reputation: 272

Like
0Likes
Like

Posted 23 July 2013 - 10:56 AM

I make the cursor invisible, set the cursor position to the center of the screen each time I get it's position (each frame), and use the difference between actual position and screen center to determine how to displace a virtual cursor (for GUI purposes) and/or route the detected movement to a camera system. That is of course unless I've detected that the window has lost focus, then I re-enable the cursor's visibility and stop setting it to the center of the screen.

 

Yeah that's how i re-programmed it now. I just make the normal cursor invisible and get the rest of the input with DirectInput now. What's left now is to make my own curser and display it at the according screen position.


"Errare humanum est, sed in errare perseverare diabolicum."





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS