GBA Development From the Ground Up, Volume 3
This article is going to be short and sweet. It's going to cover input, a remarkably easy topic that should be refreshing after the last volume. In fact, I'm not even sure this article is worthy of a whole volume, but this is how I'm going to do it anyhow.
Unlike the PC where you have a gagillion different possible input devices, we're blessed enough to only have one to deal with. What's more, this input device is simple enough to cover swiftly and efficiently.
Input from the keypad is detected in a single register located at 0x4000130. Thus, our definition would look something like this:
[indent][code]#define KEYS (*(volatile u16*)0x4000130)[/code][/indent] OR we could use the version defined in gba.h called [b]REG_P1[/b]. It's already defined, so you wouldn't need the above snippet. However, I like using the name [b]KEYS[/b] instead of [b]REG_P1[/b] because it seems more explanatory to me. You do whatever you want.
The variable is volatile because the register changes outside the code. Each bit in this register gives the status of a specific key. Thus, we want a header file so that we can access all the different bits with simple variable names. Here we go:
#define KEYA 1
#define KEYB 2
#define KEYSELECT 4
#define KEYSTART 8
#define KEYRIGHT 16
#define KEYLEFT 32
#define KEYUP 64
#define KEYDOWN 128
#define KEYR 256
#define KEYL 512
//This is already in gba.h, but it has a much less reader-friendly name (REG_P1)
#define KEYS (*(volatile u16*)0x4000130)
#define KEY_DOWN(k) ( ! ( ( KEYS ) & k ) )
#endif[/code][/indent] That's the entire file. Now to check if a specific button is pressed, we check that respective bit (using &) to see if it is clear. If so, the key has been pressed. Look at this:
[indent][code]if ( ! ( (KEYS) & DESIREDKEY ) )
//The ! makes sure the bit is NOT 1... if it's not 1, the key is pressed
}[/code][/indent] I prefer to make a little macro like this:
[indent][code]#define KEY_DOWN( k ) ( ! ( ( KEYS ) & k ) )[/code][/indent] Then I can just use [b]KEY_DOWN[/b] in my [b]if[/b] statement instead of remember the & and ! of [b]KEYS[/b].
So, with the help of keypad.h, all you have to do is use [b]KEY_DOWN[/b] to figure out if a button was pressed.
If you somehow don't understand input, here's a little example to clench the deal:
[indent][code]#include "gba.h" //Defined in Volume 2 (ALWAYS include this first)
#include "screenmodes.h" //Defined in Volume 2
u16* theVideoBuffer = (u16*)VideoBuffer;
#define RGB(r,g,b) (r+(g<<5)+(b<<10)) //Macro to build a color
void FillScreen( u16 color )
u16 x, y;
for ( x = 0; x < 240; x++ )
for ( y = 0; y < 160; y++ )
theVideoBuffer[ y * 240 + x ] = color;
SetMode( SCREENMODE3 | BG2ENABLE );
while ( 1 )
if ( KEY_DOWN( KEYUP) )
FillScreen( RGB(31,0,0) );
else if ( KEY_DOWN( KEYDOWN) )
FillScreen( RGB(0,31,0) );
else if ( KEY_DOWN(KEYLEFT) )
FillScreen( RGB(0,0,31) );
else if ( KEY_DOWN(KEYRIGHT) )
FillScreen( RGB(31,31,0) );
else if ( KEY_DOWN(KEYA) )
FillScreen( RGB(0,31,31) );
else if ( KEY_DOWN(KEYB) )
FillScreen( RGB(31,0,31) );
else if ( KEY_DOWN(KEYSTART) )
}[/code][/indent] All the demo does is fill the screen with a certain color depending on what button is pressed. Start ends the program.
In the next article, I'm going to give you information on a slightly complex/tricky topic-tile modes.
I would like to thank dovoto, as his tutorials have been my biggest source of information on GBA development since I started. Check out his site at [url="http://www.thepernproject.com/"]www.thepernproject.com[/url]. I'd also like to thank the guys in #gamedev and #gbadev on EFNet in IRC for all their help, and all the help they'll be giving me as I write these articles. Furthermore, I would like to thank [url="http://www.gbadev.org/"]www.gbadev.org/[/url]. The site is a great resource, and it is definitely worth your time.
Special thanks to [url="http://www.gamedev.net/"]www.gamedev.net[/url] for being my largest game development resource and for putting my articles on their site. Extra special thanks to Inspi for helping me with my coding woes and checking over this article.
If you have any questions or comments, you can always e-mail me at [email="email@example.com"]firstname.lastname@example.org[/email] or [email="email@example.com"]firstname.lastname@example.org[/email]. I can't promise I'll answer your questions, like your comments, or listen to your suggestions, but I'll at least read the e-mail (as long as it doesn't look like spam).