Hello,
I am creating simple application with SFML. Yesterday Ive found funny "bug".
In my program I create views i.e. "MainMenuView", "GameView". Each of them holds reference to renderWindow. Game controller holds view as unique_ptr. My application stuck when I pass reference of renderWindow to new "View" and then delete this unique_ptr.
I fixed this using shared_ptr instead of reference. Is this a known issue? Am I doing something wrong?
class mainMenuView : public IView
{
public:
mainMenuView (sf::RenderWindow& w) : window(w) {}
void draw(); // drawing main menu
private:
sf::RenderWindow& window;
}
class gameView : public IView
{
public:
gameView (sf::RenderWindow& w) : window(w) {}
void draw(); // drawing map, units etc...
private:
sf::RenderWindow& window;
}
class gameController
{
public:
gameController(sf::RenderWindow& w) : window(w)
{
std::unique_ptr<mainMenuView > uiView(new mainMenuView (window));
currentView_ = std::move(uiView);
}
void createGame()
{
std::unique_ptr<GameView> gView(new GameView(window)); // If I wouldn't store this unique_ptr anywhere the game will stop and It doesnt work anymore
}
private:
std::unique_ptr<IView> currentView;
sf::RenderWindow& window;
}
main()
{
sf::RenderWindow w();
gameController gc(w);
game loop
{
gc->processSignals....
gc->update.....
gc->draw....
}
}
Remember that this is only pseudo-code.