Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can sf::Font font be in a Constructor parameter?  (Read 2152 times)

0 Members and 1 Guest are viewing this topic.

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Can sf::Font font be in a Constructor parameter?
« 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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Can sf::Font font be in a Constructor parameter?
« Reply #1 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Can sf::Font font be in a Constructor parameter?
« Reply #2 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.
« Last Edit: June 07, 2014, 06:27:00 pm by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

 

anything