|
-
Oct 5th, 2003, 06:07 PM
#1
Thread Starter
Addicted Member
How do I make a game loop? [SOLVED]
I've always used a timer despite there lag of at least 1 ms.
How do I make a game loop? when and where do I initially call if from?
and can I have one and still use all the nice functons on the form like mouse_up/down and key_up/down?
It seems like whenever i use one it 'freezes' the game or at least ignors all built in fxns.
Thanks!
NOMAD
Last edited by NOMADMAN; Oct 7th, 2003 at 11:14 AM.
-
Oct 5th, 2003, 06:54 PM
#2
Frenzied Member
hope this helps you:
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim Running As Boolean
Dim Pause As Boolean
Private Sub Form_Load()
Me.Show
Main
End Sub
Private Sub Main()
Dim TempTime As Long
While Running
If TempTime <= GetTickCount Then
TempTime = GetTickCount + Interval
While Pause
DoEvents
Wend
'game code goes here
End If
DoEvents
Wend
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Running = False
End Sub
Just set the Pause bool to true to make the game pause.
-
Oct 6th, 2003, 07:35 AM
#3
-
Oct 6th, 2003, 09:48 AM
#4
Frenzied Member
And
If you want an FPS counter, use GetTickCount to save time elapsed in a var, then use an if sentece, something like:
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim Running As Boolean
Dim Pause As Boolean
Dim FPS, FPSCounter As Integer
Dim FPSTimer As Integer
Private Sub Form_Load()
Me.Show
Main
FPSTimer=GetTickCount
FPS=0
FPSCounter=0
End Sub
Private Sub Main()
Dim TempTime As Long
While Running
If TempTime <= GetTickCount Then
TempTime = GetTickCount + Interval
While Pause
DoEvents
Wend
If GetTickCount=FPSTimer+1000 Then
FPS=FPSCounter
FPSCounter=0
FPSTimer=GetTickCount
Else
FPSCounter=FPSCounter+1
End If
Me.Caption="MyGame - FPS: " & FPS
'game code goes here
End If
DoEvents
Wend
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Running = False
End Sub
Something like that should work, haven't tried it though... was actually just an idea I got when reading the post, hehe.
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Oct 6th, 2003, 10:33 AM
#5
Frenzied Member
ir you could do like this:
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim Running As Boolean
Dim Pause As Boolean
Dim Fps As Double
Const Interval As Long = 50
Private Sub Form_Load()
Me.Show
Running = True
Main
End Sub
Private Sub Main()
Dim TempTime As Long
Dim FpsTimer As Long
While Running
If TempTime <= GetTickCount Then
TempTime = GetTickCount + Interval
While Pause
DoEvents
Wend
FpsTimer = GetTickCount - FpsTimer
Fps = 1000 / FpsTimer
Me.Caption = Fps
FpsTimer = GetTickCount
'game code goes here
End If
DoEvents
Wend
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Running = False
End Sub
-
Oct 6th, 2003, 12:43 PM
#6
So Unbanned
I have a submission on pscode.com
If you search under Visual Basic for "fx fireworks" you'll be sure to find it.
It's a loop which includes FPS adjustment. That is, despite what the system may run the code at, it'll be adjusted to a fixed FPS.
-
Oct 7th, 2003, 11:13 AM
#7
Thread Starter
Addicted Member
WOW
Hey thanks cyborg, I can finally stop using those slow Timers
For anyone interested when using a timer the code took approx. 55ms, now with the game loop it takes <1ms.
Thanks all for the FPS code too...
NOMAD
-
Oct 8th, 2003, 09:28 PM
#8
Frenzied Member
no problems, man! we're here to help!
-
Oct 9th, 2003, 02:43 PM
#9
Frenzied Member
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Oct 9th, 2003, 03:45 PM
#10
Frenzied Member
yes ofcourse!
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
|