• Advertisement
Sign in to follow this  

cursor position

This topic is 2407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello!
I'm trying to get the cursor position so:
GetCursorPos(&point);
ScreenToClient(hwnd,&point);
cursor_x = point.x;
cursor_y = point.y;

But GetCursorPos doesen't work when the cursor is outside my game window.
How can i access always to the cursor position?

Share this post


Link to post
Share on other sites
Advertisement
GetCursorPos() works regardless of where the cursor is. The problem is due to your call to ScreenToClient(), which converts screen coordinates to window-space coordinates. And of course if your cursor is outside the window, you don't have valid window-space coordinates smile.gif

If you want to get the cursor position in just screen coordinates, remove the ScreenToClient() call entirely. If you want to detect if the cursor is inside or outside your window, check the return value of ScreenToClient() - if I remember correctly, a FALSE return indicates that the screen coordinates provided are outside the window boundary.


Also, it depends on where you are calling this from. If you do it in, say, your WM_MOUSEMOVE handler, then it won't work because you only get WM_MOUSEMOVE for when the cursor is over your window. In that case you can work around it with capturing as Adam mentioned.

Share this post


Link to post
Share on other sites
Ok I finally understood my problem.
I execute the code to get mouse position here

while (GetMessage (&messages, NULL, 0, 0))
{
[color="#1C2837"]GetCursorPos(&point);[color="#1C2837"]
cursor_x = point.x;
cursor_y = point.y;

TranslateMessage(&messages);
DispatchMessage(&messages);
}

But this code is not executed when there are no messages, and if mouse is outside the window, most of times no messages are sent to the window.
How can i execute a piece of code always? If i put it in a while(true) my application will get stuck

Share this post


Link to post
Share on other sites

Ok I finally understood my problem.
I execute the code to get mouse position here

while (GetMessage (&messages, NULL, 0, 0))
{
[color="#1C2837"]GetCursorPos(&point);[color="#1C2837"]
cursor_x = point.x;
cursor_y = point.y;

TranslateMessage(&messages);
DispatchMessage(&messages);
}

But this code is not executed when there are no messages, and if mouse is outside the window, most of times no messages are sent to the window.
How can i execute a piece of code always? If i put it in a while(true) my application will get stuck


then use PeekMesage

Share this post


Link to post
Share on other sites
A second, more subtle, bug is that GetMessage returns -1 on an error. If an error occurs you've got an infinite loop.

But that aside, yeah, use PeekMessage instead.

Share this post


Link to post
Share on other sites
This is what i wrote:


while(true)
{
RenderBegin();
//render stuff
RenderEnd();
TranslateMessage(&messages);
DispatchMessage(&messages);
PeekMessage(&messages,hwnd,0,0,true);

if(game_end) break;
}

It does work ;D

Share this post


Link to post
Share on other sites
It might "work", but it not how you should be doing those things... Here how it should be done:


LRESULT CALLBACK MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
//Process quit message
case WM_DESTROY:
case WM_CLOSE:
PostQuitMessage(0);
break;
//Mouse move messages
case WM_MOUSEMOVE:
MouseX = LOWORD(lParam);
MouseY = HIWORD(lParam);
break;
// Process more message here...
}

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

void GameLoop()
{
MSG msg;
bool done = false;

// Application loop.
while(!done){
if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)){
if(msg.message == WM_QUIT)
done = true;
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
UpdateScene(EngineTimer.g_ElapsedTime);
RenderScene();
}
}
}

Share this post


Link to post
Share on other sites
Your PeekMessage should come before Translate/Dispatch and the last param isn't a bool; you probably want PM_REMOVE there instead (although I have seen arguments for using PM_NOREMOVE followed by a GetMessage if Peek returns true, forgive me for not having any links to provide).

See here: http://www.mvps.org/...e_game_loop.htm for a sample game loop.

Edit: scooped!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement