Homebrew Calculate time with libnds

Morgawr

Well-Known Member
OP
Member
Joined
Dec 18, 2008
Messages
330
Trophies
0
Age
32
Website
Visit site
XP
138
Country
Italy
I know I've been posing many questions about libnds programming, but it's getting very frustrating since the API seems incomplete or really really bad put (I can't find half of the function I've seen used in examples) and the forums around the internet are almost all deserted... it seems like libnds is an unused, dropped, project.. wtf >_.
 

Jeremysr

Member
Newcomer
Joined
Jul 26, 2006
Messages
21
Trophies
0
Age
30
Location
Kamsack, Saskatchewan, Canada
Website
viewsourcecode.org
XP
209
Country
Canada
Oh, and to answer your question:

I have always done timings using the DS's V-blanks. A V-blank is basically the time it takes for the screen to refresh. It is almost exactly 1/60 of a second on the DS, so there are 60 V-blanks in a second. Your main game loop should look something like this:

Code:
touchPosition stylus;

while (1) {
ÂÂÂÂscanKeys();
ÂÂÂÂstylus = touchReadXY();

ÂÂÂÂ// ...
ÂÂÂÂ// (handle input, update gamestate, render to screen)
ÂÂÂÂ// ...

ÂÂÂÂswiWaitForVBlank();
}

The call to 'swiWaitForVBlank()' at the end of the loop causes the game loop to run every 1/60th of a second (resulting in a framerate of 60 frames per second unless your other code in the loop takes longer than that to execute). If you want to read input from the stylus only every X milliseconds you could keep a count of the number of frames (or V-blanks) that have passed since the beginning of the loop, and only read input from the stylus when the current V-blank count is divisible by a certain number. If that number is 30, then input will be read every 30 V-blanks (30/60 = 1/2 of a second). If the number is 5, then input will be read every 5 V-blanks (5/60 = 1/12th of a second).

So here's what it might look like after you do all this:

Code:
touchPosition stylus;
unsigned long vblank_count = 0;

while (1) {
ÂÂÂÂscanKeys();

ÂÂÂÂ// If vblank_count is divisible by 5...
ÂÂÂÂif (vblank_count % 5 == 0) {
ÂÂÂÂÂÂÂÂstylus = touchReadXY();
ÂÂÂÂ}

ÂÂÂÂ// ...
ÂÂÂÂ// (handle input, update gamestate, render to screen)
ÂÂÂÂ// ...

ÂÂÂÂswiWaitForVBlank();
ÂÂÂÂvblank_count++;
}
 
General chit-chat
Help Users
    Psionic Roshambo @ Psionic Roshambo: I'm not your pal buddy!