Game Development Reference
In-Depth Information
5.6G RIDS
A grid is an organization framework of space in n-dimensions, in the context of user inter-
faces we are primarily interested in two dimensional grids, though there can be interesting
uses for three dimensional grids. Grids are divided into m rows and n columns where each
cell can be addressed by an index i,j in two dimensions or i,j,k in three dimensions.
For games we usually prefer Cartesian grids, grids in which cells are unit squares (or unit
cubes in 3D) and vertices can be mapped by integer points.
The goal behind a grid system is to be completely logical, it should not be aware or even
care about the data contained within, but it should provide an interface by which we can or-
ganize any type of data in a grid.
To create a very versatile grid system, we will take advantage of some template program-
ming, it's not strictly necessary to do it this way, it can certainly be done by other means
such as dynamic allocation of the grid array. However, when designing systems that may be
reused several times and in general, it's best to avoid dynamic allocations.
Before we go into the specific functions of a grid we will create a grid object that gives us
the flexibility of specifying the dimensions of the grid as well as the type of data each cell
will contain.
template <int m, int n, typename T>
class grid {
. . .
};
Thisdeclarationofthegridclassallowsustocreateagridobjectandspecifyitsdimensions,
the following example shows a very simple example.
Grid<3,3,int> exampleGrid;
The exampleGrid object will be a 3x3 grid and each cell, or slot will hold an int. So in order
to define the grid, we need to bring in the concept of a slot , the slot will be a cell in the grid
thatwillholdaninstanceofthedatatypewespecify.Morethanthat,aslotcanbeconfigured
to have a maximum number of instances of an object within it, and also to store information
about how many instances there are.
class slot
{
public:
Search WWH ::




Custom Search