Mouse Events for a Board Game in SDL

Started by
5 comments, last by rip-off 12 years, 3 months ago
After hours of trying to figure this out on my own, I've given up... I need help.

I'm making a board game. All the "hard work" like graphics, gameplay, AI are done. The only thing that's in my way now are mouse events. I haven't been able to find a tutorial that gets to the point of what I'm trying to do.

How do I define the coordinates of a clickable area on a graphic (my board image)?

And how do I give the user control to put pieces in that area?

Your help would be greatly appreciated.
Advertisement
Unfortunately, no such high level input exists in SDL. You need to do it yourself, or find a higher level library (possibly written in terms of SDL).

For instance, you could have a collection of rectangles, and write or find a function that can do a point/rectangle intersection test. You need to listen for mouse events, and test the mouse pointer position against this rectangle set when a click or drag is detected.

It is hard to give more than a broad overview to such a broad question. The details are up to you - but given how far you've gotten you should be capable. If you have a specific question about how to achieve any particular piece of this puzzle then it will be easier to provide a more detailed answer.
Unfortunately, no such high level input exists in SDL. You need to do it yourself, or find a higher level library (possibly written in terms of SDL).

That sucks... I may just have to look for another cross-platform multimedia library, or just make a CLI-based game. Trying to connect everything to these graphics is the only thing that's getting in my way.

For instance, you could have a collection of rectangles, and write or find a function that can do a point/rectangle intersection test. You need to listen for mouse events, and test the mouse pointer position against this rectangle set when a click or drag is detected.[/quote]
Very clever. I'll look around for some non-SDL tutorials to detail this.

It is hard to give more than a broad overview to such a broad question. The details are up to you - but given how far you've gotten you should be capable. If you have a specific question about how to achieve any particular piece of this puzzle then it will be easier to provide a more detailed answer.
[/quote]
I just want the mouse to work with the board. When the player clicks a spot, I want a piece to show up in that spot. And once the computer calculates its move, I want a piece to show up there. I'll eventually figure it out. You've pointed me in a better direction.

Thank you for your time and help.
I use something like this for doing Mouse events is this is what you mean. All this does is catch any times the mouse is moved over the image area. This only works for rectanges though.
Haven't gotten round to writing something for different shapes yet. for the rest of the code look at sourceforge and search for newdawn.

void OnHover() { // whilst mouse is over any of list items
if(event.type ==SDL_MOUSEMOTION) {

int xoff;
int yoff;
xoff = event.motion.x;
yoff = event.motion.y; //Get the mouse offsets
bool hover_first = false;

if((xoff > tileterrain[0].getX()) && (xoff < tileterrain[0].getX() + tileterrain[0].box.w) && (yoff > tileterrain[0].getY()) && (yoff < tileterrain[0].getY() + tileterrain[0].box.h)) {
hover_first = true;
}

if(hover_first) {
// Only after has inital focus
hover_first = false;
}
}
}
You can just take a look at this tutorial http://lazyfoo.net/S...on09/index.php. The first half of it just describes the class he used to describe each of the screen parts(which i guess would be useful as u'd have to do the same with ur game board) and the second talks about the SDL mouse events. It's not a very difficult concept, if u were able to do the rest this shouldn't be any trouble.
So what you'd do is initialize a SDL_Rect (which contains a top left described by x, y and then width and lenght to give the bottom right) and u'd just use the mouse events to see if it's inside that rectangle and know which 1 to activate.
That sucks... I may just have to look for another cross-platform multimedia library, or just make a CLI-based game. Trying to connect everything to these graphics is the only thing that's getting in my way.
Give allegro some 5min reading, it has a great events system for mouse and keyboard (and others) while is also free and cross-platform!
Allegro, by itself, will not give the OP the kind of high level input they were originally outlining. That is, it doesn't offer a significant improvement over SDL in this area.

This topic is closed to new replies.

Advertisement