Sign in to follow this  
Walkerism

SDL Mouse event issue (pong game)

Recommended Posts

Walkerism    108
Hi guys,

I've started building the routine Pong clone, and I've stumbled into a problem. I'm trying to map the mouse to the players panel; if the mouse moves up, the panel moves with it.

The problem, I'm unsure on how to do this. Currently I'm using a very backward way of accomplishing this (merely to allow myself to continue building the game in the mean time) where if the mouse moves along the Y axis, it essentially increases the velocity of the panel along the Y axis.

[code]void PlayerStick::handle_input()
{

if( event.type == SDL_MOUSEMOTION )
{
signed int y_motion = event.motion.yrel;

if( y_motion > 0 )
{
++posY;
}
if( y_motion < 0 )
{
--posY;
}
}
}
[/code]

I began looking into using the mouse state to link the y axis to the players panel, but I'm lost on hos to achieve this and the articles, tutorials and advice I've seen so far around using SDL_mousestate do not provide a background to using it in the first place. Besides, I'm not even sure that's the correct way around it.

Can anyone here recommend me a way of linking the y coordinates of the mouse to the y coordinates of the player panel?

Cheers!

Share this post


Link to post
Share on other sites
BeerNutts    4401
Just check how much it moved from the last location, and add it. If you get a mouse event, you check it's relative location, and add that to your paddle.

You are just adding 1, regardless how how much it moved.


[code]

void PlayerStick::handle_input()
{

if( event.type == SDL_MOUSEMOTION )
{
int y_motion = event.motion.yrel;

posY += ymotion;

}
}
[/code]

Share this post


Link to post
Share on other sites
rip-off    10979
If you want panel.x == mouse.x, then you SDL_GetMouseState(). It copies the current position of the mouse to the input pointers (if they are not-null), and returns the current button state of the mouse. There is no other real background, other than to note that it can (and probably should) be used outside the event loop *.

Example:
[code]
int mouseY;
SDL_GetMouseState(NULL, &mouseY);

paddle.position.y = mouseY;
[/code]
If paddle.position.y is an int you could pass its address directly to SDL_GetMouseState(), but I like the extra clarity of the variable.

* [size="1"]You must still have an event loop to process quit events, and to "pump" events.[/size]

Share this post


Link to post
Share on other sites
Walkerism    108
Thanks, both of you!

In the end I found your method rip-off was exactly what I was after, giving direct control over the panel.

Cheers again for your help =D

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