Homebrew Calculate time with libnds

  • Thread starter Thread starter Morgawr
  • Start date Start date
  • Views Views 2,785
  • Replies Replies 2

Morgawr

Well-Known Member
Member
Joined
Dec 18, 2008
Messages
330
Reaction score
0
Trophies
1
Age
36
Website
Visit site
XP
188
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 >_.
 
Sounds like you haven't been to the gbadev forums. There is a DS section (which is the most active section) with a lot of people who will gladly help you with libnds programming. libnds is definitely not unused or "dropped". (Any other methods of DS programming (such as PAlib and Lua) depend on libnds!)
 
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++;
}
 

Site & Scene News

Popular threads in this forum