Title: SFML Essentials
Author: Milcho G. Milchev
Published: February 2015
Publishers summary: A fast-paced, practical guide to building functionally enriched 2D games using the core concepts of SFML
I have decided to write this review chapter by chapter. Perhaps because it is a relatively small book covering a wide selection of topics there are worthwhile points to be made about suitability and content specific to each chapter. So here goes.
First of all despite being called “SFML Essentials” it doesn’t teach how to get started. If you are a C++ veteran you can follow the tutorial on the SFML website if you are a complete beginner and want to do it step by step have a look at Setting up Visual C++ and SFML you will also need to know how to configure and build your first SFML game project in order to experiment with the code in this book.
Chapter 1: Getting started with SFML
The topics covered in chapter one include:
- Window creation
- The game loop
- Event handling
- Shape rendering and transformations
The first thing I noticed as I got started with the first chapter was that the code in the book was shown as screen shots so couldn’t quickly copy & paste it. The code is available for download from the publishers site but one of the advantages of reading it digitally is usually that you can grab lines or blocks of code instantly.
Despite this minor inconvenience the rest of the chapter is very succinct and thorough with it. The code is so simple that a complete beginner to any programming language could follow along. Whether the author or SFML should take credit for this simplicity is difficult to say. Probably a bit of both.
The chapter leaves the reader understanding a basic game loop as well as how to draw shapes and handle the user quitting the game.
Chapter 2: Loading and using textures
One chapter in and I’m thoroughly enjoying myself. Eager to see the right way to uses images and textures with SFML. Here is what this chapter covers.
- Loading textures
- Rendering shapes with textures
- What is a sprite?
- Managing resources
If you are unsure of the difference between an image and a texture; an image is an object hat can be created and manipulated; a texture can simply be created (either from an image file or an SFML Image object) but must be set as a surface such as a shape and manipulated via the surface holder. But what also struck me as relevant to the review in the context of Game Code School is how beginner friendly the author is being. He explains how to add the texture to a shape in a way that someone with the most basic C++ knowledge would understand and goes to the trouble of explaining why a bit of code uses data in the way it does. So far I am not only enjoying the book but could recommend it to a C++ newbie as well. But let’s see if that continues? The chapter does eventually introduce some non-beginner topics like singletons and maps but you will find they don’t break the flow and are explained at a level which would likely enable the beginner reader to continue even if they don’t completely understand it.
Chapter 3: Animating sprites
- Capturing time
- Animating sprites
- Building an animator
Capturing time is explained in a straightforward manner and is indeed a straightforward process. Internal sprite sheet animations are made exceptionally simple compared to the same feat on Android or other platforms and then the chapter goes further to build a simple but effective system for managing multiple internal animations.
This section that goes a little beyond the level of a C++ beginner. However, if you are new to C++ but have read these basic C++ game tutorials you could sit down and piece it all together along with the explanations from the author. It was also made slightly more challenging because I couldn’t find the image files to go with the code in the download bundle. I had to download images from the web version of the book and then resize them for use in the app I was building. But it didn’t spoil the thrill of animating a sprite it was just an inconvenience.
Chapter 4: Manipulating a 2d camera
In this chapter we get to see behind the scenes (literally) of how a game character can move around the game world. How a camera works and manipulating cameras with sf::view takes the code back to a beginner level again as well. Moving the game camera around the world to follow the action of a game is shown to be exceptionally trivial using SFML. Even zooming and rotating are a breeze. This chapter covers the following topics.
- What is a camera?
- Manipulating cameras withsf::View
- What is OpenGL?
- Using OpenGL inside SFML
Part way through the chapter the topic turns to viewports which enable the coder to map different transformations of the view to different viewports (areas of the screen). This is the tool we could use for creating mini maps and user interfaces. The code stays simple but the explanation doesn’t go very deep which is frustrating but then this is “SFML Essentials” so I suppose it is to be brief.
The last topic of this chapter is OpenGL (Open Graphics Library) and if you already know how to use it then this short section gives some useful tips for using your existing knowledge with SFML. If you don’t know what it is then you get a brief heads-up. I have made a few Android games with OpenGL ES (the mobile equivalent) and the brief code samples looked like SFML is really promising to use in conjunction but we will need to look elsewhere for any more guidance (see my review of SFML Game Development coming soon).
Chapter 5: Exploring a world of sound and text
Sounds like this could be a good chapter. Pun intended. This is what is covered.
- Audio module—an overview
- Sound versus music
- Audio in action
- sf::SoundSourceand sound in 3D
- Getting started with sf::Text
Once again this chapter is easily suitable for a beginner. That’s not to say that the topics covered (spatialization, attenuation, sources and listeners) is completeley straightforward but it can certainly be comprehended with minimal C++ knowledge. The AssetManager code from chapter 2 is revisited, this time to store and manage sound FX but that is a good thing for any readers that didn’t take it all in the first time. The brief chapter builds a working example of how you can not only play sound FX but play them as if they are coming from a specific distance and direction. As you can see from the contents list the chapter also shows us how to play background music and print text on the screen for a simple HUD/score etc.
Chapter 6: Rendering special effects with shaders
This chapter is introduced as the beginning of the “Advanced section”. Here is the topic list:
- sf::RenderTarget and sf::RenderWindow
- Rendering directly to a texture
- Shader programming
- Combining everything
This for me was my favourite chapter because it brings together so much of what the book has already taught. It introduces the concept of drawing onto a texture in the same way you might draw to the main window of the game. Why would you want to do that?
The point is that by drawing lots of different things onto a texture; shapes, other textures, etc, you can create a part of a scene on it. By having multiple aspects of a scene on a single texture you can then manipulate it as one. This is useful because the manipulation can happen on the entire texture and visually this has the effect to the player of happening to all the different objects that the texture holds. The author explains this is the computationally efficient way to do things. But what manipulations or effects exactly can you do to a texture?
Introducing shaders: Shaders are great. Shaders are code that runs on the GPU called a shader program. We can write code to manipulate the geometry (position) of our graphics in what is called a vertex shader. We can also write code that manipulates the appearance of each and every pixel individually in code called a fragment shader.
So if you want to darken your scene dynamically to get a really effective and computationally cheap day night cycle, turn your scene black and white or sepia, send an explosive shock-wave effect radiating from a bomb strike or “cartoonify” the scene; then shaders are your friend. Note that this book doesn’t cover all these effects but it is a great introduction and most importantly it shows you where and how to integrate shaders into your game.
In the book there are just two examples of shaders in use but they give a real sense of the variety and depth of shaders. Take a look at these images. Both of these effects are produced with a pair of shaders and less than 10 lines of C++ code.
Full code and explanation of how to achieve these effects is in the book as well as a fully working code sample which owners of the book can download.
Chapter 7: Building multi-player games
Just to put this chapter into context so you know where I am coming from. I am not a professional network programmer, I am not really a professional programmer at all. But back in the 1990’s, like many people I had something of a fascination for networks. It blew my mind that we could type and talk to someone anywhere in the world instantly. Of course if you were born post-1990 you probably don’t understand what the silly old man is saying.
The point was that I played around with and studied TCP, UDP and the related layers that make up a network. I messed around with some C programs and made what I thought was a really cool communications program and much more recently I reviewed a book called iOS and OS X Network Programming Cookbook which has given me something of a refresher course. Anyway, take it from me if you don’t know, networks, especially the Internet and the associated programming techniques are complicated.
Here is what the chapter talks about.
- Understanding networking
- The Transport layer—TCP versus UDP
- TCP with SFML
- UDP with SFML
- Exchanging packets
- Putting it all into practice
So as I began to browse chapter 7 I was expecting I might just skim it and make a note to come back and study it at a later date, when I had more time. The chapter starts with a beginner level introduction to the different types of network we might like to use in our games like client-server or peer to peer. Then it gets down to the nuts and bolts.
Here is some code to send a message to another computer using SFML:
Now that’s not everything we need to know to make a multi-player game by a long way; but that is a very significant building block. The one topic I was sure would be desperately technical and it turns out it’s not.
The chapter gets more in-depth as it proceeds but receiving messages is almost as straightforward and it seems that SFML has a solution to all the common problems associated with networking like endianess, platform type variation, blocking sockets and more besides.
All of this therefore, it is my guess, is nothing a determined beginner who has a basic grasp of C++ couldn’t handle.
So should I buy SFML Essentials?
It depends who you are.
If you already have SFML experience and know how all the different modules work together then you will get nothing from this book! But if that is you then you probably aren’t reading this review anyway- just wanted to make that plain.
One criticism/content request is that I would have loved to have seen some basic getting set up advice. Not a click by click tutorial like the one on this site but one that can briefly show the differences between platforms. Perhaps a chapter that gets a hello world app deployed to Linux, PC and Mac.
For me personally what I enjoyed was the insight into how all the different abilities of the SFML library go together and for that reason I definitely recommend this book to an intermediate or better C++ programmer (who doesn’t know anything about SFML) who wants to make games. If this is you just be warned that the book does occasionally teach some real back to basics stuff too, which you probably already know about. This “flaw” however, helps the reader which I am really writing this review on behalf of- the beginner.
My overall impression was mild shock at just how simple this book makes some quite challenging things appear. That is not to say it doesn’t occasionally become what a beginner would call complicated in places; but in my opinion none of it would be incomprehensible to someone who has had a fast but thorough introduction to C++.
Also for a beginner, it would make an exciting if occasionally challenging companion to a regular, probably duller C++ beginners guide.
Finally, SFML Essentials gives such a full, rounded view that you can almost certainly begin planning your first C++/SFML game as soon as you are done with it.