I am currently trying to make a game in DirectX, but I was just wondering if there was anything better then using timers, because i thought I read before that Timers weren't really good for games.
Printable View
I am currently trying to make a game in DirectX, but I was just wondering if there was anything better then using timers, because i thought I read before that Timers weren't really good for games.
alternatively you could call sleep to delay, or use settimer functions or multimedia timers, depends really what you're upto
Well I used timers for the first version I made of the game, where the timers handled moving, so that hitting more then one key wouldn't jam the program. Is there a function to maybe do something like:
[Highlight=VB]
..Main Code..
if wait is done then
dostuff
startwait 15milliseconds
end if
no, but assign a variable say wait=gettickcount()+15 and wait til wait is less than gettickcount()
btw gettickcount may not be that accurate, so you might have to choose another timer (such as multimedia timer or performancecounter)
Just remember that the GetTickCount API has diffrent resolution on diffrent Win plattforms.....
Excuse me NoteMe? That would really screw some of my programs! Is it lower in older platforms? Is it always a millisecond after such and such point?
Sorry to screw up your apps...but it is true.....look it up on MSDN. Older Win versions have a really bad resolution on the GetTickCount API.....
VB Code:
public sub Wait(Milliseconds as long) dim EndTime as long EndTime = GetTickCount+Milliseconds While GetTickCount < EndTime DoEvents Wend end sub
now picture a program using that consistently when your resolution is actually more along the lines of deciseconds (a tenth). :D Although I really don't think I have to worry about it cause everyone I know has 98+ ... and I don't use it TOO much...
If I can remember it right it was something like
Win2k/Xp = 10 ms
WinMe = 20 ms
Win98 = 40ms
Win95 = 50ms
Or something like that...
But is it always calculated in milliseconds?
or are you just saying it updates less frequently? because that's fine by me.
Try this:
VB Code:
Option Explicit Private Declare Function GetTickCount Lib "kernel32" () As Long Dim Pause As Boolean 'check if game is paused Dim Running As Boolean 'check if game is started Dim Fps As Double 'current fps Dim FrameCount As Long 'count frames between fps update Private Sub Form_Load() Me.Show 'show the window Running = True 'no the game is on Main 'start main loop End Sub Private Sub Main() 'the main loop Dim TempTime As Long 'to keep track of time While Running 'is the game running? If TempTime <= GetTickCount Then 'is it time to calc next frame? TempTime = GetTickCount + 10 'set next tick time While Pause 'is the game paused? DoEvents 'infinit loop Wend 'Code goes here. FrameCount = FrameCount + 1 'fps counter If Fps + 1000 <= GetTickCount Then Me.Caption = "Fps: " & Round(1000 / ((GetTickCount - Fps + 0.00001) / FrameCount), 0) Fps = GetTickCount FrameCount = 0 End If End If DoEvents 'handle win msgs Wend End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Running = False 'game over End Sub Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyP Then 'pause code If Pause = False Then Pause = True Me.Caption = "Paused" ElseIf Pause = True Then Pause = False End If End If End Sub
Yes it always updates in milliseconds.....but not every millisecond....if you want it more accurat....try using querreryperformmace something....API
Ok, whew! I thought you meant that it actually saved 10 seconds as 100, instead of 10000. Which would ahve made all waits a BIT longer ;)
Hehe...no no no..but if you have a waith for 10ms and the app is running on an old OS the waith will probably be more like 50ms....