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,

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,845
Country
Poland
Quite well written! A polished product.
Glad you guys like it! I've written the first few paragraphs for the next chapter, I think it should be up next week. Weekly updates sound sensible. :P
Publish it as "Ds Programming for Dummies"?
Hehe, well, it was supposed to be titled like that, but then I though - "I hate "For Dummies" guides - they imply I'm an idiot for reading them!" so I chose something more approachable, like "101" since it's only going to feature what could be considered the absolute basics. The readers definatelly won't be able to write "Crisis 3" by the time they finish reading, but it'll be a solid base to start - more advanced tutorials are already out there, they're just not approachable by compelete beginners.
 
  • Like
Reactions: 1 person

Zerosuit connor

Baby I'm Back ♥
Member
Joined
Sep 17, 2010
Messages
1,848
Trophies
1
Location
Eorzea
XP
703
Country
Quite well written! A polished product.
Glad you guys like it! I've written the first few paragraphs for the next chapter, I think it should be up next week. Weekly updates sound sensible. :P
Publish it as "Ds Programming for Dummies"?
Hehe, well, it was supposed to be titled like that, but then I though - "I hate "For Dummies" guides - they imply I'm an idiot for reading them!" so I chose something more approachable, like "101" since it's only going to feature what could be considered the absolute basics. The readers definatelly won't be able to write "Crisis 3" by the time they finish reading, but it'll be a solid base to start - more advanced tutorials are already out there, they're just not approachable by compelete beginners.
Also I think you need to edit from zero to hero
Make it from Zeroas you "borrowed" the prefix of my name ;)
 
  • Like
Reactions: 1 person

Terminator02

ヽ( 。 ヮ゚)ノ
Member
Joined
Apr 10, 2010
Messages
4,516
Trophies
1
Location
Somewhere near monkat
XP
1,089
Country
United States
nice guide Foxi4. But I don't recommend putting anything in spoiler tags.


By hiding the content into a spoiler tag, you also hide it from Google and other crawler bots.
Which means it'll make it harder for people to find your review on search engines.
Just make a big ass page and everyone will be fine.

And personally I don't like having to click all sorts of buttons to open all sections one by one... the content should be there right under my eyes, 0 click away.

and congrats again for the guide. :)
I should have known this a long time ago, I must go edit some of my guides.

Good work on this Foxi, haven't read it yet, and probably won't til later this week, but it looks like you put a lot of effort into it.
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,845
Country
Poland
Hey guys, Foxi here with a new chapter for the tutorial! I didn't quite meet the deadline I imposed on myself but I didn't want the interested parties to wait any further for the next installment, thus I will simply present what I have so-far and expand upon it (talk about if statements and switches) later on. Without further ado, I present to you the Introductory Chapter 3 and I hope you will all enjoy it!


Introductory Chapter 3: Operators in C


So far, we've learned the basic structure of an application in C, the types of Variables and looked at some functions. I hope everything so-far sunk in because we're about to look at if statements and switches with various conditions! Before we get there though, we need to know how to build those conditions, and for that, we'll need to have a look at Operators. Without further ado, let's get to it, starting from explaining what exactly they are.

Operators are very simple elements which tell the program how to combine, compare or modify Values. By putting them in-between of two Operands we create Expressions. We can also use some of them connect Expressions together, designing more advanced Logic. Let's use an example that's known from our daily life and math - an expression is for example "3+4" where "+" is the Operator. Operators are divided into 5 groups, each of them with different uses. Those groups are Arithmetic, Comparison, Logical, Assignment and the Ternary Operator. First, we'll have a look at Arithmetic Operators as we've already used a few. They are used exactly the same as you'd use them in everyday math tasks, so their function should not pose any problems to anyone. Those Operators are:

ARITMETHIC OPERATORS
+ addition
- substraction (- when placed before numbers negates them)
* multiplication
/ division
( and ) define precedence (to define in what order the calculations happen)
++ incrementation (adding 1)
-- decrementation (substracting 1)
>> bit shift right
<< bit shift left
% modulus (calculating the remainder of dividing two numbers)
Example of use: AVariable=3+3;
(Assign the result from adding 3 to 3 to AVariable)
As of now, only the first 5 (well, maybe 7 ) are of our interest, the rest will be covered in more detail in
subsequent chapters if they pop up, for now you just need to know they exist.
Next up are operators which we also used, called Assignment Operators. We use them to assign Values to a given Variable. Those operators are:

ASSIGNMENT OPERATORS
= Simple assignment. The Value of the right operand is assigned to the Variable on the left.
*= Multiplication assignment. The Value of the left operand is multiplied by the Value of the right operand and the
result is assigned to the left one.
/= Division assignment. The Value of the left operand is divided by the Value of the right operand and the result is assigned to the left one.
%= Remainder assignment. The remainder of dividing the Value of the left operand by the right operand is assigned to the left operand.
+= Addition assignment. The Values of operands are added and the result is assigned to the left one.
–= Subtraction assignment. The Value of the right operand is substracted from the Value of the left operand and the result is assigned to the left one.
<<= Left-shift assignment. The Value of the left operand is shifted left by the ammount of bits specified by the right operand and the result is assigned to the left one.
>>= Right-shift assignment. The Value of the left operand is shifted right by the ammount of bits specified by the right operand and the result is assigned to the left one.
&= Bitwise-AND assignment. Obtains the bitwise AND of the left and right operands and assigns the result in the left operand.
^= Bitwise-exclusive-OR assignment. Obtains the bitwise exclusive OR of the left and right operands and assigns the result in the left operand.
|= Bitwise-inclusive-OR assignment Obtains the bitwise inclusive OR of the left and right operands and assigns the result in the left operand.
Example of use: AVariable=AnotherVariableOrAValue;
(Assign the value of AnotherVariableOrValue to AVariable)
Once again, I've divided them into two groups - the ones you should be concerned with firstly and the ones that we'll be using much, much later.
Now we can move on to what interests us the most today, which would be Comparison Operators. These will be the basis of building conditions and you need to know them by heart. Their purpose is to compare two Values and return the result of this comparison. Those operators are:

COMPARISON OPERATORS
== Checks if the Value on the left is equal to the Value on the right.
!= Checks if the Value is not equal to the Value on the right.
< Checks if the Value on the left is smaller then the Value on the right.
> Checks if the Value on the left if greater then the Value on the right.
<= Checks if the Value on the left is smaller or equal to the Value on the right.
>= Check if the Value on the left is greater or equal to the Value on the right.
Example of use: if(Variable<3) Variable++;
(if the Variable's value is less then three, increment it)
ALL Comparison Operators are essential; there is literally nothing we could leave for later.
Finally we can move on to another useful kind of Operators which we will frequently use to join together different expressions. Those operators are called Logical Operators and are relatively simple to remember since there are only three of them.

LOGICAL OPERATORS
! Is used to negate a given expression, this operator is called NOT.
&& If both parts of the expression are true, the whole expression is true. If either part if false, the expression will be false. This operator is called AND.
|| If either part of the expression is true, the whole expression is true. This operator is called OR.
Example of use: if(AVariable==3 && AnotherVariable==3) AVariable++;
(if both variables are equal 3, increment the first one)
That'd be all we need to know now - the Ternary Operator will be introduced together with if statements.

That's all for this chapter and tune in next time to learn how to build Conditions with our new-found knowledge, alongside some fun examples!
 
  • Like
Reactions: MusicOfMusiX

Walthor

Well-Known Member
Newcomer
Joined
Feb 2, 2009
Messages
56
Trophies
0
XP
76
Country
Netherlands
Great job on the tutorial. Only one question. i use Visual studio 2010 and installed the template wizard,only my vs 2010 is not so happy with it since i cannot find the DS wizard or template in the new project dialog. Do you know if there is a version available of the setup for VS 2010?
On a side note, i did do a google search and came upon this website wich explains how to manually add nds compilation to VS wich is also for the 2008 version.

If the manual steps work for me on vs 2010, will i be able to run your tutorial code?

I'm already a bit experienced with c++ since i had a class in it last year.(so i know some of the basics, only not really applicable to DS programming i think).

2 thumbs up for the tutorials! keep up the good work!
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,845
Country
Poland
Great job on the tutorial. Only one question. i use Visual studio 2010 and installed the template wizard,only my vs 2010 is not so happy with it since i cannot find the DS wizard or template in the new project dialog. Do you know if there is a version available of the setup for VS 2010?
On a side note, i did do a google search and came upon this website wich explains how to manually add nds compilation to VS wich is also for the 2008 version.

If the manual steps work for me on vs 2010, will i be able to run your tutorial code?

I'm already a bit experienced with c++ since i had a class in it last year.(so i know some of the basics, only not really applicable to DS programming i think).

2 thumbs up for the tutorials! keep up the good work!
There is no pre-prepared wizard application for Visual Studio 2010 series I'm afraid, this is why I recommended installing 2008 for newbie programmers. You will still be able to compile normally if you follow the steps you found properly and integrate successfuly, in fact, you can still compile the code even by using Notepad and the standard devkitPro or NightfoxLib template. We're using Visual Studio to facilitate programming and resource management as thanks to Intellisense it is easier to keep up with all the created structures and included functions, however it is not obligatory. Thank you for the kind words, stay tuned as we are almost at the end of the Introduction and C revision and I believe that same time next week we'll be analyzing the first DS-Specific routines, starting with Input methods. :)
 

Walthor

Well-Known Member
Newcomer
Joined
Feb 2, 2009
Messages
56
Trophies
0
XP
76
Country
Netherlands
I installed vc++ 2008 expres and created a project in there. Opened it in 2010, upgraded the solution file, and 2010 still compiled the solution. i'm happy.

looking forward to the rest of the tutorials.

Ever thought of creating a pdf document with all the tutorials?
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,845
Country
Poland
I installed vc++ 2008 expres and created a project in there. Opened it in 2010, upgraded the solution file, and 2010 still compiled the solution. i'm happy.

looking forward to the rest of the tutorials.

Ever thought of creating a pdf document with all the tutorials?
Perhaps if I get that far, right now we barely revised some basic C routines. ;)

I really want this tutorial to be more... organic. I want the readers to get involved, ask questions, request clarifications - sort of like on a Wiki, so that every beginner gets the best possible "start". A PDF won't provide the users with that, it's static. :)
 

Walthor

Well-Known Member
Newcomer
Joined
Feb 2, 2009
Messages
56
Trophies
0
XP
76
Country
Netherlands
Ah okey, Well i already have 1 questions,
during my c++ classes i got to work with vectors and maps and multimaps and all those others. Are these also available for ds c++?
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,845
Country
Poland
Ah okey, Well i already have 1 questions,
during my c++ classes i got to work with vectors and maps and multimaps and all those others. Are these also available for ds c++?
There is nothing you know from C/C++ classes that wouldn't be available on the DS from a technical standpoint, the question here is how you're going to handle it with very limited resources.

Vectors and Vector graphics generally require alot of processing power to properly generate, advanced Vectors are also created using float values which the DS detests so you would have to create everything based on Fixed Point maths. Moreover, Vectors would have to be drawn on a Bitmap, and the DS has very limited capacity of VRAM. All those points considered, we will be using graphics that are standard to the GBA and the DS, meaning tiled graphics. Of course we will still be calculating alot of stuff, but at the same time we'll try to keep it simple to avoid floats at all costs. Does that answer your question?
 
  • Like
Reactions: 1 person

stab244

Well-Known Member
Member
Joined
Jun 1, 2008
Messages
662
Trophies
1
XP
333
Country
United States
This is cool. I already have pretty basic knowledge in C++ and C# and JS. Can't wait until we get to learn how to actually make a basic game!
 

Walthor

Well-Known Member
Newcomer
Joined
Feb 2, 2009
Messages
56
Trophies
0
XP
76
Country
Netherlands
There is nothing you know from C/C++ classes that wouldn't be available on the DS from a technical standpoint, the question here is how you're going to handle it with very limited resources.

Vectors and Vector graphics generally require alot of processing power to properly generate, advanced Vectors are also created using float values which the DS detests so you would have to create everything based on Fixed Point maths. Moreover, Vectors would have to be drawn on a Bitmap, and the DS has very limited capacity of VRAM. All those points considered, we will be using graphics that are standard to the GBA and the DS, meaning tiled graphics. Of course we will still be calculating alot of stuff, but at the same time we'll try to keep it simple to avoid floats at all costs. Does that answer your question?

Hmm i see, i was more thinking about the vector container. But knowing about the drawn vectors is good to know to, since i only had console programming during the c++ classes, very basic. never worked with images.
Thnx for the answer.
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,845
Country
Poland
There is nothing you know from C/C++ classes that wouldn't be available on the DS from a technical standpoint, the question here is how you're going to handle it with very limited resources.

Vectors and Vector graphics generally require alot of processing power to properly generate, advanced Vectors are also created using float values which the DS detests so you would have to create everything based on Fixed Point maths. Moreover, Vectors would have to be drawn on a Bitmap, and the DS has very limited capacity of VRAM. All those points considered, we will be using graphics that are standard to the GBA and the DS, meaning tiled graphics. Of course we will still be calculating alot of stuff, but at the same time we'll try to keep it simple to avoid floats at all costs. Does that answer your question?

Hmm i see, i was more thinking about the vector container. But knowing about the drawn vectors is good to know to, since i only had console programming during the c++ classes, very basic. never worked with images.
Thnx for the answer.
Whether you draw the vector or not, the calculation behind it is exactly the same. Nothing stops you from trying, it should to work just fine, it's just that it will be wasteful. :)
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    Metoroid0 @ Metoroid0: im more interested in metroid prime 4