Search code examples
c++sfml

SFML - sf::RenderWindow, dividing files


so it is probably something trival but I really need to know why it does happen the way it does and how can I change it.

So I started to learn SFML today and I was reading SFML Game Development ebook and saw very interesting and well written code. I went through tutorials about SFML and started to learn language as I understood general idea of way how it should work.

So I wanted to remember new keywords, constructors, methods but also make my code well organized - using what I have learned to keep it clean and easy to edit, debug.

My first code was to display Window and I created same code in both ways, normally putting everything to main function and separated. Thing is that first Window is displayed as long as I won't close it and second one is displaying for less than second and program is turning off.

It probably because destructor is called right after I turn it on and adding more functions to keep object busy is way to go but well, I want to understand it. It's last thing which I don't really understand as I learned objective programming. The way objects are working. Right after I create them, I am using them for certain task, but then when I am done they are being deleted, well sometimes I need them again. I just wish to understand how does it work and find really easy and quick fix/idea to make it work as long as I want it to.

Code :

First program:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow mainWindow(sf::VideoMode(800,600),"Main Window");

    while(mainWindow.isOpen())
    {
        sf::Event openEvent;
        while(mainWindow.pollEvent(openEvent))
        {
            switch(openEvent.type)
            {
                case sf::Event::Closed:
                mainWindow.close();
                break;
            }
            mainWindow.clear();
            mainWindow.display();
        }
    }
}

Second program:

main.cpp

#include <SFML/Graphics.hpp>
#include "Game.cpp"

int main()
{
    Game game;
    game.run();
}

Game.cpp

#include "Game.h"

    Game::Game()
    {
        sf::RenderWindow mainWindow(sf::VideoMode(800,600),"Main Window");
    }

    void Game::run()
    {
        while(mainWindow.isOpen())
        {
            sf::Event openEvent;
            while(mainWindow.pollEvent(openEvent))
            {
                switch(openEvent.type)
                {
                    case sf::Event::Closed:
                    mainWindow.close();
                    break;
                }
            }
            mainWindow.clear();
            mainWindow.display();
        }
    }

Game.h

class Game
{
    public:
    Game();
    void run();

    private:
    sf::RenderWindow mainWindow;

};

Solution

  • Game::Game()
    {
        sf::RenderWindow mainWindow(sf::VideoMode(800,600),"Main Window");
    }
    

    In your constructor here, you are creating a new RenderWindow object, which is immediately destroyed once the constructor exits. What you want to do is initialize the RenderWindow which is a member of your class. You can do that in one of two ways, either using the RenderWindow constructor in the member initializer list:

    Game::Game()
        :mainWindow(sf::VideoMode(800,600),"Main Window")
    {}
    

    Or calling the create function in the constructor body like following:

    Game::Game()
    {
        mainWindow.create(sf::VideoMode(800,600),"Main Window");
    }