SFML community forums

Help => Graphics => Topic started by: AndreeU17 on June 05, 2014, 09:25:53 am

Title: Can sf::Font font be in a Constructor parameter?
Post by: AndreeU17 on June 05, 2014, 09:25:53 am
Not quite sure how this works, i only have such a huge knowledge in c++ (Just kidding, constantly studying).
But i created a class, withing the class i did this:
class Game
{
private:
        sf::RectangleShape rectangle;
        sf::CircleShape circle;
        sf::Text text;
public:
        Game(sf::Vector2f size, sf::Vector2f position, sf::Color color)
        {
                rectangle.setSize(size);
                rectangle.setPosition(position);
                rectangle.setFillColor(color);
        }
        Game(float radius, sf::Vector2f position, sf::Color color)
        {
                circle.setRadius(radius);
                circle.setPosition(position);
                circle.setFillColor(color);
        }
        Game(/*sf::Font loadFont ,*/std::string title, int characterSize, sf::Vector2f position, sf::Color color)
        {
                //text.setFont(loadFont);
                text.setString(title);
                text.setCharacterSize(characterSize);
                text.setPosition(position);
                text.setColor(color);
        }

        //function for converting string
        //function for updating ball
        //function for random number
        //function for Colission
        //function for computer AI
};
 

The thing is, i want to add sf::Font font as one of the parameters since it has a method called, setFont that would work perfectly with the text constructor. However, i'm not sure how to call it or set it up in the class. I first inserted, sf::Font font, as one of the parameters and called it using text.setFont(font); but once i was in the main function, i didnt know what to put in the parameter for font:
Game startTitle(Game(/*sf::Font loadFont ,*/"Start", 24, sf::Vector2f(0,0), sf::Color::White));
 
I though i could simply use, loadFromFile("file.png"); but that doesnt work, how can i work around this? Also i was wondering that with private variable members, the best way to call them into main would be to use getter/setter methods, correct?
Title: Re: Can sf::Font font be in a Constructor parameter?
Post by: Nexus on June 05, 2014, 09:32:10 am
Pass a const-reference to the constructor.

No, the best is to hide private member variables completely. Only provide getter/setter methods if you really need them, and try to find a better encapsulation first.
Title: Re: Can sf::Font font be in a Constructor parameter?
Post by: BaneTrapper on June 07, 2014, 06:23:38 pm
There are quite some ways of achieving what you want to do.
Why not pass in font by reference?
Code: [Select]
Game(sf::Font & loadFont ,std::string title, int characterSize, sf::Vector2f position, sf::Color color)
You usually never want to load one font multyple times, and in here it seems that is the case.
Have sf::Font already loaded and checked for error, then whenever you need it you can use it.
Code: [Select]
sf::Font fontArial;
fontArial.loadFr...
//...
Game startTitle(Game(loadFont ,"Start", 24, sf::Vector2f(0,0), sf::Color::White));
Game startTitle(Game(loadFont ,"CrashAttempt", 32 sf::Vector2f(screen_size_x / 2, screen_size_y / 2), sf::Color(255,255,255,126));

But be aware, in order for your sf::Text to use sf::Font, you need to keep sf::Font instance alive.