I specifically picked
NightFox Lib as the library of choice for this guide, simply because its use is incredibly simple and because the library is still in active developed, not to mention that it's well-documented. I believe that we will all get the quickest and best results if we focus on it as our graphics library, so let's get to it.
Integrating the library with your
Projects is incredibly easy – all you really have to do is copy/paste the
Template provided in the lib’s folder into Your project… and that’s it! There are further integration options that you may choose that will be discussed in the second part of this chapter. As you’ve already noticed, a few new folder have appeared in your
Project Folder – those will be used to store various
Resources.
Visual Studio is capable of
linking those folders to particular elements of your
Solutions. By doing that, you will always see the contents of your folders which will make coding this one bit easier. Including those folders in your solution is pretty straight-forward, but we’ll leave that for later as I already know you all can’t possibly wait any longer to get to the juicy parts.
For now, attempt to compile the Template. If everything goes well and without hiccups, you’ve just successfully included NightFox Lib in your Project –
well done!
Now that we’ve included NightFox Lib in our project and we’re all acustomed with how
RAM and
VRAM works, we can finally move on to what we’re really interested in – using
Graphics in our games and applications. As I already mentioned, the DS mostly uses
Tiled Graphics – they’re much easier to render and their size is lower, thus they don’t clutter the RAM and VRAM as much. To convert an image to that kind of a format, we will be using GRIT. The name stands for GBA RASTER IMAGE TRANSMOGRIFIER and it has been the tool of choice for both GBA and NDS development due to the variety of functions it offers. It’s been created and maintained by Jasper Vijn and it’s available at
http://www.coranac.com/projects/grit/ however you already have it as it’s the default tool used by NightFox Lib – you can find it in the Tools directory at nflib/tools.
There, you will find several Batch files that we will be using to convert our Bitmaps with. Those include:
Convert_Affine - this Batch file will convert all the graphics in the BMP folder into Tiled Affine Backgrounds and copy them into the affine folder.
Convert_Backgrounds - this Batch file will convert all the graphics in the BMP folder into Tiled Backgrounds and copy them into the backgrounds folder.
Convert_bitmap8 - this Batch file will convert all the graphics in the BMP folder into 8-bit Bitmaps and copy them into the bitmap8 folder.
Convert_bitmap8_shared - this Batch file will convert all the graphics in the BMP folder into 8-bit Bitmaps with a shared palette and copy them into the bitmap8 folder.
Convert_bitmap16 - this Batch file will convert all the graphics in the BMP folder into 16-bit Bitmaps and copy them into the bitmap16 folder.
Convert_CMaps - this Batch file will convert all the graphics in the BMP folder into Collision Maps and copy them into the cmaps folder.
Convert_Fonts - this Batch file will convert fonts and copy them to the fonts folder.
Convert_Sprites - this Batch file will convert all the graphics in the BMP folder into DS-Compatible Sprites and copy them into the sprites folder.
Convert_Sprites_autopal - this Batch file will convert all the graphics in the BMP folder into DS-Compatible Sprites with a shared palette and copy them into the sprites folder.
As you can see, with NightFox Lib’s pre-prepared Batch converter files, converting resources will be a breeze! Remember, by default, all Tiled Graphics have a transparent colour selected, and this colour is Magenta (RGB 255,0,255) – use it whenever you want parts of your graphics to be transparent as it will not appear on the hardware. Another thing worth noting is that our
Backgrounds must have sizes divisible by 256 pixels and our
2D Sprites must have sizes divisible by 8 pixels, 64 pixels being the maximum width/heigh, so keep that in mind before converting to avoid errors.
I didn’t quite know how to start off the subject of
Graphics in
NightFox Lib to make it approachable to anyone, so I figured that the best way to do so would be to do it methodically. In the following few chapters I’m going to introduce the idea behind
MODE’s that the
DS’s 2D Engine uses, what they do and how to take the best advantage of them. We’re going to start with
MODE 0 and climb upwards until we’ve discussed them all. Just so that nobody’s bored during these chapters, I’m also going to introduce the types of graphics most commonly used in the
MODE in question, so that we get a little bit of practical and theoretical knowledge. This chapter will be dedicated to
MODE 0 of the
2D engine in NightFox. This mode offers
4 layers of Tiled Graphics on the screen in question, and it’s likely going to be the
MODE you use most commonly. To initialize a
MODE, we use the command:
NF_Set2D(Screen, MODE);
Simply substitute Screen with use 0 or 1 to select the
Top or
Bottom one, and substitute
MODE with
0, 2 or 5 – the default
MODE’s of
NightFox Lib.
Now, before we do anything else, we have to define the default Folder for our Resources, using this function:
NF_SetRootFolder("ROOT");
Now, to use the Internal filesystem, we substitute the ROOT with NITROFS, in all other cases, we simply input the folder name and the function will enable FAT instead.
With that out of the way, we can start having fun with Tiled Graphics in our projects. We’ll start with Backgrounds. Before we can use any, we have to initialize the buffers for backgrounds, as NightFox streams them from NitroFS or FAT by default. To do so, we use these functions:
NF_InitTiledBgBuffers();
NF_InitTiledBgSys(Screen);
And again, we substitute the word
Screen with the screen we are interested in –
0 or 1.
With that out of the way, we’re ready to select a background to load into
VRAM. Our backgrounds have to be placed in the
“nitrofiles” folder of our project, once they’re there, we can use this function:
NF_LoadTiledBg("Path", "Name", Width, Height);
It will open the background in question, load it into
RAM, attach a name to it and store the parameters of its
Width and
Height (divisible by 256!) so that we don’t have to remember them later on as we code. It’s pretty convenient, but our
Tiled Background is not ready to be displayed yet. As we learned from the previous chapter about RAM and VRAM, we still have to copy it over to VRAM to display it. To do so, we simply use this command:
NF_CreateTiledBg(Screen, Layer, "Name");
As you can see, NightFox will deal with RAM-VRAM communications for us and all we really have to do is to select the
Screen and
Layer on which we’d like to display our background. We won’t tackle the idea of Layers for now beyond the fact that there are
4 Layers on each
Screen, numbered
from 0 to 3, 0 being the
top-most and 3 being the
bottom-most Layer.
Okay, so we have our
Background ready to be displayed – it’s going to appear on our screen right after the next
swiWaitForVBlank(); - perfect!
Just to make sure, let's review our sample code, shall we?
Code:
/*
#############################################
##DS Programming Guide - From Zero To Hero!##
####Example #2 - MODE 0 Tiled Backgrounds####
#############################################
*/
/*
############
##Includes##
############
*/
// Include C
#include <stdio.h>
// Include Libnds
#include <nds.h>
// Include NFLib
#include <nf_lib.h>
/*
###############
##Main(){...}##
###############
*/
int main(int argc, char **argv) {
// Turn on MODE 0 on the Top Screen
NF_Set2D(0, 0);
// Set the Root Folder
NF_SetRootFolder("NITROFS");
// Initialize the Tiled Backgrounds System on the Top Screen
NF_InitTiledBgBuffers();
NF_InitTiledBgSys(0);
// Load the Tiled Background
NF_LoadTiledBg("Background", "Background", 256, 256);
NF_CreateTiledBg(0, 3, "Background");
while(1){
swiWaitForVBlank();
}
return 0;
}
Let's compile this... and...
SUCCESS! We've just learned how to create, load and display
Tiled Backgrounds! Just to make sure that everything is understandable, I'll attach the Example compile folder so that everyone can review the source:
http://www.mediafire...lg6tvxd78gw3kly
I hope that everybody had fun and stay tuned for the next chapters, which will concern Sprites! Foxi4 over and out!