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:
- Insert the following code to the source:
Private Sub advanceFrameRate()
Static ptlu As Double Static callCount As Integer
callCount += 1
If (Environment.TickCount - ptlu) >= 1000 Then
currentFrameRate = callCount
callCount = 0
ptlu = Environment.TickCount
Else
End If
End Sub
- Add the following to your Declarations section, and ensure the function will have access to it (e.g., keep them in the same class):
Private currentFrameRate As Integer
- In your drawing routine (e.g.,
myForm_Paint
), add the following lines:
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:
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.