flopoloco's picture

Window title updated with Timer eventhandler.

Hello, 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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

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:

using System;
using System.Timers;
using OpenTK;
using OpenTK.Graphics;
 
namespace TestTK
{
    internal class MainApplication : GameWindow
    {
        private Timer fpsDisplayTimer;
        private double openTkUpdateFPSDisplay = 0.0;
        private double openTkRenderFPSDisplay = 0.0;
        private bool fpsUpdate = false;
 
        public override void OnLoad(EventArgs e)
        {
            fpsDisplayTimer = new Timer();
            fpsDisplayTimer.Elapsed += new ElapsedEventHandler(fpsDisplayTimer_Elapsed);
            fpsDisplayTimer.Interval = 1000;
            fpsDisplayTimer.Start();
        }
 
        public override void OnRenderFrame(RenderFrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit);
 
            this.openTkRenderFPSDisplay = (1.0 / e.Time);
 
            SwapBuffers();
        }
 
        public override void OnUpdateFrame(UpdateFrameEventArgs e)
        {
            this.openTkUpdateFPSDisplay = (1.0 / e.Time);
 
            if (fpsUpdate)
            {
                this.Title = string.Format("Update FPS: {0} | Render FPS: {1}",
                    this.openTkUpdateFPSDisplay.ToString("F2"),
                    this.openTkRenderFPSDisplay.ToString("F2"));
 
                fpsUpdate = false;
            }
        }
 
        void fpsDisplayTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            fpsUpdate = true;  // 1 second passed, indicate that we should update.
        }
    }
 
    public class Program
    {
        [STAThread]
        public static void Main()
        {
            using (MainApplication mainApplication = new MainApplication())
            {
                mainApplication.Run(60.0);
            }
        }
    }
}
objarni's picture

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

flopoloco's picture

Oh... You are definitely correct, I had confused the System.Timer with Forms.Timer :P, thanks for the help!