Results 1 to 21 of 21

Thread: Control program speed

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2002
    Location
    Aurora, CO
    Posts
    16

    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

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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....

  3. #3
    New Member
    Join Date
    Nov 2002
    Posts
    1
    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

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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....

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2002
    Location
    Aurora, CO
    Posts
    16
    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

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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....

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2002
    Location
    Aurora, CO
    Posts
    16
    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

  8. #8
    Addicted Member
    Join Date
    Aug 2002
    Location
    Baltimore, MD
    Posts
    230

  9. #9
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    VB Code:
    1. Private Declare Function GetTickCount Lib "kernel32" () As Long
    2. Private LastTickCount As Long
    3. Private NewTickCount
    4.  
    5. Dim Speed As Long
    6.  
    7. Speed = 49  'Speed of input
    8.  
    9. If (NewTickCount - LastTickCount) > Speed Then
    10.     NewTickCount = GetTickCount
    11.     'Code goes here
    12.     LastTickCount = GetTickCount()  'Must be the last thing in the statement
    13. 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!

  10. #10
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2002
    Location
    Aurora, CO
    Posts
    16
    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

  12. #12
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    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!

  13. #13
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    BTW..Sorry bout my code. I forgot to include the most important line of updating the tickcount.

  14. #14
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    INF3RN0666: If you read my post, you can see what is "wrong" with the update of your code...

  15. #15
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    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!

  16. #16
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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...

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Feb 2002
    Location
    Aurora, CO
    Posts
    16
    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

  18. #18
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    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!

  19. #19
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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...

  20. #20
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    That's what I thought. So it would be wrong to put it the statement after a WHILE loop!

  21. #21
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    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
  •  



Click Here to Expand Forum to Full Width