
Window title updated with Timer eventhandler.
Posted Friday, 28 November, 2008 - 09:27 by flopoloco inHello, with this application I want to know how much FPS I got in my OpenTK applications but I have some difficulties. Too bad I spoil the surprise (instead of posting the forum example category) but I hope that learning is the process of completeness.
The point, is that I do not want to get update and render information as fast as the application updates, I want to get the values every 1 second (or an amount of time that is understood by humans). I found useful to use the System.Forms.Timer class and it appears to work fine. The problem is that information in window title is updated as soon as I move the window (Invoking Form Events).
I do not know if there is something in OpenTK or Windows.Forms I will have to look, I am confused... Any ideas?
using System; using System.Windows.Forms; using OpenTK; using OpenTK.Graphics; namespace TestTK { internal class MainApplication : GameWindow { private Timer fpsDisplayTimer; private double openTkUpdateFPSDisplay = 0.0; private double openTkRenderFPSDisplay = 0.0; public override void OnLoad(EventArgs e) { fpsDisplayTimer = new Timer(); fpsDisplayTimer.Tick += new EventHandler(FpsDisplayTimerUpdate); fpsDisplayTimer.Interval = 1000; fpsDisplayTimer.Start(); } public override void OnRenderFrame(RenderFrameEventArgs e) { this.openTkRenderFPSDisplay = (1.0 / e.Time); } public override void OnUpdateFrame(UpdateFrameEventArgs e) { this.openTkUpdateFPSDisplay = (1.0 / e.Time); } // Point of interest. private void FpsDisplayTimerUpdate(object sender, EventArgs e) { // Window title can not be updated... this.Title = string.Format("Update FPS: {0} | Render FPS: {1}", this.openTkUpdateFPSDisplay.ToString("F2"), this.openTkRenderFPSDisplay.ToString("F2")); } } public class Program { [STAThread] public static void Main() { using (MainApplication mainApplication = new MainApplication()) { mainApplication.Run(60.0); } } } }
Thanks. :)


Comments
Re: Window title updated with Timer eventhandler.
You cannot use the winforms Timer on non-winforms applications. You should use the System.Timer instead.
Since the System.Timer fires its Elapsed event on a different thread, you cannot directly modify the GameWindow title. What you should do is add a flag (
bool fpsUpdate) that indicates whether it's time to update the title:Re: Window title updated with Timer eventhandler.
flopoloco: I think the GLControl tutorial in the docs does just this - without any timer. Check it out:
http://opentk.com/doc/chapter/2/glcontrol
Re: Window title updated with Timer eventhandler.
Oh... You are definitely correct, I had confused the System.Timer with Forms.Timer :P, thanks for the help!