Home | Sales | Downloads | Forum | FAQ | Links | Terms | Email
     
 
Navigation
Programming



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

Bitmaps
Define a tructure to store the bitmap data in. Essentially its 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
}
}


Note that 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)
free(bmPtr->pixels);
fseek(bmpFile,18,SEEK_SET);
fread(&bmPtr->width,4,1,bmpFile);
fread(&bmPtr->height,4,1,bmpFile);
bmPtr->pixels=(char*)malloc(bmPtr->height*bmPtr->width);
fseek(bmpFile,1078,SEEK_SET);
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);
}


Note: 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
Transparancy

Transparant sections can be included in sprites quite easily. Just use a particular pallette index as the transparant colour. Zero is a sensible choice. Now ammend 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])
.................................................................


And thats it! Download a working example

Clipping

If you try to draw a sprite and it is even partly offscreen, the computer will crash. This drawing function will prevent this occuring 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 carefull on the Y axis!). Download a working example

If your confident with this the next step is to get animation underway, but I'll leave that to you...

 

 

Search




Links


Our freeware
DJGPP