Reputation: 158
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;
};
Upvotes: 0
Views: 415
Reputation: 103703
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");
}
Upvotes: 3
Reputation: 683
You need to change this:
Game::Game()
{
sf::RenderWindow mainWindow(sf::VideoMode(800,600),"Main Window");
}
To this:
Game::Game() : mainWindow(sf::VideoMode(800,600),"Main Window");
{
}
In the first example, you're creating an sf::RenderWindow
within the constructor, then destroying it immediately, whereas in the second example, you're initializing the member-variable version, which will keep your window as long as the Game
object that contains it is not destroyed (or as long as you don't manually destroy the window).
Upvotes: 2
Reputation: 548
Your second program should be working with one small Change.
You already have a variable called mainWindow of type RenderWindow. Instead of creating it the way you are doing, you'll have to use the create() function within the mainWindow.
Game::Game()
{
mainWindow.create(sf::VideoMode(800,600),"Main Window");
}
Just a tip, generally you don't want to be using the isOpen() for the game-loop. I recommend you look into enum classes.
Here is a great video explaining how you would use them.
Upvotes: 1