1. Technology

Programming Games in C - Tutorial Two on Star Empires


1 of 5

Having Tutorial One run on CC386
Programming Games in C - Tutorial Two on Star Empires

There's still a fair way to go with the Star Empires source code but this tutorial is a summary of what been said about C in the first tutorial.

C programs are created as text files in an editor, or IDE. To compile them and run them you need a compiler such as

Both of these Compilers can compile Star Empires though CC386 has a few problems with some of the Windows API calls to do with screen size so the following functions need to be commented out:

  • GetConsoleSize
  • SetScreenSize
  • gotoxy(int x,int y)

However there's no need to do this manually, let the compiler do it, look at line 16 which should look like this:

/* #define CC386 /* leave line commented out if not compiling with CC386

If you are using any other compiler, leave it as it is. If you are using CC386, remove the first /* so it looks like this:

#define CC386 /* leave line commented out if not compiling with CC386

It should now compile without errors on CC386. On my system, CC386 said the linker had an internal error but I found it ran all the same.

If you look through the source code you'll see a couple of places that start with a #ifndef CC386 and end with a #endif. Line 92 and line 994. This means that if the symbol CC386 is not defined, all the code between the #ifndef and the #endif is included. Ie. that code only gets compiled and built if not compiled with CC386.

There's also a #ifdef which only compiles code if the symbol is refers to is defined but that's not used here. Using #define, #ifdef,#ifndef and #endif lets you customize one source code file for different build targets. You can learn more about #defines later in this tutorial.

C Programs

A program consists of two parts- code and data. The code manipulates the data, while the data consists of numbers and text used by the code. In C, the data is held in the form of variables. These are memory locations that can hold numbers or text characters. We don't know the exact memory location but we can use it because the variable's name lets us set and get the value.

In Star Empires we use int numbers where int means an integer, a whole number with no decimal part. The only place where a number with a decimal part (called a float) was used was in the distance function which calculates the distance from a vector (x,y).

The sqrt function returns a float but by putting thw word (int) in front we force it to drop the part after the decimal point and return an int. However before this happens, the value of 0.5 was added to the result of the sqrt so it rounded it up. If the sqrt gave a value 3.6, then adding 0.5 would give 4.1 and (int) of 4.1 gives 4.

/* returns distance as int. Values are always in the range 1-6 */
int distance(int x,int y) {
 int distsqr= (x*x) + (y*y) ;
 return (int)(sqrt((float)distsqr)+0.5) ;

On the next page: About Functions

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C Programming
  5. C Tutorials
  6. How to program games in C. Tutorial Two - Star Empires

©2014 About.com. All rights reserved.