OpenGL ES, C++, and sprite animation
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:
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!
The only thing that looks useful is the clock() and CLOCKS_PER_SEC macro.
My theory would be to:
- Divide CLOCKS_PER_SEC by the frame rate you desire, say 12 (for 12 FPS)
- Get the difference in time of cock() and the last frame value of clock().
- 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!
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.
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.
I guess this makes the most sense, considering all platforms and devices would perform differently when it comes to something as precise as time.
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(¤tTime);
return (double) currentTime.QuadPart / frequency.QuadPart;
#elif defined(__linux)
struct timespec currentTime;
clock_gettime(CLOCK_MONOTONIC, ¤tTime);
return currentTime.tv_sec + currentTime.tv_nsec * 0.000000001;
#endif
}
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 | 8,520 |
Mar 16, 2009 06:31 PM Last Post: AnotherJake |
|
Objective-C Sprite Animation | Case | 2 | 6,802 |
Jun 25, 2003 04:58 PM Last Post: Case |