Book Image

Beginning C++ Game Programming

Book Image

Beginning C++ Game Programming

Overview of this book

This book is all about offering you a fun introduction to the world of game programming, C++, and the OpenGL-powered SFML using three fun, fully-playable games. These games are an addictive frantic two-button tapper, a multi-level zombie survival shooter, and a split-screen multiplayer puzzle-platformer. We will start with the very basics of programming, such as variables, loops, and conditions and you will become more skillful with each game as you move through the key C++ topics, such as OOP (Object-Orientated Programming), C++ pointers, and an introduction to the Standard Template Library. While building these games, you will also learn exciting game programming concepts like particle effects, directional sound (spatialization), OpenGL programmable Shaders, spawning thousands of objects, and more.
Table of Contents (24 chapters)
Beginning C++ Game Programming
Credits
About the Author
About the Reviewer
www.PacktPub.com
Dedication
Preface
17
Before you go...

Setting up the development environment


Now you know a bit more about how we will be making these games, it is time to set up a development environment so we can get coding.

What about Mac and Linux?

The games that we make can be built to run on Windows, Mac and Linux! The code we use will be identical for each. However, each version does need to be compiled and linked on the platform for which it is intended and Visual Studio will not be able to help us with Mac and Linux.

It would be unfair to say that this book is entirely suited for Mac and Linux users, especially complete beginners. Although, I guess, if you are an enthusiastic Mac or Linux user, and you are comfortable with your operating system, the vast majority of the extra challenges you will encounter will be in the initial setup of the development environment, SFML, and the first project.

To this end, I can highly recommend the following tutorials which will hopefully replace the next 10 pages (approximately), up to the section Planning Timber!!!, when this book should again become relevant to all operating systems.

For Linux, read this for an overview: http://www.sfml-dev.org/tutorials/2.0/start-linux.php .

For Linux, read this for step-by-step guidance: http://en.sfml-dev.org/forums/index.php?topic=9808.0 .

On Mac, read this tutorial as well as the linked out articles: http://www.edparrish.net/common/sfml-.osx.html .

Installing Visual Studio Express 2015 on your desktop

Installing Visual Studio can be almost as simple as downloading a file and clicking a few buttons. It will help us, however, if we carefully run through exactly how we do this. For this reason, I will walk through the installation process a step at a time.

The Microsoft Visual Studio site says that you need 5 GB of hard disk space. From experience, however, I would suggest you need at least 10 GB of free space. In addition, these figures are slightly ambiguous. If you are planning to install it on a secondary hard drive, you will still need at least 5 GB on the primary hard drive because no matter where you choose to install Visual Studio, it will need this space too.

Note

To summarize this ambiguous situation: It is essential to have a full 10 GB space on the primary hard disk, if you intend to install Visual Studio to that primary hard disk. On the other hand, make sure you have 5 GB on the primary hard disk as well as 10 GB on the secondary, if you intend to install to a secondary hard disk. Yep, stupid, I know!

  1. The first thing you need is a Microsoft account and the login details. If you have a Hotmail or MSN email address then you already have one. If not, you can sign up for a free one here: https://login.live.com/.

  2. Visit this link: https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx. Click on Visual Studio 2015, then Express 2015 for desktop then the Downloads button. The next screenshot shows the three places to click:

  3. Wait for the short download to complete and then run the downloaded file. Now you just need to follow the on-screen instructions. However, make a note of the folder where you choose to install Visual Studio. If you want to do things exactly the same as me, then create a new folder called Visual Studio 2015 on your preferred hard disk and install to this folder. This whole process could take a while depending on the speed of your Internet connection.

  4. When you see the next screen, click on Launch and enter your Microsoft account login details.

Now we can turn to SFML.

Setting up SFML

This short tutorial will take you through downloading the SFML files that allow us to include the functionality contained in the library. In addition, we will see how to use the SFML DLL files that will enable our compiled object code to run alongside SFML:

  1. Visit this link on the SFML website: http://www.sfml-dev.org/download.php. Click on the button that says Latest stable version as shown next.

  2. By the time you read this guide, the latest version will almost certainly have changed. That doesn't matter as long as you do the next step correctly. We want to download the 32-bit version for Visual C++ 2014. This might sound counter-intuitive because we have just installed Visual Studio 2015 and you probably (most commonly) have a 64-bit PC. The reason we choose this download is because Visual C++ 2014 is part of Visual Studio 2015 (Visual Studio offers more than just C++) and we will be building games in 32-bit so that they run on both 32- and 64- bit machines. To be clear, click the following download:

  3. When the download completes, create a folder at the root of the same drive where you installed Visual Studio and name it SFML. Also create another folder at the root of the drive where you installed Visual Studio and call it Visual Studio Stuff. We will store all kinds of Visual Studio-related things here so Visual Studio Stuff seems like a good name. Just to be clear, here is what my hard drive looks like after this step:

  4. Obviously, the folders you have in between the highlighted three folders in the screenshot will probably be totally different to mine. Now we are ready for all the projects we will soon be making, create a new folder inside Visual Studio Stuff. Name the new folder Projects.

  5. Finally, unzip the SFML download. Do this on your desktop. When unzipping is complete you can delete the zip folder. You will be left with a single folder on your desktop. Its name will reflect the version of SFML that you downloaded. Mine is called SFML-2.3.2-windows-vc14-32-bit. Your file name will likely reflect a more recent version. Double-click this folder to see the contents, then double-click again into the next folder (mine is called SFML-2.3.2). The following screenshot shows what my SFML-2.3.2 folder's contents looks like, when the entire contents have been selected. Yours should look the same.

  6. Copy the entire contents of this folder, as seen in the previous screenshot, and paste/drag all the contents into the SFML folder you created in Step 3. For the rest of the book I will refer to this folder simply as your SFML folder.

Now we are ready to start using C++ and SFML in Visual Studio.

Creating a reusable project template

As setting up a project is a fairly fiddly process, we will create a project and then save it as a Visual Studio template. This will save us quite a significant amount of work each time we start a new game. So, if you find the next tutorial a little tedious, rest assured that you will never need to do this again:

  1. Start Visual Studio and, in the New Project window, click the little drop-down arrow next to Visual C++ to reveal more options, then click Win32, and click Win32 Console Application. You can see all these selections in the next screenshot.

  2. Now, at the bottom of the New Project window, type HelloSFML in the Name: field.

  3. Next, browse to the Visual Studio Stuff\Projects\ folder that we created in the previous tutorial. This will be the location where all our project files will be kept. All templates are based on an actual project. So, we will have a project called HelloSFML but the only thing we will do with it is make a template from it.

  4. When you have completed the steps above, click OK. The next screenshot shows the Application Settings window. Check the box for Console application, and leave the other options as shown below.

  5. Click Finish and Visual Studio will create the new project.

  6. Next, we will add some fairly intricate and important project settings. This is the laborious part but, as we will create a template, we will only need to do this once. What we need to do is tell Visual Studio, or more specifically the code compiler that is part of Visual Studio, where to find a special type of code file from SFML. The special type of file I am referring to is a header file. Header files are the files that define the format of the SFML code. So when we use the SFML code, the compiler knows how to handle it. Note that the header files are distinct from the main source code files and they are contained in files with the .hpp file extension. (All this will become clearer when we eventually start adding our own header files in the second project). In addition, we need to tell Visual Studio where it can find the SFML library files. From the Visual Studio main menu select Project | HelloSFML properties.

  7. In the resulting HelloSFML Property Pages window, perform the following steps, flagged in the next screenshot.

  8. Select All Configurations from the Configuration: drop-down.

  9. Select C/C++ then General from the left-hand menu.

  10. Locate the Additional Include Directories edit box and type the drive letter where your SFML folder is located, followed by \SFML\include. The full path to type, if you located your SFML folder on your D drive, is as shown in the screenshot: D:\SFML\include. Vary your path if you installed SFML to a different drive.

  11. Click Apply to save your configurations so far.

  12. Now, still in the same window, perform these next steps, again flagged in the next screenshot. Select Linker then General.

  13. Find the Additional Library Directories edit box and type the drive letter where your SFML folder is, followed by \SFML\lib. So the full path to type if you located your SFML folder on your D drive is, as shown in the screenshot, D:\SFML\lib. Vary your path if you installed SFML to a different drive.

  14. Click Apply to save your configurations so far.

  15. Finally for this stage, still in the same window, perform these steps, which again refer to the next screenshot. Switch the Configuration: drop-down(1) to Debug as we will be running and testing our games in Debug mode.

  16. Select Linker then Input (2).

  17. Find the Additional Dependencies edit box (3) and click into it on the far left-hand side. Now copy and paste/type the following: sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;sfml-network-d.lib;sfml-audio-d.lib; at the indicated place. Again, be really careful to place the cursor precisely, and dont overwrite any of the text that is already there.

  18. Click OK.

  19. Let's make a template from our HelloSFML project so we never have to do this slightly mind-numbing task again. Creating a reusable project template is really easy. In Visual Studio select File | Export Template.... Then, in the Export Template Wizard window, make sure the Project template option is selected, and then select HelloSFML project for the From which project do you want to create a template option.

  20. Click Next and then Finish.

Phew, that's it! Next time we create a project, I'll show you how to do it from this template. Now let's build Timber!!!