1. Computing & Technology

Discuss in my forum

Game Programming in C++ Tutorial Two - Star Empires

By , About.com Guide

Both cout and printf!

This is the second of two tutorials on programming a simple text game in C++.

In the C version of Star Empires, all text output was done using printf. This is the standard way to do output in C. It's also pretty powerful with formatting but has the problem that it can crash or print something wrong if you pass in the wrong type. Try

printf("%i","This should be an int");

So C++ has it's own admittedly very powerful system for input and output using cout. I'm not going into the details here when there's a perefctly good existing C++ tutorial on Input and Output.

From that you can see that doing precise formatting with cout is tedious, people have developed alternatives like the Boost Format library. It provides printf-like formatting, in a type-safe manner which allows output of user-defined types. So you get the flexibility (ie the same type of format) of printf with the type-safeness of cout.

To keep the code shorter, I've use cout where possible but when not possible I've used printf and sprintf. It's not a good practice but it kept the program short; feel free to replace all printf calls with Boost! The sprintf function is like printf but outputs to a char * (a C type string) buffer whereas in C++ it's always good if possible to use std::string.

C and C++ Strings

Again mixing the two worlds of C and C++ is less than ideal but it is possible. C++ strings are preferable as you don't get buffer overflow errors. You don't have to worry how they are implemented either whereas in C, you have a pointer to an array of characters that end with a '\0' (0 value byte). The C string library functions (strcpy, etc) append a '\0' but if you do any character level manipulation you have to add them in yourself.

In C++ the std::string type is a lot safer and there are a good range of functions. Again there's a C++ tutorial that looks at C++ strings.

The good things about the two different strings types is that they are interoperable; you can copy from C to C++ or back again. C strings can be assigned to C++ strings directly and C++ strings have a function c_str() that returns the C string equivalent. This example shows a C string that is copied to a C++ string, printed out with cout then copied back into another C string and printed out using printf.

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
    char * name="My Name is David";
    std::string cppname=name;
    std::cout << cppname << std::endl;
    char cname[20];
    strcpy(cname,cppname.c_str());
    printf("Again, my name is %s",cname);
    return 0;
}

But as rule don't mix them if you can!

#define and const

All the #defines from the C version are gone, replaced by const int. This improves the program's robustness, especially if non int #defines are used. The #define just replaces the defined text with whatever text is defined. This happens before compiling. It effectively edits the source code prior to the compiler reading it. While this is potentially more flexible, e.g. you might do a #define to a function instead of a value, using const type lets the compiler handle it properly.

Other differences between C and C++

Until the latest C version C99, strictly only /* .. */ comments were valid in C though Visual C++ accepts // in .c files. In C++ you can use /* .. */, as C++ is C plus objects and other bits.

Variable Declaration In C, you declare variables at global level or at the start of functions. In C++ this is relaxed and you can declare them anywhere as the above example shows. You can for instance declare them in a for loop

for (int i=0;i<10;i++) ...

Reference Parameters

Another difference between C and C++ is the extra way parameters can be passed out of a function. In C, parameters are passed by value, A copy is made of the value and that is used. Only if it's a pointer to a variable can the value be changed. In C++ you can make it a reference parameter. In the example below the function ChangeValue() acts on the parameter passed in and changes its value.

#include <iostream>
void ChangeValue(int & value) {
    value--;
}

int main(int argc, char* argv[])
{
    int myValue=10;
    std::cout << "Value is " << myValue << std::endl;
    ChangeValue(myValue);
    std::cout << "Value is " << myValue << std::endl;
}

Run this and you'll see Value is 10 followed by Value is 9 on the next line. Again this contributes to the robustness as the compiler can't alter the value unless an int is passed in. In C you could have passed in a pointer to anything.

The Next Tutorial

This will be Snake in C++/SDL.

©2012 About.com. All rights reserved.

A part of The New York Times Company.