top of page

Written 2nd September 2018, by S.A.Ray

MD Dev part 3: Resource files

Eventually you will want to load bitmaps and music files into your Megadrive programs and you do that by means of a resource file.  For example, say you want to add a bitmap image into your program, you would basically append the bitmap data to the Megadrive rom file that you would use in your favourite Sega emulator.  Lets start by creating a resource file and referencing a bitmap file...

Lets make that resource file!

In the project folder of your Code::Blocks project you will ultimately end up with two files;  The first is the resource file itself and will have the extension of ".res", and the second will be a Code::Blocks generated header file which naturally comes with the ".h" extension.

​

Now, before we go any further we need a simple bitmap file which can be created with MS Paint or any other suitable paint program that can save the image in the 16 colour format.  Assuming we are using MS Paint, create a 32 * 32 image and then save it as a bitmap using the "16 color bitmap" option. Name it something simple like "image.bmp" and then make sure you place it in the Code::Blocks project folder.  The contents of this bitmap are not important for this tutorial, just so long as we have a 16 colour bitmap file present so that the resource compiler has something to work with...

Now that you have a bitmap file we can then make the resource file itself.  In the Code::Blocks project folder create an empty text file and save it as "resources.res".  Now open that file in a text editor of your choice and add the following code...

#include "resources.h"

BITMAP BMP_IMAGE_A "image.bmp"

...and save it.  Now, when you next go to build your project in Code::Blocks it will read this file and then generate a header file called "resources.h".  The first line with the include directive will tell Code::Blocks to do exactly that.

​

Now the second line is super important and uses the syntax; [ file type ]  [ identifier ]  [ file name ]. 

​

So, the file type can be of two types;  BITMAP and XGM.  I'm not sure if there are others but for the sake of this workshop series they are the only two we shall be using.  Simples!  Next is the identifier BMP_IMAGE_A and we shall use this in our program's source code to access the file data.  Last we provide the file name in double qoutes, "image.bmp", which tells the resource compiler which bitmap file we want to use.

Meanwhile, back at the ranch...

Now we need to return to our project back in the Code::Blocks IDE and make a small change to our main source file.  At the top of your source file, where you have your include directives, add an include directive for the header file resources.h so it looks like this...

#include <genesis.h>
#include "resources.h"

...and we should be set to then clean and build our project...

The header file

In the  Code::Blocks menu bar click on the following... Build->Clean Workspace, then Build->Rebuild Workspace.  This will generate the header file and it should appear in the project folder.

​

Now, lets just pause for a moment and consider the possibility of making changes to the resources.res file.  If you make changes to this file then remember to manually delete resources.h so that a new version can be created.  For some reason Code::Blocks will not update the header file but will generate a new one if its missing.  Whenever you do this, make sure you also clean and then rebuild the workspace.

​

Now we can move on to inspecting the contents of the header file...

#ifndef __RESOURCES_H_
#define __RESOURCES_H_

extern const Bitmap BMP_IMAGE_A;


#endif // __RESOURCES_H_

...and theres our identifier we can use in our code and its exactly the same as the label we gave it back in the resources.res file! 

​

Hopefully your project should have built correctly and will have generated the header file without any problems.

​

If you want to add more bitmap or xgm( music ) files to your resource.res file, then simply add one on a new line and ensure you give it its own unique identifier such as "BMP_IMAGE_NAME" or "XGM_MUSIC_NAME", replacing "NAME" with whatever you want.  Just make sure you delete the existing header file and then clean and rebuild the project.

​

In the next part we shall learn how to add palette data to our bitmap files using GIMP and access them as resources in our source code.

​

...for now, have fun!  ^_^

​

​

...

bottom of page