2006/08/25

Linking DS Code to Binary Image Data

Having to deal with sprite/texture/background data is a pretty common task in DS programming. The best way to do it is to strip any sort of header information and undo compression that is part of many graphics formats so you can just link the binary data along with your code. The problem is you have to know how to do this.

It looks like a lot of people convert the image to a header file and then #include it in their code. For small demos this really isn't a big deal, but it's kind of bad once you go beyond loading a single image. Why? Since the data is included by the pre-processor you have to recompile it along with your code EVERY time you make a change.

I figured out the tools well enough to get around this so I thought I'd share two different ways of doing this. Anyway, the two methods are:
  1. Image->Source->Binary
  2. Image->Binary
The end result is essentially the same, the process is just a little different.

Both require a program call gfx2gba from gbadev.org (NOTE: There are two versions of gfx2gba around, but you want v0.13. v1.03 is a similar but different program from a different author. I'm pretty sure you could use it I just won't talk about it here). This program can read various graphics file formats and extract the image data from them in various ways and seems to be an indespensible part of both GBA and DS development (yes, despite its name it is also useful in DS development because the two platforms have several similarities).

Image->Source->Binary


  1. Use gfx2gba to convert your image (bmp/pcx/tga/...) to a C source file. I will assume your image file is called image.bmp.

    gfx2gba -t8 -f src -o arm9/source image.bmp

    This converts image.bmp into a master.pal.c file containing the palette information and a image.raw.c file containing the image data arranged as 8x8 tiles. This would be used for sprites. For backgrounds you don't want tiling so you leave off "-t8".

    "-f src" specifies that the output type will be source (C) files.

    "-o arm9/source" specifies the output directory that the *.c files will be placed it. Set this to your source directory.

    gfx2gba has several other options so be sure to try running it without arguments or looking at the readme.txt file.


  2. Next, you want to make sure that this image data will be compiled as part of your build process (when you type "make"). You will either have to add it if you are using a custom Makefile, or place it in a directory that is searched for *.c files.


  3. Finally, you have to make the data accessible from your program. This is done by adding a declaration to your code so the compiler will let you use the arrays in the external object files. You will have a palette array called master_Palette in master.pal.c and an image array called image_Bitmap in image.raw.c. Putting the following code somewhere in your program source code will allow you to use them:

    extern const unsigned char image_Bitmap[];
    extern const unsigned short master_Palette[];

    Unfortunately, there doesn't seem to be a way to determine the size of the arrays at compile-time when you use this method. sizeof() gave me an error since they are arrays of unspecified size. You either have to include the dimension of the arrays from the generated source files:

    extern const unsigned char image_Bitmap[4096]; // Or whatever the size is
    extern const unsigned short master_Palette[256];

    OR, declare additional variables that hold them:

    unsigned int image_Bitmap_size = 4096; // Or whatever
    unsigned int master_Palette_size = 256;

    OR, add two other variables to the generated source files to store the size and then declare them as well:

    /* Add to the end of image.raw.c */
    const unsigned int image_Bitmap_size = sizeof( image_Bitmap );
    /* Add to the end of master.pal.c */
    const unsigned int master_Palette_size = sizeof( master_Palette );

    /* In your program source code */
    extern const unsigned int image_Bitmap_size;
    extern const unsigned int master_Palette_size;
    /* Don't forget the data array externs as well */

    I prefer the latter method because it is the most automatic of the three. If you ever change the image files you will have to regenerate the source and re-add the lines, but they're quite short and you probably won't be changing the images often.



Now when you run "make" it will compile the image data source files once and then you won't have to compile them again. The compiled image data will be linked into the executatable (don't forget to tell the linker about them if necessary).

Personally, I don't really like this method because it requires some manual work. I like things to be as automated as possible so I don't screw something up when I forget to do some manual step. For this reason I prefer the second method.

Image->Binary



  1. First, extract the image data from the image file with gfx2gba (assuming an image file called image.bmp):
    gfx2gba -f raw -t8 -St.bin -pmaster.bin image.bmp

    Again, leave off "-t8" if you are working with a background rather than a sprite.

    "-f raw" for binary output is the default.

    "-St.bin" sets the extension of the image data to ".bin" instead of the default ".raw".

    "-pmaster.bin" sets the palette filename to "master.bin" instead of the default ("master.pal"). There is a "-Sp" option to set the palette extension similar to "-St", but it doesn't seem to work.

    This will generate two binary files: image.bin containing the image data, and master.bin containing the palette.

  2. Next, you need to make these files linkable. This requires a bit of "magic" as it's hard to see how the example programs do this.

    If you look at a Makefile for one of the example programs you will find the following rule way down at the bottom:

    %.bin.o : %.bin
    @echo $(notdir $<) @$(bin2o)

    Basically, it takes *.bin files and trys to make them *.bin.o files by calling "bin2o". Now, if you look in devKitPro/devKitARM/base_rules (a file that gets included in all the Makefiles) you will find:

    define bin2o
    bin2s $< | $(AS) $(ARCH) -o $(@) echo "extern const u8" `(echo $( `(echo $(> `(echo $(> `(echo $(

    What this basically does is convert a binary file to assembly language, assemble it so it can be linked to your program, then generates master_bin.h and image_bin.h files to be #include'd in your program source. They contain:

    /* In master_bin.h */
    extern const u8 master_bin_end[];
    extern const u8 master_bin[];
    extern const u32 master_bin_size;

    /* In image_bin.h */
    extern const u8 image_bin_end[];
    extern const u8 image_bin[];
    extern const u32 image_bin_size;


    The extern's from the first method all nice and automagically declared for you.



I've shown you 2 different approaches to getting away from header file #include'd data and moving to more professional linking of binary files. I prefer the latter method as it can be pretty much completely automated, but which method you use will largely depend on your needs.

This is not to say these are the only two methods, mind you. While trying to figure this all out I saw references to some program named "katie" and another called "bin2c" ("binary to C" I would assume) that sounded like they could likewise be used. I'll leave that to you. Also, I'm willing to bet that there's a plugin for The Gimp or some other graphics program that will do all of this.

2006/08/24

My bad, DevKitPro devs! orz

It looks like I jumped the gun a bit in yesterday's post when I complained about the ease of learning to use DevKitPro (DKP).

Got some sleep and woke up a lot less grumpy today so I thought I'd give it another shot. This time with a clear head.

I came across the PataterSoft tutorial and in the process of messing around with it I ended up going back through the "examples" directory of DKP. And, I must say.... they're really well commented, fully explained, easily understood examples. Maybe that's why all the tutorials have fallen into states of disrepair; they're unneeded. You just pick the directory you want and learn about what's in there- it's kind of like "Choose Your Own Adventure".

Really didn't take much to get a few easy examples going. I retract all the nasty things I said earlier and instead tip my hat in their general direction.

Hmmm.... the API has changed, eh?

In general, DevKitPro is a pretty nice software package. GBA, PSP, and (most importantly) DS development libraries all in one place. They even have a fancy updating tool that will keep your libraries up to date and add new ones (they recently added dswifi).

Biggest problem? Documentation... of course. Unless there's some guys that are really dedicated to working on it, the documentation for every project generally seems to limp along coughing and wheezing behind the coders. And, DevKitPro is no exception.

Don't they have a Wiki? Well... kinda. But its out of order.

Doxygen? Well... kinda. But its pretty obviously not complete.

In case you're wondering, there doesn't seem to be any documentation in the software directory- save for the PSP.

For this reason, tutorials are nice just to avoid the frustration of figuring things out. If you check out any other DS dev-related sites: NDS Homebrew, Drunken Coders, PataterSoft, etc. They all tend to link to the same tutortials- all of which are either quite outdated or incomplete.

Far and away the best is PALib. This is assuming you want to use PALib, that is.

I was using the highly regarded double.co.nz tutorials. But, they no longer work. I haven't looked into it very deeply yet, but when I tried to compile the very short programs there were several errors. devKitARM (part of devKitPro) seems to have changed since the tutorials were last updated.

Most notably:

  • The development tools all changed from arm-elf-* to arm-eabi-*

  • The DISP_SR symbol seems to have completely disappeared.


The first isn't really a bit deal. But it illustrates the a problem that may be more difficult to solve for a real beginner. I didn't really bother with it after than. I'll give it another go tonight when I get home.

Perhaps writing an up to date tutorial is something I should undertake. We'll see.

2006/08/21

DirectX Programming with Visual C++ 2005 Express Edition

At some point I feel like I should embrace the winner in graphics APIs for computer games and learn DirectX (seriously, this time). In some obvious attempt to entice me, Microsoft began offering the Visual C++ 2005 Express Edition for free.

I looked at how to get it setup for DirectX development and these are my notes. You obviously need it and the DirectX SDK to start with.


  1. Setup the Platform SDK as described here here.

  2. Start Visual C++

  3. File->New->Project

  4. For "Project type" select "Visual C++" or "Win32" and create a "Win32 Console Application".

  5. When the Win32 Application Wizard comes up, make sure you select "Windows applicaiton" for the "Application type", and "Empty project" from "Additional options".

  6. Project->Add New Item. Select "C++ File".

  7. Project->Properties. Configuration Properties->Linker->Input. Add necessary DirectX libraries (ddraw.lib, dxguid.lib, etc.) to "Additional Dependencies".

Out with OpenGL, in with DirectX

I'll admit, I was originally in the OpenGL camp. OpenGL + GLUT was just vastly easier than DirectX for those of us with no Windows programming experience (even with the "easy to use" application framework they introduced in DirectX 8 or 9). Plus, I was fortunate to use it in a graphics programming class I had as an undergrad.

However, as part of my renewed interest in game programming, the coming release of Vista and DirectX 10, and the apparent demise of OpenGL, I decided to give DirectX another shot. But that requires tools.

The DirectX SDK is easy enough. That's been freely available for some time. A compiler and other development tools... that's a different story. I'd known about Visual C++ Toolkit 2003 (now deprecated, free command-line tools) for sometime, but I didn't know that they'd gone another step further and introduced Visual C++ 2005 Express Edition. Basically the same thing, but now with an IDE to boot. Very sweet.

This actually got me to dust off my DirectX and WinAPI books and do a little Windows programming for the first time in quite a while.

Refactoring

Well, I knew it was bound to happen...

I went ahead and created the blog and decided on a name, and then I thought of a name I'd rather have the very next day. I figure, with the blog still in its infancy there's no harm in changing it at this point. So...

R.I.P. "Rendered Useless". Long live "Rendered Obsolete"!!!!

On the topic of blog content/maintenance/etc., we should be seeing a new design here shortly. There really isn't much to the Blogger templates, now I'm just busy mucking around with CSS. I'm a bit old-skool when it comes to web-design; I haven't done much since the heyday of HTML 2.0... yeah... I remember being all impressed when I got RealAudio meta-files figured out (are they even still around?). Anyway, stay tuned for that.

Also, in order to placate my insatiable whims for name changes, yet still amuse myself, I've decided to use a "sub-title" of sorts for the blog. Just something I can change from time to time, yet is essentially meaningless. What does this mean for you? Absolutely nothing. But, please think of it as being +1 to the "amusement" factor of the blog. Unexpected. Spontaneous. Random.

2006/08/20

Let them eat XNA!

Although hardly news anymore, Microsoft's annoucement of a free XNA Game Studio Express is well worth mentioning. At launch it will only allow for Windows development in C# with the XNA Framework (R.I.P. Managed DirectX), but they will eventually include support to build games for the Xbox 360.

How cool is that?

Granted, you have to join the XNA Creator's Club with a "value-added" price of $99 per year to develop *OR* even play someone else's games (via FAQ). But, that's still quite a bit cheaper and with less red tape than an official Wii developer's kit (hint: $2,500-$10,000).

Beta begins Aug. 30.

Hello world?

Should the first post of a blog about console development/game development/general programming have any other title?

I should think not.

Partially inspired by my "extra-curricular" interests as of late, my long-standing love for Mixi (a Japanese social networking site), and partially driven by news of Google's intent to update the extremely outdated and ghetto Blogger, we have a new blog. Hopefully it won't befall the same fate as my last ill-conceived and neglected blog.

My hope is that interest in one will help drive and sustain progress/motivation in the other.

I intend to try and keep this reasonably focused on the area of console development. Logically, it will on occasion expand into other areas such as general game development and programming.

I'll be looking into the templates, CSS, etc. in short order to try and make it look a little less generic.