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,

Pugalotl

New Member
Newbie
Joined
Sep 9, 2021
Messages
3
Trophies
0
Age
26
XP
43
Country
Australia
I try to make it but this error shows up (using Windows)

'basename' is not recognized as an internal or external command, operable program or batch file. Makefile:9: /opt/devkitpro/devkitARM/ds_rules: No such file or directory make: *** No rule to make target '/opt/devkitpro/devkitARM/ds_rules'. Stop.
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,839
Country
Poland
I try to make it but this error shows up (using Windows)

'basename' is not recognized as an internal or external command, operable program or batch file. Makefile:9: /opt/devkitpro/devkitARM/ds_rules: No such file or directory make: *** No rule to make target '/opt/devkitpro/devkitARM/ds_rules'. Stop.
Are you just coding from scratch or are you using the devkitPro template? It should come with an up-to-date makefile.
 

placebo_yue

Well-Known Member
Member
Joined
Aug 7, 2019
Messages
739
Trophies
0
Age
33
XP
1,264
Country
Argentina
I need this in video tutorials, i need to go back and forth freely and i need visual aid, seeing how the software looks like, where's the buttons and such. Posts on a forum kinda get lost amid other people talking...
Anyone (if not OP) is up to doing such thing?
Also, btw, i don't need a basic coding tutorial, you could skip that part IMO. Just the compiling part and such is what i really need. And tbh it's the most important part, you can learn how to code anywhere else!
 

KonPet

Active Member
Newcomer
Joined
May 2, 2020
Messages
31
Trophies
0
XP
445
Country
Germany
I need this in video tutorials, i need to go back and forth freely and i need visual aid, seeing how the software looks like, where's the buttons and such. Posts on a forum kinda get lost amid other people talking...
Anyone (if not OP) is up to doing such thing?
Also, btw, i don't need a basic coding tutorial, you could skip that part IMO. Just the compiling part and such is what i really need. And tbh it's the most important part, you can learn how to code anywhere else!
Just watch this video by PolyMars then:
It's the best you can currently get. If you want other resources look at my post
 
  • Like
Reactions: placebo_yue

xqwzts251

New Member
Newbie
Joined
Jul 23, 2022
Messages
4
Trophies
0
Age
30
Location
Poland
XP
29
Country
Poland
Hi!

I know this tutorial may be too old, but thanks for posting it, it's very helpful.

However, I have issues with displaying background with NFLib:
  • I have the most recent version of devkitpro & lib nds, and NFLib (8 oct 2020 last commit, from github)
  • I draw my image, convert to BMP with 8 bits depth - didn't work
  • use image from example - also
  • however I compile some examples from NFLib, and... it doesn't work also.
I'm using DeSmuMe 0.9.13 and No$GBA 3.0.

I have assumption that I have too new version of libraries.
Do I have to find old version of libnds or ARM compiler? I'm using Windows 10.

Also my code:
C:
#include <stdio.h>
#include <nds.h>
#include <nf_lib.h>

#define SCREEN_TOP 0
#define SCREEN_BOTTOM 1
#define ROOT_DEFAULT "NITROFS"

#define BACKGROUND_NAME "nfl"
#define BACKGROUND_SIZE 256

int main(void) {
    //0 - TOP
    //1 - BOTTOM
    NF_Set2D(SCREEN_TOP, 0);
    NF_Set2D(SCREEN_BOTTOM, 0);   

    swiWaitForVBlank();

    NF_SetRootFolder(ROOT_DEFAULT);

    NF_InitTiledBgBuffers();
    NF_InitTiledBgSys(SCREEN_TOP);
    NF_InitTiledBgSys(SCREEN_BOTTOM);

    NF_LoadTiledBg(BACKGROUND_NAME, BACKGROUND_NAME, BACKGROUND_SIZE, BACKGROUND_SIZE);
    NF_CreateTiledBg(SCREEN_TOP, 3, BACKGROUND_NAME);

    while (1)
    {
        swiWaitForVBlank();
    }
    

    return 0;
}
 
Last edited by xqwzts251,

Foxi4

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

I know this tutorial may be too old, but thanks for posting it, it's very helpful.

However, I have issues with displaying background with NFLib:
  • I have the most recent version of devkitpro & lib nds, and NFLib (8 oct 2020 last commit, from github)
  • I draw my image, convert to BMP with 8 bits depth - didn't work
  • use image from example - also
  • however I compile some examples from NFLib, and... it doesn't work also.
I'm using DeSmuMe 0.9.13 and No$GBA 3.0.

I have assumption that I have too new version of libraries.
Do I have to find old version of libnds or ARM compiler? I'm using Windows 10.

Also my code:
C:
#include <stdio.h>
#include <nds.h>
#include <nf_lib.h>

#define SCREEN_TOP 0
#define SCREEN_BOTTOM 1
#define ROOT_DEFAULT "NITROFS"

#define BACKGROUND_NAME "nfl"
#define BACKGROUND_SIZE 256

int main(void) {
    //0 - TOP
    //1 - BOTTOM
    NF_Set2D(SCREEN_TOP, 0);
    NF_Set2D(SCREEN_BOTTOM, 0);   

    swiWaitForVBlank();

    NF_SetRootFolder(ROOT_DEFAULT);

    NF_InitTiledBgBuffers();
    NF_InitTiledBgSys(SCREEN_TOP);
    NF_InitTiledBgSys(SCREEN_BOTTOM);

    NF_LoadTiledBg(BACKGROUND_NAME, BACKGROUND_NAME, BACKGROUND_SIZE, BACKGROUND_SIZE);
    NF_CreateTiledBg(SCREEN_TOP, 3, BACKGROUND_NAME);

    while (1)
    {
        swiWaitForVBlank();
    }
    

    return 0;
}
If the examples from the library aren’t working as expected, there’s a very good chance that there may be something wrong with your devKit Pro installation or libnds, perhaps something changed between now and 10 years ago. :P Does it compile successfully? More importantly, are you trying to load a bitmap or a tiled background? Your code suggests tiled. Is your background in the appropriate path?
 
  • Like
Reactions: xqwzts251

xqwzts251

New Member
Newbie
Joined
Jul 23, 2022
Messages
4
Trophies
0
Age
30
Location
Poland
XP
29
Country
Poland
Yay, thanks for reply!

So:
  • it compiles perfectly
  • path is okay, when e.g. *.map file was missing the ROM throws error message
About image:
  • I draw image in GIMP, with indexed colors to 256 colors
  • I've found old tools scripts to convert background (Convert_backgrounds):
Code:
@echo off
cls
del backgrounds\*.img
del backgrounds\*.map
del backgrounds\*.pal
del h\*.h
grit.exe bmp\*.bmp -g -gTFF00FF -m -mRtf -mLs -p -ftb -gB8
for /f "delims==" %%F in ('dir /b *.bin') do ren "%%~nxF" "%%~nF"
move *.img backgrounds
move *.map backgrounds
move *.pal backgrounds
move *.h h
pause
exit
And put that in file.
I've used other backgrounds and it didn't seem to work. Mayb I'm messing tools with older grit version also :D .
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,839
Country
Poland
Yay, thanks for reply!

So:
  • it compiles perfectly
  • path is okay, when e.g. *.map file was missing the ROM throws error message
About image:
  • I draw image in GIMP, with indexed colors to 256 colors
  • I've found old tools scripts to convert background (Convert_backgrounds):
Code:
@echo off
cls
del backgrounds\*.img
del backgrounds\*.map
del backgrounds\*.pal
del h\*.h
grit.exe bmp\*.bmp -g -gTFF00FF -m -mRtf -mLs -p -ftb -gB8
for /f "delims==" %%F in ('dir /b *.bin') do ren "%%~nxF" "%%~nF"
move *.img backgrounds
move *.map backgrounds
move *.pal backgrounds
move *.h h
pause
exit
And put that in file.
I've used other backgrounds and it didn't seem to work. Mayb I'm messing tools with older grit version also :D .
Is the background the correct size in pixels as well? They must be 256x256 minimum in order to correctly slice into 8x8 tiles (technically 192x256 minimum, but that tends to mess with NFLib for some reason. Just as a little test, could you try compiling the libnds example for backgrounds and see if that works?
 
  • Like
Reactions: xqwzts251

xqwzts251

New Member
Newbie
Joined
Jul 23, 2022
Messages
4
Trophies
0
Age
30
Location
Poland
XP
29
Country
Poland
Libnds example works great. I've put my background and it looks good, altough I've used *..h & *.o files.

I figured it out - I've put old NFLib binaries from your example and it works!
Somewhere I saw that when NFLib has problems, it should be rebuild from source.

Thank you!

EDIT:

I've just rebuild NFLib and it works!!!

Thank you, I'm so happy that I don't have to use libnds :D .
 
  • Like
Reactions: Foxi4

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,839
Country
Poland
Libnds example works great. I've put my background and it looks good, altough I've used *..h & *.o files.

I figured it out - I've put old NFLib binaries from your example and it works!
Somewhere I saw that when NFLib has problems, it should be rebuild from source.

Thank you!

EDIT:

I've just rebuild NFLib and it works!!!

Thank you, I'm so happy that I don't have to use libnds :D .
Working backwards from the problem tends to help, yes. NFLib is great as a start, but realistically you should learn both. Once you’re comfortable with the DS and how it operates, you should slowly transition because odds are you will encounter more problems like this - devKit Pro is still in active development, NFLib not so much, it’s been over a decade. :P
 

LoboNer

New Member
Newbie
Joined
Jul 24, 2022
Messages
3
Trophies
0
Age
23
Location
Peru lmao
XP
40
Country
Peru
I don’t understand the question.

I'm sorry if you don't understand but my problem is that I need the "PAL" of the image of my "SPRITE" so that it appears correctly with its color palettes on screen, I use NightFox's Lib. Do you know how to get the "PAL" of an image? (here is a reference image to what I mean) I don't know how to get or make the "PAL" :(
 

Attachments

  • example.png
    example.png
    8 KB · Views: 32

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,839
Country
Poland
Oh, you need a palette file? It’s covered in the GRIT chapter. Keep in mind that the DS has a limited amount of palette slots, so you should process sprites in batches.
 

Deleted member 587507

Active Member
Newcomer
Joined
Mar 6, 2022
Messages
26
Trophies
0
XP
167
Working backwards from the problem tends to help, yes. NFLib is great as a start, but realistically you should learn both. Once you’re comfortable with the DS and how it operates, you should slowly transition because odds are you will encounter more problems like this - devKit Pro is still in active development, NFLib not so much, it’s been over a decade. :P
Even devkitPro isn't super heavy worked on. At least not the DS homebrew side of it.
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,839
Country
Poland
Even devkitPro isn't super heavy worked on. At least not the DS homebrew side of it.
IIRC a few recent updates did break compatibility with some older libraries, there was a whole kerfuffle about people sharing older versions of the kit to maintain compatibility. Something to be aware of - if it doesn’t work, it’s not necessarily your fault, we’re working with tools that are over a decade old here.
 

Deleted member 587507

Active Member
Newcomer
Joined
Mar 6, 2022
Messages
26
Trophies
0
XP
167
IIRC a few recent updates did break compatibility with some older libraries, there was a whole kerfuffle about people sharing older versions of the kit to maintain compatibility. Something to be aware of - if it doesn’t work, it’s not necessarily your fault, we’re working with tools that are over a decade old here.
Oh, I wasn't trying to use NightFox's lib. I use pure libnds 😅
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    SylverReZ @ SylverReZ: b