One of the problems that has been quite pronounced so far is how long and unwieldy the code gets. OOP allows you to break your projects up into logical and manageable chunks called classes. Learn how you can make a big improvement to the manageability of the code.
- Hi. Welcome to Structuring the Thomas Was Late Code. In the previous video, we looked at Thomas Was Late Game. In this video, we're going to improve the manageability of the code in this project with the help of Engine class. One of the problems that has been quite pronounced in both projects so far is how long and unwieldy the code gets. OOP allows us to break our projects up into logical and manageable chunks called classes.
We will make a big improvement to the manageability of the code in this project with the introduction of an engine class. Among other functions, the engine class will have three private functions. They are input, update and draw. This should sound very familiar. Each of these functions will hold a chunk of the code that was previously all in the main function. Each of these functions will be in a code file of its own. Input.cpp, Update.cpp and Draw.cpp respectively.
There will also be one public function in the Engine class, which can be called with an instance of Engine. This function is run and will be responsible for calling Input, Update and Draw once for each frame of the game. Furthermore, because we have abstracted the major parts of the game engine to the engine class, we can also move many of the variables from Main and make them members of Engine. All we need to do to get our game engine fired up is create an instance of Engine and call its Run function.
Here's a sneak preview of the super-simple Main function. Don't add this code just yet. To make our code even more manageable and readable, we will also abstract responsibility for big tasks, such as loading a level and collision detection to separate functions in separate code files. These two functions are loadLevel and detectCollisions. We will also code other functions to handle some of the new features of the Thomas Was Late project. We will cover them in detail as and when they occur.
To further take advantage of OOP, we will delegate responsibility for particular areas of the game entirely to new classes. You probably remember that the sound and HUD code was quite lengthy in previous projects. We will build a sound manager and HUD class to handle these aspects in a cleaner manner. Exactly how they work will be explored in depth when we implement them. The game levels themselves are much more in-depth than previous games, so we'll also code a level manager class. As you would expect, the playable characters will be made with classes as well.
For this project, however, we will learn some more C++ and implement a playable character class with all the common functionality of Thomas and Bob and then Thomas and Bob classes, which will inherit this common functionality as well as implement their own unique functions and abilities. This perhaps unsurprisingly is called inheritance. I will go into more detail about inheritance in the next section. We will also implement a number of other classes to perform specific responsibilities. For example, we'll make some neat explosions using particle systems.
You might also be able to guess that to do this, we will code a Particle class and a ParticleSystem class. All these classes will have instances that are members of the Engine class. Doing things this way will make all the features of the game accessible from the game engine, but encapsulate the details into appropriate classes. The last thing to mention before we move on to see the actual code that will make the Engine class is that we will reuse without any changes whatsoever the texture holder class, which we discussed and coded for the Zombie Arena game.
Cool! In this video, we learned structuring the Thomas Was Late code. Next up, we'll see building the game engine.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Abstract classes
- Level design
- Collision detection
- The HUD class
- Extending SFML classes
- Particle systems