Gremlin Engine Articles
The bitmap loading procedure
14/02/02
In this article I will explain the current (1.10 beta) bitmap loading procedure of the engine.
As you may know from the previous article, the engine uses a BMessage
(a simple container class)
for every Area file that is in fact a game screen. Each BMessage contains 100 strings that contain
the names of the bitmaps (one per tile).
The bitmaps themselves are stored as resources in a separated file named "tileset".
So let's see, how the engine loads the Area files and draws all the bitmaps in a 320x320 window.
First of all, file opening:
BMessage AreaMessage;
BFile file;
file.SetTo("/boot/home/areafile", B_READ_ONLY);
if (file.InitCheck() == B_NO_ERROR)
{
if (fAreaDEVView->fTilesView->fInformationView->AreaMessage.Unflatten(&file) != B_OK)
beep();
}
With this code we open the file (/boot/home/areafile) and copy it to our AreaMessage through the BMessage function
Unflatten(BFile).
Now we have a message with 100 strings named bxy where x and y are the coordinates of the bitmap in the area, but 0 bitmaps: let's make some:
BBitmap *Bitmaps[10][10];
BResourceSet ResourceSet;
BFile *file;
file = new BFile("/boot/home/tileset", B_READ_ONLY);
BResources *rsrc = new BResources(file);
ResourceSet.AddResources(rsrc);
int x, y;
for (x=0; x<10; x++)
{
for (y=0; y<10; y++)
{
Bitmaps[x][y] = (BBitmap*)malloc(1);
BString String;
String << "b" << x << y;
char *bitmap;
AreaMessage.FindString(String.String(), &bitmap);
if (strcmp(Bitmap, "null") != 0)
{
String.Truncate(0);
String << "BMP:" << bitmap;
Bitmaps[x][y] = (BBitmap *) fResourceSet.FindBitmap(B_PNG_FORMAT, String.String());
}
else
{
Bitmaps[x][y] = NULL;
}
}
}
With this great piece of code we first create an array of 10*10 bitmaps, then we search our tileset for the bitmaps using the new BResourceSet class (checkout
AreaDEV source with cvs to learn more about that) and finally we use a nested for cycle to allocate the needed memory and create our bitmaps using the names
stored in our BMessage.
In particualr, we create a BString objet taht contains the x and y coordinates after the letter b (our bitmap name pattern) and copy the right bitmap name in the
"bitmap" string with the BMessage's FindString() function and, if the name is not null (that means no bitmap), we assign the correct bitmap from the tileset to the
Bitmaps[x][y] pointer.
Now, all we have to do is drawing:
SetDrawingMode(B_OP_OVER);
int x, y;
for (x=0; x<<10; ++x)
{
for (y=0; y<<10; ++y)
{
if (fBitmaps[x][y] != NULL)
{
DrawBitmap(fBitmaps[x][y], BPoint((x * 32),(y * 32)));
}
}
}
Adding these few lines of code to our BView's (the content of the window) Draw() function will do the job for us.
Easy enough?
Rafael Romo
Back to Rockman's page
This site looks best at resolutions of 800*600*24 or higher ones.
This site is still under construction. It will take me a few weeks to complete it.
|