Sign in to follow this  
dream0331

SDL two states button

Recommended Posts

Hi there. I am trying to build a button which has two states. I make use of SDL_MOUSEBUTTONDOWN & SDL_MOUSEBUTTONUP, and the button rendered in a forever loop, keep checking input from user. The problem is, when I click the button, the state changes; but before I release, the loop repeats and it changes back to the previous state again. I tried another method, that is to use 'BUTTONUP' to check the 'times' of mouse clicks. The button states change according to a bool variable when user clicks, and the bool changes in between when user releases. The method seems works, but the checking is not smooth, which means the button doesn't change its state sometimes when I click. Any better suggestion? Thank you!

Share this post


Link to post
Share on other sites
Post your code.

It should be quite simple - either you keep the button state identical to the mouse state each iteration, or you explicitly turn the button on and off when the mouse button goes down or up.

Share this post


Link to post
Share on other sites
if(evt->type==SDL_MOUSEBUTTONDOWN){//mouse pressed
if(evt->button.button==SDL_BUTTON_LEFT){
if(evt->button.y>yPos && evt->button.y<(yPos+20) && evt->button.x>xPos && evt->button.x<(xPos+50)){ //correct position
if(check==true)
condition=0;
else
condition=1;
}
}
}
if(evt->type==SDL_MOUSEBUTTONUP){ //mouse released
if(evt->button.button==SDL_BUTTON_LEFT){
if(check==false)
check=true;
else
check=false;
}
}

In the code, variable 'condition' decides the image of button to be loaded, xPos & yPos are the position of button on the screen.

The above function is included in an infinite loop, until user exits.

Share this post


Link to post
Share on other sites
I think your bug is elsewhere, if condition does what you say it does. But to be sure, we'd need to see more code. Please format anything you post in the source tags.

I'm not entirely sure what you're doing with 'check' there, but there's surely no need to toggle it on mouse-up.

Share this post


Link to post
Share on other sites
Sorry I am new to this forum and I am not sure how to use source tags.

This is the overall view of the class Button:
class Button:public Control
{
private:
int xPos; //coordinate of button
int yPos;
bool check;
public:
Button(SDL_Surface* Image,int x, int y); //constructor
void render();
bool update(Uint8* keystates, SDL_Event* evt);
};

And the regarding function:

bool Button::update(Uint8 *keystates, SDL_Event *evt){
if(evt->type==SDL_MOUSEBUTTONDOWN){//mouse pressed
if(evt->button.button==SDL_BUTTON_LEFT){
if(evt->button.y>yPos && evt->button.y<(yPos+20) && evt->button.x>xPos && evt->button.x<(xPos+50)){ //correct position
if(check==true)
condition=0;
else
condition=1;
}
}
}
if(evt->type==SDL_MOUSEBUTTONUP){ //mouse released
if(evt->button.button==SDL_BUTTON_LEFT){
if(check==false)
check=true;
else
check=false;
}
}
return true;
}

the boolean check simply functions as to toggle the button.

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