top of page

Written 6th May 2019, by S.A.Ray,   Updated 29th June 2020

MD Dev part 5: Loading Bitmaps

After a long haitus the Megadrive madness continues!  I'd like to welcome you back to more shannigans with the 16-bit wonder...

​

In today's workshop we look at how to take a bitmap resource, load it, and then display it on screen.

Preparing for the task ahead...

Before we set off, lets us make sure we have our bitmap resource at the ready.  Please ensure you have read and understood the workshops in "Part 3: Resource files" and "Part 4: Bitmap files".

​

Using that knowledge you need to create a bitmap resource for an image with a resolution of 320*240.  This could be a scaled down version of your favourite picture, or even a screen grab from a Megadrive game(using an emulator).  Save your bitmap file with the name "splash.bmp" and then add the following line to your resource file( the one with the ".res" extension )...

BITMAP BMP_SPLASH "splash.bmp"

...then save the file.

​

Its important to note that if you have previously compiled your resource file( you generated an accompanying ".h" file for your ".res" file ) you will need to delete the accompanying ".h" file.  The reason for this is because Code::Blocks will not notice the change to your resource file and so we must force it to recompile it.  So, delete the ".h" file and then in Code::Blocks go to Build->Clean Workspace.  Finally, build the project with Build->Build.

​

At this point have a look in your project folder and open the header file. You should see the following line included...

extern const Bitmap BMP_SPLASH;

​

...which means we can now use the resource.  Happy days!  ^_^

​

So, lets return to our main source file and declare the following global...

struct  GAME_BITMAP  bmpSplash;

​

...for which GAME_BITMAP is a data type provided by SGDK to store the width and height of a bitmap.

​

In the game_setup() function,  add the following lines...

bmpSplash.width     = BMP_SPLASH.w;
bmpSplash.height    = BMP_SPLASH.h;

VDP_loadBMPTileData( (u32*)BMP_SPLASH.image,
                            0x0000,
                            bmpSplash.width / 8,
                            bmpSplash.height / 8,
                            bmpSplash.width / 8);

 

VDP_setPalette( 0, BMP_SPLASH.palette->data );

​

...and this will ultimately accomplish two things;  store our bitmap and its palette data into the VDP's video memory and first palette.  Lets break this down...

​

The first two lines simply store the bitmap resource's width and height into our GAME_BITMAP structure. 

​

VDP_loadBMPTileData()  is a bit more involved as it takes five arguments.  The first we need to provide a pointer to the bitmap resource's image data.  The second is the starting tile in video memory we place our bitmap,  and the last three require some explaination...

​

These final arguments are given in tiles - not pixels.  The first two are the width and height in tiles( which are 8*8 pixel tiles,  and why we have to divide by 8 ) of the region we wish to store( which may not be all of the bitmap ),  while the third is simply the width of the bitmap in tiles.

​

Last but not least is the call to VDP_setPalette().  We store the bitmap resource's palette data into the first( "zero" ) entry of the VDP's palette memory.

​

And thats it for loading a bitmap resource into the VDP.  Build your rom and load it into Gens.   As you did back in Part 2: colour palettes,  have a look at the first palette entry and the "offset" display of the VDP's video memory.  The palette should contain only the colours found in the bitmap's image data,  while the video memory should contain tiles from it,  but stored in a jumbled up order...

​

And thats it for today.    Next time we shall take the image data we have stored in the video memory and display it on the screen. 

​

Have fun!   ^_^

​

...

bottom of page