Reader small image

You're reading from  Beginning C++ Game Programming. - Second Edition

Product typeBook
Published inOct 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781838648572
Edition2nd Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

Chapter 14: Abstraction and Code Management – Making Better Use of OOP

In this chapter, we will take a first look at the penultimate project of this book. The project we will be building will use advanced features such as directional sound, which has the effect of appearing to play relative to the position of the player. It will also have split-screen cooperative gameplay. In addition, this project will introduce the concept of Shaders, which are programs written in another language that run directly on the graphics card. By the end of Chapter 18, Particle Systems and Shaders, you will have a fully functioning, multiplayer platform game built in the style of the hit classic Thomas Was Alone.

This chapter's focus will be getting the project started and exploring how the code will be structured to make better use of OOP. Here are the details of the topics that will be covered in this chapter:

  • Introducing the final project, Thomas Was Late, including the gameplay...

The Thomas Was Late game

Tip

At this point, if you haven't already, I suggest that you go and watch a video of Thomas Was Alone at http://store.steampowered.com/app/220780/.

Notice the simple but aesthetically excellent graphics. The video also shows a variety of gameplay challenges such as using the character's different attributes (height, jump, power, and so on). To keep our game simple without losing the challenge, we will have fewer puzzle features than Thomas Was Alone but will have the additional challenge of creating the need for two players to play cooperatively. Just to make sure the game is not too easy, we will also make the players have to rush to beat the clock, which is why the name of our game is Thomas Was Late.

Features of Thomas Was Late

Our game will not be nearly as advanced as the masterpiece that we are attempting to emulate, but it will have a good selection of exciting game-play features, such as the following:

  • A clock that counts...

Structuring the Thomas Was Late code

One of the problems that has been getting worse from project to project, despite taking measures to reduce the problem, is how long and unwieldy the code gets. Object-oriented programming (OOP) allows us to break our projects up into logical and manageable chunks, known as 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. These are input, update, and draw. These should sound very familiar. Each of these functions will hold a chunk of the code that was previously in the main function. Each of these functions will be in a code file of its own, that is, 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...

Building the game engine

As we suggested in the previous section, we will code a class called Engine that will control and bind the different parts of the Thomas Was Late game.

The first thing we will do is make the TextureHolder class from the previous project available in this one.

Reusing the TextureHolder class

The TextureHolder class that we discussed and coded for the Zombie Arena game will also be useful in this project. While it is possible to add the files (TextureHolder.h and TextureHolder.cpp) directly from the previous project, without recoding them or recreating the files, I don't want to assume that you haven't jumped straight to this project. What follows is very brief instructions, along with the complete code listing we need, to create the TextureHolder class. If you want the class or the code explained, please see Chapter 10, Pointers, the Standard Template Library, and Texture Management.

Tip

If you did complete the previous project and you...

Coding the main function

Let's rename the TFL.cpp file that was autogenerated when the project was created to Main.cpp. Right-click the TFL file in the Solution Explorer and select Rename. Change the name to Main.cpp. This will be the file that contains our main function and the code that instantiates the Engine class.

Add the following code to Main.cpp:

#include "Engine.h"
int main()
{
    // Declare an instance of Engine
    Engine engine;
    // Start the engine VRRrrrrmmm
    engine.run();
    // Quit in the usual way when the engine is stopped
    return 0;
}

All we do is add an include directive for the Engine class, declare an instance of Engine, and then call its run function. Everything will be handled by the Engine class until the player quits and the execution returns to main and the return 0 statement.

That was easy. Now, we...

Summary

In this chapter, we introduced the Thomas Was Late game and laid the foundations of understanding as well as the code structure for the rest of the project. It is certainly true that there are a lot of files in the Solution Explorer but, provided we understand the purpose of each, we will find the implementation of the rest of the project quite easy.

In the next chapter, we will learn about two more fundamental C++ topics, inheritance and polymorphism. We will also begin to put them to use by building three classes to represent two playable characters.

FAQ

Here is a question that might be on your mind:

Q) I don't fully understand the structure of the code files. What should I do?

A) It is true that abstraction can make the structure of our code less clear, but the actual code itself becomes so much easier. Instead of cramming everything into the main function like we did in the previous projects, we will split the code up into Input.cpp, Update.cpp, and Draw.cpp. Furthermore, we will use more classes to group together related code as we proceed. Study the Structuring the Thomas Was Late code section again, especially the diagrams.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Beginning C++ Game Programming. - Second Edition
Published in: Oct 2019Publisher: PacktISBN-13: 9781838648572
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton