Navigation: Introduction to Programming

Tutorial 2 - Writing your first program


Overview

Note: These tutorials are about developing games for Windows. However once you have learned the C++ language you can use c++ with Unreal engine for example to develop games for other platforms such as Android and Mac.

Let's cut to the chase. This tutorial is about setting everything up so when the time comes and you do more advanced 3d programming you will have what you require for that. Here we will focus on writing a first C++ program. It won't look like much but by the end of the tutorial you will have a standalone exe and a basic understanding of how to code.

The reason we set things up first is because you will learn the C++ language better if you can try out what you have learned in future tutorials.

Introducing Visual Studio Express

Visual Studio(Express Edition) is a free development environment developed by Microsoft. You can download it from the Microsoft website. It incorporates the framework you need to create cool 2d or 3d games. You do not need to pay a penny to create high-quality 3d games with Visual Studio Express. It comes with a C++ compiler and an editing environment IDE(Integrated Development Environment) with good code editing features that makes editing code much easier than using a plain text-editor like Notepad.
I will be using Visual Studio 2008 for the tutorials. Feel free to use a newer edition if you want, it will not prevent anything covered in the tutorials, programs you write from running. I just prefer to use Visual C++ 2008.
For the remainder of the C++ tutorials on this website, we will be using Visual Studio to create our applications. For the topics on 3D graphics we will also be using Visual Studio along with DirectX.

Installing Visual Studio

Once you have downloaded it run the setup, choose Visual Studio C++ from the menu if you downloaded the bundle for Visual Basic, C# etc. Follow the setup wizard instructions and choose default or extra features.

Your First Program

Start Visual Studio.
For our first application we will create a Console project. We use Console Project because it gives us a nice and plain setup for a first program. Then once you understand C++ a bit better we will want to create a Win32 project because that will let us create a standard window.
Go to File > New > Project and Click Win32.
Select Win32 Console Application.
Enter a name for the project, For example "MyFirstProject". (See Figure 1.)
Click OK.
Click Next.
Make sure Empty Project is checked.
Click Finish.


Figure 1.

Now you have created an empty project.

Writing Your First C++ Program.

I will explain the different parts of a c++ program and discuss the c++ language and how to write code in the next tutorial "Tutorial 3". There you will learn about what headers and source files are and how they work. But in this tutorial you will just write a few lines of c++ code without necessarily understanding it.

For now let's just get a program working.

Right click Source Files located on the left-side menu.
Click Add > New Item.
In the screen that appears, Select C++ File (.cpp)
This will be the file that contains the main code of our game so type main in the Name field and click Add.
A text editor appears where you can edit the main.cpp file.
Type the following code into the text area:
int main()
{
	return 0;
}

Now the program is complete.
Click the green arrow at the top of the screen to build(compile) and run the application.
If a prompt appears asking you if you would like to build click Yes.

You might see a black console window flash up and disappear, that is because the program doesn't actually do anything at the moment, it simply runs and terminates.

The Executable

Now that you have compiled your project, the compiler has created a standalone executable for you.

The executable .exe file is located in the folder where your project is located on your computer. For example Documents/Visual Studio 2008/Projects/MyFirstProject/Debug/MyFirstProject.exe

You can run it like any other executable by double-clicking on it.

Conclusion

You have created a bare-bone C++ application. Note that you have created a Debug version of the application at this point. Notice at the top of the Visual Studio editor there is a drop-down menu that has Debug displayed in it (See Figure 2.). A debug build is a build that makes it possible to debug an application thourouly, which I will describe in a future tutorial. And a release build is a build that you want to give your customers or users. A release build typically makes your application highly optimized and it will run much faster but if you haven't tested it very well a release version of your program will be more error prone because fewer error checks are made.


Figure 2.
Next Tutorial >>