Drunken Coders http://drunkencoders.com Breaking new ground in console homebrew development Sat, 30 Jan 2016 22:37:17 +0000 en-US hourly 1 http://wordpress.org/?v=4.1 Geometrix [GBC Game][GBDev 2015]http://antoniond_blog.drunkencoders.com/?p=447 http://antoniond_blog.drunkencoders.com/?p=447#comments Fri, 04 Dec 2015 15:32:01 +0000 http://8.447 Hello!

This is Just a preview of my entry for the GBdev GameBoy Coding Compo 2015! Sadly, only one more game entered the compo. But anyway, here it is!

Video: https://www.youtube.com/watch?v=qAvW2n8Qbeg

Source, binary and more information: https://github.com/AntonioND/geometrix


Geometrix GBC 1

http://antoniond_blog.drunkencoders.com/?p=447/feed/ 0
Tetris made with a PIC18F2550http://antoniond_blog.drunkencoders.com/?p=430 http://antoniond_blog.drunkencoders.com/?p=430#comments Sun, 20 Sep 2015 21:06:16 +0000 http://8.430 Hello again!

This time I’m writing to show you my newest electronic creation: a “videogame console” made with a PIC18F2550 as its brain. It has a 8×16 LED matrix as main display and 4 seven segment displays to show the player score, for example. It has 8 buttons (4 in a pad, the rest like a Game Boy or similar). The PIC doesn’t have enough pins to control everything easily, so I’ve used a few 74HC595 to control the LED matrix and the seven segment displays, and a 74HC166 to read the state of all buttons.

Tetris circuit. Front.Tetris circuit. Back.


The game I’ve decided to code for the demonstration is a simple Tetris, written in 100% assembly. YouTube video here: https://www.youtube.com/watch?v=5fmSbreJsDI


– Move left/right.

– Rotate clockwise/counterclockwise.

– Go down quickly, go down instantly.

– Pause, reset.

At the moment I haven’t decided if I should post the schematics and the source code for various reasons:

– I used different systems to power the LED matrix and the seven segment displays. A decent design should use the same system.

– Some resistors are too high and the LEDs, when lit, are too dim. I didn’t consider the scanline refresh when calculating the resistors, so the actual intensity is a lot lower than I thought.

– The source code of the game is a bit messy (the “drivers” code is good, though).

Well, I hope you like it!

EDIT: Well, what the hell, I’ll just put the schematics and the code… :P


Tetris Schematics ColorTetris Schematics B/W


Don’t expect it to be clean and/or human readable. The file ‘main.asm’ contains the ‘drivers’, and ‘tetris.asm’ contains the game. It’s fairly easy to modify it to change some of the pin assignments.

Download (gEDA schematics + assembly code + assembled .hex file)

New version (2015/10/06), with a small modification to prevent “ghosting” between displays when performing the scanline change. Also fixed a bug that didn’t remove completed lines before checking if game over.


Possible improvements

– Change pull-down resistors for the buttons (R29-R36) by 10K resistors.

– Change LED resistors (330 Ohm) by 300 or maybe 220 resistors (R1-R16 and R37-R44).

– Make the LED matrix and the seven segment displays work the same way (changing the displays by common-anode equivalents and the transistors used to drive them).

http://antoniond_blog.drunkencoders.com/?p=430/feed/ 0
Electronic dicehttp://antoniond_blog.drunkencoders.com/?p=412 http://antoniond_blog.drunkencoders.com/?p=412#comments Sun, 12 Jul 2015 18:15:38 +0000 http://8.412 Well, after a few weeks not doing almost anything i’ve decided to post the last useless device I’ve made. It’s a simple electronic dice using a decade counter and some inverters (used for logic and as oscillator). I decided not to use a microcontroller because it felt like it was cheating for a circuit this simple… There’s not much to say: pressing the button will enable the counter and releasing the button will stop it and show a “random” number. The counter is very fast so it’s impossible to guess what number is going to show when the button is released. Here are a few pictures (clic to enlarge):

Electronic dice 1

Electronic dice 2

I used foam to hold the board and the battery in place so that it could be put into a handmade box.

Electronic dice 3

Electronic dice 4

And, at last, a video of the circuit working: https://www.youtube.com/watch?v=t7blrTmsfKg

While it’s a really simple circuit to design and make, I’ve decided to create schematics in case someone wants to make this (clic the image to enlarge it):

Electronic dice schematics.

Almost all components are specified in the schematic. The ones that aren’t completely specified are the voltage regulator (78L05), the inverter IC (74LS04) and the decade counter (HFC4017).

There are two interesting things about this circuit, and the reason of both of them is that I didn’t want to buy many components, so I decided to use whatever I had at home:

– The first one is that the oscillator is made of two inverters (and a capacitor and resistor). The values I’ve used for R and C make it oscillate at 390Hz approximately.

– The second one is that I don’t use an IC with OR gates. I’ve decided to use some diodes and resistors to create them (https://en.wikipedia.org/wiki/Diode_logic).

The circuit uses a 9V battery. The logic of the circuit needs about 11mA, and the LEDs need around 9mA each. That means it uses from 20mA (when only 1 LED is on) to 65mA (when 6 LEDs are on). The 7 LEDs can’t be turned on at the same time, obviously.

http://antoniond_blog.drunkencoders.com/?p=412/feed/ 0
GBT Player usagehttp://antoniond_blog.drunkencoders.com/?p=392 http://antoniond_blog.drunkencoders.com/?p=392#comments Tue, 07 Apr 2015 23:36:10 +0000 http://8.392 Last months I’ve seen a few tutorials about GB programming that use my GBT Player for music. I have to say a few things about that.

First: Thanks a lot for using my library! The fact that you like it enough to use it in your projects is what keeps it alive. Version 2 only happened because I was asked to add a few things to previous versions.

Second: You are using it wrong!

Ok, it’s partly my fault, but it’s your fault too, obviously: You are using GBDK just don’t understand the implications of the ROM banking system the GB uses. I know I don’t really have to explain this kind of things, but I feel a bit responsible for my library, so I’m going to explain why you are using it wrong and how to fix it. Let’s see…

My (now old) example does something like this:


While the new one does this:


So what’s the big deal?

Well, let’s explain how the GB banking system works. You should know that. If you don’t know that, you shouldn’t be coding for GB, but whatever… The first 16 KB of the ROM are always mapped to addresses 0000h-3FFFh. This means that this code and data is allways available to read! That’s the place where you want your game engine to be. But GBT code doesn’t go there by default. It goes to bank 1. Why? Because 16 KB is not much space. GBT player needs quite a bit of space and it’s stupid to put it in bank 0 when GBDK is already using a lot of that space. It would leave you even less space for your code!

Banks 1 and beyond are mapped to addresses 4000h-7FFFh. Only one of them can be mapped at once there, so they must be switched when the program needs to access to data in other bank. This means that you can have as much 16 KB banks as you need (so you could have ROMs with infinite banks), but you have to take care to switch to the correct bank whenever you are trying to get some data or execute some code.

So GBT runs in bank 1, again, what’s the big deal?

The problem here is that the first example uses interrupt for the player update function. If your game loop is small, and your ROM is small, and your game is more or less a “Hello World!” this won’t be a problem because you won’t use bank switching and bank 1 will always be available. If your game is big and you need to load data from other banks, there are problems because you will sometimes use data from other banks (in fact, GBT Player loads data from the bank where the song is placed, but it returns to bank 1 afterwards to execute its code).

An interrupt means that the CPU jumps to an interrupt vector, executes its code and then returns to its previous state. Again, you should know that. If you don’t, something is wrong and you should stop and learn this kind of things first. But you won’t, that’s why I’m explaining this. This is useful to know when hardware events happen, like screen redraw. Vertical blanking period is entered when the current frame has been drawn, so it happens once every frame (around 60 times per second). This interrupt is very useful to synchronize the game, and GBT Player uses it to update itself.

Let’s say you want to load a background placed in bank 4. You switch to bank 4, start copying… and vertical blank period is reached. An interrupt is triggered and CPU jumps to the VBL interrupt vector at address 0040h. It eventually calls gbt_update(), which switchs to bank 1, and returns. The CPU returns to the same state as before the interrupt… but the ROM bank is still bank 1, so the copy won’t work because it’s reading incorrect data! This can also happen with code. In that case, the game is likely to crash immediately. If you only had banks 0 and 1, this couldn’t happen. That’s why small demos and games run ok, but they will eventually crash if they are more than 32 KB big (2 banks).

That’s why the code of gbt_player.h says “this changes to bank 1!”. To warn you that this does changes to the memory that you have to revert. I don’t know what system GBDK uses to know what ROM bank is being used or anything like that. When I made a game (that I didn’t finish) I created a ROM bank stack to keep track of the banks that I used, so I could save the bank that was being used when gbt_update() was called and I could restore it afterwards. I suppose most of you won’t be able to do something like that, so the new example is an alternative.

The new example calls gbt_update() every frame in the main loop of the game. You need to put it in EVERY main loop, and you need to synchronize the game to VBL with wait_vbl_done(). That’s it. No more problems of banking. Well, there still can be problems, but it’s more difficult. If you can write code that has that kind of problems, you should be able to fix them.

In fact, if your game doesn’t use wait_vbl_done(), your game is evil. This function enters a low consumption state until VBL is entered, and it should be used whenever it’s possible. I’ve seen code of “games” that don’t use it. They are bad. No, seriously, use that function. Please.

Examples of bad tutorials (I could only find 2 right now, both in spanish):



PS: But you are, in fact, using GBDK, which should be avoided. :P It’s better if you try to learn ASM and use RGBDS or WLA-DX assemblers instead. The GB CPU is very slow, so you need every CPU cycle you can get. Maybe you are doing a text-only game, and then GBDK should be fine, but you are wasting a lot of CPU power. GBDK functions are mostly written in ASM. I know it’s harder and you want to see results right now, but it’s better.

PS2: Anyway, update to the latest version (2.1.1 at the time of writing this, the same as 2.1.0 but with a new GBDK example): https://github.com/AntonioND/gbt-player

http://antoniond_blog.drunkencoders.com/?p=392/feed/ 0
Game Boy Camera documentationhttp://antoniond_blog.drunkencoders.com/?p=382 http://antoniond_blog.drunkencoders.com/?p=382#comments Thu, 12 Mar 2015 12:20:26 +0000 http://8.382 The last few weeks I’ve been doing some reverse engineering on the GB Camera and I think I’ve documented it good enough for most things. I’ve reversed the catridge controller registers and measured the capture timings. The only thing that I can’t understand is the level/gain control of the sensor (its datasheet is really bad) but it shouldn’t matter for an emulator. I’ve implemented this in GiiBiiAdvance and it’s working perfectly (you’ll have to wait for the next version if you want to chek it, though), so I guess that’s all that matters.

And a pic of my hardware setup (click to zoom):


GB Camera + Arduino

The repository is here: https://github.com/AntonioND/gbcam-rev-engineer

The first release is here: https://github.com/AntonioND/gbcam-rev-engineer/releases/tag/v1.0

PS: Did I mention before that I won the GBdev 2014 compo? :P http://gbdev.gg8.se/forums/viewtopic.php?id=244

http://antoniond_blog.drunkencoders.com/?p=382/feed/ 0
Pong 3DS v1.0http://antoniond_blog.drunkencoders.com/?p=372 http://antoniond_blog.drunkencoders.com/?p=372#comments Tue, 17 Feb 2015 23:21:15 +0000 http://8.372 Well, after a few weeks of coding at last I have a release of this game. :)

This is just a 3D pong, inspired by my old Pong 3DS for Nintendo DS: http://antoniond.drunkencoders.com/pong3ds.html

This was a test to see if I could manage to setup a correct perspective for stereoscopic 3D, but I decided to make a simple game out of it.

I haven’t used hardware accelerated graphics because I couldn’t manage to compile any shader assembler (except from Picasso, but it’s a bit outdated). I originally made this 3D software engine for GBA, so there are still a few files from Tonc’s library for GBA. Anyway, this game always runs at 60 FPS. :)

If anyone cares, the 3D engine can draw lines, triangles, quads, line strips, triangle strips and quad strips, all of them With alpha blending. Quads are drawn using a function used just for them to draw them faster, not by drawing two triangles. It supports directional lighting (up to 32 lights, limited to 2 because why more) and ambient lighting. It can sort polygons with painter’s algorithm (no buffer-Z). It doesn’t support textured polygons.

Oh, the 3D slider actually does something when you change its position. The engine renders each top screen in a separate core when working in 3D mode to make it run always at the same FPS.

Press Y to take a screenshot in PNG format (2 files in 3D mode, 1 in 2D mode) or SELECT to exit. Use the joystick to move the pad, and A to jump in the 2.5D room.

For now, in the main menu, press a button in the touchscreen to enter a game room. Pressing START in a room will return the game to the main menu.

Holding L and R at the same time will show the FPS and the CPU usage.

This game is licensed under the GPL Version 3.

Binaries here:


Source code and more information here:


And a few screenshots:





http://antoniond_blog.drunkencoders.com/?p=372/feed/ 0
Pong 3DS, now in an actual Nintendo 3DS!http://antoniond_blog.drunkencoders.com/?p=363 http://antoniond_blog.drunkencoders.com/?p=363#comments Sat, 24 Jan 2015 16:16:36 +0000 http://8.363 Ok, this has nothing to do with this: http://antoniond.drunkencoders.com/pong3ds.html

This is just a stupid stereoscopic 3D software rendering test. Oh, the 3D slider actually does something when you change its position.

No screenshot because it’s just a bunch of wireframe boxes IN THREE DIMENSIONS. Maybe next time.

Link: https://github.com/AntonioND/pong-3ds

EDIT (2015/01/25):

Well, I’ve updated it a bit. Filled polygons, ligthing, screenshots

Pong 3DS screenshot.

http://antoniond_blog.drunkencoders.com/?p=363/feed/ 0
Back to Color [GBC Demo][GBDev 2014]http://antoniond_blog.drunkencoders.com/?p=314 http://antoniond_blog.drunkencoders.com/?p=314#comments Sun, 29 Jun 2014 22:01:20 +0000 http://8.314 Hello again!

This time I’m here to show you my first GBC demo! Made specially for the 2014 GBDev Game Boy Coding Compo. In fact, I started it like four years ago, but stopped after doing 3 scenes (the first effect, the text scroller at the end of the first song and the colorful horizontal bars before the matrix effect). I’ve coded the rest in just a few weeks, in fact I started doing new effects a few days before the GBDev compo announcement. I’ve coded it and made the music, the only thing I haven’t done is the train screen graphics and the credits graphics (by jordirafel). I’m not a very good musician, so… Oh, and it shows a little credits screen in non-color GBs! Some screenshots here:


Back to Color screenshots

Back to Color DMG screenshot


And here it is, the download link:



If you want to test it in an emulator (like BGB, Gambatte or VBA-M), most of them have a “Frame Blending” option, enable it if possible. It works in real hardware, of course! Tested in DMG, MGB, GBC, GBA and GBA SP. If you want to skip some parts, hold up/right/down/left when the demo is loading if you want to skip 1/2/3/4 songs. Non-color GB only show the reduced credits screen!


I have also recorded a video with VBA-M in case you are too lazy to download an emulator! :P The shadows in the bouncing balls effect are incorrect, they should be the same color, but the rest is correct. In a real GBC or an emulator it looks fine.




The demo has been coded using my fork of RGBDS (with a few bugfixes), a GBT Player version modified to use only the first ROM bank, OpenMPT to make the music, GBTK and GBMB. To test it i’ve used BGB, Gambatte, VBA-M, my GiiBii Advance… and real hardware.

I hope you like it! :)





One last thing…


Source code GitHub repository


Yeah, that’s everything. :P


http://antoniond_blog.drunkencoders.com/?p=314/feed/ 0
Faceball 2000 Double Speed Mod. Version 0.1http://antoniond_blog.drunkencoders.com/?p=304 http://antoniond_blog.drunkencoders.com/?p=304#comments Wed, 04 Jun 2014 14:29:57 +0000 http://8.304 Hello!

A few days ago I discovered this game, I tried it, and got disappointed because it has a painfully low framerate (video)… So I opened it with BGB and decided to fix it! :)

I’ve changed the initial jump to an empty area just before the ROM header. I just switch to CPU double speed mode, wait for VBL, load greyscale palettes for BGs and sprites and jump to real start point. Of course, it only works in GBC and GBA now! Header’s GBC flag has been changed, and checksums have been fixed.

Just to make it clear: This makes the CPU faster, so everything changes. Music is still the same because it uses interrupts for synchronization, framerate is multiplied by at least 2, but timer countdown is affected! Also, link communications are affected.

– You’ll need an IPS patcher like Lunar IPS: http://www.romhacking.net/utilities/240/

– And, of course, the “Faceball 2000 (USA).gb” ROM, which I won’t provide here, sorry.

The patch you have to apply is here:


I haven’t tested Link functions, but it should work between modified copies of the game. I haven’t changed the way the game works. It still copies VRAM data manually instead of using GDMA or something like that, but that would need some extra work, and it runs fine now. I hope you like it!


http://antoniond_blog.drunkencoders.com/?p=304/feed/ 0
GBT Player v1.2http://antoniond_blog.drunkencoders.com/?p=298 http://antoniond_blog.drunkencoders.com/?p=298#comments Thu, 01 May 2014 13:36:22 +0000 http://8.298 Hello again!

This is just a bugfix release. I’ve modified the samples included in the mod file in order to avoid transposing notes when converting it with mod2gbt. This means that you can use all notes from C3 to B8.

You can find source, binaries and examples (RGBDS and GBDK) for version 1.2 here:


And any other improvements, here:



EDIT: This is an old version, please, check this link for the latest release: https://github.com/AntonioND/gbt-player/releases

http://antoniond_blog.drunkencoders.com/?p=298/feed/ 0
Switch GBA into GBC mode by software.http://antoniond_blog.drunkencoders.com/?p=296 http://antoniond_blog.drunkencoders.com/?p=296#comments Sun, 20 Apr 2014 18:46:36 +0000 http://8.296 Hello!

I know dwedit did something like this a few years ago, but I never managed to compile its code and make it run (even though the precompiled binary worked fine). Anyway, the code was just an incomprehensible mess:


This morning I’ve read that the GBA bios still had the piece of code that switches the GBA into GBC mode, like Nintendo planned to do in the begining (before deciding that it would be better to do it with a hardware switch).

I’ve disassembled the part that performs this switch and made a little demo to show that it works! Here it is:


But it doesn’t end here! It can also apply an affine transformation to the GBC video output, and even enable mosaic effect or GREENSWP register! The GREENSWP register works a bit different than in GBA mode, by the way.

The bad thing is that the video output only works in GB Micro, in GBA and GBA SP it shows a black screen, and in DS it doesn’t work at all (it doesn’t have a GB CPU).

I’ve also uploaded a video:




http://antoniond_blog.drunkencoders.com/?p=296/feed/ 0
GiiBiiAdvance 0.2.0http://antoniond_blog.drunkencoders.com/?p=281 http://antoniond_blog.drunkencoders.com/?p=281#comments Fri, 04 Apr 2014 10:43:59 +0000 http://8.281 Hello!

After a few years, at last I continue with this emulator. :P I’ve been coding for a few weeks and at last I have a release.

The main change is that it now uses SDL2 for the GUI, not WinAPI. It can compile under Windows or Linux with the included Code::Blocks project file (I want to add a Makefile later). You can check the source code here:




Some screen captures:



This time I’ve included GB Camera emulation again. In GiiBiiAdvance 0.1.0 was removed (GiiBii had it). This time I’ve documented it all, and I’m planning on doing some reverse engineering on the real hardware (I’ve bought a few cartridges just in case I break one). For now, I’ve only used the GB Camera software to see how it works, and I’ve managed to emulate almost everything (contrast, dithering and brightness). It is quite complicated to do emulate brightness because it depends on the webcam and on the GB Camera software, so it changes weirdly sometimes. The documentation is in “/docs/” folder in github.



Of course, there is a debugger. I’ve improved it since 0.1.0, and now it allows to dump any image that it displays. There are more changes, see the changelog in the source code if you are interested.



The binaries are here. There are binaries for Windows and Linux Mint/Ubuntu, with and without webcam support:




I hope you like it!


http://antoniond_blog.drunkencoders.com/?p=281/feed/ 0
Mtheall has responded to the challenge… Spaceship generator ported to an html5 canvashttp://drunkencoders.com/2013/08/mtheall-has-responded-to-the-challenge-spaceship-generator-ported-to-an-html5-canvas/ http://drunkencoders.com/2013/08/mtheall-has-responded-to-the-challenge-spaceship-generator-ported-to-an-html5-canvas/#comments Fri, 30 Aug 2013 21:28:24 +0000 http://drunkencoders.com/?p=6220 In responce to the call for extra credit in the Space Invaders test compo Mtheall has implemented the space ship generator code on an HTML 5 canvas. Now you have no excuses!



http://drunkencoders.com/2013/08/mtheall-has-responded-to-the-challenge-spaceship-generator-ported-to-an-html5-canvas/feed/ 4
How do I rotate a background Image?http://answers.drunkencoders.com/how-do-i-rotate-a-background-image/ http://answers.drunkencoders.com/how-do-i-rotate-a-background-image/#comments Tue, 27 Aug 2013 14:29:50 +0000 http://46.314 /*--------------------------------------------------------------------------------- Rotate a bitmap ---------------------------------------------------------------------------------*/ #include #include "drunkenlogo.h" //--------------------------------------------------------------------------------- int main(void) { //--------------------------------------------------------------------------------- videoSetMode(MODE_5_2D); vramSetBankA(VRAM_A_MAIN_BG); int bg = bgInit(3, BgType_Bmp16, BgSize_B16_256x256, 0,0); dmaCopy(drunkenlogoBitmap, bgGetGfxPtr(bg), drunkenlogoBitmapLen); bgSetCenter(bg, SCREEN_WIDTH/2, SCREEN_HEIGHT/2); bgScroll(bg, SCREEN_WIDTH/2, SCREEN_HEIGHT/2); while(1) { swiWaitForVBlank(); bgRotate(bg, 10); bgUpdate(); } }

Remember to call bgUpdate() once per frame when using rotations and scaling. You can set a specific angle with bgSetRotate().

http://answers.drunkencoders.com/how-do-i-rotate-a-background-image/feed/ 0
How do I print on both screens AND use the keyboard.http://answers.drunkencoders.com/how-do-i-print-on-both-screens-and-use-the-keyboard/ http://answers.drunkencoders.com/how-do-i-print-on-both-screens-and-use-the-keyboard/#comments Tue, 27 Aug 2013 14:08:08 +0000 http://46.311 /*--------------------------------------------------------------------------------- Printing on both screens with a keyboard ---------------------------------------------------------------------------------*/ #include #include PrintConsole pcMain; PrintConsole pcSub; void keyUp(int c) { consoleSelect(&pcSub); iprintf("%c", c); } //--------------------------------------------------------------------------------- int main(void) { //--------------------------------------------------------------------------------- videoSetMode(MODE_0_2D); videoSetModeSub(MODE_0_2D); vramSetBankA(VRAM_A_MAIN_BG); vramSetBankC(VRAM_C_SUB_BG); //two print consoles consoleInit(&pcMain,0,BgType_Text4bpp, BgSize_T_256x256, 2,0,true,true); consoleInit(&pcSub,0,BgType_Text4bpp, BgSize_T_256x256, 2,0,false,true); //create a window for the sub display so it stays above the keyboard. consoleSetWindow(&pcSub, 0,0,32,8); //set up our keyboard. I used mtheal's vram tool to figure out where to put everything Keyboard* kb = keyboardInit(0,1,BgType_Text4bpp, BgSize_T_256x256, 3,1,false,true); kb->OnKeyReleased = keyUp; keyboardShow(); //prove the top screen works consoleSelect(&pcMain); iprintf("hello cruel world...."); while(1) { keyboardUpdate(); swiWaitForVBlank(); } }

Probably the most challenging aspect of this is setting up your background memory so your maps and your graphics dont overlap between the keyboard and the console. I used mtheall’s slick vram tool to figure it out:


http://answers.drunkencoders.com/how-do-i-print-on-both-screens-and-use-the-keyboard/feed/ 0
Aperture Science DS is featured on Polygonhttp://drunkencoders.com/2013/08/aperture-science-ds-is-featured-on-polygon/ http://drunkencoders.com/2013/08/aperture-science-ds-is-featured-on-polygon/#comments Mon, 26 Aug 2013 14:44:22 +0000 http://drunkencoders.com/?p=6195 Great work Smea and always fun to see homebrew show up in something akin to mainstream media.


http://drunkencoders.com/2013/08/aperture-science-ds-is-featured-on-polygon/feed/ 3
Compo engine test #2. Create a space invaders clone.http://drunkencoders.com/2013/08/compo-engine-test-2-create-a-space-invaders-clone/ http://drunkencoders.com/2013/08/compo-engine-test-2-create-a-space-invaders-clone/#comments Sun, 25 Aug 2013 16:26:05 +0000 http://drunkencoders.com/?p=6192 Another test of the compo engine to see if it is ready to handle the load necessary for a smooth winter coding compo.

Your goal is to create the worlds greatest Space Invaders clone for the Nintendo DS. There is no requirement for this to run on hardware (I am too lazy to test outside of the emulator anyway) nor are there any other rules really. Compos will be judged against arbitrary standards to be announced soon ish.

You have one week!

Space Invaders!

To get you started, here are some spaceship graphics I stole from http://web.archive.org/web/20080228054410/http://www.davebollinger.com/works/pixelspaceships/ (bonus points to anyone who can get the space ship generator on that site working as an html5 canvas) Mtheall has done it! http://mtheall.com/~mtheall/spaceship.html


http://drunkencoders.com/2013/08/compo-engine-test-2-create-a-space-invaders-clone/feed/ 0
How do I display a 16-bit image on the screen?http://answers.drunkencoders.com/how-do-i-display-a-16-bit-image-on-the-screen/ http://answers.drunkencoders.com/how-do-i-display-a-16-bit-image-on-the-screen/#comments Sat, 24 Aug 2013 21:15:26 +0000 http://46.300 Here is a very simple example on how to take an image stored as a .png file (or any of the supported file formats supported by grit) and put it on the screen. The first thing we need is a target image. For that let’s chose our standard Drunkencoder logo:


I cant tell you exactly how this was made but I am fairly certain there was some sort of paint program involved. Anyway, notice it is 256×256 png file which matches one of the allowed background sizes for the DS (this isn’t necessary it just makes the copy to video memory trivial and this demo is…well…trivial).

Next we need to convert our image into something the DS can understand and there are a few ways to make that happen. We could write it to an SD card file system or use the special file system supported by Nintendo DS roms called nitroFS and then try to parse the data as we load it. Or we could do it the easy way and run it through grit which is included with libnds and devkitPro.

To do this is very easy. Simply create a folder called “gfx” or “data” in your project directory and drop the png file into it. Next create a text file with a name that matches your image file but with an extension of .grit.

This file is how we tell grit what to do with our image….in this case we want it to create a binary array of pixel data we can just copy into video memory. Its pretty simple and we only need three options. First is -gT! this tells grit to ignore any transparency data in the image file and set all the pixels to visible (if you don’t use this there is a good change your image won’t render correctly or you will have to set the alpha bits of each pixel manually as you load them into video memory, using the alpha channel of an image is very useful but for now its likely just to confuse things so lets turn it off).

Then we do -gB16 and -gb which tells grit to produce 16 bit pixel bitmap data and to write that out as a straight binary file. There are a LOT of options for grit and we will explore many of them in these FAQs but your best bet is the grit readme or website to really get a feel for how it all works.

Your grit file should look something like the following:

# disable alpha and set opaque bit for all pixels

# 16 bit bitmap

The magic makefile included in the libnds example templates will automatically call grit on all the image files in your gfx directory using the options supplied in the grit file. This will produce a binary file and a header file for easy inclusion in your project.

Here is what the actual code for the demo looks like:


Render a 16 bit per pixel bitmap to the screen


int main(void) {


	int bg = bgInit(3, BgType_Bmp16, BgSize_B16_256x256, 0,0);
	dmaCopy(drunkenlogoBitmap, bgGetGfxPtr(bg), drunkenlogoBitmapLen); 


	return 0;

That “drunkenlogo.h” is the header file auto generated by grit. You can find it in your “build” directory after you call make. If you were to open it up you would see something like the following:


//	drunkenlogo, 256x256@16, 
//	Alphabit on.
//	+ bitmap not compressed
//	Total size: 131072 = 131072
//	Time-stamp: 2013-08-24, 17:01:25
//	Exported by Cearn's GBA Image Transmogrifier, v0.8.10
//	( http://www.coranac.com/projects/#grit )


#define drunkenlogoBitmapLen 131072
extern const unsigned int drunkenlogoBitmap[32768];


http://answers.drunkencoders.com/how-do-i-display-a-16-bit-image-on-the-screen/feed/ 0
How do i combine a bitmap background with text?http://answers.drunkencoders.com/how-do-i-combine-a-bitmap-bacground-with-text/ http://answers.drunkencoders.com/how-do-i-combine-a-bitmap-bacground-with-text/#comments Sat, 24 Aug 2013 15:53:08 +0000 http://46.289 Let’s just modify the libnds 16-bit bitmap demo included in libnds examples to allow text on the same screen. All we really need to do is set up the console for the main display using background layer 0 and move our bitmap graphics to make room for it by adjusting its map base offset (an offset of 1 moves the memory 16KB further into memory which is plenty of room for a font and a map layer for text).


int main(void) {

        int bg = bgInit(3, BgType_Bmp16, BgSize_B16_256x256, 1,0);
	decompress(drunkenlogoBitmap, bgGetGfxPtr(bg), LZ77Vram); //Displays/decompresses top image

	consoleInit(0,0, BgType_Text4bpp, BgSize_T_256x256, 4,0, true, true);
	iprintf("Hello there !");
       while(1) {
	return 0;

And we get:

http://answers.drunkencoders.com/how-do-i-combine-a-bitmap-bacground-with-text/feed/ 0
How do I draw to the screen as a 16 bit bitmap?http://answers.drunkencoders.com/how-do-i-draw-to-the-screen-as-a-16-bit-bitmap/ http://answers.drunkencoders.com/how-do-i-draw-to-the-screen-as-a-16-bit-bitmap/#comments Sat, 24 Aug 2013 03:21:48 +0000 http://46.279 /*--------------------------------------------------------------------------------- Basic template code for starting a DS app ---------------------------------------------------------------------------------*/ #include <nds.h> //--------------------------------------------------------------------------------- int main(void) { //--------------------------------------------------------------------------------- int x, y; //set the mode to allow for an extended rotation background videoSetMode(MODE_5_2D); videoSetModeSub(MODE_5_2D); //allocate a vram bank for each display vramSetBankA(VRAM_A_MAIN_BG); vramSetBankC(VRAM_C_SUB_BG); //create a background on each display int bgMain = bgInit(3, BgType_Bmp16, BgSize_B16_256x256, 0,0); int bgSub = bgInitSub(3, BgType_Bmp16, BgSize_B16_256x256, 0,0); u16* videoMemoryMain = bgGetGfxPtr(bgMain); u16* videoMemorySub = bgGetGfxPtr(bgSub); //initialize it with a color for(x = 0; x < 256; x++) for(y = 0; y < 256; y++) { videoMemoryMain[x + y * 256] = ARGB16(1, 31, 0, 0); videoMemorySub[x + y * 256] = ARGB16(1, 0, 0, 31); } while(1) { swiWaitForVBlank(); } }

The only thing of note in the above that may not be clear from the code and cross referencing the libnds documentation is the macro ARGB16(). This takes an alpha value (1 for visible, 0 for transparent) and 5 bits of red green and blue to form the 16 bit color.

http://answers.drunkencoders.com/how-do-i-draw-to-the-screen-as-a-16-bit-bitmap/feed/ 0