• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
VisualChaser

Getting mouse position

15 posts in this topic

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:

[CODE]
POINT GetMousePos(HWND hWnd)
{
POINT cursorPos;
GetCursorPos(&cursorPos);
ScreenToClient(hWnd, &cursorPos);
return cursorPos;
}
[/CODE]

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:
[CODE]if(message.message == WM_MOUSEMOVE)
{
MousePosition = MAKEPOINTS(message.lParam);
}[/CODE]

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

And here's a picture of what happens (The red block is the texture that uses position[21]):
[img]http://i40.tinypic.com/122je5h.png[/img]

and the only warning I get is:
[CODE]warning C4244: '=' : conversion from 'int' to 'float', possible loss of data[/CODE]
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?
0

Share this post


Link to post
Share on other sites
How do you actually know that the mouse position is wrong? You say [i]"When my mouse is at 200,200 it displays 205,210"[/i], 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?
0

Share this post


Link to post
Share on other sites
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.
[CODE]
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;
[/CODE]
Then it creates the sprite.
[CODE]
for(int i = 0; i < _SPRITES; i++)
{
D3DXCreateSprite(d3ddev, &sprite[i]);
}
[/CODE]
Then it draws the texture.
[CODE]
sprite[21]->Begin(D3DXSPRITE_ALPHABLEND);
sprite[21]->Draw(texture[21], NULL, NULL, &position[21], 0xFFFFFFFF);
sprite[21]->End();
[/CODE]
And finally the update function.
[CODE]
position[21].x = MousePosition.x - 16;
position[21].y = MousePosition.y - 16;
[/CODE]

I don't see any wrong codes here.
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
You should use the [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ms632665%28v=vs.85%29.aspx"]AdjustWindowRect[/url] 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).
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
It should be really as easy as this:
[code]
RECT rc = {0, 0, backbufferWidth, backbufferHeight};
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);
windowWidth = rc.right - rc.left;
windowHeight = rc.bottom - rc.top;
[/code]
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.
0

Share this post


Link to post
Share on other sites
[quote name='VisualChaser' timestamp='1335277308' post='4934449']
Alright thanks for helping :3
[/quote]
Still not working?
0

Share this post


Link to post
Share on other sites
That's really weird.
When you do this, the rectangle doesn't change at all (or only 1 pixel as you said before)?
[code]
RECT rc = {0, 0, 200, 100};
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);
[/code]

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).
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
I've got really no idea, sorry. Maybe someone else could step in?

Btw, what style do you use for the window?
0

Share this post


Link to post
Share on other sites
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!
0

Share this post


Link to post
Share on other sites
So also the mouse coordinates are accurate now?
Glad I could help, good luck with the project.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0