Calculate time with libnds

Discussion in 'NDS - Emulation and Homebrew' started by Morgawr, Jan 23, 2009.

Jan 23, 2009

Calculate time with libnds by Morgawr at 6:35 PM (1,245 Views / 0 Likes) 2 replies

  1. Morgawr
    OP

    Member Morgawr GBAtemp Fan

    Joined:
    Dec 18, 2008
    Messages:
    330
    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 >_.
     
  2. Jeremysr

    Newcomer Jeremysr Member

    Joined:
    Jul 26, 2006
    Messages:
    21
    Location:
    Kamsack, Saskatchewan, Canada
    Country:
    Canada
    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!)
     
  3. Jeremysr

    Newcomer Jeremysr Member

    Joined:
    Jul 26, 2006
    Messages:
    21
    Location:
    Kamsack, Saskatchewan, Canada
    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++;
    }
     

Share This Page