Tim's C++ Guide

Setting up

To program in C++, you will need a compiler (which converts your code into computer-language that can be run by the PC) and a text editor (like Notepad).

I use Dev-C++, which has both a compiler and a text editor. It's also completely free, which is nice. Go to this page, scroll down to Downloads, then download the Dev-C++ version that says "with Mingw/GCC". Click the download link ("SourceForge") below that, then click any of the icons with 0s and 1s on them (right hand side of the page).

To program games, it's useful to have an add-on called Allegro, which makes it much easier to make games. To get it, go to allegro.cc and click the "Files" link. Scroll down to the heading "Binary" and click the link "MinGW32" (It's about 6MB). Download that file into the folder where you installed Dev-C++ (probably c:\dev-cpp). Then, unzip it with a program like Winzip.

That's it :)


Getting started

Now it's all set up, here's an example program to test it's all working.

Load up Dev-C++, and select "New Project" from the File menu. In Dev-C++, usually each program you make will have its own project.

Type in a name for the project, e.g. "Test", and then click "Windows Application". Then, click OK. It will ask you where you want to save the project, so choose a filename and save it somewhere. (It might be a good idea to make a folder inside the Dev-C++ folder for you to put all your programs in. I've got one called "progs" on my computer.)

It will make a file called "main.cpp" with a lot of text in it. You don't need this, so just close it, don't save it or anything.

Now, create a new file by clicking "New Source File" from the File menu. Click "Yes" that you want to add the file to the project. You should now have a blank page in front of you. Now, here's an example program which you can copy into the Dev-C++ page: By the way, C++ is case-sensitive, so if you type it in yourself, make sure you get the capitalisation of everything correct.

#include "allegro.h"
int main()
{
allegro_init();
allegro_message("Hello World!");
}
END_OF_MAIN();


Ok - here's what each line does:
#include "allegro.h" - The program needs to use items from the Allegro library of functions, and this line of code tells the compiler this, so that the program is able to use functions from Allegro

int main() - In C++, code is divided into units called functions. For example, one function might draw a graphic onto the screen, or work out which way to move a character in a game depending on which keys are pressed. Functions can "call" (load) each other, so for example one function could call a sound-playing function or a function for drawing graphics, depending on which keys the user presses. main() is the name of the main function which is called by the operating system (e.g. Windows, Linux) when the program starts. All other functions are called from main(), or from functions called by main().

{ - Curly brackets enclose the contents of a function, so that the compiler knows where one ends and the next begins.

allegro_init(); - This is a statement (a command) which calls the function allegro_init(), which sets up Allegro. Every Allegro program has to have this before any other Allegro functions are called. Also, all statements end with a semicolon.

allegro_message("Hello World!"); - This tells the program to put a message-box on the screen saying "Hello World!".

END_OF_MAIN(); - This just tells the compiler where the end of your main() function is, because Allegro needs to know.

Now, to compile this program, you'll first need to change a setting. To make the compiler put the Allegro functions into your program, you need to right-click on the name of your project in the "Project" window on the left-hand side of the screen. Click "Project Options". Click the "Parameters" tab. Go to the "Linker" box, and type in:
-lalleg
Now, press OK, and then press F9 to compile and run your program. If it works, it should display "Hello World!" on the screen.

This is only a very short introduction though: there is a lot more to C++ and Allegro than messageboxes :)
Hosted by www.Geocities.ws

1