Jump to content
  • Advertisement
Sign in to follow this  
sefo

mouse interaction with 'objects' (SDL/C++)

This topic is 3662 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, Having a win32 coding background, I'm looking for a way in SDL to parse mouse events and retrieve the handle of the object (bitmap, surface) clicked. After some research I'm starting to believe it's not possible? I could not think of the right keywords to search on the forum or google. Is calculating the mouse coordinates the only way to check were the user clicked? In this case it's nearly to impossible to build a game with selectable units, build a card game or simply interact with more than 20 'objects' not organised in a (virtual) grid, like you can do for a board game. Can I avoid doing something like this for 100 objects? (maybe SDL+openGL?)
if(mX > 177 && mx < 240 && mY > 12 && mY < 22) {
    //cliked on oject #1
}
if(mX > 277 && mx < 340 && mY > 112 && mY < 122) {
    //cliked on oject #2
}

Share this post


Link to post
Share on other sites
Advertisement
Quote:

Having a win32 coding background, I'm looking for a way in SDL to parse mouse events and retrieve the handle of the object (bitmap, surface) clicked.


I think you don't understand what SDL does. It isn't a GUI library. There is, to SDL, no such thing as an "object clicked", because the main surface is just a bunch of pixels. Functions like SDL_Blit just transfer pixels between surfaces.

But of course it is possible to do the things you mentioned; you'll just have to do the work yourself. For example, in a strategy game, you'd have a list of all units with their bounding rectangles. When a mouse button is clicked, you'd have to loop through the list of units and check each unit's bounding rectangle against the mouse click coordinates to see which one the user wanted to select.

Share this post


Link to post
Share on other sites
Quote:

But of course it is possible to do the things you mentioned.
[...]in a strategy game, you'd have a list of all units with their bounding rectangles. When a mouse button is clicked, you'd have to loop through the list of units and check each unit's bounding rectangle against the mouse click coordinates to see which one the user wanted to select.


Alright, so basically it is NOT possible to do what I mentioned.

Theoretically, how would I go about doing this?
Even if I have an array or vector of structures (with unitID, x, y, w, h), i still need to do:


if(mX > unit1.x && mX < unit1.w && mY > unit1.y && unit1.h < 22) {

//cliked on unit #1

}
.....


Can anyone give me a pseudo-code or link to a resource so I can implement that?

Share this post


Link to post
Share on other sites
Lets say all your objects are located in an std::list, when the player clicks the mouse button you would have to loop through the list and check each object for if the mouse click is colliding with the object. Then one method would be to have each object contain an onMouseDown event.

So for example you could write:

[source language="cpp"]
int mouseX, mouseY;

SDL_GetMouseState(&mouseX, &mouseY);

for(std::list<GameObject*>::iterator it = m_objectList.begin(); it != m_objectList.end(); ++it)
{
if(mouseX >= (*it)->getXPos() && mouseX <= (*it)->getXPos() + (*it)->getWidth() && mouseY >= (*it)->getYPos() && mouseY <= (*it)->getYpos() + (*it)->getHeight())
{
(*it)->onMouseDown(mouseX, mouseY);
}
}



That would call on mouse event for every object that the mouse is colliding with, and therefore doesnt take anything such as z ordering or only chose the first object int he list etc, but its a base.

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!