Jump to content

  • Log In with Google      Sign In   
  • Create Account


Getting mouse position


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
15 replies to this topic

#1 VisualChaser   Members   -  Reputation: 181

Like
0Likes
Like

Posted 21 April 2012 - 04:32 PM

I've been working on a game lately using DirectX, and I want to add some mouse controls in it.
First, I tried to get the mouse data using GetCursorPos (I only need the position, not the buttons).
Here's the function:

POINT GetMousePos(HWND hWnd)
{
POINT cursorPos;
GetCursorPos(&cursorPos);
ScreenToClient(hWnd, &cursorPos);
return cursorPos;
}

This made a really weird bug in the game.
The mouse position was correct at the top left of the screen, but going down with the mouse creates a gap between the mouse and the given coordinates.
The more I move to the right and bottom, the bigger the gap gets.
For example, when my mouse is at 5,5 it displays 5,5.
When my mouse is at 200,200 it displays 205,210
When my mouse is at 500,500 it displays 510,520
(These is not the data as it displays, but it comes really close)
Note that the Y is growing faster than the X.

After this I tried using the WM_MOUSEMOVE method, but this gave the same result.
I'm only using 1 monitor and the size of the window is 1024x768 (window mode).
I tested all methods on other computers and other mouses, but all give the same results.
I made a texture drawing in the middle of the mouse.
Here are my current codes:
if(message.message == WM_MOUSEMOVE)
{
MousePosition = MAKEPOINTS(message.lParam);
}

position[21].x = MousePosition.x - 16; //Texture width = 32
position[21].y = MousePosition.y - 16; //Texture height = 32

And here's a picture of what happens (The red block is the texture that uses position[21]):
Posted Image

and the only warning I get is:
warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
But I don't think this should be a problem because the other methods aren't giving any warnings and get the same results.

Can someone help me out?

Sponsor:

#2 Tom KQT   Members   -  Reputation: 1504

Like
0Likes
Like

Posted 22 April 2012 - 02:37 AM

How do you actually know that the mouse position is wrong? You say "When my mouse is at 200,200 it displays 205,210", but isn't really the mouse at 205,210? How do you know it is at 200,200 and it should give you these values?

When you draw the texture at the mouse position, are you sure the texture drawing code isn't wrong?

#3 VisualChaser   Members   -  Reputation: 181

Like
0Likes
Like

Posted 22 April 2012 - 03:31 AM

I added a row of textures with 20 width en height, this way I can see that the texture positions are the right ones and the mouse isn't.
And it's not just this texture on the mouse. It also happens in the main menu when I try to click buttons. (It basically happens everywhere I use mouse positions)
This is how I draw the texture on the mouse. First it loads the texture.
D3DXCreateTextureFromFileEx(d3ddev,"Data/Textures/MouseItem1.png", 0, 0, 1, 0, D3DFMT_A8R8G8B8,
  D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT_NONPOW2,
  0, 0, NULL, &texture[21]);
position[21].x = 0;
position[21].y = 0;
Then it creates the sprite.
for(int i = 0; i < _SPRITES; i++)
  {
   D3DXCreateSprite(d3ddev, &sprite[i]);
  }
Then it draws the texture.
sprite[21]->Begin(D3DXSPRITE_ALPHABLEND);
   sprite[21]->Draw(texture[21], NULL, NULL, &position[21], 0xFFFFFFFF);
   sprite[21]->End();
And finally the update function.
position[21].x = MousePosition.x - 16;
position[21].y = MousePosition.y - 16;

I don't see any wrong codes here.

#4 Tom KQT   Members   -  Reputation: 1504

Like
1Likes
Like

Posted 22 April 2012 - 10:48 AM

How do you create your application window?
A common mistake is to choose some backbuffer resolution (for example 1024x768), create a backbuffer with this resolution and also create a window with this size - while forgetting, that if window is 1024x768 then its client area is smaller (because of the title bar and borders) and DirectX rendering will be stretched.

#5 VisualChaser   Members   -  Reputation: 181

Like
0Likes
Like

Posted 23 April 2012 - 09:42 AM

Thanks, looks like this was causing it. I had the same size for the backbuffers as for the window.
The mouse coordinates are fine now, but the textures aren't. When using the old backbuffers the textures fit perfectly in the window.
But with the new backbuffers the textures fall below the window, while they don't seem scaled. It ony happens at the bottom of the screen, the other sides are fine and look the same as before :S
How can I get the size of the window without the borders so I can edit my textures to the right size?

#6 Tom KQT   Members   -  Reputation: 1504

Like
0Likes
Like

Posted 23 April 2012 - 09:49 AM

You should use the AdjustWindowRect function.
You enter your desired client area size and the function tells you how large the window has to be, based on its style (you use the same window style as in CreateWindow).

#7 VisualChaser   Members   -  Reputation: 181

Like
0Likes
Like

Posted 23 April 2012 - 04:28 PM

I tried to use that function and I made a breakpoint right after it, but it seems that it only changes the rect 1 pixel in both width and height. So it doesn't make a difference, the textures are still wrong. :/
Can you tell me exactly how it works?

#8 Tom KQT   Members   -  Reputation: 1504

Like
0Likes
Like

Posted 24 April 2012 - 12:21 AM

It should be really as easy as this:
RECT rc = {0, 0, backbufferWidth, backbufferHeight};
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);
windowWidth = rc.right - rc.left;
windowHeight = rc.bottom - rc.top;
Don't forget to replace WS_OVERLAPPEDWINDOW with the style you are using when creating the window, if you use something else than WS_OVERLAPPEDWINDOW.

#9 VisualChaser   Members   -  Reputation: 181

Like
0Likes
Like

Posted 24 April 2012 - 08:21 AM

Alright thanks for helping :3

#10 Tom KQT   Members   -  Reputation: 1504

Like
0Likes
Like

Posted 24 April 2012 - 09:53 AM

Alright thanks for helping :3

Still not working?

#11 VisualChaser   Members   -  Reputation: 181

Like
0Likes
Like

Posted 24 April 2012 - 11:58 AM

I thought it worked, but it didn't. The window size is still the same :/

#12 Tom KQT   Members   -  Reputation: 1504

Like
0Likes
Like

Posted 25 April 2012 - 01:38 AM

That's really weird.
When you do this, the rectangle doesn't change at all (or only 1 pixel as you said before)?
RECT rc = {0, 0, 200, 100};
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);

I tried it on Vista with Aero and the rectangle changed to:
top: -28
bottom: 108
left: -8
right: 208

Which means that the window has to be 216x136 pixels.
And the function also tells you how many pixels has to be added around the client area in individual directions, it's 8 pixels for borders and 28 pixels for the title bar (on my Vista, this depends on your OS and your settings, you can change the title bar size somewhere).

#13 VisualChaser   Members   -  Reputation: 181

Like
0Likes
Like

Posted 25 April 2012 - 03:43 AM

It changes to
Top: -1
Bottom: 101
Left: -1
Right: 201

But when I use the WS_OVERLAPPEDWINDOW (which is NOT the style my window uses), it creates a window that is so small, it has a height lower than 30 and a width lower than 100.

#14 Tom KQT   Members   -  Reputation: 1504

Like
0Likes
Like

Posted 26 April 2012 - 01:19 AM

I've got really no idea, sorry. Maybe someone else could step in?

Btw, what style do you use for the window?

#15 VisualChaser   Members   -  Reputation: 181

Like
0Likes
Like

Posted 26 April 2012 - 03:10 AM

I'm using WS_OVERLAPPED | WS_SYSMENU | WS_BORDER.
I tried to change the window style to WS_OVERLAPPEDWINDOW and it worked fine with the AdjustWindowRect function.
But I want to make sure the window can't be maximized, can't be resized, and also I want to get rid of the minimize button at the top (not disable it).
How can I do this using other styles?

EDIT: Nevermind, I fixed it by changing the styles to WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU.
Everything is working now, thanks again!

#16 Tom KQT   Members   -  Reputation: 1504

Like
0Likes
Like

Posted 26 April 2012 - 03:31 AM

So also the mouse coordinates are accurate now?
Glad I could help, good luck with the project.




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