Want to create a game using C++ and SFML? This is the perfect place to start! This tutorial series will follow the creation of a simple game in C++ from the very beginning till the end, a micro-version of the overall purpose of this site. C++ is an evolving language standard and a great many tutorials out there are horrifically outdated or just plain bad. Similarly, most tutorials in books or on sites are by their very nature required to be very short and concise. This site, fortunately, does not have that restriction, so I can babble on and on and on…
This series is meant to be a living document, so if you have any questions on a specific section, let me know and I will update accordingly! Comments are available on every page, please use them, I am reading and will respond. Each post in this series builds on the last and may completely replace some code you wrote previously. It may feel a bit repetitive at times, but I guarantee the experience will be useful to you if you are just starting out.
Finally, I don’t pretend to be an expert on C++ nor SFML, so if you see a mistake or you disagree with something I have said, let me know. On occasion, I make choices that prefer clarity over “rightness”, but due to the length available to me, these should be few in number. At the end of each section, there will be a zipped download of the project in case you encountered any errors following along.
Alright, let’s get this started.
EDIT: This tutorial was written a couple of years ago and targets Visual Studio 2010. Since it was released, new versions of Visual Studio have been released that aren’t binary compatible with the files I included for SFML 1.6. You can either build a version of SFML 1.6 with the newest VS version ( a somewhat daunting process ), or much easier, install Visual C++ Express 2010. It will install side by side with newer versions without issue.
In this section, we have an overview of what this project is going to involve as well as a list of programs and libraries you are going to need to follow along. If you already have Visual C++ and SFML 1.6 installed and working, you can safely skip this section.
In this section, we set up and configure your Visual C++ project and configure Visual C++ to work properly with SFML. This is an area that new developers often get hung up on, so it goes into a fair bit of detail. If you are able to successfully compile SFML 1.6 projects in Visual C++ 2010 you can probably skip this section as well. If you run into linker errors, this part should help resolve them
Code, we finally start to code. In this part, we create the basic classes of our game including our basic game loop and a game state mechanism. By the end of this part, we finally have a working executable. Granted, an exception boring one, but it works!
In this section, we add a splash screen and a basic menu to our game. We also have our initial exposure to some object oriented concepts as well as start to see some of the advantages of using a state driven game design.
In this section, we create a base graphic class for game objects, as well as define the beginnings of our player’s paddle. This part gets a bit more in depth with object oriented programming and starts to demonstrate a few of the advantages.
In this section, we add the GameObjectManager class for managing our game objects. We look in-depth at the std::map datatype, introduce iterators, and deal with pointers for the first time.
In this section we start things moving, literally! We make the player’s paddle controllable as well as adding the ball to the world. In terms of C++, we look into the concept of protected and asserts.
In this section we get the game ball moving and add collision detection to the game. We also introduce C++ type casting and explorer the different casting options. We are now one paddle away from a game!
In this section, we add audio support by writing some very bad code. That very bad code is there to introduce some very good design. Using audio as the example, we implement a design pattern, the Service Locator, to show how to manage a globally available object in a safe manner. In the end, we have a class that is capable of playing audio using SFML or FMod with only a single line of code changed. This chapter is quite a bit different than previous chapters, but I hope you enjoy it.
In this section, we implement a cache system for SFML music and sound files. We also create our enemy paddle and give it a rudimentary ( stupid ) AI. Along the way, we introduce the concept of exception handling and have a brief introduction to C++ templates.
This section looks at moving from using debug libraries to the release versions. In addition, it covers pairing down the included files so you can share your game with other people.