In this weeks tutorial, we start work on a camera component for our game. We’ll start simply by implementing the functionality to follow the player around the level on the x-axis (the y-axis will be locked in place for now). As we develop the game we’ll want a more advanced camera features such as the ability to follow the player at a variable speed, camera shake, and locking to the level edge so that we don’t see area beyond our level. But starting small will help us get the hang of how SFML’s Views work (more on that shortly) and provide a refresher of how we create and add components to our objects.
To help us with our camera component we’ll make use of SFML’s Views. I’ll go into more detail about how they work when we start doing more advanced things with them but for now, you can think of them as a 2D camera (and they are described as such in the documentation). Using sf::View we can rotate and zoom the scene and also easily move the ‘camera’ around, which is the exact functionality we need for today.
- Retrieve the View.
- Edit the View.
- Update the windows View with the modified View.
Now our game scene has access to the window we can create the camera component and add it to the player object.