Bug with Framerates


Please see the post:http://www.codeproject.com/Articles/3144/SharpGL-a-C-OpenGL-class-library?msg=4396854#xx4396854xxIt contains details of a bug in the framerate setter and a suggestion for an improvementHello Dave, I discovered another bug: setting the framerate doesn't work at all Here's why: your code reads: [Description("The rate at which the control should be re-drawn, in Hertz."), Category("SharpGL")] public int FrameRate { get { return frameRate; } set { frameRate = value; timerDrawing.Interval = 1000 / 20; } } Note the hardcoded 20 at the end. It should be "frameRate" instead. Additionally, IMHO the code should be extended to stop the timer if frameRate is <=0.When changing it up again to something >0, then the timer should be re-started.Here's the implementation I suggest: private int frameRate = 20; private bool constructorFinished = false; // this bool is needed to prevent the timer from starting when the Constructor code isn't finished yet [Description("The rate at which the control should be re-drawn, in Hertz."), Category("SharpGL"), DefaultValue(20)] public int FrameRate { get { return frameRate; } set { if(value <= 0) { frameRate = 0; } else { frameRate = value; timerDrawing.Interval = 1000 / frameRate; } if(constructorFinished) { timerDrawing.Enabled = frameRate>0; } } }and public OpenGLControl() { InitializeComponent(); // Set the user draw styles. SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); int f = FrameRate; FrameRate = 0; constructorFinished = true; FrameRate = f; // will start the timer } Note that I've changed the constructor to no longer fiddle with the timerimplementation details. Instead, only the setter for FrameRate now does. Regards,Christophe
Closed Dec 30, 2012 at 4:19 PM by dwmkerr
Resolved in SharpGL 2.1


dwmkerr wrote Dec 30, 2012 at 2:07 PM

Thanks for the detailed info and suggestion - these changes have just been implemented and will be available in SGL 2.1