Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / Windows Forms

Framerate Counter

Rate me:
Please Sign up or sign in to vote.
3.60/5 (4 votes)
27 Jul 2007CPOL2 min read 30.7K   9   10  
A minimalist framerate counter.

Screenshot - framerate.jpg

Introduction

This is a quick, functional frame-rate counter for your graphics app which draws and draws (as typical games do). It could be updated for use in other apps, such as keeping track of bytes downloaded of a file. The rate could then be used to estimate a time. However, this is provided with the intent of using it as a counter for your graphical performance.

Using the code

This may look familiar, but it's not. I've seen two types of frame-rate counters. This is one, the other is a function which returns the callCount, and the Paint routine checks: If frameRate() > 0 Then and promptly updates a local value. This one differs in that it does this for you, and changes the value of a variable (herein called currentFrameRate). The value is changed once per second, and is set to the number of times your Paint loop was executed that second. You only need to use this value as needed, such as drawing it onto your surface, and GDI+ based instructions are provided for this. It's by no means decimal-point-accurate, and I would allow a variation of about 1. However, if your frame-rate gets below 10, then I would be concerned about the user experience regardless of the code's potential inaccuracy.

Creative people may even be able to adapt this to other uses, such as keeping track of a process' performance (objects dealt with per second). You could even change the time to allow you a call count per minute. However, it is configured for seconds, and if you change the time scale, be wary of the callCount becoming too high.

To actually use the code:

  1. Insert the following code to the source:
  2. VB
    ' Framerate Counter
    Private Sub advanceFrameRate()
        Static ptlu As Double ' Time of last framerate update.
        ' Show me hardware that can do 32k F/sec...
        Static callCount As Integer
        ' Increment the callCounter
        callCount += 1
        ' Change 1000 if an alternate time value is desired.
        If (Environment.TickCount - ptlu) >= 1000 Then
    
            currentFrameRate = callCount
            ' Reset the callCount, AFTER updating the value.
            callCount = 0
            ' Reset the timeUpdated
            ptlu = Environment.TickCount
        Else
        End If
    End Sub
  3. Add the following to your Declarations section, and ensure the function will have access to it (e.g., keep them in the same class):
  4. VB
    Private currentFrameRate As Integer
  5. In your drawing routine (e.g., myForm_Paint), add the following lines:
  6. VB
    ' The Call statement is not needed, but makes it obvious.
    Call advanceFrameRate()
    e.Graphics.DrawString(paintingFrameRate & " F/sec", & _
        framerateFont, Brushes.Black, Xpos, Ypos)

    Note: the above line uses GDI+ and assumes you are in the Me.Paint handler. Feel free to employ DirectX drawing, or even use a MessageBox to report this to the user. framerateFont is a System.Drawing.Font of your choosing. Also, the brush colour can change, especially if your background is black. I tend to use:

    VB
    Private framerateFont As Font = _
            New Font("Verdana", 12, FontStyle.Regular, GraphicsUnit.Pixel)

    Note: This line requires Imports System.Drawing in the declarations. Xpos and Ypos represent wherever you wish to put the code on your form, and can be replaced with a System.Drawing.Point if need be.

I'm not totally fussed about whether you put my name on the code or not. Although, I would quite enjoy downloading an app which had "Contains code from a Ninja" on the download page.

History

This is the original version which I submitted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Scotland Scotland
Hobbyist developer from the UK.
I just make stuff for the benefit of making stuff.

Comments and Discussions

 
-- There are no messages in this forum --