Okay, so you are here. You’ve decided, hey, I want to start making games. Good idea! I certainly encourage you in your mission, but lets face facts. Making games is hard. Really hard. And not to throw unscientific percentages at you, but practically 90% of all those wanting to make games never finish anything. There are always two reasons, lack of discipline, or lack of understanding. You hit a wall, try to find the easiest way around, and then give up for the next project. Or, you get tired of the game you are working on, think of a “better” idea, and then start on that. Games are subjective, tastes change, and along with helping you on the way to programming, I also hope I can give you some discipline to finish your work.
Now, here we are, at the very beginning of it all. Don’t worry, before you know it you’ll be making games in no time. Don’t rush yourself. Believe me, once you know how to program, you’ll miss all the learning (not that you won’t have more to learn). The journey is the most important part, and the most fun.
Some very very basics first. That thing in front of you, that’s a computer (okay, technically a monitor probably, but let’s not be picky). For all you know, it does stuff. You click, you open windows, you download music (hopefully legally), play games, talk to friends. That’s about all it has been so far. Then, you got the brilliant idea to make a game. Well, exactly what is a game? More pointedly, how does anything work on a computer?
As you may know, everything in the computer world is represented in binary, 0 and 1. Meaning, on or off, true or false, or whatever other representation you want. This poses a slight problem to human beings, we cannot figure out the right combination of 0s and 1s to do anything. Imagine putting billions upon billions of 0s and 1s to write a game – it’s impossible! So, “in the beginning”, nice nerd people developed languages. They were crude at first, getting simple things done. These were called low-level languages. They performed basic things, extremely well. They told the computer to store bits into memory, or to get them back out, and so on. Well, that worked for a while, and then things got more complex. Now, we have high-level languages. C++, happens to be one of those languages. So, we use C++ and a bunch of commands and such, and it gets converted into those nice 0s and 1s (please understand, I am being very basic here).
Now, in order to get your C++ code into binary, we use something called a compiler. This is a middle-man program that takes your code, and spits out a program. Click the program just like anything else, and hey, it works! Now, alongside this, is something called a linker. You see, linker and compiler work together, they’re brothers! Linker is nice, it lets you take Joe’s C++ code and combine it with your own code – this then is sent over to compiler. Beyond this, as a beginner, don’t worry about how it works right now. As you progress, and gain experience, you’ll learn more about how these two tools (and many other tools) actually work. But for now, understand code go in to compiler, program come out of compiler.
So what is code exactly? Well, it’s just text (basically). It’s like the text on this page, except it has a different meaning. Take, for example, this math equation:
That can be seen as code. In itself, it doesn’t mean anything. But if given to something like a compiler, it can be a program!
Now, one important aspect is to have the right environment. First, we know, you need a compiler and a linker. And for beginners, you’ll need an IDE (technically, you could code in text editor if you wanted). Now, an IDE, or Integrated Development Environment, is basically a program where you code. But it comes with some nice features, like it comes with a compiler and linker, and sets it up for you! It also gives you a lot of other people’s code; most of which we’ll be using in these tutorials. Why use other people’s code? Because, believe me, if you started with a clean slate, you’d have to code even the ability to print a single pixel to the screen. So, first task! Go download CodeBlocks:
http://www.codeblocks.org
Important note: Download the most stable, and the one with mingw. I’ve had quite a few people download the one without (sigh). Once installed, and opened, you’re ready to continue.
Click File, and then New Project. On the window that opens, click Empty Project and then Go. For the project title, pick what you want. I am putting C++ Basics. For a folder, put whatever. Click Next. Check only the “Create ‘Release’ Configuration”. Click Finish. Now click, File and then New Empty File. On the window that pops up, be sure to add to your project. For the file name, put main.cpp. Copy this code below, and paste it in:
int main() {
std::cout << "Hello World!\n";
return 0;
}
Click Build and then Build and run. If successful, you should see “Hello World!” followed by some other text. If so, congratutations! You just made your first program, sort of.
How lets explain what’s going here. First off at the very top, we have #include. Now would be a good time to explain the differences between two areas of your code. The first area is code to used by your compiler, the second area is your actual code to be made into a program. Anything that has a # before it, is code to be used by the compiler. Meaning, this code never actually makes it into the program. Why is this useful? Well, in this tutorial we have #include, this tells the compiler to include another file. In this case, we’re telling the compiler to go out and grab the file iostream and include it in the program. Another example would be #define.
std::cout << MY_NAME;
Now, wherever we put MY_NAME, it is replaced with “Tim.” This first area is called preprocessor directives. These “direct” the compiler before getting to the actual code. So, to explain the first line again, we’re including the file iostream into our program. The reason why we do that is because the code for std::cout is in there. And we need that.
Next, we have int main() { }. This is called a function, and while we won’t get into functions until later, I need to try to explain them briefly. First, whenever you first run your program, the operating system needs to know where your code begins. That’s what main() is for, that’s where your code begins. So, when you open your program, it starts at the top of main, and goes down line by line. When it reaches the end, it’s done, and your program stops. Now, the { } simply define ownership. Everything within the curly braces { } belongs to main. Secondly, main is also a function. A function is simply a group of code. So, for example, we might have:
}
int Group2() {
}
int main() {
}
We have three functions, each with its own code. But there are some important things about functions. A function may return some data:
return 22;
}
In this case, the function MyAge returns 22. Meaning, when the function is done, it’ll give back the number 22 from wherever it was first called. So, when the operating system opens our program, and our function main returns 0, it’s giving the number 0 back to the operating system. Why do we return 0? Well, lets say something wen’t wrong with your program when loading, in that case we can return 1, and if everything is okay, we can return 0. Again, this is a very very basic explanation of functions, and there’s a lot more to them. Just understand that when your program first opens that it starts at the top of main.
Next, we have the actual magic, std::cout. Now, this is really hard to explain to beginners, but for all purposes std::cout is simply a way for you to print text. You can use the << to separate text as well. Like:
Here’s the things I want to get across though. Notice the ; at the end? This is important, this is your period, so to speak. It signals the end of your particular command/code. Notice the code below:
std::cout << "Test";
std::cout << "Test";
Each command is separated not by a newline, but by a semi-colon. For beginners this is important, don’t forget your semi-colons! (Notice the one on return too). So, what’s that \n funny thing? That’s a newline character, which basically says, hey, move down a line. Like pressing Enter in a text editor.
Well, that’s all for now. This tutorial was very basic, and was intended that way. Future tutorials will tackle harder subjects at an increasing rate.
Note: If your program opens and closes suddenly, run it from the command prompt. Start -> Run -> cmd. Navigate to your folder, and then type in your program. Hit enter!
C++ Basics – Tutorial Files:
Win32: Zip, Rar
Did you like this tutorial/blog post? Feel free to donate to keep more comin', and have more contests.
Thanks guys. I’ll get to the next one asap.
WOot!
Awesome tutorial
I did this on Ubuntu hehe.
Please do continue with this tutorial, I want to learn and you’re a great teacher!
Thanks, I used to programming in C long years ago when i was an student. I would like to see more complex material in the next part, i hope you post it soon.
(pd: sorry if i have errors in my english..)
You’re welcome Clem.
Thank you Tim.
Even though the idea was to demonstrate what #define is, as these are the basics of C++, by starting out with telling them what define is before using std::string and using it as a string, it’ll make the user reach back for the #define when things don’t work (as it is the first thing they’ve learned). But macro’s usually should be avoided in C++, as they can be replaced by functions (sure, the macro is replaced by the code it represents before compiling and there is no overhead at all, but when you’re not writing a needs-to-be-extrmely-fast application or an OS, the overhead isn’t that important).
So I’d have to agree with Revan.
Revan,
The premise of this tutorial was to introduce the very basics without clouding it with things that aren’t necessary. As people progress in programming they will learn the “ethics” of programming, but for the purposes of this tutorial, I did not want to confuse them further with more things like string and std. It would be one heck of a time trying to explain how that class even works. Plus, the idea was to show how #define works, not necessarily how to use strings. I understand your concern, but just hold out, I’ll teach them in future tutorials what to do and what not to do. Thanks for the feedback!
Uhm, only a little thing…
you use #define to set a macro, with the value "Tym"…
is not better use for esample or a std::string, or a const std::string?
the #define is better to not use in c++, because not all times wath is a good thing in c [#define], is good also in c++, the compiler had no control on it, and is so bad this thing ^^
you have a standard, use it
for the other things, is a good tutorial ^^