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?