Search code examples
c++sfml

SFML - Object Static Definition


I am new to Object oriented programming. I was going through some code to learn some object oriented programming. Game from Scratch C++ has some code for a game called Pang that helps learn OOP concepts. In the below code I can see that an object from the sf::RenderWindow class is created and this object is defined as static in another class. I am confused as to what is going on here and is it possible to do something like this. If someone with good familiarity of SFML could answer this I would appreciate it. Also, what does sf? stand for over here?

#pragma once
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
class Game
{

public:
  static void Start();

private:
  static sf::RenderWindow _mainWindow;
};

Solution

  • sf is the namespace, similar to std being the namespace for cout. Technically it would mean "Simple and Fast", but really has no importance other than to provide a unique context to define functions in. This is so that, for example, you could have a printNumber() function in both foo and bar namespaces, with distinct implementations, and you could call each of them with foo::printNumber() and bar::printNumber(). It's an organisation technique.

    In this context, a static _mainWindow member means there is only one instance created no matter how many Game class instances you create there will only ever be one _mainWindow. Because of this, you won't access it how you normally would, this->mainWindow, but because the instance is independent of any particular Game instance, you access it with Game::_mainWindow. Not sure This is probably just to ensure only one single window is ever open.

    Note: Both namespace and static use the syntax foo::bar which means "look for bar in context foo".