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.
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.
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.
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.
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:
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.