Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

How to get more accurate game timing

Last updated: December 09, 1997
Article ID: RS25433

The information in this article applies to:

SUMMARY

This article explains how to provide a timing mechanism in Visual Basic to provide smoother motion while preventing temporal aliasing and tearing.

MORE INFORMATION

Visual Basic provides the Timer control which can be used to time events. The problem with the control is that it doesn't provide consistant timing. It's lower messaging priority means events can be skipped as well the interval for each event is really based from the previous event, meaning that a 100 millisecond interval causes the next event to follow the previous event (given time to execute your code in the event) after 100 milliseconds. It DOES NOT fire an event every 100 milliseconds as you are led to believe.

To get better timing we must go to the API and check elapsed time and create our own events. There are also other factors that take place in timing. You must consider to display at least 12 images per second, more if possible. But do not try and display more images per second that your monitor can handle (or the target monitor can handle).

For example, if the target monitor has a refresh rate of 60mhz the monitor can only refresh 60 times per second, that means you can only display up to 60 frames per second on that monitor before you start loosing frames which causes image tearing).

The factors involved in whether or not you can even meet this speed also depends on other factors such as how you display your images and other types of processing that must take place between processes.

The following code example describribes the basic timing loop using the Windows API. Your game will center around this loop.

Step-by-Step Instructions

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Drop a listbox control (List1) on the form. Size the control so that it occupies half of the form space. Set it's OLEDropMode=1-Manual.
  3. Drop a picturebox control (Picture1) on the form. Resize the control to occupy the other half of the form next to the listbox control.
  4. Paste the following code into the forms code editor:
    Dim PicArray() As Picture
    
    
    
    Private Sub Form_Unload(Cancel As Integer)
    
        Erase PicArray
    
    End Sub
    
    
    
    
    
    Private Sub List1_Click()
    
        With PicArray(List1.ListIndex)
    
            Picture1.PaintPicture PicArray(List1.ListIndex), 0, 0, ScaleX(.Width, vbHimetric, vbTwips), ScaleY(.Height, vbHimetric, vbTwips), 0, 0, ScaleX(.Width, vbHimetric, vbTwips), ScaleY(.Height, vbHimetric, vbTwips), vbSrcCopy
    
        End With
    
    End Sub
    
    
    
    
    
    Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    
        Dim ThisFile As Variant
    
        Dim PicCount As Byte ' 256 images max in this case
    
        
    
        On Error Resume Next
    
        
    
        List1.Clear
    
        Erase PicArray
    
        Screen.MousePointer = vbHourglass
    
        For Each ThisFile In Data.Files
    
            ' add to the list
    
            List1.AddItem ThisFile
    
            ReDim Preserve PicArray(PicCount)
    
            Set PicArray(PicCount) = LoadPicture(ThisFile) ' get bitmap file
    
            PicCount = PicCount + 1
    
        Next
    
        List1.ListIndex = 0
    
        Screen.MousePointer = vbDefault
    
    End Sub
    
    
  5. Save the project.
  6. Run the program (or press the F5 key). Drag and drop a group of files onto the listbox control. You will see the file name added to the list. Each file is also being stored in a PICTURE object. As you click on an item in the list, it's image is displayed in the picturebox.

NOTE

This is a very basic example of what you can do with the PICTURE object. You can store your sprites in many different picture objects and call on them when it's time to BitBlt them in an animated sequence.


THE INFORMATION PROVIDED IN THE RABIT SOFTWARE KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. RABIT SOFTWARE DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL RABIT SOFTWARE OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF RABIT SOFTWARE OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.