Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualVortez

Posted 06 July 2013 - 08:55 AM

 

Here is mine. I use the "HasElapsed" function to do things periodically in a main loop. For example, you can send a packet every 20ms by calling HasElapsed.

#pragma once
class GameTimer
{
public:
	GameTimer(void);
	~GameTimer(void);

	void  Reset();
	float Tick();
	bool HasElapsed(float milliseconds);

private:
	bool    IsPerfCounterAvailable;
	float   TimeScale;
	__int64 PerfCounterFrequency;
	__int64 LastTime;
	__int64 CurrentTime;	
};

GameTimer::GameTimer(void)
{
	//Check if a High resolution timer is available 
	if(QueryPerformanceFrequency((LARGE_INTEGER*)&PerfCounterFrequency)){ 
		IsPerfCounterAvailable = true;
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime); 
		TimeScale = 1.0f / PerfCounterFrequency;
	} else { 
		IsPerfCounterAvailable = false;
		CurrentTime = timeGetTime(); 
		TimeScale	= 0.001f;
    } 
	
	Reset();
}


GameTimer::~GameTimer(void)
{
}

bool GameTimer::HasElapsed(float milliseconds)
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	if( ((CurrentTime - LastTime) * TimeScale) > (milliseconds*0.001f))
	{
		LastTime = CurrentTime;
		return true;
	}

	return false;
}

float GameTimer::Tick()
{
	LastTime = CurrentTime;

	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Calculate the elapsed time
	float ElapsedTime = (CurrentTime - LastTime) * TimeScale;

	return ElapsedTime;
}

void GameTimer::Reset()
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Initialization
	LastTime = CurrentTime;
}

 

Lol, that's my code laugh.png . Glad to see someone else found it usefull.

 

To the op: there's no perfect solution for what you are asking, if you want very high precision wait, you have to burn more cpu cycles, if not, then sleep is fine. Sleep() only have a precision of approx. 16 ms, that's how windows work. I've heard something about select() that could be used for this, with a timeout value, but i never tested it. Even then, the timeout value is still in milliseconds so...

 

But... what's the issue exactly? Are you trying to get a particular framerate, or are you trying to get a smooth animation of your circle?

 

My guess is the second one, so, just use the high precision timer shown above and you should be fine. That's how i do it:

 

 

In the game loop, i have:

            UpdateScene(Timer.Tick());
            RenderScene();

Then, in the update function, i do the animation

float xCoord, yCoord;

...

void UpdateScene(float ElapsedTime)
{
    xCoord += 1.0f * ElapsedTime;
    yCoord -= 2.5f * ElapsedTime;
}

That's just an example to move something in a straigh line, based on xCoord and yCoord, but it could be anything you want really.

 

Just scale the value of how many units the object should move, in 1 second, by ElapsedTime and you will have perfect animation no matter the framerate.

 

If you want to move it only at specified interval of time, then use this:

// 100 ms delay
float Delay = 0.1f;

while(ElapsedTime >= Delay){

    xCoord += 1.0f;
    yCoord -= 2.5f;

    ElapsedTime -= Delay;     // <-- assuming you don't want to use ElasedTime afterward, otherwise, use a copy of it
    if(ElapsedTime < 0.0f)    // <-- optional if... not really needed    
        ElapsedTime = 0.0f;
}

That might look a bit ugly, but it can be wrap in 1 line of code using a function. For the sake of simplicity, i've shown it this way.

 

Just don't use sleep() for stuffs like that in a game, it's not what it was made for. If you want to save cpu cycles, just turn vsync on.

 

The full source code for my timer class is here, down at the very bottom of the page.

 

Edit: Actually, i've polished the code a bit since then, use this instead.


#23Vortez

Posted 06 July 2013 - 08:55 AM

 

Here is mine. I use the "HasElapsed" function to do things periodically in a main loop. For example, you can send a packet every 20ms by calling HasElapsed.

#pragma once
class GameTimer
{
public:
	GameTimer(void);
	~GameTimer(void);

	void  Reset();
	float Tick();
	bool HasElapsed(float milliseconds);

private:
	bool    IsPerfCounterAvailable;
	float   TimeScale;
	__int64 PerfCounterFrequency;
	__int64 LastTime;
	__int64 CurrentTime;	
};

GameTimer::GameTimer(void)
{
	//Check if a High resolution timer is available 
	if(QueryPerformanceFrequency((LARGE_INTEGER*)&PerfCounterFrequency)){ 
		IsPerfCounterAvailable = true;
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime); 
		TimeScale = 1.0f / PerfCounterFrequency;
	} else { 
		IsPerfCounterAvailable = false;
		CurrentTime = timeGetTime(); 
		TimeScale	= 0.001f;
    } 
	
	Reset();
}


GameTimer::~GameTimer(void)
{
}

bool GameTimer::HasElapsed(float milliseconds)
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	if( ((CurrentTime - LastTime) * TimeScale) > (milliseconds*0.001f))
	{
		LastTime = CurrentTime;
		return true;
	}

	return false;
}

float GameTimer::Tick()
{
	LastTime = CurrentTime;

	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Calculate the elapsed time
	float ElapsedTime = (CurrentTime - LastTime) * TimeScale;

	return ElapsedTime;
}

void GameTimer::Reset()
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Initialization
	LastTime = CurrentTime;
}

 

Lol, that's my code laugh.png . Glad to see someone else found it usefull.

 

To the op: there's no perfect solution for what you are asking, if you want very high precision wait, you have to burn more cpu cycles, if not, then sleep is fine. Sleep() only have a precision of approx. 16 ms, that's how windows work. I've heard something about select() that could be used for this, with a timeout value, but i never tested it. Even then, the timeout value is still in milliseconds so...

 

But... what's the issue exactly? Are you trying to get a particular framerate, or are you trying to get a smooth animation of your circle?

 

My guess is the second one, so, just use the high precision timer shown above and you should be fine. That's how i do it:

 

 

In the game loop, i have:

            UpdateScene(Timer.Tick());
            RenderScene();

Then, in the update function, i do the animation

float xCoord, yCoord;

...

void UpdateScene(float ElapsedTime)
{
    xCoord += 1.0f * ElapsedTime;
    yCoord -= 2.5f * ElapsedTime;
}

That's just an example to move something in a straigh line, based on xCoord and yCoord, but it could be anything you want really.

 

Just scale the value of how many units the object should move, in 1 second, by ElapsedTime and you will have perfect animation no matter the framerate.

 

If you want to move it only at specified interval of time, then use this:

// 100 ms delay
float Delay = 0.1f;

while(ElapsedTime >= Delay){

    xCoord += 1.0f;
    yCoord -= 2.5f;

    ElapsedTime -= Delay;     // <-- assuming you don't want to use ElasedTime afterward, otherwise, use a copy of it
    if(ElapsedTime < 0.0f)    // <-- optional if... not really needed    
        ElapsedTime = 0.0f;
}

That might look a bit ugly, but it can be wrap in 1 line of code using a function. For the sake of simplicity, i've shown it this way.

 

Just don't use sleep() for stuffs like that in a game, it's not what it was made for. If you want to save cpu cycles, just turn vsync on.

 

The full source code for my timer class is here, down at the very bottom of the page.

 

Edit: Actually, i've polished the code a bit since then, use this instead.


#22Vortez

Posted 06 July 2013 - 08:49 AM

 

Here is mine. I use the "HasElapsed" function to do things periodically in a main loop. For example, you can send a packet every 20ms by calling HasElapsed.

#pragma once
class GameTimer
{
public:
	GameTimer(void);
	~GameTimer(void);

	void  Reset();
	float Tick();
	bool HasElapsed(float milliseconds);

private:
	bool    IsPerfCounterAvailable;
	float   TimeScale;
	__int64 PerfCounterFrequency;
	__int64 LastTime;
	__int64 CurrentTime;	
};

GameTimer::GameTimer(void)
{
	//Check if a High resolution timer is available 
	if(QueryPerformanceFrequency((LARGE_INTEGER*)&PerfCounterFrequency)){ 
		IsPerfCounterAvailable = true;
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime); 
		TimeScale = 1.0f / PerfCounterFrequency;
	} else { 
		IsPerfCounterAvailable = false;
		CurrentTime = timeGetTime(); 
		TimeScale	= 0.001f;
    } 
	
	Reset();
}


GameTimer::~GameTimer(void)
{
}

bool GameTimer::HasElapsed(float milliseconds)
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	if( ((CurrentTime - LastTime) * TimeScale) > (milliseconds*0.001f))
	{
		LastTime = CurrentTime;
		return true;
	}

	return false;
}

float GameTimer::Tick()
{
	LastTime = CurrentTime;

	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Calculate the elapsed time
	float ElapsedTime = (CurrentTime - LastTime) * TimeScale;

	return ElapsedTime;
}

void GameTimer::Reset()
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Initialization
	LastTime = CurrentTime;
}

 

Lol, that's my code laugh.png . Glad to see someone else found it usefull.

 

To the op: there's no perfect solution for what you are asking, if you want very high precision wait, you have to burn more cpu cycles, if not, then sleep is fine. Sleep() only have a precision of approx. 16 ms, that's how windows work. I've heard something about select() that could be used for this, with a timeout value, but i never tested it. Even then, the timeout value is still in milliseconds so...

 

But... what's the issue exactly? Are you trying to get a particular framerate, or are you trying to get a smooth animation of your circle?

 

My guess is the second one, so, just use the high precision timer shown above and you should be fine. That's how i do it:

 

 

In the game loop, i have:

            UpdateScene(Timer.Tick());
            RenderScene();

Then, in the update function, i do the animation

float xCoord, yCoord;

...

void UpdateScene(float ElapsedTime)
{
    xCoord += 1.0f * ElapsedTime;
    yCoord -= 2.5f * ElapsedTime;
}

That's just an example to move something in a straigh line, based on xCoord and yCoord, but it could be anything you want really.

 

Just scale the value the object should move, in second, by ElapsedTime and you will have perfect animation no matter the framerate.

 

If you want to move it only at specified interval of time, then use this:

// 100 ms delay
float Delay = 0.1f;

while(ElapsedTime >= Delay){

    xCoord += 1.0f;
    yCoord -= 2.5f;

    ElapsedTime -= Delay;     // <-- assuming you don't want to use ElasedTime afterward, otherwise, use a copy of it
    if(ElapsedTime < 0.0f)    // <-- optional if... not really needed    
        ElapsedTime = 0.0f;
}

That might look a bit ugly, but it can be wrap in 1 line of code using a function. For the sake of simplicity, i've shown it this way.

 

Just don't use sleep() for stuffs like that in a game, it's not what it was made for. If you want to save cpu cycles, just turn vsync on.

 

The full source code for my timer class is here, down at the very bottom of the page.

 

Edit: Actually, i've polished the code a bit since then, use this instead.


#21Vortez

Posted 06 July 2013 - 08:39 AM

 

Here is mine. I use the "HasElapsed" function to do things periodically in a main loop. For example, you can send a packet every 20ms by calling HasElapsed.

#pragma once
class GameTimer
{
public:
	GameTimer(void);
	~GameTimer(void);

	void  Reset();
	float Tick();
	bool HasElapsed(float milliseconds);

private:
	bool    IsPerfCounterAvailable;
	float   TimeScale;
	__int64 PerfCounterFrequency;
	__int64 LastTime;
	__int64 CurrentTime;	
};

GameTimer::GameTimer(void)
{
	//Check if a High resolution timer is available 
	if(QueryPerformanceFrequency((LARGE_INTEGER*)&PerfCounterFrequency)){ 
		IsPerfCounterAvailable = true;
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime); 
		TimeScale = 1.0f / PerfCounterFrequency;
	} else { 
		IsPerfCounterAvailable = false;
		CurrentTime = timeGetTime(); 
		TimeScale	= 0.001f;
    } 
	
	Reset();
}


GameTimer::~GameTimer(void)
{
}

bool GameTimer::HasElapsed(float milliseconds)
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	if( ((CurrentTime - LastTime) * TimeScale) > (milliseconds*0.001f))
	{
		LastTime = CurrentTime;
		return true;
	}

	return false;
}

float GameTimer::Tick()
{
	LastTime = CurrentTime;

	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Calculate the elapsed time
	float ElapsedTime = (CurrentTime - LastTime) * TimeScale;

	return ElapsedTime;
}

void GameTimer::Reset()
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Initialization
	LastTime = CurrentTime;
}

 

Lol, that's my code laugh.png . Glad to see someone else found it usefull.

 

To the op: there's no perfect solution for what you are asking, if you want very high precision wait, you have to burn more cpu cycles, if not, then sleep is fine. Sleep() only have a precision of approx. 16 ms, that's how windows work. I've heard something about select() that could be used for this, with a timeout value, but i never tested it. Even then, the timeout value is still in milliseconds so...

 

But... what's the issue exactly? Are you trying to get a particular framerate, or are you trying to get a smooth animation of your circle?

 

My guess is the second one, so, just use the high precision timer shown above and you should be fine. That's how i do it:

 

 

In the game loop, i have:

            UpdateScene(Timer.Tick());
            RenderScene();

Then, in the update function, i do the animation

float xCoord, yCoord;

...

void UpdateScene(float ElapsedTime)
{
    xCoord += 1.0f * ElapsedTime;
    yCoord -= 2.5f * ElapsedTime;
}

That's just an example to move something in a straigh line, based on xCoord and yCoord, but it could be anything you want really.

 

Just scale the value the object should move, in second, by ElapsedTime and you will have perfect animation no matter the framerate.

 

If you want to move it only at specified interval of time, then use this:

// 100 ms delay
float Delay = 0.1f;

while(ElapsedTime >= Delay){

    xCoord += 1.0f;
    yCoord -= 2.5f;

    ElapsedTime -= Delay;     // <-- assuming you don't want to use ElasedTime afterward, otherwise, use a copy of it
    if(ElapsedTime < 0.0f)    // <-- optional if... not really needed    
        ElapsedTime = 0.0f;
}

That might look a bit ugly, but it can be wrap in 1 line of code using a function. For the sake of simplicity, i've shown it this way.

 

Just don't use sleep() for stuffs like that in a game, it's not what it was made for. If you want to save cpu cycles, just turn vsync on.

 

The full source code for my timer class is here, down at the very bottom of the page. (remove the fps counter part if you don't need it)


#20Vortez

Posted 06 July 2013 - 08:38 AM

 

Here is mine. I use the "HasElapsed" function to do things periodically in a main loop. For example, you can send a packet every 20ms by calling HasElapsed.

#pragma once
class GameTimer
{
public:
	GameTimer(void);
	~GameTimer(void);

	void  Reset();
	float Tick();
	bool HasElapsed(float milliseconds);

private:
	bool    IsPerfCounterAvailable;
	float   TimeScale;
	__int64 PerfCounterFrequency;
	__int64 LastTime;
	__int64 CurrentTime;	
};

GameTimer::GameTimer(void)
{
	//Check if a High resolution timer is available 
	if(QueryPerformanceFrequency((LARGE_INTEGER*)&PerfCounterFrequency)){ 
		IsPerfCounterAvailable = true;
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime); 
		TimeScale = 1.0f / PerfCounterFrequency;
	} else { 
		IsPerfCounterAvailable = false;
		CurrentTime = timeGetTime(); 
		TimeScale	= 0.001f;
    } 
	
	Reset();
}


GameTimer::~GameTimer(void)
{
}

bool GameTimer::HasElapsed(float milliseconds)
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	if( ((CurrentTime - LastTime) * TimeScale) > (milliseconds*0.001f))
	{
		LastTime = CurrentTime;
		return true;
	}

	return false;
}

float GameTimer::Tick()
{
	LastTime = CurrentTime;

	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Calculate the elapsed time
	float ElapsedTime = (CurrentTime - LastTime) * TimeScale;

	return ElapsedTime;
}

void GameTimer::Reset()
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Initialization
	LastTime = CurrentTime;
}

 

Lol, that's my code laugh.png . Glad to see someone else found it usefull.

 

To the op: there's no perfect solution for what you are asking, if you want very high precision wait, you have to burn more cpu cycles, if not, then sleep is fine. Sleep() only have a precision of approx. 16 ms, that's how windows work. I've heard something about select() that could be used for this, with a timeout value, but i never tested it. Even then, the timeout value is still in milliseconds so...

 

But... what's the issue exactly? Are you trying to get a particular framerate, or are you trying to get a smooth animation of your circle?

 

My guess is the second one, so, just use the high precision timer shown above and you should be fine. That's how i do it:

 

 

In the game loop, i have:

            UpdateScene(Timer.Tick());
            RenderScene();

Then, in the update function, i do the animation

float xCoord, yCoord;

...

void UpdateScene(float ElapsedTime)
{
    xCoord += 1.0f * ElapsedTime;
    yCoord -= 2.5f * ElapsedTime;
}

That's just an example to move something in a straigh line, based on xCoord and yCoord, but it could be anything you want really.

 

Just scale the value the object should move, in second, by ElapsedTime and you will have perfect animation no matter the framerate.

 

If you want to move it only at specified interval of time, then use this:

// 100 ms delay
float Delay = 0.1f;

while(ElapsedTime >= Delay){

    xCoord += 1.0f;
    yCoord -= 2.5f;

    ElapsedTime -= Delay;     // <-- assuming you don't want to use ElasedTime afterward, otherwise, use a copy of it
    if(ElapsedTime < 0.0f)    // <-- optional if... not really needed    
        ElapsedTime = 0.0f;
}

That might look a bit ugly, but it can be wrap in 1 line of code using a function. For the sake of simplicity, i've shown it this way.

 

Just don't use sleep() for stuffs like that in a game, it's not what it was made for. If you want to save cpu cycles, just turn vsync on.

 

The full source code for my timer class is here, down at the very bottom of the page.


#19Vortez

Posted 06 July 2013 - 08:26 AM

 

Here is mine. I use the "HasElapsed" function to do things periodically in a main loop. For example, you can send a packet every 20ms by calling HasElapsed.

#pragma once
class GameTimer
{
public:
	GameTimer(void);
	~GameTimer(void);

	void  Reset();
	float Tick();
	bool HasElapsed(float milliseconds);

private:
	bool    IsPerfCounterAvailable;
	float   TimeScale;
	__int64 PerfCounterFrequency;
	__int64 LastTime;
	__int64 CurrentTime;	
};

GameTimer::GameTimer(void)
{
	//Check if a High resolution timer is available 
	if(QueryPerformanceFrequency((LARGE_INTEGER*)&PerfCounterFrequency)){ 
		IsPerfCounterAvailable = true;
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime); 
		TimeScale = 1.0f / PerfCounterFrequency;
	} else { 
		IsPerfCounterAvailable = false;
		CurrentTime = timeGetTime(); 
		TimeScale	= 0.001f;
    } 
	
	Reset();
}


GameTimer::~GameTimer(void)
{
}

bool GameTimer::HasElapsed(float milliseconds)
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	if( ((CurrentTime - LastTime) * TimeScale) > (milliseconds*0.001f))
	{
		LastTime = CurrentTime;
		return true;
	}

	return false;
}

float GameTimer::Tick()
{
	LastTime = CurrentTime;

	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Calculate the elapsed time
	float ElapsedTime = (CurrentTime - LastTime) * TimeScale;

	return ElapsedTime;
}

void GameTimer::Reset()
{
	if(IsPerfCounterAvailable){
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
	} else {
		CurrentTime = timeGetTime();
	}

	// Initialization
	LastTime = CurrentTime;
}

 

Lol, that's my code laugh.png . Glad to see someone else found it usefull.

 

To the op: there's no perfect solution for what you are asking, if you want very high precision wait, you have to burn more cpu cycles, if not, then sleep is fine. Sleep() only have a precision of approx. 16 ms, that's how windows work. I've heard something about select() that could be used for this, with a timeout value, but i never tested it. Even then, the timeout value is still in milliseconds so...

 

But... what's the issue exactly? Are you trying to get a particular framerate, or are you trying to get a smooth animation of your circle?

 

My guess is the second one, so, just use the high precision timer shown above and you should be fine. That's how i do it:

 

 

In the game loop, i have:

            UpdateScene(Timer.Tick());
            RenderScene();

Then, in the update function, i do the animation

float xCoord, yCoord;

...

void UpdateScene(float ElapsedTime)
{
    xCoord += 1.0f * ElapsedTime;
    yCoord -= 2.5f * ElapsedTime;
}

That's just an example to move something in a straigh line, based on xCoord and yCoord, but it could be anything you want really.

 

Just scale the value the object should move, in second, by ElapsedTime and you will have perfect animation no matter the framerate.

 

If you want to move it only at specified interval of time, then use this:

// 100 ms delay
float Delay = 0.1f;

while(ElapsedTime >= Delay){

    xCoord += 1.0f;
    yCoord -= 2.5f;

    ElapsedTime -= Delay;     // <-- assuming you don't want to use ElasedTime afterward, otherwise, use a copy of it
    if(ElapsedTime < 0.0f)    // <-- optional if... not really needed    
        ElapsedTime = 0.0f;
}

That might look a bit ugly, but it can be wrap in 1 line of code using a function. For the sake of simplicity, i've shown it this way.

 

Just don't use sleep() for stuffs like that in a game, it's not what it was made for. If you want to save cpu cycles, just turn vsync on.


PARTNERS