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,

smealum

growing up sucks.
Member
Joined
May 1, 2006
Messages
635
Trophies
2
Age
31
Location
SF
Website
www.smealum.net
XP
2,516
Country
United States
-proof reading-
OUPSIE! Thanks for that, I didn't notice the error in the picture nor did I notice the double && where there's only supposed to be one! See, this is what I meant by "this kind of stuff needs proof-reading.

As for "returning a value", I meant that strictly in a linguistic sense. Assigning "KEY_A" to a Variable is basically equal to assigning the Value "1" to it. It's sort of hard for me to properly phrase it in English, how would you phrase it?

As for ending the Array with a Null terminer, yeah, one is always automatically placed at the end, I'm not sure where you're getting the info that there isn't one. But you are correct, Type Array[3] has 3 variables in it - Array[0], Array[1] and Array[2]. A Null terminer *is* placed on Array[3] to my knowledge, could you elaborate on that part? ;)

I'll fix that in a sec.
For the "return" part it's really just nitpicking, it's not that important. It's just that it makes it sound like BIT is a function when it isn't. (and even that isn't that important) The more important part is saying that the value of BIT(i) is i+1 which it looks to me like you're doing but maybe I just misunderstood. I mean, if you're really just saying KEY_A is the first value, KEY_B is the second etc., than I don't really see the point. Explaining what KEY_A, KEY_B etc's values are would only be useful if you actually explained the bitwise operations involved in using keyHeld afterwards, but since you don't if I were you I'd just list all the existing KEY_*s without really explaining what they are.
I'd also point out that the & operator is not something the reader is supposed to know about yet as you did for the pointer part ( "What's that & doing there? That shouldn't be there, right?". Well, yeah, it should. This "&" refers to a Pointer, you don't know what those are yet and for now you don't need to.") (and come to think of it, maybe say something about how they really aren't the same thing at all ?)

As for the NULL-terminated array - what I'm saying is that having a 0 value at the end of an array is not a necessity. If you define array[3] as you do and it's all correctly initialized, you'll notice that all the values are set to 0, not just the last. The way you put it (especially in your drawing where you distinguish "NULL" from value1 and value2 and when you define an array with a numberofunits+1 number of slots), it seems like the last slot of the array being set to 0 is a necessity and shouldn't be changed, which isn't true. Maybe I'm just reading too much into it, but it was honestly my first impression, so I think changing it would be wise. Also, think about it, how would having a null value work for signaling the end of an array ? That would mean you couldn't store 0s in your array, which would kind of suck. It works for strings because it's the convention that the '\0' character signals the end, but a priori that's the only case where it's true.

EDIT:
EDIT: Wait, I see what you mean about the Array not ending with a NULL - it had 3 members as specified, [0], [1], [2] as specified and neither of them has to be a NULL - I'm a dummy, you're absolutely right. Sorry about that, I got confused. NULL's are only placed when a given member of the Array hasn't got a value assigned, right? I'll fix that now, thanks. I probably wasn't thinking straight that day lol.
Actually, in C, NULL is just a fancy way of saying 0, in a way. To be more accurate, it's a fancy way of saying (void*)0; it's actually a pointer (which is something else that kind of irked me, but I figured it wasnt really worth mentioning since you could have meant "null", which is valid English for 0 :P), but if you do cast it to int, short or whatever it'll just be a 0.
So if you don't assign a value to a slot of an initialized array, it'll stay 0, but it being 0 doesn't mean that its value wasn't changed.
 
  • Like
Reactions: 1 person

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,851
Country
Poland
-proof reading-
OUPSIE! Thanks for that, I didn't notice the error in the picture nor did I notice the double && where there's only supposed to be one! See, this is what I meant by "this kind of stuff needs proof-reading.

As for "returning a value", I meant that strictly in a linguistic sense. Assigning "KEY_A" to a Variable is basically equal to assigning the Value "1" to it. It's sort of hard for me to properly phrase it in English, how would you phrase it?

As for ending the Array with a Null terminer, yeah, one is always automatically placed at the end, I'm not sure where you're getting the info that there isn't one. But you are correct, Type Array[3] has 3 variables in it - Array[0], Array[1] and Array[2]. A Null terminer *is* placed on Array[3] to my knowledge, could you elaborate on that part? ;)

I'll fix that in a sec.
For the "return" part it's really just nitpicking, it's not that important. It's just that it makes it sound like BIT is a function when it isn't. (and even that isn't that important) The more important part is saying that the value of BIT(i) is i+1 which it looks to me like you're doing but maybe I just misunderstood. I mean, if you're really just saying KEY_A is the first value, KEY_B is the second etc., than I don't really see the point. Explaining what KEY_A, KEY_B etc's values are would only be useful if you actually explained the bitwise operations involved in using keyHeld afterwards, but since you don't if I were you I'd just list all the existing KEY_*s without really explaining what they are.
I'd also point out that the & operator is not something the reader is supposed to know about yet as you did for the pointer part ( "What's that & doing there? That shouldn't be there, right?". Well, yeah, it should. This "&" refers to a Pointer, you don't know what those are yet and for now you don't need to.") (and come to think of it, maybe say something about how they really aren't the same thing at all ?)

As for the NULL-terminated array - what I'm saying is that having a 0 value at the end of an array is not a necessity. If you define array[3] as you do and it's all correctly initialized, you'll notice that all the values are set to 0, not just the last. The way you put it (especially in your drawing where you distinguish "NULL" from value1 and value2 and when you define an array with a numberofunits+1 number of slots), it seems like the last slot of the array being set to 0 is a necessity and shouldn't be changed, which isn't true. Maybe I'm just reading too much into it, but it was honestly my first impression, so I think changing it would be wise. Also, think about it, how would having a null value work for signaling the end of an array ? That would mean you couldn't store 0s in your array, which would kind of suck. It works for strings because it's the convention that the '\0' character signals the end, but a priori that's the only case where it's true.

EDIT:
EDIT: Wait, I see what you mean about the Array not ending with a NULL - it had 3 members as specified, [0], [1], [2] as specified and neither of them has to be a NULL - I'm a dummy, you're absolutely right. Sorry about that, I got confused. NULL's are only placed when a given member of the Array hasn't got a value assigned, right? I'll fix that now, thanks. I probably wasn't thinking straight that day lol.
Actually, in C, NULL is just a fancy way of saying 0, in a way. To be more accurate, it's a fancy way of saying (void*)0; it's actually a pointer (which is something else that kind of irked me, but I figured it wasnt really worth mentioning since you could have meant "null", which is valid English for 0 :P), but if you do cast it to int, short or whatever it'll just be a 0.
Yes, I edited my post as I've realized exactly what you meant, I wrote that particular chapter pretty late at night. :P

You are correct, I probably shouldn't have used the word return as it suggest that it actually return()'s something when it isn't, so I'm getting rid of that altoghether - it's not like it's a useful piece of information. The double "&&" was a slight of hand to be honest and it got duplicated when copy-pasting the same, wrong if statement - my bad.

As for the Array being declared as "Number of Slots+1", I wanted people to know that when declaring an Array, when they want to have access to slots from [0] to [3] then they have to declare it as [4] as this is the factual ammount of slots in that array - now that I've re-read it it was indeed misleading, I've edited that and I'll proof-read from that point on again.

I know Null is a fancy way of saying zero, but I'd like the users to be accustomed with using that. :P Besides, in C, it's not "exactly zero" - zero is a value "0", Null is "literally nothing", the "\0", surely you catch my drift. :P

Thanks again for reading the guide and picking up my slips, I wouldn't want the readers to digest something that isn't right just because I wasn't paying attention. :)

In any case...

@CannonFoddr

You're going to kill me for this, but the Array chapter has been edited... *again*... I need to pay more attention, lol.
 

smealum

growing up sucks.
Member
Joined
May 1, 2006
Messages
635
Trophies
2
Age
31
Location
SF
Website
www.smealum.net
XP
2,516
Country
United States
I know Null is a fancy way of saying zero, but I'd like the users to be accustomed with using that. :P Besides, in C, it's not "exactly zero" - zero is a value "0", Null is "literally nothing", the "\0", surely you catch my drift. :P
Well it's not very important, but actually I'm not sure I do catch your drift. I mean, I'm pretty sure NULL is defined as being (void*)0, so for all intents and purposes, NULL is 0, just not with an integer type. But in memory, it *is* a 0. And the same goes for '\0'; in fact, '\n'=n if n is a number.
Anyway, my point is there is no way to distinguish a 0 from NULL or even '\0'.
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,851
Country
Poland
I know Null is a fancy way of saying zero, but I'd like the users to be accustomed with using that. :P Besides, in C, it's not "exactly zero" - zero is a value "0", Null is "literally nothing", the "\0", surely you catch my drift. :P
Well it's not very important, but actually I'm not sure I do catch your drift. I mean, I'm pretty sure NULL is defined as being (void*)0, so for all intents and purposes, NULL is 0, just not with an integer type. But in memory, it *is* a 0. And the same goes for '\0'; in fact, '\n'=n if n is a number.
Anyway, my point is there is no way to distinguish a 0 from NULL or even '\0'.
What I mean by this is that it doesn't always have to be an "integer situation", a NULL could be used to signify an end of a stream (EOF), it's also acting differently when inside a string, where a "0" will be a "0" and a NULL will be... well, nothing, really. It's a sign that is often appended to Strings and such by native C functions, so I want the distinction to be clear.
 

smealum

growing up sucks.
Member
Joined
May 1, 2006
Messages
635
Trophies
2
Age
31
Location
SF
Website
www.smealum.net
XP
2,516
Country
United States
I know Null is a fancy way of saying zero, but I'd like the users to be accustomed with using that. :P Besides, in C, it's not "exactly zero" - zero is a value "0", Null is "literally nothing", the "\0", surely you catch my drift. :P
Well it's not very important, but actually I'm not sure I do catch your drift. I mean, I'm pretty sure NULL is defined as being (void*)0, so for all intents and purposes, NULL is 0, just not with an integer type. But in memory, it *is* a 0. And the same goes for '\0'; in fact, '\n'=n if n is a number.
Anyway, my point is there is no way to distinguish a 0 from NULL or even '\0'.
What I mean by this is that it doesn't always have to be an "integer situation", a NULL could be used to signify an end of a stream (EOF), it's also acting differently when inside a string, where a "0" will be a "0" and a NULL will be... well, nothing, really. It's a sign that is often appended to Strings and such by native C functions, so I want the distinction to be clear.
Ah, I think I see where the confusion lies. I guess I was talking about 0 and you were talking about '0' ? Because yeah, in a string, '0'==48, and '\0'==0 (and to be entirely correct, not NULL since you'd have to cast it or get a warning).
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,851
Country
Poland
I know Null is a fancy way of saying zero, but I'd like the users to be accustomed with using that. :P Besides, in C, it's not "exactly zero" - zero is a value "0", Null is "literally nothing", the "\0", surely you catch my drift. :P
Well it's not very important, but actually I'm not sure I do catch your drift. I mean, I'm pretty sure NULL is defined as being (void*)0, so for all intents and purposes, NULL is 0, just not with an integer type. But in memory, it *is* a 0. And the same goes for '\0'; in fact, '\n'=n if n is a number.
Anyway, my point is there is no way to distinguish a 0 from NULL or even '\0'.
What I mean by this is that it doesn't always have to be an "integer situation", a NULL could be used to signify an end of a stream (EOF), it's also acting differently when inside a string, where a "0" will be a "0" and a NULL will be... well, nothing, really. It's a sign that is often appended to Strings and such by native C functions, so I want the distinction to be clear.
Ah, I think I see where the confusion lies. I guess I was talking about 0 and you were talking about '0' ? Because yeah, in a string, '0'==48, and '\0'==0 (and to be entirely correct, not NULL since you'd have to cast it or get a warning).
Yeppers, that is exactly what I meant. :)
 

CannonFoddr

Regular GBATemp Lurker
Member
Joined
Sep 23, 2006
Messages
4,134
Trophies
1
Age
56
Location
Sitting by computer
Website
www.youtube.com
XP
1,287
Country
@CannonFoddr
You're going to kill me for this, but the Array chapter has been edited... *again*... I need to pay more attention, lol.
Good job I've been a little slow getting back onto PC then...anyway new v1-6.1 out now (so includes any changes up to 5th April) - Contains
> Chapters 1-6 of 'Introductory'
> Chapter 1 of 'Practical use of libnds'
[Didn't know you were gonna have different 'sections' (Introductory/Practical use...) but think I've done a fair job splitting it up... :unsure: ]

Also slight layout change to PDF.
I've start each 'chapter' on a new 'page' - this allows easier printouts (I hope) of individual chapters - & used Word's spell check to correct a few spelling mistakes (e.g. 'immediatelly' :wacko: )
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,851
Country
Poland
I've start each 'chapter' on a new 'page' - this allows easier printouts (I hope) of individual chapters - & used Word's spell check to correct a few spelling mistakes (e.g. 'immediatelly' :wacko: )
Good thinking, spelling is my vice sometimes lol. I ought to do the same thing myself, but Word likes to screw up the code side of things.
 

gillman

Well-Known Member
Newcomer
Joined
Nov 30, 2007
Messages
61
Trophies
0
XP
132
Country
United States
Hello, I just had a few questions about programming on the DS in general. My main question is: is this is the only way to do it? Basically is this how everyone in the homebrew community programs for the DS? I have experience in programming in quite a few languages (C, C++, Java, Python etc.) and on other platforms such as Android. Basically it seems like to write homebrew someone wrote a library for C which can interact with the special DS hardware. Are there other packages like this for other languages? Also I had a question of how this is done by actual companies making commercial games on the DS. I'm guessing there's just a devkit for them which allows them a lot of functionality.

Thank you.
 

smealum

growing up sucks.
Member
Joined
May 1, 2006
Messages
635
Trophies
2
Age
31
Location
SF
Website
www.smealum.net
XP
2,516
Country
United States
Hello, I just had a few questions about programming on the DS in general. My main question is: is this is the only way to do it? Basically is this how everyone in the homebrew community programs for the DS? I have experience in programming in quite a few languages (C, C++, Java, Python etc.) and on other platforms such as Android. Basically it seems like to write homebrew someone wrote a library for C which can interact with the special DS hardware. Are there other packages like this for other languages? Also I had a question of how this is done by actual companies making commercial games on the DS. I'm guessing there's just a devkit for them which allows them a lot of functionality.

Thank you.
Most people making stuff for the DS use C/C++. Obviously you can use assembly as well, and given the limited nature of the DS's hardware I'm sure quite a few people use it at least to optimize certain aspects of their code (and that's when they're not crazy enough to make their whole game in assembly like headsoft does). And yeah, most people use libnds, and most other available libraries use libnds themselves. PAlib used to be "autosufficient" but that was like 6 years ago and didn't last long. Of course you don't *have* to use libnds; you could probably program in C using just gbatek as reference, or even make your own alternative to libnds, but I don't really see what the point would be (aside from doing it as a learning project that is).
The only other "widely" used language would be lua via applications like microLua, but then lua is a scripting language (read : interpreted, not native code) and it's debatable whether it's really a good fit to make a whole game/application.
As for official developers, they make their stuff in C/C++/asm just like us. They have a set of libraries which is more complete than libnds, but whether it's actually "better" is also up for debate. Can't really get into details here though, but aside from debugging tools I'd say their setup is pretty much the same as ours.
 

corenting

Well-Known Member
Member
Joined
Jul 17, 2008
Messages
288
Trophies
1
Website
www.corenting.fr
XP
986
Country
France
Bump !

I just saw this topic. It's very good, I'm looking forward to NFLib chapters to improve my skills :D
I hope this will inspire people to develop for the DS as the homebrew scene for is quite dead now.

If someone wants to see how to use MaxMod in a NFLib project, LeRodeur helped with that for my DSDaft homebrew.
You can get the source code on this Github repo if you want to learn from example : https://github.com/corenting/DSDaft
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,851
Country
Poland
Bump !

I just saw this topic. It's very good, I'm looking forward to NFLib chapters to improve my skills :D
I hope this will inspire people to develop for the DS as the homebrew scene for is quite dead now.

If someone wants to see how to use MaxMod in a NFLib project, LeRodeur helped with that for my DSDaft homebrew.
You can get the source code on this Github repo if you want to learn from example : https://github.com/corenting/DSDaft
Thanks for the bump, new chapters will be on the way but let's just say that I've been through some difficult time at university plus Deus Ex: Human Revolution plus Syndicate. I've been *quite busy*. I should have more spare time next week though and I am planning a new Chapter, most likely concerning the way the DS divides its memory for certain things as it was requested and it will help in later Chapters dedicated purely to NFLib and its implementation of the Sprite/Background systems.
 

corenting

Well-Known Member
Member
Joined
Jul 17, 2008
Messages
288
Trophies
1
Website
www.corenting.fr
XP
986
Country
France
Thanks for the bump, new chapters will be on the way but let's just say that I've been through some difficult time at university plus Deus Ex: Human Revolution plus Syndicate. I've been *quite busy*. I should have more spare time next week though and I am planning a new Chapter, most likely concerning the way the DS divides its memory for certain things as it was requested and it will help in later Chapters dedicated purely to NFLib and its implementation of the Sprite/Background systems.

Cool ! Looking forward for new chapters but take your time to play too :P
 

CannonFoddr

Regular GBATemp Lurker
Member
Joined
Sep 23, 2006
Messages
4,134
Trophies
1
Age
56
Location
Sitting by computer
Website
www.youtube.com
XP
1,287
Country
Thanks for the bump, new chapters will be on the way but let's just say that I've been through some difficult time at university plus Deus Ex: Human Revolution plus Syndicate. I've been *quite busy*. I should have more spare time next week though and I am planning a new Chapter, most likely concerning the way the DS divides its memory for certain things as it was requested and it will help in later Chapters dedicated purely to NFLib and its implementation of the Sprite/Background systems.
Cool ! Looking forward for new chapters but take your time to play too :P
Nah - take your time over the chapters -
It'll mean I don't have to update the PDF in the near future ;) :lol:
 

Kiaku

Well-Known Member
Member
Joined
Mar 22, 2010
Messages
273
Trophies
1
Age
30
XP
381
Country
United States
Is there a way to compile the codes into nds with another program other than Microsoft Visual C++? Preferably a free one, since I don't have much expense to give up, and that I'm just starting to learn coding.

Edit: Nevermind. I didn't read closely that registration is free for the program. xD Fail
 

Snailface

My frothing demand for 3ds homebrew is increasing
Member
Joined
Sep 20, 2010
Messages
4,324
Trophies
2
Age
40
Location
Engine Room with Cyan, watching him learn.
XP
2,256
Is there a way to compile the codes into nds with another program other than Microsoft Visual C++? Preferably a free one, since I don't have much expense to give up, and that I'm just starting to learn coding.
Microsoft Visual Express C++ is free.
http://www.microsoft...ual-cpp-express

I just use Programmer's Notepad that comes with Devkitpro for my projects. It's free too.
(I would recommend this option, it's a lot less hassle.)
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,851
Country
Poland
Is there a way to compile the codes into nds with another program other than Microsoft Visual C++? Preferably a free one, since I don't have much expense to give up, and that I'm just starting to learn coding.
Microsoft Visual Express C++ is free.
http://www.microsoft...ual-cpp-express

I just use Programmer's Notepad that comes with Devkitpro for my projects. It's free too.
(I would recommend this option, it's a lot less hassle.)
Less hassle but less functionality - with Visual C++ you can keep track of all your resources in one window, even all Includes, NitroFiles, Data and so on and so forth. It's just more convenient.
 

Kiaku

Well-Known Member
Member
Joined
Mar 22, 2010
Messages
273
Trophies
1
Age
30
XP
381
Country
United States
Let's start with Keys. First and foremost, to use Key Input, we need to scan for their use. To do that, we place the function:

scanKeys();





Within the main while(); loop of the program. This will initialize the Key scanner and thus allow us to use them as a method of input.


What did you mean by "Within the main while(); loop of the program"? I don't see such a line in the default "template.c", and I can't seem to get my compiled nds file to scan the keys... Here are my source codes:
Code:
#include 
#include 
int main(void) {
scanKeys();
consoleDemoInit();
iprintf("Hold the A button to\ngenerate text.\n");
if(KEY_A & keysHeld()) {
iprintf("The A button is held.");
}
while(1) {
swiWaitForVBlank();
}
}
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    K3Nv2 @ K3Nv2: https://youtu.be/9kE3Env_2AY?si=Bs6lUZ0ZIlqmYaGT