Results 1 to 6 of 6

Thread: Timer without a form

  1. #1

    Thread Starter
    Addicted Member matbrophy's Avatar
    Join Date
    Sep 1999
    Location
    Kent, United Kingdom
    Posts
    149

    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

  2. #2
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    try this:

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Dim Pause As Boolean                                        'check if game is paused
    5. Dim Running As Boolean                                      'check if game is started
    6. Dim Fps As Double                                           'current fps
    7. Dim FrameCount As Long                                      'count frames between fps update
    8.  
    9. Private Sub Form_Load()
    10.     Me.Show                                                 'show the window
    11.     Running = True                                          'no the game is on
    12.     Main                                                    'start main loop
    13. End Sub
    14.  
    15. Private Sub Main()                                          'the main loop
    16.     Dim TempTime As Long                                    'to keep track of time
    17.    
    18.     While Running                                           'is the game running?
    19.         If TempTime <= GetTickCount Then                    'is it time to calc next frame?
    20.             TempTime = GetTickCount + 10                    'set next tick time
    21.            
    22.             While Pause                                     'is the game paused?
    23.                 DoEvents                                    'infinit loop
    24.             Wend
    25.            
    26.             'Code goes here.
    27.            
    28.             FrameCount = FrameCount + 1                     'fps counter
    29.             If Fps + 1000 <= GetTickCount Then
    30.                 Me.Caption = "Fps: " & Round(1000 / ((GetTickCount - Fps + 0.00001) / FrameCount), 0)
    31.                 Fps = GetTickCount
    32.                 FrameCount = 0
    33.             End If
    34.         End If
    35.         DoEvents                                            'handle win msgs
    36.     Wend
    37.    
    38. End Sub
    39.  
    40. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    41.     Running = False                                         'game over
    42. End Sub
    43.  
    44. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    45.     If KeyCode = vbKeyP Then                                'pause code
    46.         If Pause = False Then
    47.             Pause = True
    48.             Me.Caption = "Paused"
    49.         ElseIf Pause = True Then
    50.             Pause = False
    51.         End If
    52.     End If
    53. End Sub
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  3. #3

    Thread Starter
    Addicted Member matbrophy's Avatar
    Join Date
    Sep 1999
    Location
    Kent, United Kingdom
    Posts
    149
    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

  4. #4
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    its much faster than the timer control...

    try this code...its smaller:

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3. Dim Running As Boolean                                      'check if game is started
    4.  
    5. Private Sub Form_Load()
    6.     Me.Show                                                 'show the window
    7.     Running = True                                          'no the game is on
    8.     Main                                                    'start main loop
    9. End Sub
    10. Private Sub Main()                                          'the main loop
    11.     Dim TempTime As Long                                    'to keep track of time
    12.    
    13.     While Running                                           'is the game running?
    14.         If TempTime <= GetTickCount Then                    'is it time to calc next frame?
    15.             TempTime = GetTickCount + 10                    'set next tick time
    16.            
    17.             'Code goes here.
    18.  
    19.         End If
    20.         DoEvents                                            'handle win msgs
    21.     Wend
    22. End Sub
    23. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    24.     Running = False                                         'game over
    25. End Sub
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  5. #5
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    In this thread wokawidget answers the question. Still thanking him....
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  6. #6

    Thread Starter
    Addicted Member matbrophy's Avatar
    Join Date
    Sep 1999
    Location
    Kent, United Kingdom
    Posts
    149
    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
  •  



Click Here to Expand Forum to Full Width