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

Author Topic: Creating a font in initializer list  (Read 3169 times)

0 Members and 1 Guest are viewing this topic.

Dante12129

  • Newbie
  • *
  • Posts: 24
    • View Profile
Creating a font in initializer list
« on: June 14, 2013, 09:30:51 am »
I have a state machine in my game in order to handle different menus, gameplay sequences, credits and etc. Each state stores a reference to the main font in the game. The problem is that I am using a game class so that I can keep the code in my main down to a minimum. This class stores the actual font. The thing is that I initialize my state manager with the first state, so I use the initializer list. The thing is that I have to initialize the font before it, but I can't get it to work.

The variables are declared like this:
sf::RenderWindow main_window;
sf::Font main_font;

dte::StateManager m_state_manager;
 

And the game class' constructor looks like this:
Game::Game() : main_window(sf::VideoMode(640, 480), "Animal Instincts"), main_font(), m_state_manager(new MenuState(main_window, main_font)) { main_font.loadFromFile("Unispacerg.ttf"); }
 
The state manager has to take its first state in its constructor in order to work properly, as I have no function to initialize it later.

And the MenuState's constructor:
MenuState::MenuState(sf::RenderWindow& window, sf::Font& font) : m_window_ptr(&window), m_font(font), m_needs_change(false), m_welcome_message("Welcome", m_font, 30)
    {
        m_welcome_message.setColor(sf::Color::White);
        dte::CenterTextHor(m_welcome_message, *m_window_ptr);

        std::cout << "Menu state created." << std::endl;
    }
 

The problem is that this code will only show the text if I cause a new Menustate to be created. How would I go about initializing the font in the initializer list so that it will work the first time?
« Last Edit: June 14, 2013, 09:33:03 am by Dante12129 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Creating a font in initializer list
« Reply #1 on: June 14, 2013, 10:06:51 am »
sf::Font loadFont(...) {...}

..., main_font(loadFont(...)), ...

But this is a very bad design in my opinion. It doesn't allow you to detect and handle a loading error. And saying "my code wants that, but I can only do this" is usually a bad sign; don't forget that you write the code, so you should adapt the design to what you want to do, not the other way round.
Laurent Gomila - SFML developer

Silvah

  • Guest
Re: Creating a font in initializer list
« Reply #2 on: June 14, 2013, 01:31:32 pm »
sf::Font loadFont(...) {...}

..., main_font(loadFont(...)), ...

But this is a very bad design in my opinion. It doesn't allow you to detect and handle a loading error.
Actually, it does.

sf::Font loadFont(const Foo& foo)
{
  if(loadingFailed)
    throw std::runtime_error("oh noes");
}

Game::Game()
try
  : /* ... */, main_font(loadFont(someFoo)), /* ... */
{
}
catch(std::exception& e)
{
  // handle the error, or just allow it to propagate
}

In fact, just letting it propagate is likely the right thing to do.
« Last Edit: June 14, 2013, 01:40:24 pm by Silvah »

Dante12129

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Creating a font in initializer list
« Reply #3 on: June 15, 2013, 03:26:35 am »
I'm adding a method to my state manager to allow it to be initialized with a state after construction.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Creating a font in initializer list
« Reply #4 on: June 15, 2013, 11:20:20 am »
I'm adding a method to my state manager to allow it to be initialized with a state after construction.
...and the design gets uglier and uglier.

Why don't you just keep things simple? Construct the state manager as a member of another class (not globally), and initialize it during construction. If there is an error, throw an exception -- that's what exceptions are for. But don't leave the object in an invalid state and require to call an initialize method after construction, this just injures invariants and works around the intent of constructors.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Dante12129

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Creating a font in initializer list
« Reply #5 on: June 15, 2013, 08:20:04 pm »
The state manager already is part of my game class, and it wouldn't work properly because I couldn't initialize the game class' font in the initialzer list before it.

 

anything