3D Game Programming in C++,Part 1: Getting Started
this tutorial is under construction and intended for proofreaders only for now.
Welcome to the first article in the 3D Game Programming in C++ tutorial series! These tutorials are designed to take you from zero to a decent entry level in a somewhat smooth fashion. We start at the absolute basics: all that you need to get started is a decent laptop or PC, a fair bit of time, and a bit of dedication.
From the absolute basics (i.e., this first article) we will rapidly move to 3D graphics (in the second article!), without making things too complex – but you’ll have to trust us on that.
Of course, we hope you don’t just read these articles. Most of them have things to try out, and at the end of each article you will find a brief ‘assignment’. If you take the interactive tour, each tutorial is supposed to take you between two and four hours. Aim to complete a minimum of two tutorials per week. That means that you have an extra workload of 4-8 hours, and rapid progress towards the goal: getting yourself acquainted with C++ and game programming.
About the Authors
This course was originally created for the Games program at the Breda University of Applied Sciences (known as IGAD at the previously named NHTV); see https://www.buas.nl/games.
It was recently updated for the students of the Utrecht University as well; see https://www.uu.nl/en/organisation/faculty-of-science/education/bachelors-programmes. Even more recently it was converted from 2D to 3D.
This tutorial was written by Jacco Bikker, Brian Beuken, Nils Deslé, Dino Dini, Carlos Bott and Robby Grigg. It was updated for Visual Studio 2019 by Robbie Grigg and Phil de Groot and published on 3DGEP by Jeremiah van Oosten. The 3D version uses a voxel engine developed by Jacco Bikker: more on that in the second article.
Getting the Stuff You Need
For the purpose of these tutorials, I will assume you develop your games on PCs. The preferred development environment is Microsoft’s Visual Studio 2019. A free version (the Community Edition) of this software is available from Microsoft, via https://www.visualstudio.com/downloads. Despite being free, it has everything you need, which is really nice.
- A PC or laptop. Pretty much any CPU will do, but for the 3D engine, your GPU needs at least 2GB of RAM. For reference: an NVIDIA 940M is fine; this is a 2015 GPU.
- Windows 10 (64-bit, which is standard these days).
- Visual Studio 2019 Community Edition (Professional and Enterprise also welcome but not required at all), installed with default settings.
Starting Visual Studio 2019
When you start Visual Studio 2019 for the first time you are greeted with this window:
Selecting not now, maybe later is luckily a perfectly viable option, so let’s go for that (no need to leave any more personal information with a tech giant than strictly necessary, right?). In the next window, you get to choose a theme. As the window says, you can always change this later, so let’s go with dark. Now we finally get into the Integrated Development Environment (IDE).
Create a New Program
Now that you have Visual Studio up and running, select the 4th option Create a new project as pictured:
You then need to select the type of project. Because we are building a very simple project then select Empty project as you can see here:
You now need to change a few things in the following dialog – this is all about where you place your files.
Set the Project name to
Getting Started, or something else, if you’re feeling adventurous.
Set the Location to
C:\my_projects (or whichever folder you keep all of your programming projects).
It is not recommended to keep your projects in the Desktop folder (
C:\Users\[username]\Desktop) folder or in your Documents (
C:\Users\[username]\Documents) folder because these folders are indexed by the Microsoft indexing services to improve search performance. The indexing services will have a negative impact on the performance of the build process.
Set the Solution name to
C++ Fast Track for Games Programming. Then, click the Create button to create your new project. You should see something similar to the screenshot below:
This section is completely optional but it is our preference to tidy up the IDE to make room to view the C++ source code. Optionally, you can skip ahead to the Hello World section below.
As you can see, there is a lot of stuff on the screen. Let’s make some room:
- Click on Team Explorer, then close it to get rid of it. The Team Explorer can also be hidden by selecting View > Team Explorer from the main menu.
- Do the same to Toolbox and Properties.
- Drag the Solution Explorer to the left of the screen.
- If you don’t like toolbars and their buttons then on the main menu, click on View, go to Toolbars, and deselect Standard.
You should now have something like this:
I often see people work on the area of a stamp due to all the toolbars… This layout prevents that. We didn’t remove anything that we need (apart from one thing), and we assumed control of the software in the process, which is a good habit.
The one thing we are missing is two drop-down boxes (OK, so two things). We can store these next to the menu bar, so they don’t need their own toolbar.
In the main menu, select Tools > Customize and select the Commands tab. Now click on Add Command, select the Build category, and choose the Solution Configurations command. This adds the drop-down box to the left of your menu bar; click the Move Down button a few times, until it is all the way to the right. Repeat this for the Solution Platforms command.
The top of the screen now looks like this:
The IDE is as tidy as it gets, for now.
Solution Explorer Tidy-Up
Your Solution Explorer should currently look like this:
Now, let’s delete some things we don’t need. Select all three folders (Header files, Resource files and Source files and delete them.
From the main menu, select Tool > Options and expand Text Editor > C/C++ > Advanced option or search for “Disable External Dependencies Folder” in the Search Options (Ctrl+E). Set the “Disable External Dependencies Folder” option to true.
Your solution explorer should now look something like this:
Before we go on:
This is the first and only article in these series that is this boring. Starting with part 2, you will be coding, not downloading + installing + adjusting settings. Promise.
That being said, let’s get coding!
Now it’s finally time to add a C++ source file to play with. Right-click on your project Getting Started in the Solution Explorer and select Add > New Item.
In the Add New Item dialog that appears, select C++ File (.cpp), and name it
main.cpp (or anything else really, as long as it ends with
.cpp). Then click the Add button.
The new file is immediately opened, and we’re ready for some coding.
A tutorial on C++ is not complete without a ‘hello world’ example, so here we go. Enter the following program:
printf( "Hello world!\n" );
You can start the program by hitting F5. Visual Studio will ask you if you want to build the program; you want this so click yes.
You should see a console window with the text
Hello world! printed in it.
Press Enter (while the console window has the keyboard focus) to close the console window.
When you go to
D:\Projects\C++ Fast Track for Games Programming (or wherever you saved your Visual Studio solution files) you will see that Visual Studio created a large amount of files for you. The most interesting one is in the Debug folder, and it’s called
Getting Started.exe. When you start this application, you will see that you created it yourself.
Apart from this file, we have:
C++ Fast Track for Games Programming.sln: your solution file, which points to
Getting Started.vcxproj: your project file, which points to
main.cpp: the source code file.
A project can have multiple source files, and a solution can have multiple projects. Other files:
Getting Started.vcxproj.filters: stores the references to all of the source files in your project;
opendb: some internal information for Visual Studio;
- various other files in the Debug folder:
Quite confusing perhaps, but all that matters right now is the
.cpp file in
Projects\C++ Fast Track for Games Programming\Getting Started folder and the executable that it was built from it.
If you do not see the
.vs folder as shown in the screenshot above, make sure you you select “Show hidden files, folders, or drives” in File Explorer Options in windows control panel.
To change this setting, open the Windows control panel and go to File Explorer Options, then the select the View tab, and select the Show hidden files, folders, and drives radio button.
If you don’t see file extensions (
.h) in the File Explorer, make sure the Hide extensions for known file types is not checked as shown in the screenshot below.
As promised, every article in this series will end with a small practical assignment. We recommend that you complete this assignment. Once you have done so, you may continue with the next part.
So, here’s your task:
Create an executable that prints your name 10 times, then waits for a key, then prints Coding is awesome! 10 times, then waits for a key again, then exits.
In the next article, we leave the boring world of flat text, and move on to 3D graphics. For that, we will use a ‘template’, or, in case of a somewhat more elaborate template: an engine.
Articles in this series:
- Variables and Functions
- Sprites and Loops
- Floating Point Numbers
- Memory, Addresses and Pointers
- Tile Maps
- Data Structures
- Fixed Point Math
- File I/O