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,

iProgramInCpp

Member
Newcomer
Joined
Jun 29, 2018
Messages
5
Trophies
0
Location
Earth
XP
60
Country
Japan
Welp, I managed to get this far without Nightfox Lib. The program is pretty inefficient, but it allows graphics generation to some degree.
Code:
/*---------------------------------------------------------------------------------

    Simple console print demo which prints to bottom screen
    and also has a frame buffer for the top screen to which
    you can draw stuff
    -- iProgramMC - Graphics Engine
    -- dovoto - Console bottom screen print

---------------------------------------------------------------------------------*/
#include <nds.h>
#include <stdio.h>
#include "drunkenlog2o.h" //include the face image
#include "drunkenlogo.h" //include the face image


PrintConsole bottomScreen;

const char TileSize = 8; //8px wide

short question_image[256];

unsigned short rng;

short sky_colour = 0xEEEF;

uint32 global_timer = 0;

char ticks = 0;

unsigned short rng_function(unsigned short input){
    if(input == 0x560A) input=0;
    unsigned short S0 = (unsigned char)input<<8;
    S0 = S0 ^ input;
    input = ((S0 & 0xFF) << 8) | ((S0 & 0xFF00) >> 8);
    S0 = ((unsigned char)S0 << 1) ^ input;
    short S1 = (S0 >> 1) ^ 0xFF80;
    if((S0 & 1) == 0){
        if(S1 == 0xAA55){input = 0;}else{input=S1 ^ 0x1FF4;}
    }else{
        input = S1 ^ 0x1FF4;
    }
    return (unsigned short)input;
}

void rng_update() {
    rng = rng_function(global_timer);
}

void make_top_screen() {consoleSelect(&bottomScreen);}
void make_btm_screen() {consoleSelect(&bottomScreen);}
void clear_screen();
void printchar(char x, char y, char c) { printf("\x1b[%d;%dH%c", y, x, c); }

short screenbuffer_maindisplay[49152];
short screen_width = 256;
short screen_height = 192;

void update_frame(){
    dmaCopy(&screenbuffer_maindisplay, BG_GFX, 49152*2);
}

void clear_screenbuffer(short colour){
    for(int x = 0; x < 49152; x+=1){
        screenbuffer_maindisplay[x] = colour;
    }
}

void draw_image(short* image, int w, int h, int dx, int dy){
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            screenbuffer_maindisplay[(dy+y)*screen_width+(x+dx)] = image[y*w+x];
        }
    }
}

//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
    touchPosition touch;

 
 
    videoSetMode(MODE_5_2D);
    videoSetModeSub(MODE_0_2D);

    vramSetBankA(VRAM_A_MAIN_BG);
    vramSetBankC(VRAM_C_SUB_BG);
    decompress(drunkenlogoBitmap, &question_image,  LZ77);

    bgInit(2, BgType_Bmp16, BgSize_B16_256x256, 0,0);
 
    //consoleInit(&topScreen, 3,BgType_Text4bpp, BgSize_T_256x256, 31, 0, true, true);
    consoleInit(&bottomScreen, 3,BgType_Text4bpp, BgSize_T_256x256, 31, 0, false, true);

    make_btm_screen();
    iprintf("\n\n\tGraphics Engine\n\tCoded by iProgramMC\n\tMore modern-like\n\tDo whatever you want!");
 
    clear_screenbuffer(0xEEEE);

    while(1) {
        clear_screenbuffer(sky_colour);
    
        global_timer++;

        if(global_timer % 60 == 30){

            //decompress(drunkenlogoBitmap, &screenbuffer_maindisplay,  LZ77);
        }
        if(global_timer % 1 == 0){
            rng = rng_function(8762);
            rng_update();
            iprintf("\x1b[0;0H%d", rng);
            draw_image(question_image, 16, 16, rng%(256-16), rng%(192-16));
        }
        update_frame();
        swiWaitForVBlank();
    }

    return 0;
}

The program within the spoiler renders a question block image to the top screen at a random position and enables console output on the bottom screen.

I based off the 16bit_color_bmp and print_both_screens devkitPro examples, and used the Makefile from the 16bit_color_bmp image.

Here's a built version if you want to try the example out:
http(semicolon)//www(dot)mediafire(dot)com/file/ppzdzwwdiwqzxcm/DSGameEngine(dot)nds

Used the question mark block bitmap from NSMB.

And I didn't use VS! Used make to build.

Beware of seizures though!
 
Last edited by iProgramInCpp,

MrMcTiller

GBATemp's Tiller
Member
Joined
Mar 7, 2017
Messages
1,185
Trophies
0
Age
20
Location
Iowa
XP
1,557
Country
United States
So, when you install devkitPro's thing you get a code editor right?

--------------------- MERGED ---------------------------

Programmer's Notepad right?
 

jurassicplayer

Completionist Themer
Member
Joined
Mar 7, 2009
Messages
4,484
Trophies
1
Location
Pantsuland
Website
www.youtube.com
XP
2,903
Country
United States
So, when you install devkitPro's thing you get a code editor right?
No. Code editor can be whatever you want. If you wanted to, you could even use notepad...as awful as that would be. The only preferable thing would be to pick something that integrates well with C/C++.
 
  • Like
Reactions: MrMcTiller

MrMcTiller

GBATemp's Tiller
Member
Joined
Mar 7, 2017
Messages
1,185
Trophies
0
Age
20
Location
Iowa
XP
1,557
Country
United States
Oh, wait. I have Notepad++ that works right? After editing the code in Notepad++, I just use devkitPro's tools to compile it right?
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,841
Country
Poland
All of this is perfectly relevant, however I'd download the PDF instead of relying on the posts because the code blocks got mangled when GBATemp transitioned to Xenforo and I never really fixed them. Other than that you should be able to compile functional DS software based on these tips.
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,841
Country
Poland
Whoops, you're right! Welp, you'll have to go through some debug. :P Usually it's just the spacing that got messed up, if you encounter any problems, post here! The general principles are still fine.
 
  • Like
Reactions: MrMcTiller

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,841
Country
Poland
Any particular reason why you're starting with DS and not 3DS or Switch? It's a bit of an older platform. Good for training, but don't pick up bad habits - don't get overly reliant on libraries.
 
  • Like
Reactions: MrMcTiller

MrMcTiller

GBATemp's Tiller
Member
Joined
Mar 7, 2017
Messages
1,185
Trophies
0
Age
20
Location
Iowa
XP
1,557
Country
United States
Any particular reason why you're starting with DS and not 3DS or Switch? It's a bit of an older platform. Good for training, but don't pick up bad habits - don't get overly reliant on libraries.
I want to start with 3ds homebrew... but I don't have a 3ds. I have a DS Lite and I am getting a flashcart.
 

MrMcTiller

GBATemp's Tiller
Member
Joined
Mar 7, 2017
Messages
1,185
Trophies
0
Age
20
Location
Iowa
XP
1,557
Country
United States
You see, the thing is... I don't really have my own computer. I use a school windows PC, and I don't have admin privileges. I think I can still use devkitpro if I install it in %appdata%, but I am not 100% for sure. That said, I can't install Citra emulator or anything like that.


EDIT: And, yes I know devkitPro is the organization name.
 

MrMcTiller

GBATemp's Tiller
Member
Joined
Mar 7, 2017
Messages
1,185
Trophies
0
Age
20
Location
Iowa
XP
1,557
Country
United States
@Foxi4 , I am having a REALLY hard time getting the devkitpro's tools to work. I can open msys, but when I try to make something, it says I need to set environment variables.... but I don't have admin. Can you try to install this stuff without using admin please? (In order for me to even run the updater, I had to make a batch with this in it:

cmd /min /C "set __COMPAT_LAYER=RUNASINVOKER && start "" "%1""
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,841
Country
Poland
There's no way to install devkitPro without access to an Admin account that I know of, correct compilation requires adding some PATHS to the OS which you won't be able to do without administrative privileges.
 
  • Like
Reactions: MrMcTiller

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    Maximumbeans @ Maximumbeans: butte