SVat's picture

Questions about the GLControl (game loop)

Hello! Prompt please, how to create a FPS counter for Windows forms + GLControl game loop?

And question about code:

void Application_Idle(object sender, EventArgs e)
{
    while (glControl.IsIdle)
    {
        Render();
    }
}

This function works in cycle, why else is needed while? 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

Without the while-loop, your application would draw only a single frame whenever the message pump goes empty (e.g. after moving the mouse or resizing the window.) You need the while-loop for continuous rendering.

For a fps counter, something like this is simple and works:

Stopwatch watch = new Stopwatch();
double timestamp;
int frames;
int frames_per_second;
 
static void Main()
{
    watch.Start();
    Application.Run(...);
}
 
void Application_Idle(object sender, EventArgs e)
{
    while (glControl.IsIdle)
    {
        Render();
        frames++;
 
        // Every second, update the frames_per_second count
        double now = watch.Elapsed.TotalSeconds;
        if (now - timestamp >= 1.0)
        {
            frames_per_second = frames;
            frames = 0;
            timestamp = now;
        }
    }
}
SVat's picture

@the Fiddler
Thank you. How to get the delta time for moving objects?

double deltaTime = now - timestamp;
updateGame(deltaTime);

like this?

the Fiddler's picture

Exactly, but you need a separate timestamp for delta time compared to the fps counter (for delta time you update the timestamp every frame, for delta time only once a second.)