Jump to content
  • Advertisement
Sign in to follow this  
ms75214

code for frame rate

This topic is 2491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what's some good mac c++ code for frame rate?

here's what i want to do:

- measure the time to draw the last frame really precisely in seconds
- take the reciprocal

Share this post


Link to post
Share on other sites
Advertisement
While you can do that, it produces a really unstable and generally not very helpful number. What I've found is that it's best to record the time taken for each frame, but DON'T take the reciprocal. To compute frame rate, count frames summing up the times until the total exceeds 1 second, and then report that number and reset the counters.

Oh, and CFAbsoluteTimeGetCurrent is the way to go for timing on Mac, I think.

Share this post


Link to post
Share on other sites

what's some good mac c++ code for frame rate?

here's what i want to do:

- measure the time to draw the last frame really precisely in seconds
- take the reciprocal




timeval tim;
gettimeofday(&tim, NULL);
double t1=tim.tv_sec+(tim.tv_usec/1000000.0);

double delta = t1 - previous_time;
double fps_d = 1.0/delta;
int fps = fps_d;

previous_time=t1;

sprintf(str, "FPS: %d", fps);

Share this post


Link to post
Share on other sites

While you can do that, it produces a really unstable and generally not very helpful number. What I've found is that it's best to record the time taken for each frame, but DON'T take the reciprocal. To compute frame rate, count frames summing up the times until the total exceeds 1 second, and then report that number and reset the counters.

Oh, and CFAbsoluteTimeGetCurrent is the way to go for timing on Mac, I think.


good call.

i switched to this:


char str[80];

timeval tim;
gettimeofday(&tim, NULL);
double t1=tim.tv_sec+(tim.tv_usec/1000000.0);

double delta = t1 - previous_time;
fps_accum+=delta;
frame_count++;

if (fps_accum > 1) {

global_fps = frame_count;
frame_count=0;
fps_accum=0;

}


previous_time=t1;



sprintf(str, "FPS: %d", global_fps);

Share this post


Link to post
Share on other sites
class Timer
{
public:
Timer(bool autoStart = false);
~Timer();

void Reset();
void Update();
inline float DeltaTime() const { return deltaTime; }
inline float TotalTime() const { return totalTime; }

private:
Timer(Timer const&);
Timer& operator=(Timer const&);

#ifdef WIN32
unsigned __int64 startTicks;
unsigned __int64 lastTick;
unsigned __int64 ticksPerSecond;
#else // iOS
int64_t startTicks;
int64_t lastTick;
double ticksPerSecond;
#endif

float totalTime;
float deltaTime;
};


// http://macresearch.org/tutorial_performance_and_time

#include <mach/mach_time.h>
#include "Timer.h"

namespace
{
#ifdef __APPLE__
float GetTimeBase()
{
float ticksPerSecond = 0.0;

mach_timebase_info_data_t info;
kern_return_t err = mach_timebase_info( &info );

//Convert the timebase into seconds
if( err == 0 )
{
ticksPerSecond = 1e-9 * static_cast<float>(info.numer) / static_cast<float>(info.denom);
}

return ticksPerSecond;
}
#endif

}

Timer::Timer(bool autoStart) : totalTime(0.0f), deltaTime(0.0f), startTicks(0), lastTick(0)
{
ticksPerSecond = GetTimeBase();

if(autoStart)
{
Reset();
}
}

Timer::~Timer()
{
}

void Timer::Reset()
{
startTicks = mach_absolute_time();
lastTick = startTicks;
}

void Timer::Update()
{
int64_t const currentTicks = mach_absolute_time();
int64_t const deltaTicks = currentTicks - lastTick;
deltaTime = static_cast<float>(deltaTicks) * ticksPerSecond;
totalTime = static_cast<float>(currentTicks - startTicks) * ticksPerSecond;
lastTick = currentTicks;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!