P. 1
Gold - Object-Oriented Game Development

Gold - Object-Oriented Game Development

|Views: 98|Likes:
Published by János Strasszer

More info:

Published by: János Strasszer on Apr 21, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/08/2011

pdf

text

original

When we learned C, our first program probably contained a line that looked
rather like this:

printf( "Hello World\n" );

I’ll also wager that a good deal of code today still contains printfor scanfor
one of their relatives. Generally, the C libraries – despite their vagaries – are very
useful for a limited number of tasks, and all compilers on almost all platforms –
even the consoles – support them.
C++ has had a chequered history when it comes to supplying standard
libraries. Originally, the equivalent of C’s input/output (IO) functions iostream,

istream, ostream, strstream, etc. – were supplied but were not at all standard.

Now it turns out that these objects are less efficient than their less OO cousins
and consequently aren’t much use for game code. Nevertheless, it took a long
time for vendors to consistently support the functionality.
Thanks to the ANSI committee, the stream library is a standard part of a
C++ distribution these days, and it now comes as part of a larger set of objects
called the Standard Template Library (STL). We’ll discuss general use of

Software engineering for games

59

Padding

A

B

Increasing memory

Padding

A

Figure 3.7

Aggregation of class data
(multiple inheritance).

8985 OOGD_C03.QXD 1/12/03 2:27 pm Page 59

templates in the next subsection, but some of these objects are very useful
indeed and C++ programmers ignore them at their peril. However, STL is a two-
edged blade, and it is worth examining it in a little detail to make a balanced
assessment of its usefulness.
First, the ‘Standard’ part of the name is a bit of a misnomer, because
although the interfaces are (very nearly) identical between compiler vendors and
other public domain authors, the internal details vary wildly. Some STL imple-
mentations are rather more efficient than others, and one should be careful not
to rely blindly on things being fast when developing for several platforms.
Second, STL has a serious image problem. It is not particularly user-friendly.
As anyone who has opened an STL header file can testify, the actual code is for-
matted poorly, almost to the extent of impenetrability. Web searches in a quest
to find out how to use it result in trawling through equally impenetrable help
files and documents, and even buying a book can leave the programmer
bemused enough to write off STL as impenetrable.
Now, the Hacker’s Charter has encouraged a culture of ‘If I didn’t write it, I
won’t use it’, so it is difficult to encourage use of any library code, let alone C++
template systems that sell themselves short. Yet it remains the case that if one
makes the effort to get over the initial conceptual barriers that STL raises, then it
can become as or even more useful – in specific circumstances – as printf()
and friends are to C programmers.

What STL supports

STL provides a bunch of type-safe container classes that hold collections of
objects in interesting ways: dynamic arrays, lists, queues, double-ended queues
(deques), stacks, heaps, sets, hash tables, associative maps, trees and strings are all
supplied with STL free of charge with every compiler that supports the C++ ANSI
standard. Coupled with some careful typedef-ing, one can swap the containers
arbitrarily, more complex containers can be constructed using the simpler ones,
and all these classes can have custom memory managers added on a per-instance
basis that efficiently allocate and free blocks using one’s own algorithms.
There is little doubt that this is useful – and powerful – functionality that
comes for free and is robust and portable, and there is surely a place for STL in
every programmer’s repertoire.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->