Complete Noob SFML classes question.

Hi, I'm new here. I have seen these forums before and they always seem extreemly friendly. I learnt C++ using this website.

I have been using SFML for a little while and I made a few pong and Space Invaders games (all the code was extreemly messy and was in just one file). So as an attempt to write neater files I have decided to use classes and header files.

Here's the code for without classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow mainWindow(sf::VideoMode(700, 400), "My window");
        while(mainWindow.isOpen()){
    	sf::Event event;
    	while(mainWindow.pollEvent(event)){
                //for future event getting
    		switch(event.type){

    		}

    		//exiting
    		if((event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))){
    			mainWindow.close();
    	        }
    	}
        mainWindow.clear();
    	mainWindow.display();
    }

    return 0;
}


But I want to declare the window within a class, for example:

1
2
3
4
5
class Window{
    public:
            sf::RenderWindow mainWindow(sf::VideoMode(700, 400), "My window");
}Window;


with the full code being:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <SFML/Graphics.hpp>

class Window{
public:
    sf::RenderWindow mainWindow(sf::VideoMode(700, 400), "My window");



}Window;

int main()
{
        while(Window.mainWindow.isOpen()){
    	sf::Event event;
    	while(Window.mainWindow.pollEvent(event)){
                //for future event getting
    		switch(event.type){

    		}

    		//exiting
    		if((event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))){
    			Window.mainWindow.close();
    	        }
    	}
        Window.mainWindow.clear();
    	Window.mainWindow.display();
    }

    return 0;
}



With this code I get a bunch of errors, am I doing somthing obviously wrong or would you like the error messages?


PS: I am new here, are there things I should know(unwritten rules, code limits, etc)?
Last edited on
If you'll notice... your Window class didn't really make your code any easier. main() looks exactly the same... the only difference is that now you have to type Window.mainWindow instead of just mainWindow.


OOP is a very difficult concept for beginners to grasp. The general idea is that a class represents a "thing". Ideally... the "thing" operates in a self-contained manner... so outside code does not need to know how it works. Instead... the class exposes a handful of public "interface" functions that are used to do stuff with the object.


In your case... I'm not sure creating a Window class is really beneficial.... because SFML already has a Window class. So you could just use that.

Instead... think about how you'd break your program into logical building blocks. Some blocks will be bigger than others.. and some bigger blocks might use some of the smaller blocks.


For example... in a space invaders game. Some typical classes might be:

- Enemy (to represent one of the aliens)
- Bullet (to represent a friendly or hostile projectile)
- Player (to represent the player's ship)
- World (to represent the game map ... IE, this would own all the enemies and the player)
- Game (to represent the instance of the game. IE, this would own the window and the World object)


The 'Game' class might do the event processing stuff and just the general work of making the program a program. Whereas the 'World' class would not worry about those details and would just behave like a game world.

The 'Enemy' class would not have anything to do with event processing, or anything like that.... it would only do things that an Enemy would be concerned with.



Of course this is a simplified overview, but hopefully it illustrates the idea.

Like I said, I know this is a very confusing subject. The only way to learn it is to do it.
Thank you, very helpful. Will report back after a few days. Thanks!
New Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Game{
    sf::RenderWindow window(sf::VideoMode(500, 400), "Space Invaders!");
}Game;

int main(){
    while(Game.window.isOpen()){
        window.clear();
        window.display();
    }


    return 0;
}


The error I am getting with the example code that I showed is:
1
2
main.cpp:19:43: error: expected ‘)’ before numeric constant
     sf::RenderWindow window(sf::VideoMode(500, 400), "Space Invaders!");


Can someone explain what this means?
Last edited on
You cannot initialize the window member like this. This is what you would use a constructor for.
Also, lines 7 and 8 will result in undeclared identifier errors, because you aren't accessing Game.window.
Last edited on
1
2
3
4
    while(Game.window.isOpen()){
        window.clear();
        window.display();
    }


Since 'window' is owned by the Game class... you probably should not be accessing it outside of the Game class. IE... main shouldn't be sticking it's fingers inside of Game. Instead... Game should give a public function that main can call to do an abstract task.

Take another look at the class tutorials on this site.
OK, Thanks everyone, had another look at the classes page. Is this a good way of doing it?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Game{
public:
    Game();


}Game;

Game::Game(){
    sf::RenderWindow window(sf::VideoMode(500, 400), "Space Invaders!");
    while(window.isOpen()){
        window.clear();
        window.display();
    }
}

int main(){

    return 0;
}


It Compiles and runs without errors
Last edited on
Not really... you've basically just moved main into the constructor. Since you never exit the constructor, the Game object is never fully created.


A class has data members which are like its "moving parts". They are the things that the class manipulates in order to serve its purpose. In this case... your window is one of those things. The Game class "owns" the window... therefore it should be a member:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Game
{
    // The public interface
    //   These are the functions that code outside of this class will call in order to
    //   make this class do things.
public:
    Game();   // the constructor (initializes the state of all of this class's members)

    void run(); // a function to 'run' the game.  This function won't exit until the game
             // is over and the program is ready to be shut down


    // The private data members
    //  These are the "moving parts" that nothing outside this class should ever touch.
    //  In fact, nothing outside this class should even know they exist, or care about them.
private
    sf::RenderWindow window;  // <- the window for the game
};  // <- don't make a global object here.  Globals are bad, mm'kay?

int main()
{
    // inside main.... when the program starts... create our 'Game' object
    Game theGame;  // <- 'theGame' is the instance of our game.
        // creating this instance automatically calls the constructor, which initializes the window

    // now that we have a game that's initialized and ready.. actually run it!
    theGame.run();  // won't exit until the game is over

    // once game is over, we can just exit our program

    return 0;
}

////////////////////////////////////
////////////////////////////////////

// the game constructor.  Here we construct and initialize all our members
Game::Game()
    : window( sf::VideoMode(500,400), "Space Invaders!" )  // construct our window
{
}

// the 'run' function.  Here, we do our logic until the game is over
void Game::run()
{
    while(window.isOpen())
    {
        window.clear();
        window.display();
    }
}





I know OOP is confusing and difficult. Like I say, it just takes time.
Topic archived. No new replies allowed.