Sign in to follow this  

Finding Time

This topic is 2660 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

Hi again, my second thread. This time i'm looking for some opinions about Hith Resolution Timers, an other core piece of any modern game engine (modern doesn't mean cutting edge technology, it means that some old engines don't use a timer to control the framerate).

I want to keep the multiplatform feature in my engine, but sometimes it requires specific code for each platform, is the case of Hith Resolution Timer , it needs to be compiled different for windows or mac/linux.

Here is the code of my first try in Multiplatform HRT, this time i paste both definition and declaration. This is the first time that i write a multiplatform class, so i don't know if it is the right way.

multiplatform header SystemConfiguration.h
Quote:

#ifndef SYSTEMCONFIGURATION
#define SYSTEMCONFIGURATION
#define WINDOWS_OS 0x01
#define UNIX_OS 0x02

/*******************************************************************/
// this is the only line you need modify to compile in different S.O
#define OSCOMPILER WINDOWS_OS // <====[WINDOWS_SO or UNIX_SO]
/*******************************************************************/

#if OSCOMPILER == WINDOWS_OS
#include <windows.h>
#define INWINDOWS 0x01
typedef unsigned __int64 U64;
#endif


#if OSCOMPILER == UNIX_OS
//TODO: system libs in unix ?
#define INUNIX 0x02
typedef unsigned uint64_t U64;
#endif
#endif

typedef unsigned char U8;
typedef wchar_t U16;
typedef unsigned int U32;
typedef float F32;
typedef double F64;



timer header
Quote:

#include "SystemConfiguration.h"

#define FRAMES_PER_SECOND 60
#define SECONDS_PER_FRAME (1/FRAMES_PER_SECOND)

#if defined(INWINDOWS)
#define TICKS_T U64
#elif defined(INUNIX)
#include <sys/time.h>
#define TICKS_T F64
#endif

//TODO: Test cHiResTimer in unix
class cHiResTimer
{
public:
cHiResTimer(void);
virtual ~cHiResTimer(void);
F32 QueryDeltaTime();
F32 GetDeltaTime();
void SetDeltaTime(F32);
void init();
private:
F32 f32DeltaTimeSeconds;
TICKS_T tBegin;
TICKS_T tEnd;
#if defined(INWINDOWS)
LARGE_INTEGER frequency; //frequency is not needed in unix
#endif
};


timer cpp
Quote:

void cHiResTimer::init()
{
f32DeltaTimeSeconds = SECONDS_PER_FRAME;
tBegin = 0;
tEnd = 0;
#if defined(INWINDOWS)
QueryPerformanceFrequency(&frequency);
#endif
}

F32 cHiResTimer::QueryDeltaTime()
{
#if defined(INWINDOWS)
LARGE_INTEGER tim;
QueryPerformanceCounter(&tim);
tEnd = tim.QuadPart;
f32DeltaTimeSeconds = (F32)(tEnd - tBegin) / (F32)frequency.QuadPart;

#elif defined(INUNIX)
timeval tim;
gettimeofday(&tim, NULL);
tEnd = tim.tv_sec+(tim.tv_usec/1000000.0);
f32DeltaTimeSeconds = (F32)(tEnd - tBegin);
#endif

tBegin = tEnd;
if(f32DeltaTimeSeconds > 1.0f){
f32DeltaTimeSeconds = 0.016f;
}
return f32DeltaTimeSeconds;
}

F32 cHiResTimer::GetDeltaTime(){
return f32DeltaTimeSeconds;
}

void cHiResTimer::SetDeltaTime(F32 ndt){
f32DeltaTimeSeconds = ndt;
}


A sample of the game loop, it makes me sense that i'm doing something wrong :S
Quote:

theGame->Update(timer.GetDeltaTime());
theGame->Render(timer.GetDeltaTime());
glutSwapBuffers();
timer.QueryDeltaTime();
if (timer.GetDeltaTime() < SECONDS_PER_FRAME){
Sleep(SECONDS_PER_FRAME - timer.GetDeltaTime());
timer.SetDeltaTime(SECONDS_PER_FRAME);
}


Thanks for your time!!

PD: the unix code is not tested.



[Edited by - Gallo on August 31, 2010 3:53:19 AM]

Share this post


Link to post
Share on other sites
I've implemented this kind of thing in the past by having a common header for all platforms and individual .cpp files for each platform.

The header high_resolution_time.h would look something like this:
#ifndef HIGH_RESOLUTION_TIME_H_INCLUDED
#define HIGH_RESOLUTION_TIME_H_INCLUDED

// Returns the time in seconds since some reference point
double high_resolution_time();

#endif



You would then implement that function for Windows in high_resolution_time_windows.cpp, and for everything else probably in high_resolution_time_posix.cpp. Then have your makefile or project include the appropriate version.

Share this post


Link to post
Share on other sites

This topic is 2660 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this