![]() |
Languages »
VB.NET »
General
Beginner
Framerate CounterBy Ninja-the-NerdA minimalist framerate counter. |
VB 8.0, Windows, .NET 1.0, .NET 1.1, .NET 2.0, GDI+, WinForms, VS2005, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
![]()
This is a quick, functional framerate 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.
This may look familiar, but it's not. I've seen two types of framerate 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 framerate 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's 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 get using the code,
1. Insert the following code to the source:
' 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
2. Add the following to your Declarations section, and ensure the function will have access to it (eg keep them in the same class):
Private currentFrameRate As Integer
3. In your drawing routine (eg myForm_Paint), add the following lines:
' 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 a 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 if 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.
This is the original version which I submitted.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 27 Jul 2007 Editor: |
Copyright 2007 by Ninja-the-Nerd Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |