C++ Game Programming
Basic C++ Lessons
This set of lessons will give you a simple introduction to programming in the C++ language.
Allegro Lessons
This set of lessons will demonstrate how to use the Allegro graphics library to create video games.
Game Source Code
Here you will find source code to several games so that you can see the code in action.
Recommended Books
Here is a list of books which should help you with C++

Why C++?
C++ is a simple yet powerful Object Oriented programming language. It has almost all the same uses as C, but brings a few new features to the table, along with simpler code. One of the new features is a data type called class, which is used to create objects. Objects will allow you to write your code in a more organized fashion, instead of having data scattered about. This makes it much easier for the different functions throughout your program to access the data they need in order to get the job done. This way the amount of code you have to write is reduced significantly, thus making your program easier to maintain.

Where to start
First thing you will need is a compiler. A compiler basically takes the code you write and translates it into a form the computer will understand so that it can be executed. I used Dev-C++ for the examples you will find throughout this site, although the code should work with most compilers. Dev-C++ can be found on Bloodshed's site.

Lets Begin
Now that you have your compiler, lets move on to actually learning how to code C++. If you are using Dev-C++, the first thing you are going to want to do once you open it is select "File" > "New" > "Project. Then select "Console Application" and name your project. Once finished you will be presented with a small program, simply erase this so we can begin! Our first program will simply send "Hello World!" to the screen, here is the code:


//My first program

#include <iostream>

using namespace std;

int main(){


	cout << "Hello World!\n";

	system("Pause");

}


Now lets break this down. The first line "//My first program" is a comment, the compiler will ignore this line completely. To add comments to your program, simply begin the line with "//" and anything after that will be ignored.

The first line of actuall code tells the compiler that we want to include "iostream" in our program. iostream is part of the standard library, all C++ compilers will understand any command included in any part of the standard library. iostream contains the basic I/O functions used to write to the screen and recieve input from the user.

The next line "using namespace std;" defines what namespace we will be using, in this case std. A namespace is basically a list of variable and function names, in order to not get standard library names mixed with others, the standard library creates the namespace std, so any time you want to use a command from the standard library you must include this line. The semicolon at the end of the line is needed for almost every line in C++, this tells the compiler that it has reached the end of that line.

"int main(){" this is a function decleration. A function is a block of code which begins and ends with a pair of curly brackets " { } ". This way we have a way of referencing this block of code in case if we want the program to run the same code multipule times. Functions also help keep the code looking clean. "int" is the data type the function returns, we will discuss data types later. "main" is the name of this function, every C++ program must have a main() function, this is where the program will begin to execute. If you want to pass a variable to a function, you would place the type of variable and specify a name for it between the parenthesis, for example "int myFunction( int number)". The curly braket shows where the function begins.

"cout << "Hello World!\n";" This line displays the text "Hello World!" on the screen. cout takes the text using "<<" which is known as an insertion operator. Think of it as a funnel, we want the data, in this case "Hello World!", to be "funneled" into cout. The "\n" means new line, its like pressing the enter key while typing in a text editor.

"system("Pause");" system() is used to send commands to the operating system, in this case we are sending "pause" which is a DOS command which pauses the screen until the user presses any key. I included this because once the program runs out of code to execute, it will close the window and you will not get a chance to see the results.

Ok now lets see it in action, type the code into Dev-C++, do not copy and paste it. You can copy and paste if you want, it will work just fine, but it will help you memorize the code quicker if you type it yourself. Once done, save it and press the compile button, once it finished press the run button. You should be presented with a DOS prompt which will give you this output:

Hello World!
Press any key to continue...

Too easy isn't it? Lets move on to data types.

Data types
So far you've seen how to display whatever you put into your program, now lets see how to get input from the user. Before doing this we need somewhere to store the data the user gives us, this is where the different data types come in. Here is a list of the basics data types:


int: used to store integers (whole numbers, no decimals/fractions)
long: used to store larger integers
float: used to store numbers with multiple decimal places
double: also used for numbers with multiple decimal places, but has double the space in memory as a float (more decimal places)
char: used to store a single character

Ok now lets see these in action:


#include <iostream>

using namespace std;

int a;

int main(){

	int b;
	int c;

	cout << "Enter a number: ";
	cin >> a;

	cout << "Enter a second number: ";
	cin >> b;

	c = a+b;

	cout << "The sum is: " << c << "\n";

	system("pause");

}

Here I declared three integers, a, b, and c. Notice how "a" is outside of main()? In this case, a would be known as a global variable, all functions can access it freely, but since b and c were declared inside of main() it is only accessible to main().

The next new line would be "cin << a+b;". Here we see the insertion operator again, except this time its pointing in the other direction. Since cin is the command for input, we want to "funnel" the data from the function to the variable.

After collecting the information from the user we use it to give c a value. "c = a+b;" give c the value of the sum of a and b, you can also use - for subtraction, * for multiplication, and / for division.

Finally we display the value of c, notice how there are two insertion operators in the final output? This allows us to display text and variables in one line instead of having to write "cout" several times. Notice how text is written in between quotes and variables are without quotes. If you wanted to assign a variable a number you would not use quotes either ( int a = 10; ).

Google