Jump to content
  • Advertisement
Sign in to follow this  
SoulHeart

Help with making SFML buttons

This topic is 2517 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

I need some help with a button.

//Creates a rectangle around the exit button in the menu
sf::FloatRect MenuExitRectangle = MenuExit.GetRect();
//if the cursor is over the Exit button in the menu, the Exit text will light up
if(MenuExitRectangle.Contains(Event.MouseMove.X, Event.MouseMove.Y) == true)
{
MenuExit.SetColor(sf::Color(250, 20, 20));
Game.Draw(MenuExit);
}
else
{
MenuExit.SetColor(sf::Color(150, 50, 50));
Game.Draw(MenuExit);
}

I want to add if the button is left clicked(Event.Type == Event.MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left),
Exit(Game.Exit();) but I'm not sure how to add this the right way
Thanks

Share this post


Link to post
Share on other sites
Advertisement
Most people break their application's main loop into three (or more) main portions:
1) Read input from player
2) Update things like gravity, or monster AI, or animations.
3) Draw everything that is within view

Then your main game loop (for a small to medium-sized game) looks like this:
LoadResources();
SetupGameVariables();

//Main loop:
while( GameIsStillRunning == true )
{
HandlePlayerInput();
UpdateEverything();
DrawEverythingVisible();
}

FreeResources();


Note: This isn't actual code, and you can name the variables how you want or structure it how you want, but the general idea is what I'm trying to convey.

Basically, the general structure of your game should follow this. Not exactly word for word, variable for variable, function for function, but the general flow of your program should be: HandleInput ? UpdateLogic ? DrawGraphics ? Repeat

They should be seperate in your mind and seperate in your code, because they do different things and serve different purposes.

In your function that handles reading the input:

if(Event.Type == Event.MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
{
//If we clicked within the exit rectangle, then exit!
if(MenuExitRectangle.Contains(Event.MouseButton.X, Event.MouseButton.Y) == true)
{
//Exit the game.
App.Close();
}
//If we clicked within another button's rectangle, then do that button's function.
if(DifferentButtonRectangle.Contains(Event.MouseButton.X, Event.MouseButton.Y) == true)
{
//...
}
}
else if(Event.Type == Event.MouseMoveEvent)
{
//If hovered over the button, change it's color.
if(MenuExitRectangle.Contains(Event.MouseMove.X, Event.MouseMove.Y) == true)
{
//Change the color of the text.
MenuExit.SetColor(sf::Color(250, 20, 20));
}
else
{
MenuExit.SetColor(sf::Color(150, 50, 50));
}
//Change the color of other buttons, if hovered over them.
if(DifferentButtonRectangle.Contains(Event.MouseMove.X, Event.MouseMove.Y) == true)
{
//Hovered color...
}
else
{
//Normal color...
}
}


The function that handles drawing should be seperate from the function that handles input:

Game.Draw(MenuExit); //Color has already been set elsewhere - all we need to do is draw.
Game.Draw(DifferentButton);

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!