Game Development Reference
In-Depth Information
public:
PrimeSearch(int elements);
int GetNext(bool restart=false);
bool Done() { return (searches==*currentPrime); }
void Restart() { currentPosition=0; searches=0; }
};
I ' ll show you a trivial example to make a point.
void FadeToBlack(Screen *screen)
{
int w = screen.GetWidth();
int h = screen.GetHeight();
int pixels =w*h;
PrimeSearch search(pixels);
int p;
while((p=search.GetNext())!=-1)
{
intx=p%w;
inty=h/p;
screen.SetPixel(x, y, BLACK);
}
}
The example sets random pixels to black until the entire screen is erased. I should
warn you now that this code is completely stupid,
for two reasons. First, you
wouldn
t set one pixel at a time. Second, you would likely use a pixel shader to do
this. I told you the example was trivial: use PrimeSearch for other cool things like
spawning creatures, weapons, and other random stuff.
'
Memory Pools
I mentioned memory pools earlier in this chapter when I covered different types of
memory management. They are incredibly useful for frequent, small allocations and
deallocations because they are lightning fast. The idea is that you allocate a large
block of memory up front, which is then divided into chunks of even sizes. Each
chunk has a small header that points to the next element. This creates a singly linked
list of memory chunks, as shown in Figure 3.1.
When an allocation request comes in, it simply removes the chunk at the front of the
list and returns it, making the next chunk the making the next chunk the new front
(see Figure 3.2).
 
 
Search WWH ::




Custom Search