Game Development Reference
In-Depth Information
Figure 18-2. The results from the code in Listing 18-4
The set method has been overloaded to provide different functionality. The first example takes a bit
index and a value to set the bit to. The second takes just an index, and this method will then set the
given bit to 1. The final version of set will make all of the bits in the set 1.
The reset method has similar behavior. It can be passed an index that will reset the given bit to 0 or
it can be used without parameters, which sets all bits to 0. The flip method also has two versions:
When given an index it will change the given bit from true to false or false to true and when given
no parameters it flips the values of all bits.
Summary
The bitset template is a simple object provided by the STL to give programs an easy and reusable
bit flag implementation. Bitsets can be used in game development in a number of situations. I have
seen them used particularly often in multiplayer development, where transmitting the state of an
object over the Internet requires that programmers pack data into as small a location as possible.
Packing boolean values into individual bits is as small as you can possibly get.
This was our last chapter looking at the containers provided by the STL. The next chapter covers
a full implementation of the Text Adventure game. The examples of the game you have seen
so far have been compromised by the fact that we had not yet covered STL containers. Game
programming involves a lot of data object management and storing objects in appropriate data
structures is essential. Now that you have seen the different containers the STL provides, you'll be in
a better position to see how we can write a game in a more structured manner.
 
Search WWH ::




Custom Search