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,

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
EDIT: Wouldn’t you know it, @Pomegrenade scores a big W for a re-upload in our download section

I will update the OP for the first time in over a decade to add this new mirror, it’s mighty handy and probably free from export-related errors. Might be useful to future readers. Cheers!

@Foxi4 That's awesome! Thanks for pointing out the pdf! I'm glad this thread is still up, this thread sure seems to help everyone grasp the basics of C and get in to NDS Homebrew! :)

Thanks for all your help! :)

:nds:
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
@Foxi4 That's awesome! Thanks for pointing out the pdf! I'm glad this thread is still up, this thread sure seems to help everyone grasp the basics of C and get in to NDS Homebrew! :)

Thanks for all your help! :)

:nds:
I’m always available for any guide-related queries, minor bug fixing, development hints and general tea and biscuits, however keep in mind that a whole new generation of Nintendo gamers was born and started waddling about since I wrote my last line of code for the DS, so I’m rusty. Sharp, but rusty. :wink:
 

akbat

Member
Newcomer
Joined
Sep 2, 2020
Messages
6
Trophies
0
Age
27
XP
97
Country
United States
does anyone know how to change the text size with the "NF_WriteText();", or if i am able to use floats as the x and y pos?, eg:
NF_WriteText(1, 0, 15.8, 13, counterchar);, any help would be great, thank you

also if anyone knows where to find full documentation on NF it would be great as well, I've been going off the examples which come with the library and I'm unable to properly understand the code with them, like the parameters which should be inserted in to functions, thank you!

could anyone help me with NF_DeleteTextLayer()? it seems to behave like NF_DeleteTiledBg(), and it says the text layer is not an available background

So glad I archived that PDF, the person who's been maintaining nds_nflib apparently removed it from the github a few months ago. The repo is setup so if you clone it and have "Doxygen" installed, you can generate documentation in HTML format. This guarantees the documentation is always up-to-date, but of course it requires you have doxygen installed to generate it.

That PDF should be fine, but obviously things may have changed since 2012.

As for the x & y variables, they are tile coordinates (integers only):
right SCREEN_WIDTH refer to pixel coordinates, and text refer to 8x8px tile position, so max x is 32 (256/8) and max y 24 (192/8)
 
  • Like
Reactions: Foxi4

Valkyrience

Member
Newcomer
Joined
Aug 12, 2023
Messages
14
Trophies
0
Age
25
XP
27
Country
Ukraine
Good question, and yes! A map file is nothing more than a background broken down into tile numbers, you can have multiple maps that use the same tileset. I can’t quite recall any specific software that could aid you at this time, but as long as you have your tiles and palette, you can make as many maps as you want - they’re just long strings of numbers. Here’s an example:
The screen is 192x256, if we divide it into tiles, that’s 24x32 using 8x8 tiles visible tiles, however the map should be at least 32x32 to facilitate scrolling.
Very thanks, for your reply, But i need some more help with sprites)
May be i lost some rules of creating sprites. If i try to create 24x16(and some other sizes) sprite, it shows only first 8x8 tile. But if i remake it 32x16 it shows correctly. What do i wrong?
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
Very thanks, for your reply, But i need some more help with sprites)
May be i lost some rules of creating sprites. If i try to create 24x16(and some other sizes) sprite, it shows only first 8x8 tile. But if i remake it 32x16 it shows correctly. What do i wrong?
Only certain sprite sizes are supported, multiples of 8 is sadly not good enough, and you have to change the appropriate parameters. The acceptable object sizes are 8, 16, 32 and 64, the shape can be tall, wide or square, but that’s really only a concern within libnds, NF should sort it out for you. The solution is to use the next available size and adjust your collision accordingly. Even if the top 2 tiles are completely blank, they won’t count towards your total file size as they’re identical - once the sprite is converted, they’ll just count as one blank 8x8 square which you probably have on the sprite sheet already anyway. They’ll only exist as a number on the map. When converting a sprite there are no repeating tiles.
 

Valkyrience

Member
Newcomer
Joined
Aug 12, 2023
Messages
14
Trophies
0
Age
25
XP
27
Country
Ukraine
Only certain sprite sizes are supported, multiples of 8 is sadly not good enough, and you have to change the appropriate parameters. The acceptable object sizes are 8, 16, 32 and 64, the shape can be tall, wide or square, but that’s really only a concern within libnds, NF should sort it out for you. The solution is to use the next available size and adjust your collision accordingly. Even if the top 2 tiles are completely blank, they won’t count towards your total file size as they’re identical - once the sprite is converted, they’ll just count as one blank 8x8 square which you probably have on the sprite sheet already anyway, so they’ll only exist as a number on the map. When converting a sprite there are no repeating tiles.
I'm so grateful to you for your replies, it so much helps to me
 
  • Like
Reactions: Foxi4

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
I was able to compile my first nds homebrew today! I now want to get into the graphic side of things. I read through guide (which is amazing was a big help to get everything setup), but I didn't really understand the whole GRIT things. I believe that sprites and backgrounds have to be made in .bmp format then get converted to .img and .pal, but I didn't understand the process a whole lot. Anyone able to clear things up for me?
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
I was able to compile my first nds homebrew today! I now want to get into the graphic side of things. I read through guide (which is amazing was a big help to get everything setup), but I didn't really understand the whole GRIT things. I believe that sprites and backgrounds have to be made in .bmp format then get converted to .img and .pal, but I didn't understand the process a whole lot. Anyone able to clear things up for me?
GRIT is a *really* old tool that dates all the way back to GBA days, it transmogrifies what you understand as a bitmap into a collection of unique 8x8 tiles and a map that lists them. Instead of drawing on the screen, the console is then able to load an entire set of tiles and load them instead of drawing pixels individually. The advantage here is both space savings and processing time, since a lot of times a single image uses multiple instances of the same 8x8 tile.

https://www.coranac.com/man/grit/html/grit.htm

If you’re using the NFLib package, it should come with preconfigured batch files - all you have to do is drag and drop your images to convert them.
 

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
Oh ok that make sense. However I don't any preconfigured batch files from NFLib. I got the version of NFLib straight from Github then compiled it by source. I may need use that link you provided, Thanks! :)
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
Oh ok that make sense. However I don't any preconfigured batch files from NFLib. I got the version of NFLib straight from Github then compiled it by source. I may need use that link you provided, Thanks! :)
It should have all the parameters you need, along with an explanation of what they do.
 

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
If you’re using the NFLib package, it should come with preconfigured batch files - all you have to do is drag and drop your images to convert them.
Would anyone by chance have these preconfigured batch files? As I can't seem to find them on NFLib's Github page.
 

akbat

Member
Newcomer
Joined
Sep 2, 2020
Messages
6
Trophies
0
Age
27
XP
97
Country
United States
Would anyone by chance have these preconfigured batch files? As I can't seem to find them on NFLib's Github page.
They were removed from the GitHub at some point. Here's the old version though. I highly recommend at least comparing the batch files to the grit documentation to see what is actually happening.
 

Attachments

  • grit.zip
    749.7 KB · Views: 15
  • Like
Reactions: Foxi4

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
They were removed from the GitHub at some point. Here's the old version though. I highly recommend at least comparing the batch files to the grit documentation to see what is actually happening.
Cheers bub, that saves me some digging. I think the release on Sourceforge still includes them, but I may be wrong.
 

akbat

Member
Newcomer
Joined
Sep 2, 2020
Messages
6
Trophies
0
Age
27
XP
97
Country
United States
Cheers bub, that saves me some digging. I think the release on Sourceforge still includes them, but I may be wrong.
I forgot there were releases there, last one was 2014 though. It does in fact include grit, the batch files, the PDF version of the documentation and even desmume.
Post automatically merged:


https://sourceforge.net/projects/nflib/
 
  • Like
Reactions: Foxi4

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
This may sound like a silly question, but are you able to use the touch screen while there’s iprintf() statements being printed at the same time?

Edit: Another question, so say I want to move a sprite across the screen as longer the player is hitting a button, now what is the best approach of moving the actual sprite across the screen? I know NightFox lib does have NF_moveSprite(), but typically with game making libraries I use, we would create or other words draw the sprite to the screen per frame, and that can tied to change in position. So what’s the best approach, does it depend on you game style?

Hopefully this question made sense :rofl:
 
Last edited by BlueFalconNDS,

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
This may sound like a silly question, but are you able to use the touch screen while there’s iprintf() statements being printed at the same time?

Edit: Another question, so say I want to move a sprite across the screen as longer the player is hitting a button, now what is the best approach of moving the actual sprite across the screen? I know NightFox lib does have NF_moveSprite(), but typically with game making libraries I use, we would create or other words draw the sprite to the screen per frame, and that can tied to change in position. So what’s the best approach, does it depend on you game style?

Hopefully this question made sense :rofl:
The DS operates on objects. Once an object is created, it continues to be active until it is destroyed. Create a sprite once and update its position when needed, or simply once every cycle. As for the touchscreen, it is active regardless of whatever else is happening at the time.
 
  • Like
Reactions: BlueFalconNDS

plasturion

temporary hermit
Member
Joined
Aug 17, 2012
Messages
1,216
Trophies
2
Location
Tree
XP
3,507
Country
Poland
Right, objects, oams or so called sprites are supported by 2D hardware accelerator like old machines did. Sprites are not drawn every frame point by point, it's just exist and it's part of video processor same as seperate backgrounds, all you have to do is set acutall position once per vertical blank and object will be moved instantly at new place. During frame you can move sprite even twice or more if you like (let's say turbo or double speed movement after taking a bonus, depending on your game physics) and update oam on screen in constant once per vblank for smooth 60fps animation.
 

Foxi4

Endless Trash
OP
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,850
Country
Poland
Right, objects, oams or so called sprites are supported by 2D hardware accelerator like old machines did. Sprites are not drawn every frame point by point, it's just exist and it's part of video processor same as seperate backgrounds, all you have to do is set acutall position once per vertical blank and object will be moved instantly at new place. During frame you can move sprite even twice or more if you like (let's say turbo or double speed movement after taking a bonus, depending on your game physics) and update oam on screen in constant once per vblank for smooth 60fps animation.
This is correct, the video engine is very much a separate piece of the puzzle that operates independently.
 

BlueFalconNDS

Member
Newcomer
Joined
Aug 16, 2023
Messages
20
Trophies
0
XP
16
Country
Canada
Hopefully I'm not annoying you guys with these questions but I have a few more.

I notice when taking input in a if statement [ if (KEY_TOUCH & keysHeld())], why is that we use one & and not &&? I do understand it is a bitwise AND, however I want to learn about the context here.

Also what's the difference of using printf() and iprintf(), I used both of them and they to be doing the same thing, is there a difference/recommendations?
 
Last edited by BlueFalconNDS,

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    K3Nv2 @ K3Nv2: https://youtube.com/shorts/vKQN3UiNNHY?si=fP6ZlFe-DhQf9SW7