Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Mouse Events for a Board Game in SDL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Tony Sharp   Members   -  Reputation: 100

Like
0Likes
Like

Posted 19 December 2011 - 03:31 PM

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.

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8527

Like
1Likes
Like

Posted 19 December 2011 - 05:59 PM

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.

#3 Tony Sharp   Members   -  Reputation: 100

Like
0Likes
Like

Posted 19 December 2011 - 06:39 PM

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.

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.

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.

#4 Jeff78   Members   -  Reputation: 100

Like
0Likes
Like

Posted 23 December 2011 - 10:10 AM

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;
			}
		}		
	}


#5 ScSLash   Members   -  Reputation: 100

Like
0Likes
Like

Posted 23 December 2011 - 04:31 PM

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.

#6 dejaime   Crossbones+   -  Reputation: 4053

Like
0Likes
Like

Posted 25 December 2011 - 10:31 PM

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!

#7 rip-off   Moderators   -  Reputation: 8527

Like
0Likes
Like

Posted 26 December 2011 - 11:53 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS