Jump to content
  • Advertisement
Sign in to follow this  
crosel

Mouse in a menu

This topic is 3499 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

Hi, Im trying to create a menu for a tetris game. The menu simply consists of a 2d sprite background and then using directdraw i have written out the menu. My question is, how can i create/obtain a mouse pointer and use a mouse to point to certain co-ordinates, where the menu is located. So the user can simply move the mouse cursor over the menu, and click on it. How would i go about creating the mouse cursor and achieveing this? any good methods? any tutorials? Also, ive already got a directinput class that, not only aquires the mouse but also obtains the mouse's xyz states. eg float DirectInput::mouseDX() { return (float)mMouseState.lX; } thankyou

Share this post


Link to post
Share on other sites
Advertisement
Im not sure why the mouse cursor isnt showing up. Is there anywhere you coded something like ShowCursor(FALSE)?

As for clicking, well just check for a click event, then when a user clicks, get the x and y positions of the mouse and check it against the buttons position by checking if the mouse.x is >= the button's x and <= the buttons's x + the button's width, and then the same for the y's.


//get mouse position after a click

if(mouse.x >= button.x && mouse.x <= button.x + button.width)
{
if(mouse.y >= button.y && mouse.y <= button.y + button.height)
{
//player has pressed the button!
}
}

Share this post


Link to post
Share on other sites
sorry probably shouldve been more specific.

Other than obtaining the mouse and being able to retrieve its state, thats all i can do atm. I was wondering whats the best way to create the mouse visually since i havent done it.

Should i once again, use a sprite to create an image of a mouse poitner? and then somehow i can alter the sprite by using the mouse? if that is possible

Share this post


Link to post
Share on other sites

get mouse position
draw your custom mouse texture at mouse location
if mouse location is inside the menu's rectangular button
if mouse is down
do what that button should accomplish


It gets a lot more confusing than this of course, and to have all the buttons have their own on-click code, i used lua, but I'm sure there are other ways, is this what you are trying to accomplish basically though?

Share this post


Link to post
Share on other sites
The problem with DirectInput is: 1) it automatically hides the mouse cursor on you, so you need to render your own mouse cursor sprite and 2) it get's relative input rather than absolute input.

I'd recommend switching to Win32's mouse messages; they're much better suited for user interface input (and input in general really.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Tenac

get mouse position
draw your custom mouse texture at mouse location
if mouse location is inside the menu's rectangular button
if mouse is down
do what that button should accomplish


It gets a lot more confusing than this of course, and to have all the buttons have their own on-click code, i used lua, but I'm sure there are other ways, is this what you are trying to accomplish basically though?


yeah this is pretty much it.

Thanks, ill have a look at win32 mouse/cursor the next chance i get :)

Share this post


Link to post
Share on other sites
well i ended up fiddling around and ended up persisting with DirectInput mouse.

However, i discovered that directinput would return the distance of the mouse, that has changed since when Poll() was called.

Found out (eventually) that i could use the following function to obtain the position of the mouse pointer.
GetCursorPos(&mousePos);

So thats fine. however, my problem is ive used direct draw to write up my menu.
What's annoying is, the values that are returned by this function does not match up with the values that ive used to create the menu.

Eg, Ive created a RECT with the values {550, 25, 0, 0} So in other words, x = 550, and y = 25( i think so anyway :P). However, if i put my mouse over this position, the values are more closer to x = 650, y = 150.

So the values of the rect i created that contains the words "NEWGAME", does not match the values that is returned by GetCursorPos(&mousePos);

So my question is, is there a way to fix this? Because essentially, what i want to do is say

if( mousePos.x = rect.x && mousePos.y == rect.y)
\\Then start the game


anyone got a solution to my problem? :P Its just so efficent trying to find out where the menu is located, so i can actually do something with it...

Share this post


Link to post
Share on other sites
Im not sure how directdraw displays graphics, but with ID3DXSPRITE, it can render directly to screen space. This means that 0,0 will put the top left corner of the image at the very top left part of the screen.

What I do is give all my objects such as menus, buttons, textboxes, etc 4 properties: top, left, width, height. Top and left are used for rendering as well. Then I also give them a method called "within(int x, int y)" which returns a boolean of whether the mouse is within the rectangle they form.

Here is the code (again):


if(mouse.x >= button.x && mouse.x <= button.x + button.width)
{
if(mouse.y >= button.y && mouse.y <= button.y + button.height)
{
//player has pressed the button!
}
}




The problem you have with rect is - you made a non rectangle. You gave the X and Y position, but the bottom and right positions are 0 - which means you created an shape that is not even a shape at all.

The rect values should be:
rect.top =150,
rect.left = 650,
rect.bottom = rect.top + desiredButtonHeight
rect.right = rect.left + desiredButtonWidth

Share this post


Link to post
Share on other sites
getCursorPos() returns the x,y co ordinates of the mouse, in regards to my screen rather than the application itself. So when i run getCursorPos() 0,0 is the very top left corner of my 1280x1024 monitor.. not the actual application itself.

My problem is, i can draw my menu either as a sprite or as direct draw to a certain position, figure out that position and create my event on that position. However, say if i use a different computer or move the application window around, the x,y co ordinates wouldnt be the same. Therefore, the click event wouldnt work.

So my main problem is, the mouse returns x,y values of the screen. Where as everything on my application, is using values based off the application x,y co ordinates. Any suggestions where i can go from here? Im just not sure how i can correct this..

Is there something else i can use other than cursorPos()?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!