Bitmap Programming

Here you can learn how to put sprites and animations into your games. Only basic programming knowledge is assumed. We recommend users to go through the tutorial in sequence and check all the examples.

 All of the code snippets and downloadable examples are written in C

Bitmaps

Define a structure to store the bitmap data in. Essentially it's just an array of bytes for the pixel data:

typedef struct bitMap_type
{
	unsigned char* pixels;
	unsigned int width, height;
}bitmap;

This structure can now be drawn to the screen with this code:

void bit_blt(int x, int y, bitmap* bm)
{
	int xindex, yindex, bml = 0, offset = (y << 8) + (y << 6) + x;
	for (yindex = 0; yindex < bm->height; yindex++)
	{
		for (xindex = 0; xindex < bm->width; xindex++)
			double_buffer[offset + xindex] = bm->pixels[xindex + bml];
		offset += 320; // next line buffer
		bml += bm->width; // next line bitmap
	}
}
This code assumes 320 X 200 resolution.

Loading

To create good sprites you will want to import them from a paint program. Here is the code to load a 256 colour bitmap into the structure given above.

void loadBitmap(bitmap* bmPtr, char* fileName, char freeFlag)
{
	unsigned int i, j;
	FILE* bmpFile = fopen(fileName, "rb");
	if (freeFlag)//parameter optionally frees existing data before loading new
		free(bmPtr->pixels);
	fseek(bmpFile, 18, SEEK_SET);//move along file
	fread(&bmPtr->width, 4, 1, bmpFile);//load width&height
	fread(&bmPtr->height, 4, 1, bmpFile);
	bmPtr->pixels = (char*)malloc(bmPtr->height * bmPtr->width);//allocate memory
	fseek(bmpFile, 1078, SEEK_SET);//Data starts here
	for (i = 0; i < bmPtr->height; i++)
		for (j = 0; j < bmPtr->width; j++)
			bmPtr->pixels[(bmPtr->height - 1 - i) * bmPtr->width + j] = fgetc(bmpFile);
	fclose(bmpFile);
}
This code does not account for pixel data in the bitmap file being zero-padded to end on 32-bit boundaries. Subsequently it will only correctly load bitmaps with a width divisible by four.
  Download a working example

Transparency

Transparent sections can be included in sprites quite easily. Just use a particular palette index as the transparent colour. Zero is a sensible choice. Now amend the drawing code:

void trans_bit_blt(int x, int y, bitmap* bm)
{
	int xindex, yindex, bml = 0, offset = (y << 8) + (y << 6) + x;
	for (yindex = 0; yindex < bm->height; yindex++)
	{
		for (xindex = 0; xindex < bm->width; xindex++)
			if (bm->pixels[xindex + bml])
				double_buffer[offset + xindex] = bm->pixels[xindex + bml];
		offset += 320; // next line buffer
		bml += bm->width; // next line bitmap
	}
}

And that's it!

  Download a working example

Clipping

If you try to draw a sprite and it is even partly off-screen, the computer will crash. This drawing function will prevent this occurring along the X axis, and will draw partially on-screen images.

void clipped_trans_bit_blt(int x, int y, bitmap* bm)
{
	int xindex, yindex, bml = 0, offset = (y << 8) + (y << 6) + x;
	y = 0;
	if (x < 0)
	{
		y = abs(x);
		x = bm->width - abs(x);
	}
	else if (x + bm->width > 319)
	{
		x = 319 - x;
	}
	else
	{
		x = bm->width;
	}
	for (yindex = 0; yindex < bm->height; yindex++)
	{
		for (xindex = y; xindex < y + x; xindex++)
			if (bm->pixels[xindex + bml])
				double_buffer[offset + xindex] = bm->pixels[xindex + bml];
		offset += 320;
		bml += bm->width;
	}
}

You should now be able to move sprites around the screen (just be careful on the y-axis!).

  Download a working example