|
-
Nov 2nd, 2003, 06:09 PM
#1
Thread Starter
Addicted Member
Timer without a form
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.
Microsoft Visual Basic 6.0 Professional
Windows XP Professional
-
Nov 2nd, 2003, 07:37 PM
#2
Frenzied Member
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
-
Nov 2nd, 2003, 07:57 PM
#3
Thread Starter
Addicted Member
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?
Microsoft Visual Basic 6.0 Professional
Windows XP Professional
-
Nov 2nd, 2003, 09:56 PM
#4
Frenzied Member
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
-
Nov 3rd, 2003, 08:10 AM
#5
In this thread wokawidget answers the question. Still thanking him....
-
Nov 3rd, 2003, 09:23 AM
#6
Thread Starter
Addicted Member
Thanks, that code looks good.
Microsoft Visual Basic 6.0 Professional
Windows XP Professional
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|