Super Dodgeball


Hello everyone!

It’s about time for me to post my nds game on the blog and that’s what I’ll be doing in this post. I haven’t blogged yet, so this is my first entry, but I have been working on my nds game behind the scenes for quite some time.
I started studying and learning C about 4 months ago and understanding the nds took quite some time and a lot of effort. Therefore I couldn’t make a game on time for the first date and decided to give it a shot at the rebound.
I had tough time programming this little masterpiece. The main cause of this was the incorrect use of pointers. I would get the weirdest errors and crashes at runtime.
I must admit that I’m still no pointer guru, but I learned to use them a little.

Now about my game: It’s called Super Dodgeball.
As you may or may not know, dodgeball is the name of a sport. In dutch it is known as “trefbal”, but that would be quite silly to use as a name for my game, since the goal of the game is to, in fact, dodge balls :p.
You can move the ship by using the left or right buttons on the d-pad, or if you’re a lefty you can use the Y and A buttons to move. You can use the stylus to destroy the blue balls. You’re game over when you’ll get hit by a ball.
The scoring system works like this:
– You gain a point every 60 frames (1 second) you survive.
– You gain a point for every blue ball destroyed.

Probably the most problematic part of programming the game was learning how to use sprites. I found a grand total of  3 useful tutorials on the internet which explained them…. a little. They are far from complete guides, so it took a lot of effort, mostly trial and error, getting my sprites to do what I wanted them to do. I used all 3 of those tutorials to make my own sprite code, and I must say I’m happy how it turned out.
I first had to understand how the OAM (Object Attribute Memory) worked. Basically, the OAM is 1024 bytes big, each sprite will take up 8 bytes, so you can use 128 sprites at the same time. In conclusion: the OAM contains 128 SpriteEntry’s.
Then I had to learn how SpriteEntry’s work:
SpriteEntry’s contain 4 attributes, or better said 4 16-bit entry’s, because saying attribute could cause some confusion, since each ‘attribute’ is a 16-bits big, but contains more than one setting. Settings are for example what the sprite’s x and y locations are, how big it is, what sprite from the sprite memory to use, etc. I created my own struct named Sprite, which contains the x and y position of the sprite, its speed, and a pointer to the SpriteEntry.
It actually contains only 1 useful entry, which is the pointer to the SpriteEntry. The rest of the entries are also kept at object level (player and ball objects).

Another challenging part was programming the collision detection. I wanted to make a simple and a detailed version: The simple version would just check if the sprites boxes collide. And the detailed version would check on pixel basis only if the simple version would return true. I wanted to do this, because always using the detailed version would cause a huge and unnecessary amount of strain on de CPU.
I have the simple version working, but de detailed one is still on the to do list, so for now that one always returns true.
And to make it less stressful I altered the simple version a bit, to reduce the hit box on the balls and the spaceship. Schematically it now works something like this:

sprite hitbox example

So as you can see, you can touch a little edge of the ball without crashing, and your ‘wings’ can’t crash you either. At this time, this is the best solution for collision detection.
Note: There is a bug that sometimes (very rarely!!) causes collision detection to fail and you can fly straight through a ball. I haven’t been able to successfully reproduce the bug on demand, so I don’t know when or what’s going on. Feel free to look into my code and see if you can fix this. You can post back here if you have an answer ;)

As for the sound: Some or all of it may sound very familiar to you. Here’s what I used:

–          Fox Movie sound (intro)

–          Mortal Kombat sound (main game them sound)

–          Arnie’s legendary words: “I’ll be back” (game over)

–          Some popping sound (destroying a ball)

You can download the source here and the .nds file here. Or download them together in one package here.
*files are hosted on rapidshare and can only be downloaded 10 times. If you can’t download it, contact me, and I’ll try to find another solution, but for now, this will have to do.

I hope you enjoy the game!

Maarten Koot


Working game!


Hello hello!

Finally a game from the most time consuming student of contrechoc!

First a bit history, information about the things that pushed me in the directions of this little piece of art.
</sarcasm> (people who don’t know me: place these tags random in the rest of the text, you can’t go wrong)
Initially I had the idea to create a nice ‘framework’ , so you could easily make buttons in C. Just like in java or php. Make a button, put some text on it, adding a handler etc. But that was not going to happen, I found out after several weeks of time consuming research, cheers! It was too difficult for someone like me, who didn’t know anything about C yet

Contrechoc allready told me several times it was better if I made a simple game first, before I would drown myself in complicated pointer structures. I heard him but didn’t listen! So the first deadline for this class was a miss. I had to make a game in the retry.

So I decided to make a simple game with the information and basic functionality I had for the framework: some buttons on the screen, one of those pressed would take you to the next level! A hole in the market as we’d say here in the Netherlands.

Not only buttons should be visible, that’s not really a game. So I displayed the level and a timer on the screen, a lot more competitive. So after some trash buttons you press the ‘end-boss’-button of each level, proceed to the next level, repeat this like twenty times and set a new world record.
Then I decided to add sounds to this, something like drum sounds, so you press buttons like a crazy, and each ‘level’ you make a new popsong. So it’s not only competitive, but also a lot of fun. Or you just play for fun and make music, with five! different sounds.

This all could be programmed in under three hours, I made it in … I fell asleep while counting.

What took me so long?
Well the most ‘simple’ things were driving me crazy.
For example the case where I was adding the counter. It wouldn’t work for days, until master Contrechoc tested my app on the nds, and found out the emulator (wich we use to develop) doesn’t support time… awesome! I needed stuff like that too, and arranged a dstt card to get my code running on my little sister’s nds.
The last days I was trying to print the time taken in decimals on the screen, tried for days to print a double with “iprintf(…%.2f…)”. Found several working examples on the internet where they did what I wanted:  “printf(…%.2f….)”. Didn’t worked on my pc nor ds! why didn’t I just copy and paste the working code? why did I have to found out with Contrechoc that I missed the “i” as in “iprintf” in my code? Well it doesn’t matter anymore, I can’t ctrl+z my spent time anyway.
These were only two examples of some ‘easy’ to make functionality. Imagine me struggling with more ‘complicated’ C issues.

Because this was allready so time consuming for me, and especially contrechoc, the to-do-list (or wishlist) is kinda long for such a small game:
– Save the best time with name of competitor
– Make an exit program function, so I can’t be sued by addicted gamers
– Really make a good button framework (as initially planned)
– More buttons and sounds
– In-game configuring of levels, button sizes, number of buttons
– Best feature imho: switch buttons to the top screen while you have to press on the toucharea!
– Choose the sounds to use with the buttons
– Make it impossible to play the game while dragging like a crazy over the screen, so you have to ‘tap’ each button. You have to release after you tried a button. Anyway i can make a lot money with selling new touchscreens muhahaha.
– And etc.

[Edited download link on 08/03/2010]
Click here to get your own copy for free.

I decided to release the .nds file too, because there can be problems when you try to compile your own with my code. This can be caused due a difference in mine and your version of devkitpro.
Get the nds file here.

Strange dev-cpp error


Just made a test in Dev-cpp of a small bit of a sender routine. I got this error:
undefined  reference to ‘__cpu_features_init’
then all removed with an practically empty main.c, again the same error.

Found this link with the solution:

the solution being: Tools->Compiler Options->Directories->Libraries — delete all what is there and voila!

NDS Framework (in C)


Earlier I was planning a release 2 weeks ago on Sunday… But I really needed much more time, yesterday I finally got it working: my NDS Framework.

Since the whole blog is to give people more info about NDS programming and making it easier to program, I could just make a game like everyone does, but I’ve chosen to create something useful for almost everything you want to create on your NDS: A framework.

My framework works using a parent child concept (just like HTML) in order to position elements quickly to there positions. This way you can easily create a button on the right bottom side of your screen without calculating with pixels, it’s all done by the program!

Taken the above example: “You want to create a button on the right bottom side of the screen”, you will first have to create a screen to render on:

// Create a screen (width, height, backgroundimage (can be NULL))
struct CElement *screen1 = CElementCreateScreen(256, 192, backgroundimage);

// Since I don't have a button element yet, we will draw a squire
// It has the properties (RenderOn, leftOffset, topOffset, width, height)
CElementCreateSquire(screen, -1,-1,100,50);

If the leftOffset will be lower than 0, it will render it on the other side of the screen (the right side), the same counts for the topOffset. The width and height are 100px by 50px.

Another example can be found over here: where you can see it can also automatically scale (like: width = 100%) all sorts of elements by giving it a width of 0 or a height of 0. A negative width or height will create a rightOffset, so if you want to position something with a border of 5 px, you can do this:

CElementCreateRectangle(screen, 5,5,-5,-5);

I’ve created a screen, what’s next? Well now the program has to calculate the real width, height, offsetLeft and offsetRight of the element. It’s done using the same meganism that DOM is using, by looking to the parent of the element. This is because all elements are positioned relatively to their parents, for example, you’ve created a container (= a special element that can hold multiple elements) on the bottom right of the screen. If you will draw a Squire, it will be drawn on the bottom right section (so relatively to the container where it’s in). To calculate all this, you only have to call the next line:


Ok, so now you’ve got a screen holding elements, but you want to render it on one of the two screens of the NDS, this is what you will do to get that working:

CElementDraw(screen1, video_buffer1);
CElementDraw(screen1, video_buffer4);

It will now render my virtual screen to both screens of the NDS. This way you can make multiple screens and load them in memory, and when you need them, you only have to execute CElementDraw(screen_settings, video_buffer4); to get it working!

Is there more?

Well since a framework is never done, and I am limited to time, I included the following things in this framework:

  • The auto calculation of elements (biggest part of the code)
  • A lookup element to see on what element is pushed using CElementAt(screen1, x, y)
  • An element to draw a triangle
  • An element to draw a rectangle
  • An element to draw a line
  • An element to draw a circle
  • A List “class” to create dynamic arrays
  • A function to remove a bunch of elements (so if you want to delete screen1, you can just: CElementRemove(screen1) and it will remove all other elements with it! And release there pointers 😉 )
  • A draw function to create a AntiAliassed line using the Xiaolin Wu algorithm (echter deze kijkt ook echt naar de achtergrond om de anti aliassing kleur te genereren)
  • A draw function to create a Circle (not anti aliassed, since it costs 1000 lines of code to get that working)
  • An element that can contain multiple elements
  • An element that lets you devide a screen in to multiple sections (sortof a table)

Things like text elements aren’t implemented, but you can easy implement a Element by looking to my code. The only thing you really have to create is a function to draw it on your screen.


	// Create a virtual screen
	struct CElement *screen1 = CElementCreateScreen(256, 192);
	// Create a container fill it to the whole screen (width: 0, height: 0)
	struct CElement *container = CElementCreateContainer(screen1, 0, 0, 0, 0);
	// Split the screen in two sections vertically (and add it to the container)
	struct CElement *splitterY = CElementCreateSplitterY(container, 0, 0, 0, 0, 2);
	// Split the screen in three sections horizontally (and add them to the above splitter)
	struct CElement *splitterX1 = CElementCreateSplitterX(splitterY, 0, 0, 0, 0, 3);
	struct CElement *splitterX2 = CElementCreateSplitterX(splitterY, 0, 0, 0, 0, 3);
	// Add some elements to it:
	CElementCreateRectangle(splitterX1, 0,0,0,0);
	CElementCreateTriangle(splitterX1, 0,0,0,0);
	CElementCreateRectangle(splitterX1, 0,0,0,0);
	CElementCreateRectangle(splitterX2, 0,0,0,0);
	CElementCreateCircle(splitterX2, 0,0,0,0);
	struct CElement *rect= CElementCreateRectangle(splitterX2, 0,0,0,0);
	// Set properties & draw
	CElementDraw(screen1, video_buffer1);
	CElementDraw(screen1, video_buffer4);

	splashImage(video_buffer4, (u32 *)iepro_fun_img_bin);
	CElementDraw(screen1, video_buffer4);


  • Updated CDraw.h/.c + CDrawElement.h/.c; renamed Squire to Rectangle (since it is a rectangle, and squire isn’t spelled correctly
  • Added negative left / top to become the right offset like posted above


The download package will have 2 folders, C and C_NDS, the C is to easily debug (without using NDS related code), if compiled you can see exactly what it is doing. The C_NDS is the same package + NDS related code to create a NDS rom.

[ Download ]

Game final version


UPDATE: Project files can now also be found here:^_files.rar

This is the final version of the first nds game I made. Rules are simple because there are no rules. You can come up with them yourself. I hope you’ll enjoy the game.

Anti-aliased line Alpha



here you can see my “game” (which isn’t really a game) from my project. As you can see images are missing but I didn’t had time to do this. Therefor a little preview of a line with and without anti-aliasing. The algoritm I used for anti-aliasing is Xiaolin Wu’s line algorithm. For the “normal” line I used the Bresenham line’s algorithm. Source code can be found here. I hope you can see the difference 😉

Eclipse on windows using ARM9 toolchain


The following images contain some info about the settings for eclipse for the nds on windows XP. All settings can be found in the project settings.