Tutorial  Updated

DS Programming for Newbies!

3Lwpv.png

Table of Contents:

Introductory Chapters:
  1. Preparing the environment
  2. Variables!
  3. Functions!
  4. Operators in C
  5. Conditions - if/else Statements and switches
  6. Looping - for() and while() Loops
  7. Containers of Variables - Arrays and Structures
Introduction to DS Hardware:
  1. RAM and VRAM
  2. OAM and 2D Sprites
Practical use of libnds:
  1. Input: Keys and the Touchscreen
Practical Use of NightFox Lib:
  1. NightFox Lib Integration
  2. 2D MODE-0 Part 1 - Tiled Backgrounds
  3. 2D MODE-0 Part 2 - Tiled Sprites
Excercises:
  1. Your first program!
  2. MODE-0 Tiled Backgrounds Example
  3. MODE-0 Tiled Sprites Example
  4. Our very first game: Tic Tac Toe!
Additional Utilities:
  1. GRIT


:download: PDF Version maintained by CannonFoddr available on FileTrip HERE!

:download: PDF Version maintained by Pomegrenade GBAtemp Mirror HERE!




Preface



Hello and welcome! If you are reading this then it’s likely that you’re interested in getting to know more about programming for the Nintendo DS! If you are not, then you likely took the wrong turn, but let’s not get into that. Let’s also start with establishing one important thing – as the title suggests, this is a “From Zero to Hero” guide. If you are an experienced programmer then it is likely that you will not benefit from it much, if at all. It is going to introduce the very basics to users who have never even seen a compiler before and never coded in their life – stuff that you probably already know and aren’t interested in anymore. You are however still welcome as this is my first tutorial and will likely require a certain degree of proof-reading, plus, you may of course have useful suggestions! Keep in mind the target audience though, I’m doing my best not to introduce complicated concepts early on. If you’re not an experienced programmer or never programmed at all, this is a great place to start!

I’ve seen many guides approaching this subject – some were more helpful, some were rather vague, but there is one thing that was common in all of them, and it became apparent to me that something has to be done about it. The guides I’ve seen so-far are dedicated to users who are familiar with programming and only require an introduction to the DS environment, none of them are actually “tutorials” from the ground up. Does this mean that a non-experienced user simply cannot program for the DS or should not begin his adventure with programming on this exact platform? No, it does not! In fact, the DS is likely the easiest platform to program for when it comes to consoles – libnds is really not that hard to wrap your mind around and there are numerous libraries out there that facilitate programming for it even further. You probably want to ask: “If it’s so easy, why do You think it requires some sort of an explanation? The libraries are well-documented, do you expect the readers to be dill-wits who can’t follow simple examples?” and the answer to that is “No, in fact, I do believe that everybody is capable of programming, however one has to learn and acquire some basic programming habits and have some practice in C to be successful at it” and this is exactly the main goal of this tutorial. Depending on the interest shown by users and my workload at Uni this may or may not be a full-featured guide, however I promise that I will at least try to keep it up-to-date and expand upon it from time to time.

Now that the purpose is established, let’s move on to the juicy parts! I hope you will enjoy learning together and in case of any questions or suggestions, do write! Dear readers, keep in mind that the first few tutorials will be an incredibly rapid course in C, applicable to any type of programming, not just for the DS! We won’t be compiling much until this material is covered and thoroughly understood! So… Let’s get it on!
 
Last edited by Foxi4,
any idea of how saves work? Havn't been able to find any examples or metions of it online.
Sure. The way I used to do it is that I prepared an array of variables that I want to save, then I saved them to a file using FAT/loaded from said file. Libfat works pretty much the same as NitroFS, with the added benefit of being able to access external files. You should be able to find a code reference for it. Once you're done with your saving/loading you can switch back to NitroFS and the program will continue as normal, I'm pretty sure NightFox already has libfat built-in.
 
Last edited by Foxi4,
Hmmm, I don't quiet understand it, I have something that I think should set nitroFS up, make a file and write in it but it just crashes, any ideas?
------------------------------------------------
void save()
{
nitroFSInit(NULL);
chdir("nitro:/");

FILE *save_file;

save_file = fopen("test", "w+");

fprintf(save_file, "TEST123\n");

}
------------------------------------------------
 
Hmmm, I don't quiet understand it, I have something that I think should set nitroFS up, make a file and write in it but it just crashes, any ideas?
------------------------------------------------
void save()
{
nitroFSInit(NULL);
chdir("nitro:/");

FILE *save_file;

save_file = fopen("test", "w+");

fprintf(save_file, "TEST123\n");

}
------------------------------------------------
NitroFS is embedded into the binary and, as far as I know, read-only. If you want to save to an external file, you should be using FAT. It works exactly the same on every platform. You also don't need to specify the directory, the default is already root.

This should be helpful:

https://devkitpro.org/viewtopic.php?t=2547

Be sure to reinitialise NitroFS once you're done saving. I don't think you need to #include <fat.h> anymore, it's already included into libnds. It's also worth specifying that you're writing to a binary file by using the wb modifier. You don't need to update the file with w+, a normal write will discard the contents of the old file.
 
Thank you!
Post an update after your rework, I hope it'll work. Be mindful of the fact that you should be testing on real hardware if possible. Chances are your flashcart automatically DLDI-patches whatever you run, but double-check that the feature is enabled.

EDIT: Come to think of it, I vaguely remember that there was a way to keep both initialised and working at the same time, but I've always reinitialised the filesystem after saving for peace of mind. Perhaps this will shed some more light on the issue:

http://blea.ch/wiki/index.php/Nitrofs
 
Last edited by Foxi4,
It works :D, hoping to release it this the summer. Here's a link to it's github, the code is trash and burns my eyes every time I look at it but it works. https://github.com/Tarudahat/SinkHole_X/tree/master/Consoles/NDS
Glad that I could guide you on the right path, good luck with your project! :D

EDIT: @TaruDev_, I had a quick look at the NFLib documentation since you're using it in your project and I noticed that NF_SetRootFolder() already uses fatInitDefault() if the folder you select is anything other than "NITROFS" so you don't need to use the function twice in your source. Using NF_SetRootFolder() alone should switch between the two file systems as needed and should allow you to get rid of some lines of unnecessary code. It also reinitialises NitroFS once you switch back, so that's not needed either. I knew there was a way to seamlessly use both. :)

https://sourceforge.net/p/nflib/souce/1/tree/source/nf_basic.c
 
Last edited by Foxi4,
May I ask what is a good/decent image editor to create sprites with?
Or rather, the settings one has to save an image with in order to load it up with NFLib?

Thanks, amigo!
 
May I ask what is a good/decent image editor to create sprites with?
Or rather, the settings one has to save an image with in order to load it up with NFLib?

Thanks, amigo!
NFLib should come with a copy of GRIT that has all the batch files you'll need to convert bitmaps into sprites, backgrounds or textures - what you use beyond that is up to personal preference. Back in the day I used Paint.NET to draw and IrfanView to adjust palettes/colour depth.
 
  • Like
Reactions: eyeliner
NFLib should come with a copy of GRIT that has all the batch files you'll need to convert bitmaps into sprites, backgrounds or textures - what you use beyond that is up to personal preference. Back in the day I used Paint.NET to draw and IrfanView to adjust palettes/colour depth.
It did not, but I managed to find it. WinGrit isn't working because of compatibility, dammit...
I really didn't want to mess with yet another command-line app right now...
Oh well, guess I have to persevere.
 
It did not, but I managed to find it. WinGrit isn't working because of compatibility, dammit...
I really didn't want to mess with yet another command-line app right now...
Oh well, guess I have to persevere.
I've always used the command line tool, the Windows GUI unnecessarily complicates things when you can just drop all of your assets into a folder and double-click on the correct batch file.
 
I've always used the command line tool, the Windows GUI unnecessarily complicates things when you can just drop all of your assets into a folder and double-click on the correct batch file.
Thing is, I have no NF batch for grit, because apparently, the GitHub NFLib doesn't include it.
But that's ok, I'll figure something out.
Thanks.
 
Thing is, I have no NF batch for grit, because apparently, the GitHub NFLib doesn't include it.
But that's ok, I'll figure something out.
Thanks.
You should be able to download the complete package from the NFlib site, the Git repository probably only has the library on there.
 
Apparently, grit comes with devkitpro, now. It's there in the bin folder...
Yeah, it's there de facto, but the NFlib version also has pre-made configuration batch files to make things pretty much drag-and-drop. Not that it matters now, it seems you have it all figured out now. :)
 
Yeah, it's there de facto, but the NFlib version also has pre-made configuration batch files to make things pretty much drag-and-drop. Not that it matters now, it seems you have it all figured out now. :)
To make them work with modern formats you need to modify them slightly, at least I had to. It's not much tho, just add the -gB8 flag. That ensures that grit outputs the format that nflib uses when loading sprites
 
Thanks for the tutorial. I have tried to install DevKitPro using the Windows installer but are still in the dark on how to compile my code.
 
Thanks for the tutorial. I have tried to install DevKitPro using the Windows installer but are still in the dark on how to compile my code.
Late reply, but you have to run the "make" command wherever your Makefile is
 

Site & Scene News

Popular threads in this forum