OpenGL ES, C++, and sprite animation

Apprentice
Posts: 9
Joined: 2010.11
Post: #1
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!
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #2
Here are two articles that might help.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2010.11
Post: #3
(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
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
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.
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #5
(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.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2010.11
Post: #6
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.
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #7
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
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Have Hierarchical Sprite Tree, Need To Transform Screen X,Y To Determine Which Sprite kalimba 5 6,198 Mar 16, 2009 06:31 PM
Last Post: AnotherJake
  Objective-C Sprite Animation Case 2 5,874 Jun 25, 2003 04:58 PM
Last Post: Case