Thread: [GD-Windows] Mouse position
Brought to you by:
vexxed72
From: Diogo de A. <dio...@ne...> - 2005-10-18 11:35:28
|
Hey all. I'm having a small problem getting the coordinate of the mouse in the window. My current system gets the coordinate like this: POINT pt; GetCursorPos(&pt); Then I want to transform this coordinate (in absolute screen coordinates, as I see it) to my engine's coordinates (which has logical coordinates, to account for different resolutions, bitmap scaling, etc)... I get the window rectangle (also in screen coordinates): RECT rect; GetWindowRect(m_window,&rect); Finally, I do the conversion itself float sx=rect.right-rect.left; float sy=rect.bottom-rect.top; float relative_x=virtual_res_x*(pt.x-rect.left)/sx; float relative_y=virtual_res_y*(pt.y-rect.top)/sy; This works almost perfectly, except when I approach the cursor from the top of the window... The window has the standard bar (created with WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VISIBLE | WS_POPUPWINDOW), and apparently GetWindowRect gets the whole window, including the top bar, instead of getting only the "drawable" part of it... I imagine the problem is that m_window above is a parent window with 2 children (the system bar, and the drawable part), but I can't find how to get just the drawable rectangle to do the conversion... This works fine if I create the window without border of title/system bar... So, my question is: how do I get the drawable part of the window's rectangle? Thanks in advance! Diogo de Andrade Creative & Technical Director Spellcaster Studios dio...@sp... www.spellcasterstudios.com |
From: Jason M. <ja...@s2...> - 2005-10-18 12:06:20
|
Check out GetClientRect() and AdjustWindowRect as well, those should=20 give you all the info you need. Here is how we get the mouse position in our game: CRecti CSystem::GetWindowArea() { RECT full, client; int clientw, clienth; int style; if (Vid.IsFullScreen()) style =3D WS_POPUP | WS_MAXIMIZE; else style =3D WS_CAPTION | WS_MINIMIZEBOX | WS_VISIBLE; GetClientRect((HWND)m_WindowHandle, &client); clientw =3D client.right; clienth =3D client.bottom; AdjustWindowRect(&client, style, false); GetWindowRect((HWND)m_WindowHandle, &full); return CRecti(full.left - client.left, full.top - client.top, full.left - client.left + clientw, full.top - client.top + clienth); } CVec2i CSystem::GetMousePos() { CRecti winrec =3D GetWindowArea(); POINT p; ::GetCursorPos(&p); return CVec2i(p.x - winrec.left, p.y - winrec.top); } Diogo de Andrade wrote: > Hey all=85 > > I=92m having a small problem getting the coordinate of the mouse in the= =20 > window=85 My current system gets the coordinate like this: > > POINT pt; > > GetCursorPos(&pt); > > Then I want to transform this coordinate (in absolute screen=20 > coordinates, as I see it) to my engine's coordinates (which has=20 > logical coordinates, to account for different resolutions, bitmap=20 > scaling, etc)... > > I get the window rectangle (also in screen coordinates): > > RECT rect; > > GetWindowRect(m_window,&rect); > > Finally, I do the conversion itself > > float sx=3Drect.right-rect.left; > > float sy=3Drect.bottom-rect.top; > > float relative_x=3Dvirtual_res_x*(pt.x-rect.left)/sx; > > float relative_y=3Dvirtual_res_y*(pt.y-rect.top)/sy; > > This works almost perfectly, except when I approach the cursor from=20 > the top of the window... The window has the standard bar (created with=20 > WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME |=20 > WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VISIBLE | WS_POPUPWINDOW), and=20 > apparently GetWindowRect gets the whole window, including the top bar,=20 > instead of getting only the "drawable" part of it... I imagine the=20 > problem is that m_window above is a parent window with 2 children (the=20 > system bar, and the drawable part), but I can't find how to get just=20 > the drawable rectangle to do the conversion... This works fine if I=20 > create the window without border of title/system bar... So, my=20 > question is: how do I get the drawable part of the window's rectangle? > > Thanks in advance! > > Diogo de Andrade > > Creative & Technical Director > > Spellcaster Studios > > dio...@sp... > > www.spellcasterstudios.com > |
From: Alen L. <ale...@cr...> - 2005-10-18 12:11:52
|
I believe that ScreenToClient() is the simplest solution for that. Alen ----- Original Message -----=20 From: "Jason Morales" <ja...@s2...> To: <gam...@li...> Sent: Tuesday, October 18, 2005 2:12 PM Subject: Re: [GD-Windows] Mouse position Check out GetClientRect() and AdjustWindowRect as well, those should give you all the info you need. Here is how we get the mouse position in our game: CRecti CSystem::GetWindowArea() { RECT full, client; int clientw, clienth; int style; if (Vid.IsFullScreen()) style =3D WS_POPUP | WS_MAXIMIZE; else style =3D WS_CAPTION | WS_MINIMIZEBOX | WS_VISIBLE; GetClientRect((HWND)m_WindowHandle, &client); clientw =3D client.right; clienth =3D client.bottom; AdjustWindowRect(&client, style, false); GetWindowRect((HWND)m_WindowHandle, &full); return CRecti(full.left - client.left, full.top - client.top, full.left - client.left + clientw, full.top - client.top + clienth); } CVec2i CSystem::GetMousePos() { CRecti winrec =3D GetWindowArea(); POINT p; ::GetCursorPos(&p); return CVec2i(p.x - winrec.left, p.y - winrec.top); } Diogo de Andrade wrote: > Hey all=85 > > I=92m having a small problem getting the coordinate of the mouse in the= =20 > window=85 My current system gets the coordinate like this: > > POINT pt; > > GetCursorPos(&pt); > > Then I want to transform this coordinate (in absolute screen coordinate= s,=20 > as I see it) to my engine's coordinates (which has logical coordinates,= to=20 > account for different resolutions, bitmap scaling, etc)... > > I get the window rectangle (also in screen coordinates): > > RECT rect; > > GetWindowRect(m_window,&rect); > > Finally, I do the conversion itself > > float sx=3Drect.right-rect.left; > > float sy=3Drect.bottom-rect.top; > > float relative_x=3Dvirtual_res_x*(pt.x-rect.left)/sx; > > float relative_y=3Dvirtual_res_y*(pt.y-rect.top)/sy; > > This works almost perfectly, except when I approach the cursor from the= =20 > top of the window... The window has the standard bar (created with=20 > WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBO= X |=20 > WS_MAXIMIZEBOX | WS_VISIBLE | WS_POPUPWINDOW), and apparently=20 > GetWindowRect gets the whole window, including the top bar, instead of=20 > getting only the "drawable" part of it... I imagine the problem is that= =20 > m_window above is a parent window with 2 children (the system bar, and = the=20 > drawable part), but I can't find how to get just the drawable rectangle= to=20 > do the conversion... This works fine if I create the window without bor= der=20 > of title/system bar... So, my question is: how do I get the drawable pa= rt=20 > of the window's rectangle? > > Thanks in advance! > > Diogo de Andrade > > Creative & Technical Director > > Spellcaster Studios > > dio...@sp... > > www.spellcasterstudios.com > ------------------------------------------------------- This SF.Net email is sponsored by: Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl _______________________________________________ Gamedevlists-windows mailing list Gam...@li... https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows Archives: http://sourceforge.net/mailarchive/forum.php?forum_idU5 |