Chapter 12. Abstraction and Code Management – Making Better Use of OOP

In this chapter, we will take a first look at the final project of the book. The project will have advanced features such as directional sound, which comes out of the speakers 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 16Extending SFML Classes, 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 main focus will be getting the project started—in particular, exploring how the code will be structured to make better use of OOP. The following topics will be covered:

  • An introduction to the final project, Thomas Was Late, including the gameplay features and project assets
  • A detailed discussion of how we will improve the structure of the code compared to previous projects
  • Coding the Thomas Was Late game engine
  • Implementing split screen functionality

The Thomas Was Late game

At this point, if you haven't already, I would suggest you go and watch a video of Thomas Was Alone at http://store.steampowered.com/app/220780/. Notice the really 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 race against the clock, hence 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 gameplay features:

  • A clock that count downs from a time appropriate to the challenge of the level.
  • Fire pits that emit a roar relative to the position of the player and re-spawn the player at the start if they fall in. Water pits have the same effect but without the directional sound effects.
  • Cooperative gameplay—both the players will have to get their characters to the goal within the allotted time. They will frequently need to work together, for example the shorter, lower-jumping Bob will need to stand on his friend's (Thomas's) head.
  • The player will have the option of switching between full and split screen, so he can attempt to control both characters himself.
  • Each level will be designed in and loaded from a text file. This will make it really easy to design varied and numerous levels.

Take a look at this annotated screenshot of the game to see some of the features in action and the components/assets that make up the game:

Features of Thomas Was Late

Let's look at each of those features and describe a few more:

  • The screenshot shows a simple HUD that details the level number and the number of seconds remaining until the players fail and have to restart the level.
  • You can also clearly see the split screen co-op in action. Remember that this is optional. A single player can take on the game, fullscreen, while switching the camera focus between Thomas and Bob.
  • It is not very clear in the screenshot (especially in print), but when a character dies, he will explode in a starburst/firework-like particle effect.
  • The water and fire tiles can be strategically placed to make the level fun and force cooperation between the characters. More on this in Chapter 14, Building Playable Levels and Collision Detection.
  • Notice Thomas and Bob—not only are they different in heights, but they also have significantly different jumping abilities. This means that Bob is dependent upon Thomas for big jumps, and levels can be designed to force Thomas to take a route that avoids him banging his head.
  • In addition, the fire tiles will emit a roaring sound. These will be relative to the position of Thomas. Not only will they be directional and come from either the left or right speaker, they will also get louder and quieter as Thomas moves closer or further away from the source.
  • Finally, in the annotated screenshot, you can see the background. If you compare how that looks to the background.png file (shown later in this chapter), you will see it is quite different. We will use OpenGL shader effects in Chapter 16Extending SFML Classes, Particle Systems, and Shaders, to achieve the moving—almost bubbling—effect in the background.

All those features warrant a few more screenshots so we can keep the finished product in mind as we write the C++ code.

The following screenshot shows Thomas and Bob arriving at a fire pit that Bob has no chance of jumping over without help:

Features of Thomas Was Late

The following screenshot shows Bob and Thomas collaborating to clear a precarious jump:

Features of Thomas Was Late

The following screenshot shows how we can design puzzles where a "leap of faith" is required in order to reach the goal:

Features of Thomas Was Late

The following screenshot demonstrates how we can design oppressive cave systems of almost any size. We can also devise levels where Bob and Thomas are forced to split up and go different routes:

Features of Thomas Was Late

Creating a project from the template

Creating the Thomas Was Late project is the same as the other two projects. Just follow these straightforward steps in Visual Studio:

  1. Select File | New Project from the main menu.
  2. Make sure that Visual C++ is selected in the left-hand menu and then select HelloSFML from the list of presented options. The following screenshot should make this clear:

    Creating a project from the template

  3. In the Name: field, type TWL and also make sure that the Create directory for solution option is checked. Now click OK.
  4. Now we need to copy the SFML .dll files into the main project directory. My main project directory is D:Visual Studio StuffProjects TWLTWL. This folder was created by Visual Studio in the previous step. If you put your Projects folder somewhere else, perform this step there instead. The files we need to copy into the project folder are located in your SFMLin folder. Open a window for each of the two locations and highlight the required .dll files.
  5. Now copy and paste the highlighted files into the project.

The project is now set up and ready to go.

The project assets

The assets in this project are even more numerous and diverse than the Zombie Arena game. As usual, the assets include a font for the writing on the screen, sound effects for different actions such as jumping, reaching the goal, or the distant roar of fire, and, of course, graphics for Thomas and Bob as well as a sprite sheet for all the background tiles.

All the assets required for the game are included in the download bundle. They can be found in the Chapter 12/graphics and Chapter 12/sound folders, respectively.

The font that is required has not been supplied. This is because I wanted to avoid any possible ambiguity regarding the license. This will not cause a problem though, as I will show you exactly where and how to choose and download fonts for yourself.

Although I will provide either the assets themselves or information on where to get them, you might like to create and acquire them for yourself.

In addition to the graphics, sound, and fonts that we have come to expect, this game has two new asset types. They are level design files and GLSL shader programs. Let's find out about each of them next.

Game level designs

Levels are all created in a text file. By using the numbers 0 through 3, we can build level designs to challenge players. All the level designs are in the levels folder in the same directory as the other assets. Feel free to take a peek at one now, but we will look at them in detail in Chapter 14, Building Playable Levels and Collision Detection.

In addition to these level design assets, we have a special type of graphical asset, called shaders.

GLSL Shaders

Shaders are programs written in GLSL (Graphics Library Shading Language). Don't worry about having to learn another language, as we don't need to get too in-depth to take advantage of shaders. Shaders are special, as they are entire programs, separate from our C++ code, which are executed by the GPU each and every frame. In fact, some of these shader programs are run every frame, for every pixel! We will find out more details in Chapter 16Extending SFML Classes, Particle Systems, and Shaders. If you can't wait that long, take a look at the files in the Chapter 12/shaders folder of the download bundle.

The graphical assets close-up

The graphical assets make up the parts of the scene of our game. Take a look at the graphical assets and it should be clear where in our game they will be used:

The graphical assets close-up

If the tiles on the tiles_sheet graphic look a little different to the screenshots of the game, this is because they are partly transparent and the background showing through changes them a little. If the background graphic looks totally different to the actual background in the game screenshots, that is because the shader programs we will write will manipulate each and every pixel, each and every frame, to create a kind of "molten" effect.

The sound assets close-up

The sound files are all .wav format. These are files that contain the sound effects we will play at certain events throughout the game. They are as follows:

  • fallinfire.wav: A sound that will be played when the player's head goes into fire and the player has no chance of escape.
  • fallinwater.wav: Water has the same end effect as fire: death. This sound effect notifies the player they need to start from the beginning of the level.
  • fire1.wav: This sound effect is recorded in mono. It will be played at different volumes based on the player's distance from fire tiles, and from different speakers based on whether the player is to the left or the right of the fire tile. Clearly, we will need to learn a few more tricks to implement this functionality.
  • jump.wav: A pleasing (slightly predictable) whooping sound for when the player jumps.
  • reachgoal.wav: A pleasing victory sound for when the player (or players) gets both characters (Thomas and Bob) to the goal tile.

The sound effects are very straightforward and you can easily create your own. If you intend to replace the fire1.wav file, be sure to save your sound in a mono (not stereo) format. The reasons for this will be explained in Chapter 15, Sound Spacialization and HUD.

Adding the assets to the project

Once you have decided which assets you will use, it is time to add them to the project. The following instructions will assume you are using all the assets supplied in the book's download bundle.

Where you are using your own, simply replace the appropriate sound or graphic file with your chosen file, using exactly the same filename:

  1. Browse to the Visual D:Visual Studio StuffProjectsTWLTWL directory.
  2. Create five new folders within this folder and name them as graphics, sound, fonts, shaders, and levels.
  3. From the download bundle, copy the entire contents of Chapter 12/graphics into the D:Visual Studio StuffProjectsTWLTWLgraphics folder.
  4. From the download bundle, copy the entire contents of Chapter 12/sound into the D:Visual Studio StuffProjectsTWLTWLsound folder.
  5. Now visit http://www.dafont.com/roboto.font in your web browser and download the Roboto Light font.
  6. Extract the contents of the zipped download and add the Roboto-Light.ttf file to the D:Visual Studio StuffProjectsTWLTWLfonts folder.
  7. From the download bundle, copy the entire contents of Chapter 12/levels into the D:Visual Studio StuffProjectsTWLTWLlevels folder.
  8. From the download bundle, copy the entire contents of Chapter 12/shaders into the D:Visual Studio StuffProjectsTWLTWLshaders folder.

Now that we have a new project, along with all the assets we will need for the entire project, we can talk about how we will structure the game engine code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.7.174