Getting mouse position

Started by
14 comments, last by TomKQT 11 years, 12 months ago
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]):
122je5h.png

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?
Advertisement
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?
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);
}

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.
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.
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?
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).
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?
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.
Alright thanks for helping :3

Alright thanks for helping :3

Still not working?

This topic is closed to new replies.

Advertisement