O'Reilly logo

SFML Game Development by Henrik Vogelius Hansson, Artur Moreira, Jan Haller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The state context

In general, every screen will need to display some text or sprites, draw to the screen, among other common things. Due to this fact, and to avoid unnecessary memory wasting by loading the same texture or font to memory in multiple places, we introduced the State::Context structure. It works as a holder of shared objects between all states of our game.

Essentially, every state will now have access to the getContext() method, which itself contains the pointer to the window used to draw its objects and resource holders such as font and texture managers. Here's the declaration of the structure:

struct Context { Context(sf::RenderWindow& window, TextureHolder& textures, FontHolder& fonts, Player& player); sf::RenderWindow* window; TextureHolder* ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required