gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Using timer to record hold down keys

#176932 - blessingta@hotmail.co.uk - Thu Oct 27, 2011 8:15 pm

hi

What is the best method for recording keys which have been held down? I'm thinking that it would be using a timer to count "key down" and "key release", but I've got the dilemma of finding an equivalent <ctime> library for the gba

#176933 - Dwedit - Thu Oct 27, 2011 10:11 pm

You can keep time by counting frames. The GBA runs at 59.737 frames per second, but people usually refer to it as "60" frames per second.

To find out what the current frame number is, you need to do that yourself. Create a Vblank interrupt handler, and increment a variable once per frame. Something like this:
Code:
volatile u32 frame_number;
void MyVblankHandler()
{
...
frame_number++;
...
}
irqSet(IRQ_VBLANK, MyVblankHandler);
irqEnable(IRQ_VBLANK);

Then you look at frame_number to find out what frame number you're on.
This is a 32-bit int value, so it can count up to 4.294 billion frames (828 days) before it overflows back to zero.

So to determine how long a key has been held down:
* Need to know the start time of it first becoming pressed.
* Need to know invalidate the start time if it has become released.
* Need to know current time, and whether the key is currently pressed or not.
Then you subtract "now" from "then", and you have the number of frames the key has been held down.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."