View Full Version : C++ Coding Primer #1

03-31-2002, 08:01 AM
This is for all of you who want to use the SDK to develop mods, but don't know C++. The "virtual machines" in the q3 engine, that is, the gameplay part, are coded in C++ then compiled. I'll be posting C++ tutorials whenever, they will also be up on Cog Warehouse whenever I put that back online, as we are moving to support JO as well. This C++ primer is not a good one for a practical application of C++, its purpose is to familarize you with the types of C++ coding found in the SDK, so I will skip over some things.

The translation from C++ can be broken into three steps: preprocessing, compilation, and linking. Preprocessing is a confusing term, so I will explain it. "Pre" demonstrated that it is before something, and "processing" relates to the processing of data. Basically, the preprocessor is a bit of aid to the programmer. There are standard libraries of C++ code for most basic tasks. It would be very tedious for programmers to have to do all of these tasks manually. Also, there may be certain libraries of code a programmer wants to include that they have written. In most cases, the code would have to be pasted directly into the top of the C++ source file in which it's being used. But say this code was used in many projects, and had to be updated due to a large bug? If it were all located in one file, there would not be such a problem. We can use the preprocessor to do that for us, as well as many other things. For example, iostream.h is a very useful code library, allowing for the input and output of text.

#include <iostream.h>
// this is a comment
/* this is a comment as well,
but it can span multiple lines and must
be closed like this: */

// don't worry about this code below, it's just making use of iostream.h, you can ignore it.
int main(int argc, char **argv)
cout << "Hello, world!" << endl;
return 0;

Note that #include can use the greater than and less than signs to enclose the file being included. That's relative to the compiler's include directory, for example, "C:\program files\microsoft visual studio\vc98\include\". In order to include files relative to the directory of the source code file, use quotes instead:

#include "somecode.h"

I'm aware that this looks quite confusing. I kind of got off track with the definition of preprocessing, so I'll explain compilation quickly. Compilation is the process of individually turning each source code file into a language the "linker" can understand, and places the results in "object files". The linker (in the linking step) takes all these object files, and links them together to form the final executable. There is another important preprocessor directive, #define. It takes two "arguments". Here is an example:

#define STUPID 200

So now, the preprocessor would run through the program, and anywhere STUPID was found being used as an argument, a value to which a variable was equal, etc, it would be replaced with 200. Do not confuse #defines with variables. They are not the same thing, #defines are done before compilation.

Now on to basic syntax. C++ supports many things. First, let's look at the anatomy of a function. This is only a code snippet, as are most things I will show.

// function to return sum of num1 and num2
int AddNumbers(int num1, int num2)
int temp;
temp = num1 + num2;
return temp;

So, let's see. The first thing we see is "int". This is a datatype, "int" is short for integer. A datatype is a predefined type of variable. An int can hold a value from -32768 to 32768. Most functions "return" a value, and the datatype here specifies the type of value the function will return. Next, we see "AddNumber" followed by parentheses and stuff. The "AddNumber" bit is the name of the function. Directly after, there's "(int num1, int num2)". More ints! Enclosed in the parentheses are the "parameters", info which is passed to the function. In this case, "num1" and "num2" are passed to the function, and they are both ints. Note that C++ is not very sensitive to whitespace (spaces, tabs, newlines), so the first line could look like this:

int AddNumbers
( int num1,
int num2


But that's ugly. Please, don't code like that, a good programmer uses good formatting for his or her code. On the next line is a bracket. This bracket shows that the contents of the function are following. For those of you familar with Pascal or Delphi, it's equivalent to "Do" and "Begin". Next is the actual code. A variable is declared, an int named temp. This variable is declared inside the function, so unless it is passed somehow, it cannot be accessed, it's private. temp is then defined to be equal to num1 plus num2. I assume all of you understand what that's doing. That's the actual adding. Variable assignment is done with the = sign. Next, temp is returned. This is somewhat difficult to understand. Functions can be called in two ways:

AddNumbers(5, 3); // discarding the result
int sum;
sum = AddNumbers(5, 3); // using the result

The result is what's returned using "return". Think of the value returned replacing the function, so it would actually read:

sum = 8;

Then there is a closing bracked, showing that the block of code, the function, is closed. You may notice that I've been using semicolons, I'll explain that right after this. So, now we have a basic format for a function:

ReturnType FunctionName(ParamType1, Param1, ParamType2, Param2, etc)
Return value;


Now, onto the semicolons. In some other programming languages, the end of a line of code is signaled with a new line. This sounds easy enough, but whitespace is sometimes nice to break up a function. Therefore, semicolons are used in C++ to show that a statement is over.

What do we know?
Files can be "included" with the #include preprocessor directive. This is the equivalent of copying and pasting the contents. #define's are not variables. Datatypes define what type of data a variable can hold. Most functions return a value.

Any questions, comments, or suggestions? I'll fix anything I find to be unclear. I'll put up some more soon.

[This message has been edited by lordvader (edited March 31, 2002).]

[This message has been edited by lordvader (edited March 31, 2002).]

03-31-2002, 12:29 PM
Also, as a note, there definately is practicality in learning this, I hear many people talking of all the nice mods they'd like to make, you absolutely need to know C++ to do modding.

Cool Matty
03-31-2002, 03:06 PM
<font face="Verdana, Arial" size="2">Originally posted by lordvader:

int AddNumbers
( int num1,
int num2


But that's ugly. Please, don't code like that, a good programmer uses good formatting for his or her code. </font>

But that's how I always code! http://forums.massassi.net/html/wink.gif

"The future is not determined by a throw of the dice, but is determined by the conscious decisions of you and me."
I am addicted to ellipses!!! AHHH!!! ...

03-31-2002, 06:59 PM
I don't have Q3 installed anymore, so when the SDK comes out, I'll be sure to thank you.

03-31-2002, 07:59 PM
what programs will we need to make mods? do we have to compile anything? In JK/MotS you only had to gob/goo the files, but to make modfications to...weapons for example, would we have to compile? it isn't as hard as i thought it would be, thanx! It helped alot!
ps. in someways it is similar to cogging and in others it totally different!

03-31-2002, 08:15 PM
Yes, you will have to compile. I recommend you get Microsoft Visual C++. COG's syntax was based upon C, so yes, it is very similar.

03-31-2002, 08:30 PM
wow random, i've just found a copy of Microsoft Visual C++ Profresional 5, will that work?

Hell Raiser
03-31-2002, 08:46 PM
Gah, blah blah blah. http://forums.massassi.net/html/wink.gif Thanks for refreshing my memory on the basic C++ stuff. http://forums.massassi.net/html/smile.gif Heh, mods are gonna be soooooooo much more easier to do on JK2. http://forums.massassi.net/html/biggrin.gif (from a personal coding perspective)

I expect more JK2 specific stuff once the MP SDK is released. http://forums.massassi.net/html/biggrin.gif

03-31-2002, 08:51 PM
Version 5 may work, but I suggest Version 6 as that's guaranteed to work. Of course, you do have it legally, Massassi doesn't condone pirating software.

04-01-2002, 01:48 AM
<font face="Verdana, Arial" size="2">Yes, you will have to compile. I recommend you get Microsoft Visual C++. COG's syntax was based upon C, so yes, it is very similar.</font>

Does this mean we have to buy software to edit Outcast ?

04-01-2002, 04:01 AM
In most cases yes. It is possible to get the code to compile with GCC under linux, that I know. It may or may not be possible to get it to compile with Borland's free C++ compiler, and I don't have time to look right now.

04-01-2002, 07:03 AM
yeah i got it legally, if i want it illegally then i would of d/l version 6, but i think my annt bought it for me because i use so much VB http://forums.massassi.net/html/smile.gif But i guess it did seem like i just d/l it in that last post, sorry for the confusion :P

04-01-2002, 12:45 PM
Will any C++ compiler do the trick? I've got a copy of Borland C++ 5.02 that's just taking up space on my hard drive...

What sort of things will we need to use C for anyway? Every time I want to make a door is that going to need some hardcore coding?

04-01-2002, 12:54 PM
Wait a minute! Visual C++ is expensive, so you should point out that they can also make QVMs using the SDK's LCC compiler, for free! In fact, IDSoftware recommend that you distribute your mod with QVMs instead of DLLs, since they are safer and can be used on any OS.

Of course, that's assuming Raven release an SDK. They have only confirmed JORadiant so far.

[This message has been edited by UGG (edited April 01, 2002).]

04-01-2002, 01:23 PM
LCC can go from C++ code to the QVM directly? Cool! I thought that it could only take a dll and turn it into a QVM. Schnips, you'll only need it for modding. For making a door, just add a func_door entity to the brush which you want to act as a door.

04-01-2002, 01:56 PM
You know what I would LOVE to see? "Transition to C++ from COG: A Former Cogger's Perspective."
I'd pay good money for that. http://forums.massassi.net/html/smile.gif What I find is the most difficult part is getting all the stuff that I already know about C++ that I got from COG and finding out what it's all called (in fact, in my Trig math class at school, I found out I was doing sigma notation in COG long before I knew what Trigonometry stood for. Go JK!)

04-01-2002, 06:48 PM
Great Article!

Not to bust your chops, but an int (small int) is -32768 to 32767. (Sorry, I'm constantly telling poeple this at work and couldn't resist...)

Don't most C++ compilers assume int is actually long int now?

In any case its an awesome article.

There is Zero Ai

[This message has been edited by ZeroAi (edited April 01, 2002).]

04-01-2002, 09:22 PM
Great job starting this up, lordvader. I actually wanted to do something like this, maybe I'll wait and answer specific coding questions...maybe not...

sqmagellan: It's a good idea (I'd do it, but I never cogged anything), but you'll probably have an easier time just starting from scratch with C++...

Edit - I'm also glad that there's a place where I'll be able to regularly show off my programming knowledge...

[This message has been edited by Tracer (edited April 01, 2002).]

04-01-2002, 10:24 PM
<font face="Verdana, Arial" size="2">You know what I would LOVE to see? "Transition to C++ from COG: A Former Cogger's Perspective."</font>Yep, you'll find it in the horror section, right next to the Stephen King novels. http://forums.massassi.net/html/biggrin.gif

<font face="Verdana, Arial" size="2">LCC can go from C++ code to the QVM directly?</font>Yeah, IDSoftware modified it so you can't use any version of LCC.

All you do to compile the QVMs is run the batch files. Easy! http://forums.massassi.net/html/smile.gif You don't even need to run all three, just the ones for the code you changed.

DLLs may be easier to debug if your doing a large mod. I've only done small mods, but it's still hard to track down syntax errors when using batch files.


04-02-2002, 05:21 AM
Try http://www.bloodshed.net/ for a free programming environment.

On top of that, I'd like to point out for those who haven't noticed, that Code3Arena has some great Q3 based tutorials on how to do modification; highly suggested: http://www.planetquake.com/code3arena/tutorials/ .

Cease, and Decease.

[This message has been edited by moneyobie (edited April 02, 2002).]

04-02-2002, 08:49 AM
Generally you can get student editions of VC++ pretty cheap if you have a student card.
I picked up a pro edition of vc++ 6 for under 200.

Regarding C++ learning.
Its kinda like advanced, expanded COG.
After you get through the learning curve, you should be rolling in happiness.
Its what is called...powerful. http://forums.massassi.net/html/smile.gif

04-02-2002, 10:27 AM
I should show this to my Computer Science teacher. You just explained like everything in Computer Science 3 that we learned.

04-02-2002, 05:31 PM
Say, now here's a question: where can I get the SDK? I have this great idea for a goofy mod...

04-02-2002, 06:00 PM
Not yet up.