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,

RafaPB

New Member
Newbie
Joined
Sep 20, 2022
Messages
2
Trophies
0
Age
23
Location
Caracas
XP
23
Country
Venezuela
Hi, this tutorial helps me to learn about ds homebrew, thanks for posting it!

I have some questions on how some games work, I come from Unity/Godot, and this world it's very different, haha

How games, like new super mario bros, pokemon or kirby, manage to move the game camera? Or they are actually scrolling all the backgrounds and objects?
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
Hi, this tutorial helps me to learn about ds homebrew, thanks for posting it!

I have some questions on how some games work, I come from Unity/Godot, and this world it's very different, haha

How games, like new super mario bros, pokemon or kirby, manage to move the game camera? Or they are actually scrolling all the backgrounds and objects?
You have to make your own camera. Every background and object has an X/Y position - in order to have a camera, you must add or subtract from those X/Y positions to move the entire “instance”, shall we say. When developing an engine, your X/Y positions should actually be X+Camera_X and Y+Camera_Y - that’s one easy way of doing it. It really depends on the implementation - in Mario for instance, the camera only begins to scroll when you approach the border of the visible area. It’s up to you to design when and how the screen should scroll, it’s all up to the imagination.
 

RafaPB

New Member
Newbie
Joined
Sep 20, 2022
Messages
2
Trophies
0
Age
23
Location
Caracas
XP
23
Country
Venezuela
You have to make your own camera. Every background and object has an X/Y position - in order to have a camera, you must add or subtract from those X/Y positions to move the entire “instance”, shall we say. When developing an engine, your X/Y positions should actually be X+Camera_X and Y+Camera_Y - that’s one easy way of doing it. It really depends on the implementation - in Mario for instance, the camera only begins to scroll when you approach the border of the visible area. It’s up to you to design when and how the screen should scroll, it’s all up to the imagination.
Ok, thanks for reply! :D

Another question, how can I use tiles? I mean, what app should I use to create the level for use it in grit? Or is it a simple background in .bmp?
 

GigaGuenther

Member
Newcomer
Joined
Mar 20, 2023
Messages
10
Trophies
0
Age
20
XP
34
Country
Germany
Hi, nice tutorial. I tried to get the Sprites and Backgrounds to work but I only get a black screen. Even after copying the code in the example I just can't get it to work...

C:
// Include C
#include <stdio.h>

// Include Libnds
#include <nds.h>

// Include NFLib
#include <nf_lib.h>

int main(int argc, char** argv) {

    // Turn on MODE 0 on the Top Screen
    NF_Set2D(0, 0);

    // Set the Root Folder
    NF_SetRootFolder("NITROFS");

    // Initialize the Tiled Backgrounds System on the Top Screen
    NF_InitTiledBgBuffers();
    NF_InitTiledBgSys(0);

    // Initialize the Tiled Sprites System on the Bottom Screen
    NF_InitSpriteBuffers();
    NF_InitSpriteSys(0);


    // Load and Create the Tiled Background
    NF_LoadTiledBg("bgtest", "bgtest", 256, 256);
    NF_CreateTiledBg(0, 3, "bgtest");

    // Load our Tiled Sprite
    NF_LoadSpriteGfx("frog_idle", 0, 32, 32);// Tempy!
    NF_LoadSpritePal("frog_idle", 0);

    // Transfer our sprite to VRAM
    NF_VramSpriteGfx(0, 0, 0, false);
    NF_VramSpritePal(0, 0, 0);

    // Create the Sprite!
    NF_CreateSprite(0, 0, 0, 0, 0, 0);

    while (1) {
        //Update NF OAM Settings
        NF_SpriteOamSet(0);
        swiWaitForVBlank();
        //Update OAM!
        oamUpdate(&oamMain);
    }
    return 0;
}
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
Hi, nice tutorial. I tried to get the Sprites and Backgrounds to work but I only get a black screen. Even after copying the code in the example I just can't get it to work...

C:
// Include C
#include <stdio.h>

// Include Libnds
#include <nds.h>

// Include NFLib
#include <nf_lib.h>

int main(int argc, char** argv) {

    // Turn on MODE 0 on the Top Screen
    NF_Set2D(0, 0);

    // Set the Root Folder
    NF_SetRootFolder("NITROFS");

    // Initialize the Tiled Backgrounds System on the Top Screen
    NF_InitTiledBgBuffers();
    NF_InitTiledBgSys(0);

    // Initialize the Tiled Sprites System on the Bottom Screen
    NF_InitSpriteBuffers();
    NF_InitSpriteSys(0);


    // Load and Create the Tiled Background
    NF_LoadTiledBg("bgtest", "bgtest", 256, 256);
    NF_CreateTiledBg(0, 3, "bgtest");

    // Load our Tiled Sprite
    NF_LoadSpriteGfx("frog_idle", 0, 32, 32);// Tempy!
    NF_LoadSpritePal("frog_idle", 0);

    // Transfer our sprite to VRAM
    NF_VramSpriteGfx(0, 0, 0, false);
    NF_VramSpritePal(0, 0, 0);

    // Create the Sprite!
    NF_CreateSprite(0, 0, 0, 0, 0, 0);

    while (1) {
        //Update NF OAM Settings
        NF_SpriteOamSet(0);
        swiWaitForVBlank();
        //Update OAM!
        oamUpdate(&oamMain);
    }
    return 0;
}
Did you correctly convert them and put them in the right folder?
 

GigaGuenther

Member
Newcomer
Joined
Mar 20, 2023
Messages
10
Trophies
0
Age
20
XP
34
Country
Germany
I put .bmp images into Grit and use the batch files to convert them. The output is a .img and .pal file for Sprites and a .img, .pal and .map file for backgrounds. I then put them into the Nitrofiles folder of the Project. Are there any things to consider when making the sprite? For example making it an indexed image?
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
I put .bmp images into Grit and use the batch files to convert them. The output is a .img and .pal file for Sprites and a .img, .pal and .map file for backgrounds. I then put them into the Nitrofiles folder of the Project. Are there any things to consider when making the sprite? For example making it an indexed image?
This should’ve worked fine. I’d double-check if the images are correctly sized. Worst-case scenario there might be something wrong with your setup.
 
  • Like
Reactions: plasturion

GigaGuenther

Member
Newcomer
Joined
Mar 20, 2023
Messages
10
Trophies
0
Age
20
XP
34
Country
Germany
The background has a size of 256 x 256 and the sprite has a size of 32 x 32. I just tried compiling the "Example2" project and that seems to work fine.
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
The background has a size of 256 x 256 and the sprite has a size of 32 x 32. I just tried compiling the "Example2" project and that seems to work fine.
Are you sure they’re not 33x33 and 257x257? Remember that the first pixel is at 0, 0. :P

If the example complies and works correctly then we can rule out your devkit installation being the issue - something went wrong with the conversion of the images. Have you tried the source forge link above? It should have the correct version of the batch files and the GRIT executable.
 

GigaGuenther

Member
Newcomer
Joined
Mar 20, 2023
Messages
10
Trophies
0
Age
20
XP
34
Country
Germany
Ok so i tried the Grit version from the sourceforge and the same problems keep happening, it spits out the background but it doesn't show in the emulator. I don't know if the .bmp needs something besides being indexed. I'm not very well-versed in the field of image manipulation so I have no idea what the problem could be with the images I'm feeding to Grit.
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
As long as the color depth and size are correct you should be alright. Any chance you could attach your images here so we could take a look together?
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    DinohScene @ DinohScene: ahh nothing beats a coffee disaronno at work