August 09, 2020, 07:59:05 am

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Precision Timing

Started by GWS, July 02, 2013, 09:21:08 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

Hi,

So far, I've always used the GetTickCount() API for game timing ..

Code Select

declare "kernel32",GetTickCount(),int


Apparently, this has a limited timing resolution of about 10 mSec.

If a game is using 40 frames per second or so, this is only 25 mSec per frame, so 10 mSec timing is a bit crude.

Sprite positioning will not be too precise ..  ::)

Enter the much more precise functions QueryPerformanceCounter and QueryPerformanceFrequency. :o

Code Select

declare "kernel32",QueryPerformanceCounter(x:pointer),uint64
declare "kernel32",QueryPerformanceFrequency(x:pointer),uint64


These functions work down to microsecond accuracy and are hardware based, so animation should be smoother.

QueryPerformanceCounter determines the number of timer counts that have elapsed at that time.
QueryPerformanceFrequency determines the number of timer counts per second.

So animation rate = Number of Counts / Frequency.

I shall try them out and see if there is a noticeable improvement.

Here's a little test program comparing GetTickCount to the more precise values ..

Code Select

openconsole
cls

declare "kernel32",GetTickCount(),int
declare "kernel32",QueryPerformanceCounter(x:pointer),uint64
declare "kernel32",QueryPerformanceFrequency(x:pointer),uint64

def Freq,StartCount,StopCount:uint64
def TimingmSec,OverHead:double
def x1,x2,f:pointer
def t1,t2,Time:int

f = Freq: QueryPerformanceFrequency(f)

print "Frequency: ",Freq

x1 = StartCount: QueryPerformanceCounter(x1)
' do nothing between calls ..
x2 = StopCount: QueryPerformanceCounter(x2)

OverHead = StopCount - StartCount

print:print "Overhead (microsec): ",OverHead
print

x1 = StartCount: QueryPerformanceCounter(x1)
t1 = GetTickCount()

print "Start Value: ",StartCount
print "GetTickCount Start: ",t1
print

' run a task ..
for i = 1 to 20000
x = 12.345^23
next i

x2 = StopCount: QueryPerformanceCounter(x2)
t2 = GetTickCount()

print "End Value: ",StopCount
print "GetTickCount End: ",t2
print

TimingmSec = (StopCount - StartCount - OverHead) / Freq * 1000
Time = t2 - t1

print "Time Taken (msec): ",TimingmSec
print "GetTickCount TimeTaken (msec): ",Time

do:until inkey$<>""
closeconsole
end


Note that there is an overhead of about 19 microsec involved in calling the start and finish API counts.

Best wishes, :)

Graham

Tomorrow may be too late ..