What is the best way to simulate a Timer without using a form.
There are many example on psc, but most are said to have some kind of downfall.
Any suggestions would be greatly appreciated.
Printable View
What is the best way to simulate a Timer without using a form.
There are many example on psc, but most are said to have some kind of downfall.
Any suggestions would be greatly appreciated.
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
Looks like it would do the job, but i wonder if there is a way to do it without keep looping like that, must be a little memory hogging (just what i would expect to happen, haven't tried it though).
Any comments?
its much faster than the timer control...
try this code...its smaller:
VB Code:
Option Explicit Private Declare Function GetTickCount Lib "kernel32" () As Long Dim Running As Boolean 'check if game is started 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 'Code goes here. 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
In this thread wokawidget answers the question. Still thanking him....
Thanks, that code looks good.