multiple os and time function

Using three tiny functions I am waiting a time of milliseconds using
GetTickCount(). This is to slow down a C++ game. What is the technique for having the same visual speed effect for processors of different speed, those slower, and those future(faster.)

Thankfully,

Josheir
Last edited on
Set yourself a FrameRate and limit drawing to that many times per second.
Use delta timing? This is what professional games developers use.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <windows.h>
#include <iostream>

int main()
{
    float ThisDelta = 0.f;
    DWORD LastTick = GetTickCount();
    float SomeObjectPosition = 0.f;
    float SomeObjectSpeed = 2.f; // Speed of the object, in Units per second.
    while(1)
    {
        DWORD ThisTick = GetTickCount();
        if(ThisTick != LastTick)
        {
            ThisDelta = ((float)(ThisTick - LastTick))*0.001f;
            LastTick = ThisTick;
            
            // At this point, we have a delta time.
            // The delta time is the time, in MS, between the last frame and this frame.
    
            SomeObjectPosition += ThisDelta * SomeObjectSpeed;
            std::cout << SomeObjectPosition << std::endl;
        }
    }
    return 0;
}


In this example, no matter the PC, SomeObjectPosition will increase at a rate of 2 units per second.

Speed is defined by SomeObjectSpeed.

You may also want to use timeGetTime, a more precise timer, but it requires linking with winmm.lib .
http://msdn.microsoft.com/en-us/library/windows/desktop/dd757629(v=vs.85).aspx

May this also help you, for portability:
http://ideone.com/CgoSXt
Last edited on
These are the three functions:



////////////////////////////////////////////////////////

ReturnClock()
{

return(GetTickCount());

}

///////////////////////////////////////////////////////////

DWORD StartClock(void)
{


return(start_count = ReturnClock());

}

////////////////////////////////////////////////////////////

DWORD WaitClock(DWORD count)
{
//Waits clicks from start

while((ReturnClock() - start_count) < count);
return(ReturnClock());

}



And this is how they are used:


Winmain(){


//the main while loop
while(1){
Start_Clock();

.
.
.

Wait_Clock(200);
}

return(1);
}

This works well with the current computer
and I think its what is meant by "FrameRate."
I am very confused, what is going on, will
this prevent a faster computer from increasing
the rate of the inside while. I don't understand
if GetTickCount() solves this problem. Perhaps how
is GetTickCount() related to time? would someone
describe please?

Signed,

Josheir
Last edited on
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx

Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.

I don't suggest you to use GetTickCount64.
It only works since Windows Vista, and does not allow for XP.

The way it works is:
Each frame that is elapsed, GetTickCount generates the difference (in time) between those frames.
With this difference, you will be able to understand how much the game must change, in time terms.

This is the only way a game can run in a fluid way on all PC's, without wasting time in any useless thing like Sleep.

Also this does not force you to a specific framerate.
This will go as fast as the PC can.

As an example:
A poorly-made Grand Theft Auto: Vice City uses frame limitation.
It was built for 30 fps.
To demonstrate it, activate the Frame Limiter and see how many FPS you get: 30.
Disable the frame limiter and see how, when you change your views from simple to complex geometry, the time seems to be changing.
If your pc goes fast enough, and another car is bleeping at you, you will hear it bleep very fast.
This is the disvantage of creating a game on a static-framerate base.

A greatly-made Half Life 2 uses no frame limitation.
It was built for arbitrary fps, and uses Delta Timing.
To demonstrate it, there is no Frame Limiter options, and it can reach any arbitrary FPS count (specified by a console variable for CPU/GPU saving).
It also allows for Multiplayer games.
As example, the Counter Strike series is a Half Life/Half Life 2 mod.
Also is Team Fortress 2, Day Of Defeat/Source, Portal 2, Left 4 Dead/2.
Last edited on
I think it is understandable now. If the Wait is 200 milliseconds and the process of the blit is in the while, a faster processor will still have to wait for the quickest allowed that is managed by the beginning start to end wait.

A slower processor will be greater than the wait at worst and will be greater so will continue.

Looks okay?

josheir
Topic archived. No new replies allowed.