Answers to your coding questions…

A site about answers….not questions

dovoto On August - 24 - 2013

Here is a very simple example on how to take an image stored as a .png file (or any of the supported file formats supported by grit) and put it on the screen. The first thing we need is a target image. For that let’s chose our standard Drunkencoder logo:


I cant tell you exactly how this was made but I am fairly certain there was some sort of paint program involved. Anyway, notice it is 256×256 png file which matches one of the allowed background sizes for the DS (this isn’t necessary it just makes the copy to video memory trivial and this demo is…well…trivial).

Next we need to convert our image into something the DS can understand and there are a few ways to make that happen. We could write it to an SD card file system or use the special file system supported by Nintendo DS roms called nitroFS and then try to parse the data as we load it. Or we could do it the easy way and run it through grit which is included with libnds and devkitPro.

To do this is very easy. Simply create a folder called “gfx” or “data” in your project directory and drop the png file into it. Next create a text file with a name that matches your image file but with an extension of .grit.

This file is how we tell grit what to do with our image….in this case we want it to create a binary array of pixel data we can just copy into video memory. Its pretty simple and we only need three options. First is -gT! this tells grit to ignore any transparency data in the image file and set all the pixels to visible (if you don’t use this there is a good change your image won’t render correctly or you will have to set the alpha bits of each pixel manually as you load them into video memory, using the alpha channel of an image is very useful but for now its likely just to confuse things so lets turn it off).

Then we do -gB16 and -gb which tells grit to produce 16 bit pixel bitmap data and to write that out as a straight binary file. There are a LOT of options for grit and we will explore many of them in these FAQs but your best bet is the grit readme or website to really get a feel for how it all works.

Your grit file should look something like the following:

The magic makefile included in the libnds example templates will automatically call grit on all the image files in your gfx directory using the options supplied in the grit file. This will produce a binary file and a header file for easy inclusion in your project.

Here is what the actual code for the demo looks like:

That “drunkenlogo.h” is the header file auto generated by grit. You can find it in your “build” directory after you call make. If you were to open it up you would see something like the following:

Categories: Uncategorized

One Response

  1. ALVARITO says:

    And howdo i display a 16-bit image on the SUB screen? (NOT MAIN SCREEN)

    Leave a Reply

    Featured Video