iDevGames Forums
OpenGL ES, C++, and sprite animation - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: OpenGL ES, C++, and sprite animation (/thread-8329.html)



OpenGL ES, C++, and sprite animation - pchukwura - Nov 18, 2010 08:59 AM

Just wondering what is recommended for maintaining a steady framerate with sprite animation ?

The only thing that looks useful is the clock() and CLOCKS_PER_SEC macro.

My theory would be to:

  1. Divide CLOCKS_PER_SEC by the frame rate you desire, say 12 (for 12 FPS)
  2. Get the difference in time of cock() and the last frame value of clock().
  3. If the difference is equal to or greater than CLOCKS_PER_SEC/12 then the next frame of the animation should be displayed


Would this be a correct assumption?

I'm trying to stick to C/C++ as much as possible so the core engine wouldn't be tied to the platform, which is why i'm avoiding the usage of NSDate, NSTimeInterval and such.

Thanks!


RE: OpenGL ES, C++, and sprite animation - ThemsAllTook - Nov 18, 2010 09:06 AM

Here are two articles that might help.


RE: OpenGL ES, C++, and sprite animation - pchukwura - Nov 18, 2010 09:18 AM

(Nov 18, 2010 09:06 AM)ThemsAllTook Wrote:  Here are two articles that might help.

Awesome! This seems to be exactly what i'm looking for. And very fast response btw. Smile


RE: OpenGL ES, C++, and sprite animation - OneSadCookie - Nov 18, 2010 09:25 AM

To be very clear, clock() is useless. There's no cross-platform high-resolution timer. The sacredsoftware tutorial ThemsAllTook linked has a list of some options, but it seems to be missing mach_absolute_time which is the best option for Mac OS X and iOS, though certainly not the easiest.


RE: OpenGL ES, C++, and sprite animation - ThemsAllTook - Nov 18, 2010 09:37 AM

(Nov 18, 2010 09:25 AM)OneSadCookie Wrote:  it seems to be missing mach_absolute_time which is the best option for Mac OS X and iOS

Yikes, need to fix that. Thanks for pointing it out.


RE: OpenGL ES, C++, and sprite animation - pchukwura - Nov 18, 2010 09:54 AM

So i guess it seems (as someone pointed out in a similar post on another board) that time is one of those things that should be platform specific, and recommended I create an interface specifically for the time on that platform.

I guess this makes the most sense, considering all platforms and devices would perform differently when it comes to something as precise as time.


RE: OpenGL ES, C++, and sprite animation - ThemsAllTook - Nov 18, 2010 10:05 AM

Here's the one I use (returned units are in seconds):

Code:
#if defined(__APPLE__)
#include <mach/mach.h>
#include <mach/mach_time.h>
#elif defined(__linux)
#include <time.h>
#elif defined(WIN32)
#include <windows.h>
#endif

double getCurrentTime() {
#if defined(__APPLE__)
    static mach_timebase_info_data_t timebaseInfo;
    
    if (timebaseInfo.denom == 0) {
        mach_timebase_info(&timebaseInfo);
    }
    return mach_absolute_time() * (double) timebaseInfo.numer / timebaseInfo.denom * 0.000000001;
    
#elif defined(WIN32)
    static LARGE_INTEGER frequency;
    LARGE_INTEGER currentTime;
    
    if (frequency.QuadPart == 0) {
        QueryPerformanceFrequency(&frequency);
    }
    QueryPerformanceCounter(&currentTime);
    
    return (double) currentTime.QuadPart / frequency.QuadPart;
    
#elif defined(__linux)
    struct timespec currentTime;
    
    clock_gettime(CLOCK_MONOTONIC, &currentTime);
    return currentTime.tv_sec + currentTime.tv_nsec * 0.000000001;
#endif
}