|
-
Jun 2nd, 2002, 07:21 AM
#1
Thread Starter
Lively Member
Loop or Timer?
In C++, I know you use a main game loop to go through all the code constantly. But I want to know if Visual basic games usually use something like that or not, or if it all gets put on a real fast timer.
If it IS done with a loop, please tell me how.
Do you know if you will answer no to this question?
If we've never seen something happen, we can't know if its impossible.
If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
If someone orders you to disobey all of their orders, do you obey or disobey?
-
Jun 2nd, 2002, 09:01 AM
#2
Frenzied Member
Timers are Evil(tm)
VB Code:
Do
'here goes your code
Loop until ExitCondition
Sanity is a full time job
Puh das war harter Stoff!
-
Jun 2nd, 2002, 10:24 AM
#3
Addicted Member
Here's my rather messy main loop from my engine (slightly edited):
VB Code:
Dim cInc As Single
Dim PerfFrequency As Currency, PerfTime As Currency, RealFPS As Double
Dim StartTime As Currency, EndTime As Currency, m_strCaption As String
Call QueryPerformanceFrequency(PerfFrequency)
m_Engine.DesiredFramerate = 1000 \ (1000 \ ClipValue(m_Engine.DesiredFramerate, c_lngMinFramerate, c_lngMaxFramerate))
m_Engine.Running = True
Do While m_Engine.Running = True
If m_booCritical Then
DoEvents
Else
If QueryPerformanceCounter(PerfTime) = 0 Then
CurrentTime = GetTickCount
StartTime = CurrentTime
Else
CurrentTime = GetTickCount
StartTime = Int((PerfTime / PerfFrequency) * 1000)
End If
If LastTime = 0 Then LastTime = CurrentTime
FramesToRender = Abs(CurrentTime - LastTime) + FramerateBuildup
FramerateBuildup = 0
MSPerFrame = 1000 \ m_Engine.DesiredFramerate
FramerateBuildup = FramesToRender Mod MSPerFrame
If m_Engine.BalanceFramerate Then
FramesToRender = FramesToRender \ MSPerFrame
Else
FramesToRender = 1
End If
If FramesToRender > c_lngMaxFrameskip Then FramesToRender = c_lngMaxFrameskip
If FramesToRender > 0 Then
If m_Engine.Paused Then
FramesToRender = 0
Else
For RenderFrames = 1 To FramesToRender
If m_Engine.Paused Then Else m_Engine.Update
IncUPS = IncUPS + 1
If m_Engine.Running = False Then Exit For: Exit Do
Next RenderFrames
End If
If m_Engine.Running Then Redraw
If m_GFX.Window.WindowState = 1 Then
Else
If m_Engine.Running Then m_GFX.FlipImage m_Backbuffer
End If
IncFPS = IncFPS + 1
Else
If m_Engine.HarassCPU Then Else Call SleepEx(1, True)
End If
If m_Engine.Running = False Then Exit Do
If QueryPerformanceCounter(PerfTime) = 0 Then
EndTime = GetTickCount
Else
EndTime = Int((PerfTime / PerfFrequency) * 1000)
End If
If m_Engine.Running = False Then Exit Do
If Int(CurrentTime \ 1000) <> Int(LastTime \ 1000) Then
CurrentFPS = IncFPS
CurrentUPS = IncUPS
IncFPS = 0
IncUPS = 0
End If
LastTime = CurrentTime
DoEvents
End If
Loop
Shutdown
It manages to maintain very very accurate framerates, with frameskipping, all without harassing the CPU (usually CPU usage stays between 0 and 10% when running at 60fps, unless the game is really detailed at the moment).
Hope this helps, if you want some help understanding it, let me know - it's not commented :P
"1 4m 4 1337 #4xz0r!'
Janus
-
Jun 2nd, 2002, 12:05 PM
#4
Thread Starter
Lively Member
Wheres the loop go? In its own sub? If so, where does THAT sub get put?
Do you know if you will answer no to this question?
If we've never seen something happen, we can't know if its impossible.
If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
If someone orders you to disobey all of their orders, do you obey or disobey?
-
Jun 2nd, 2002, 12:19 PM
#5
Frenzied Member
well you can make a sub like this
sub MainLoop()
end sub
just place it anywhere you want
now if you want to call it you could have a button for example or just put it into the form load event
Sanity is a full time job
Puh das war harter Stoff!
-
Jun 2nd, 2002, 12:29 PM
#6
Thread Starter
Lively Member
My loop is called when I press a command button. But after I do, it freezes up. This is the code for my loop:
Sub Run()
Do
If GetKeyState(vbKeyA) = 1 Then
MsgBox "Key A pressed"
End If
If GetKeyState(vbKeyQ) = 1 Then
Running = False
End If
Loop Until Running = False
End Sub
Do you know if you will answer no to this question?
If we've never seen something happen, we can't know if its impossible.
If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
If someone orders you to disobey all of their orders, do you obey or disobey?
-
Jun 2nd, 2002, 01:13 PM
#7
Frenzied Member
well this should work but maybe you should add a
DoEvents into the loop
and format your code...
Sanity is a full time job
Puh das war harter Stoff!
-
Jun 2nd, 2002, 03:04 PM
#8
Thread Starter
Lively Member
I got my loop to stop crashing, but something might be wrong with my GetKeyState thing.
Sub CheckMove()
If GetKeyState(vbKeyUp) = 1 Then
PC.xvel = PC.xpos - PC.xview
PC.yvel = PC.ypos - PC.yview
PC.xpos = PC.xpos + PC.xvel
PC.ypos = PC.ypos + PC.yvel
frmGame.Caption = PC.xpos
End If
End Sub
I put CheckMove in my loop, and then in one press it overflows. I have in a different sub setting xpos and ypos to 0, and xview and yview to 120.
Last edited by Drakon; Jun 2nd, 2002 at 03:09 PM.
Do you know if you will answer no to this question?
If we've never seen something happen, we can't know if its impossible.
If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
If someone orders you to disobey all of their orders, do you obey or disobey?
-
Jun 2nd, 2002, 04:06 PM
#9
Frenzied Member
use gettickcount to check when the key was last pressed, and only allow another press after about 10ms or some value that you like... otherwise it can be really fast!
Sanity is a full time job
Puh das war harter Stoff!
-
Jun 2nd, 2002, 04:21 PM
#10
Thread Starter
Lively Member
How do I use it, and where do I use it?
Do you know if you will answer no to this question?
If we've never seen something happen, we can't know if its impossible.
If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
If someone orders you to disobey all of their orders, do you obey or disobey?
-
Jun 2nd, 2002, 04:43 PM
#11
Frenzied Member
VB Code:
Sub CheckMove()
static LastPress as long
if lastpress<Gettickcount then
lastpress = gettickcount +50 'this will allow a keydown all 50 milli seconds
if GetKeyState(vbKeyUp) = 1 Then
PC.xvel = PC.xpos - PC.xview
PC.yvel = PC.ypos - PC.yview
PC.xpos = PC.xpos + PC.xvel
PC.ypos = PC.ypos + PC.yvel
frmGame.Caption = PC.xpos
end if
End If
End Sub
Sanity is a full time job
Puh das war harter Stoff!
-
Jun 2nd, 2002, 06:50 PM
#12
Thread Starter
Lively Member
Now it doesn't do anything... do I have to do anything else anywhere?
Do you know if you will answer no to this question?
If we've never seen something happen, we can't know if its impossible.
If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
If someone orders you to disobey all of their orders, do you obey or disobey?
-
Jun 2nd, 2002, 07:03 PM
#13
PowerPoster
You have to call from inside a loop..
VB Code:
While DoEvents
CheckMove
Wend
-
Jun 2nd, 2002, 07:10 PM
#14
Thread Starter
Lively Member
I've already done that, otherwise it would never have worked, therefore preventing the overflow
Do you know if you will answer no to this question?
If we've never seen something happen, we can't know if its impossible.
If the soles of a shoe make faces at the floor when we don't look and isn't being watched via mirror or video tape, will we ever know?
If someone orders you to disobey all of their orders, do you obey or disobey?
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
|