Jump to content
  • Advertisement
Sign in to follow this  
SoulHeart

SFML Flow Control

This topic is 2514 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've been messing around with sfml for awhile and i have some code here:

#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
int main()
{
//Create Window
sf::RenderWindow Game(sf::VideoMode(750, 500, 32),"Ultimate Pong", sf::Style::Fullscreen);
//Sets The Frame Rate
Game.SetFramerateLimit(30);
sf::Event Event;
while(Game.IsOpened())
{
//Load Main Menu Background
sf::Image Background1;
if (!Background1.LoadFromFile("abstract-blues--backgrounds-wallpapers.jpg"))
{
return 1;
}
//Set Sprite as the Main Menu Background Image
sf::Sprite MenuBackground;
MenuBackground.SetImage(Background1);
//Resize the Menu Background
MenuBackground.Resize(1300, 800);
//Load pirulen Font From Project File
sf::Font MenuOptions;
if(!MenuOptions.LoadFromFile("pirulen.ttf"))
{
return 2;
}
//Create The Exit Option In The Menu
sf::String MenuExit;
MenuExit.SetText("Exit");
MenuExit.SetFont(MenuOptions);
MenuExit.SetSize(75U);
MenuExit.SetColor(sf::Color(150, 50, 50));
MenuExit.SetPosition(550, 600);
//Creates an invisible rectangle around the Exit text in the menu
sf::FloatRect MenuExitRectangle = MenuExit.GetRect();
//Create The About Option In The Menu
sf::String MenuAbout;
MenuAbout.SetText("About");
MenuAbout.SetFont(MenuOptions);
MenuAbout.SetSize(75U);
MenuAbout.SetColor(sf::Color(150, 50, 50));
MenuAbout.SetPosition(500, 350);
//Creates an invisible rectangle around the About text in the menu
sf::FloatRect MenuAboutRectangle = MenuAbout.GetRect();
while(Game.GetEvent(Event))
{
//If The X Is Clicked, Exit
if(Event.Type == sf::Event::Closed)
{
Game.Close();
}
//If The Esc Key Is Pressed, Exit
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
{
Game.Close();
}
//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));
}
else
{
MenuExit.SetColor(sf::Color(150, 50, 50));
}
//if the cursor is over the About button in the menu, the About text will light up
if(MenuAboutRectangle.Contains(Event.MouseMove.X, Event.MouseMove.Y) == true)
{
MenuAbout.SetColor(sf::Color(250, 20, 20));
}
else
{
MenuAbout.SetColor(sf::Color(150, 50, 50));
}
//If The Left Mouse Button Is Clicked...
if(Event.Type == Event.MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
{
//If we click the Exit Button
if(MenuExitRectangle.Contains(Event.MouseButton.X, Event.MouseButton.Y) == true)
{
//Exit the game.
Game.Close();
}
if(MenuAboutRectangle.Contains(Event.MouseButton.X, Event.MouseButton.Y) == true)
{
cout <<"About was pressed"<<endl;
}

}
}
Game.Draw(MenuBackground);
Game.Draw(MenuExit);
Game.Draw(MenuAbout);
Game.Display();
}
return EXIT_SUCCESS;
}


1.I'm sure this isn't very well organized so I would like some examples on how to clean this up
2. I also need help with screen movement(say I clicked on the about button, I want to go to a new screen)I know how to clear the screen, but how do I end the menu screen and start the new screen. Do I create a function, another while loop. I hope you get what I mean.(I need a 101 in the flow control)

Share this post


Link to post
Share on other sites
Advertisement
Run through my creating an over-engineered pong clone in SFML tutorial, it covers more or less exactly what you are looking for. Shows starting a game, showing a menu, playing the game, exiting the game, and does it by changing states, which is generally the most common way. It also shows you another way of organizing your code.

Let me know if it doesn't answer your questions.

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!