|
-
Nov 29th, 2002, 02:16 PM
#1
Thread Starter
Junior Member
Control program speed
This is the code I use to control the speed of my games and visual demos:
While DoEvents
If Speed < GetTickCount Then
Speed = GetTickCount + SP (var)
'my program'
End If
Wend
This is often too slow for what I need. What is a better way of doing this so it will run faster?
Spraky84
-
Nov 29th, 2002, 02:21 PM
#2
I can't see why this is too slowe. Of course if you have a slow computer that might be the problem. If not you could use the api functions QueryPerfomanceCount and QueryPerformanceFrequency....
-
Nov 29th, 2002, 03:51 PM
#3
New Member
I usually use this code:
Public Sub Main ()
...code....
wait(10)
...code...
End Sub
public sub Wait(num as integer)
dim i as integer
for i = 1 to 10 * num 'adjust the number 10 to your liking
doevents
next i
end sub
-Sly
-
Nov 29th, 2002, 04:04 PM
#4
chadwilks: You could use his code, but then it would go fatser on faster machines and then go slower on slower machines. And that beats the purpose of the whole idea....
-
Nov 29th, 2002, 04:18 PM
#5
Thread Starter
Junior Member
The purpose of using the API function GetTickCount is to have it run the same speed on all computers and it does. There is no problem with my computer's speed. GTC is using the internal clock and isn't subject to my computer's speed as far as I know.
If I run a for-next loop or count a variable to a given limit, then it will be subject to someone's processor speed.
Thanks for the input, but I know there is a faster way. I have seen it in many programs. I'm either not using GTC correctly or I need to use another method............
Sparky84
-
Nov 29th, 2002, 04:43 PM
#6
And an other method was just what I gave you. Read my first post about:
If not you could use the api functions QueryPerfomanceCount and QueryPerformanceFrequency....
-
Nov 29th, 2002, 05:31 PM
#7
Thread Starter
Junior Member
I have looked in MSDN before that I have on disk and there are no examples on how to use QueryPerformanceCount or Frequency and in no way shows how to use this in my programs.
Thanks again for the info but I need some HELP. This has to include how to use whatever syntax or code in my program to control the speed. I need to know HOW to use whatever, not just the general command or syntax. I need the code?????
Sparky84
-
Nov 29th, 2002, 07:24 PM
#8
Addicted Member
-
Nov 30th, 2002, 03:10 PM
#9
Addicted Member
VB Code:
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private LastTickCount As Long
Private NewTickCount
Dim Speed As Long
Speed = 49 'Speed of input
If (NewTickCount - LastTickCount) > Speed Then
NewTickCount = GetTickCount
'Code goes here
LastTickCount = GetTickCount() 'Must be the last thing in the statement
End If
Put those where you need them. I used them for keyboard input so that people can't cheat the game by speeding up their keyboard repeat rate. It's clean and fast code for that matter!
-
Nov 30th, 2002, 05:01 PM
#10
INF3RN0666: I don't think that would solve his problem. I think sparky84s problem is that GetTickCount is not precise enough.
From the MSDN:
System Resolution
Windows NT 3.5 and later The system timer runs at approximately 10ms.
Windows NT 3.1 The system timer runs at approximately 16ms.
Windows 95 and later The system timer runs at approximately 55ms.
So if he wants a more precise timer he has to use the QueryPerformanceCount or Frequency.
-
Nov 30th, 2002, 06:01 PM
#11
Thread Starter
Junior Member
I tried the link for QueryPerformanceCount but do not see how to use it to control the speed with for my own use - I wish.
Thanks to INF3RN0666 for the code - my program would not even move with it the way it's shown, so I changed it to (GetTickCount - LastTickCount) > Speed and it works - it is just slightly faster than what I was using - just barely.
Sparky84
-
Nov 30th, 2002, 06:38 PM
#12
Addicted Member
yo bud! show me your loop code. I've ran a 3D game on my system using that code and it ran as fast as light! *pretty fast if u know what i'm sayin*. It's prolly somethin in your code. I'll pin point it for u!
-
Nov 30th, 2002, 06:39 PM
#13
Addicted Member
BTW..Sorry bout my code. I forgot to include the most important line of updating the tickcount.
-
Nov 30th, 2002, 06:59 PM
#14
INF3RN0666: If you read my post, you can see what is "wrong" with the update of your code...
-
Nov 30th, 2002, 07:23 PM
#15
Addicted Member
I did read your post...But i'm ignorant that way! I just think that if my system can run a Complex 3D Game made in visual basic at 100 frame rate using code similar to mine, then this guy must be crazy trying to achieve anything higher! BTW, i'm talking about a 500 Celeron and SDRam :P!
-
Nov 30th, 2002, 07:55 PM
#16
The problem here with the GetTick count is that when you call on a Windows 95 it it will not change the returned value before it has gone ca55ms...so you cant have more then 18 frames per sec. On a new NT machine you can't have more then 100 frames per second....It's the resolution of the timer...
-
Dec 1st, 2002, 04:50 AM
#17
Thread Starter
Junior Member
Thanks for all the input, especially to INF3RN0666. I'm using Windows ME, Celeron 633 MHZ. My computer is 2 years old next month. I'm slowly getting there with my programs. Maybe there is something with my timer.
When I can't find something in one of my books or on my MSDN disk, then it's people like all of you that have helped me out a lot in the last year!
Later, eh!
Sparky84
-
Dec 1st, 2002, 09:36 AM
#18
Addicted Member
How does this work??
While DoEvents <-- DoEvents is always Zero for me :S
I'm hoping it's a typo kuz right now i'm confused if your program would run at all.
Thanx in advance!
-
Dec 1st, 2002, 09:38 AM
#19
I'm not sure but I think DoEvents is an event that is triggered so Windows can have some time to refresh screen and things like that. So I don't think it is supposed to return anyting...
-
Dec 1st, 2002, 04:51 PM
#20
Addicted Member
That's what I thought. So it would be wrong to put it the statement after a WHILE loop!
-
Dec 3rd, 2002, 08:38 PM
#21
Addicted Member
yeah, doevents tells your program to yield time on the processor to other apps. If you leave it out of a graphics loop, your computer, and even the mouse, will be inept at doing anything but sitting there. Here's what i always use for gettickcount:
dim FrameRate as long
dim LastFrame as long
FrameRate = 60
do while(true)
if GetTickCount > LastFrame + (1000 / FrameRate) then
'do stuff
LastFrame = GetTickCount
end if
DoEvents
loop
This should give you 60 FPS, however, do to the resolution of of GetTickCount, it will only give you about 50 FPS. This may be different on other systems. Also, FYI, I have found no way to get between 50-100 FPS. For example, if you set FrameRate to 75, you will still 50. This is true for every num up to 100, where the frame rate becomes 100 for every number greater than 100.
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
|