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,

plasturion

temporary hermit
Member
Joined
Aug 17, 2012
Messages
1,240
Trophies
2
Location
Tree
XP
3,536
Country
Poland
keysHeld() returns status of all the keys held in one 32bit value. KEY_TOUCH represents pressed touchscreen and let's say it's 7th bit (so KEY_TOUCH = 127) and KEY_A lets say is the first bit of returned value (1)... if only A is held and touchscreen pressed we've got 128 after recall keysHeld(). For single bits you can't use && here, && is for single datatype like byte or int. if this kind of datatype equals 1 or more it has value of "true" no matter how many and which one bits are switched on, so we can't test bits in this way.

iprintf() allows you to use escape codes, to set cursor at selected position or change colors.
 
Last edited by plasturion,
  • Like
Reactions: Foxi4

plasturion

temporary hermit
Member
Joined
Aug 17, 2012
Messages
1,240
Trophies
2
Location
Tree
XP
3,536
Country
Poland
however maybe I'm wrong because i read that iprintf can't format floating point values. It's integer pritntf.
I wonder if there are more differences, honestly idk. NDS processor hasn't hardware floating point arithmetics so I'm guessing iprintf can be more corresponding to the possible effects?
 
Last edited by plasturion,

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
however maybe I'm wrong because i read that iprintf can't format floating point values. It's integer pritntf.
I wonder if there are more differences, honestly idk. NDS processor hasn't hardware floating point arithmetics so I'm guessing iprintf can be more corresponding to the possible effects?
I can't find the source where I read this, so I could have read it wrong, but I did hear that using iprintf() is more efficient to use on the NDS as it would take up less resources compare to printf(), but apparently it's not by much. I think this refers back to what you were saying iprintf() can't format floating point values and the NDS ARM processor does not have support for floating point value calculations. Other then that, I also don't know. Anyways, any information is good information, thanks!
Post automatically merged:

Also about the GRIT tool provided earlier, with the guide and with looking at the GRIT documentation, I can't seem to getting my sprites converted. I am getting errors of file not found. It could be me using it wrong. Does anyone have a guide/tips where on how I can use that GRIT batch tools, to convert sprites and backgrounds? Thanks for any help in advance!
 
Last edited by BlueFalconNDS,

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,834
Trophies
3
Location
Gaming Grotto
XP
29,894
Country
Poland
Hopefully I'm not annoying you guys with these questions but I have a few more.

I notice when taking input in a if statement [ if (KEY_TOUCH & keysHeld())], why is that we use one & and not &&? I do understand it is a bitwise AND, however I want to learn about the context here.

Also what's the difference of using printf() and iprintf(), I used both of them and they to be doing the same thing, is there a difference/recommendations?
printf and iprintf are both functions that “print” text, but they have different modifiers. iprintf() is a restricted version of printf(), meaning it has the same behaviour and arguments, but it cannot perform any floating-point formatting. On the DS the difference is mostly inconsequential, but using iprintf() is better practice. You’re using a tiled font, it can already be anything you want.

& and && are not one and the same. && is the logical AND operator, it means that both conditions must be satisfied in order for the statement to be true. & is a binary AND operator, it copies a bit to the result if it exists in both operands.

Example:

A = 60 (0011 1100 in binary)
B = 10 (0000 1010 in binary)
A&B = 8 (0000 1000 in binary)

Imagine it this way, you’re comparing two binary numbers space by space. If both are “0” then the result is “0”. If one is “0” and the other is “1”, the result is “0”. If both are “1” then the result is “1”. Here it is illustrated:

EE75BC6E-A79C-47CF-8439-E4E64B54FB09.png

Imagine the two numbers are stacked and you’re going column by column. Your result equals whichever bits were “1” in both operand A AND operand B.
 
  • Like
Reactions: BlueFalconNDS

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
@Foxi4 Ah I see, thanks for clearing up my confusion about printf() and iprintf()! Also the & AND operator explanation really helped! :yay:

I’m still kind of confused though about the graphics side of thing of converting sprites and background with GRIT :huh:
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,834
Trophies
3
Location
Gaming Grotto
XP
29,894
Country
Poland
@Foxi4 Ah I see, thanks for clearing up my confusion about printf() and iprintf()! Also the & AND operator explanation really helped! :yay:

I’m still kind of confused though about the graphics side of thing of converting sprites and background with GRIT :huh:
The linked sourceforge repository comes with the batch files you need. As far as theory goes, all you need to know is that the bitmap you’re converting gets sliced into tiles, repeating tiles are removed, colours are stripped into a palette and presto, you have all the ingredients to recreate the image, but they take less space and are easier to handle. To give you a real world example, imagine the image is turned into a puzzle set *and* a colouring book at the same time.
 

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
The linked sourceforge repository comes with the batch files you need. As far as theory goes, all you need to know is that the bitmap you’re converting gets sliced into tiles, repeating tiles are removed, colours are stripped into a palette and presto, you have all the ingredients to recreate the image, but they take less space and are easier to handle. To give you a real world example, imagine the image is turned into a puzzle set *and* a colouring book at the same time.
Oh ok yeah the theory makes sense. I just couldn’t batch file to work, I am use the grit.zip that @akbat provided earlier in the thread. I have a sprite 32x32 in .bmp format, but I couldn’t get to convert as when I run the batch files it says file not found. Also convert_sprite.bat ask for a pallet file which I’m lost there. I try putting my sprite in different of the folders but no luck. It’s most likely I’m just using the tool wrong/lack of knowledge of how it works. Anyone with guided steps?
 

akbat

Member
Newcomer
Joined
Sep 2, 2020
Messages
6
Trophies
0
Age
27
XP
97
Country
United States
General advice: don't run batch scripts without opening them and reading what they do first.

I used "convert_bitmap8.bat". Place your .bmp files in the "bmp" folder. It will create the files in the "bitmap8" folder; there should be 2 of interest: <your_name>.img.bin and <your_name>.pal.bin. Rename them to remove the .bin part.

The "convert_sprite.bat" script looks like it will create the palette file, it's just asking you what it should name it.

Even I don't fully understand what grit is doing, just the high-level concept. If anyone has good explanation video or article, I'd also be interested.
 

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
General advice: don't run batch scripts without opening them and reading what they do first.
Yeah, that's rule number one. I did actually look in to the commands the files are trying to make cross referencing the GRIT documentation. It is thought tricky to fully understand.

I used "convert_bitmap8.bat". Place your .bmp files in the "bmp" folder. It will create the files in the "bitmap8" folder; there should be 2 of interest: <your_name>.img.bin and <your_name>.pal.bin. Rename them to remove the .bin part.

The "convert_sprite.bat" script looks like it will create the palette file, it's just asking you what it should name it.

Even I don't fully understand what grit is doing, just the high-level concept. If anyone has good explanation video or article, I'd also be interested.
I re try to follow the steps you provided, but I'm still getting a error.

EDIT: I got it to work to convert, as I use GIMP, I re exported without extra data and changing 24-bit to 16 bit color I believe. However when I try loading this sprite in, it looks really glitchy. Any fix or export option I should have to fix this?
glitchy.PNG
 
Last edited by BlueFalconNDS,

plasturion

temporary hermit
Member
Joined
Aug 17, 2012
Messages
1,240
Trophies
2
Location
Tree
XP
3,536
Country
Poland
As far I know nflib support only 256 (8-bit) colors sprites.
NDS engine support also 4-bit and 16-bit modes for sprites but that need some mod extension for nflib.
I would like to use 16-colors sprites and backgrounds, you could load bigger tileset (or frameset) in VRAM.
 

Valkyrience

Member
Newcomer
Joined
Aug 12, 2023
Messages
14
Trophies
0
Age
25
XP
27
Country
Ukraine
Hi. I have a couple more questions:
1) Is there some way to use semitransparent sprites(with alpha channel) in Night Fox lib?
2) About saving game progress. I understand that I should use libfat for this. But in this thread was written that, if I use night Fox library I need to switch between file systems. And so I have a few questions:
-Should I use fatInit() or nf_lib does it?
-How to switch between file systems? Can you write example?
Thank you in advance.
 

plasturion

temporary hermit
Member
Joined
Aug 17, 2012
Messages
1,240
Trophies
2
Location
Tree
XP
3,536
Country
Poland
some way to use semitransparent sprites(with alpha channel) in Night Fox lib?
2) About saving game progress. I understand that I should use libfat for this. But in this thread was written that, if I use night Fox library I need to switch between file systems. And so I have a few questions:
-Should I use fatInit() or nf_lib does it?
-How to switch between file systems? Can you write example?
Thank you in advance.
2) nf_lib doesn't init fat system well for DSi, to make comaptible for DSi mode and slot-1 flashcards it's good to make those kind of selection
if(isDSiMode())
chdir("sd:/");}
else
chdir("fat:/");
or ommit chdir command completely along with NF_SetRootFolder("NITROFS"); instead of this include nitrofs:/ in filename paths. What is changed since DSi scene was wide open is filesystem.h library. i'm including both but maybe this one is enough.
 

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
As far I know nflib support only 256 (8-bit) colors sprites.
NDS engine support also 4-bit and 16-bit modes for sprites but that need some mod extension for nflib.
I would like to use 16-colors sprites and backgrounds, you could load bigger tileset (or frameset) in VRAM.
Sorry for the spam, but I still need some help. I'm using GIMP, and I made sure to check that it's going to be 8 bit colors sprites, and I also check for "Do not write color space information.", then I export it as a bmp, then use the grit converter tool, "Convert_bitmap8.map", then when I actually try to use the .img and .pal, the sprite is glitchy. I made sure the resolution is right, I indexed the color pallet making sure it was 8 bit. Anyone using GIMP and or just anyone know a fix?
 
Last edited by BlueFalconNDS,

plasturion

temporary hermit
Member
Joined
Aug 17, 2012
Messages
1,240
Trophies
2
Location
Tree
XP
3,536
Country
Poland
As for the sprites "Convert_sprites_autopal*.bat" works for me.
You could also use png format if you like as grit also support it, it need only small edit.
I'm not using GIMP so I can't tell, but GIMP should do the job, I guess.
 

plasturion

temporary hermit
Member
Joined
Aug 17, 2012
Messages
1,240
Trophies
2
Location
Tree
XP
3,536
Country
Poland
also for additional grit parameters in convert_sprite_autopal.bat you can create other bat files.
I just found those in my dir, so I guess it works. for square format sprites:
16px: -Mw2 -Mh2
32px: -Mw4 -Mh4
64px: -Mw8 -Mh8
for rectangular ones mix some of them.
If you don't want to use magenta but green transparent color change -gTFF00FF to -gT00FF00
I guess those two are most popular transparent color masks used in game resources.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    Veho @ Veho: Wow, only $700?