Dark Bit Factory & Gravity

ARCHIVE => Archive => Delphi & Pascal => Topic started by: Gore Motel on November 10, 2013

Title: Delphi + OpenGL - Timing loop help
Post by: Gore Motel on November 10, 2013
Hello!

I'm trying to learn OpenGL programming with Delphi by following NeHe's tutorials (http://nehe.gamedev.net/), using Mike Lischke's OpenGL 1.2 header.

I'm at tutorial 4, Rotation (http://nehe.gamedev.net/tutorial/rotation/14001/), which explains how to rotate objects around an axis. I watched Martti Nurmikari's presentation The basics of demo programming (http://www.youtube.com/watch?v=TbcZyAO6K7c), paying particular attention to his explanation on implementing the timing loop. He presents the following solution, at around 17:55 in the video:

(http://i.imgur.com/8HGnmjg.jpg)

Is that a good timing implementation or would you do it in a different way?

I've tried to translate his implementation to Delphi code. Below are the relevant snippets:

- declaring required variables and constants:
const
  // ...
  FPS = 60;
  FRAME_LENGTH: Single = 1.0 / FPS;
  MAX_ITERATIONS = 10;
  // ...

var
  // ...
  LastTime: Integer = 0;
  CumulativeTime: Single = 0.0;
  rTri: Single = 0.0;
  rQuad: Single = 0.0;
  // ...


rTri and rQuad are the angles at which the objects are rotated.

- the update function:
procedure UpdateGL(currentTime: Integer);
var
  dt, iterations: Integer;
begin
  iterations := 0;
  dt := currentTime - LastTime;
  CumulativeTime := CumulativeTime + dt * 0.001;
  LastTime := currentTime;

  while (CumulativeTime > FRAME_LENGTH) do
  begin
    // modify the angles
    rTri := rTri + 0.2;
    rQuad := rQuad - 0.15;

    CumulativeTime := CumulativeTime - FRAME_LENGTH;

    Inc(iterations); // same as iterations := iterations + 1

    if (iterations > MAX_ITERATIONS) then
      Break;
  end;
end;


- and the main function with the running loop:

  // create the window, initialize OpenGL, check for errors

  timeBeginPeriod(1);
  currentTime := timeGetTime;
  timeEndPeriod(1);

  LastTime := currentTime;
   
  // main loop
  while (not Quit) do // Quit is a boolean initialized as false
  begin
    timeBeginPeriod(1);
    currentTime := timeGetTime;
    timeEndPeriod(1);

    UpdateGL(currentTime); // call the update function

    DrawGL; // draw the objects

    SwapBuffers(MainForm.Canvas.Handle);
 
    MainForm.ProcessMessages; // check for any messagess
  end;

  // deinitialize OpenGL, destroy the window


Quit gets set to true when the user closes the window.

Have I implemented Martti's solution correctly? The objects seem to be moving fine on my PC. What I'm unsure about is whether the drawing should be done like above, or inside the update loop, like so:

procedure UpdateGL(currentTime: Integer);
  // ...
  while (CumulativeTime > FRAME_LENGTH) do
  begin
    rTri := rTri + 0.2;
    rQuad := rQuad - 0.15;

    DrawGL;

    SwapBuffers(MainForm.Canvas.Handle);

    CumulativeTime := CumulativeTime - FRAME_LENGTH;

    Inc(iterations);

    if (iterations > MAX_ITERATIONS) then
      Break;
  end;
end;
Title: Re: Delphi + OpenGL - Timing loop help
Post by: Hotshot on November 14, 2013
I did love to help ya if I know but Last time I did Delphi must be back in early 2000 ish I think...

I am sure someone will help ya :)
Title: Re: Delphi + OpenGL - Timing loop help
Post by: Rbz on November 15, 2013
I've messed with delphi some long time ago, but I think you have coded it right.
And for your question about update loop, I would make it like in your first code, inside main loop:

  // main loop
  while (not Quit) do // Quit is a boolean initialized as false
  begin
    timeBeginPeriod(1);
    currentTime := timeGetTime;
    timeEndPeriod(1);

    UpdateGL(currentTime); // call the update function

    DrawGL; // draw the objects

    SwapBuffers(MainForm.Canvas.Handle);
 
    MainForm.ProcessMessages; // check for any messagess
  end;
Title: Re: Delphi + OpenGL - Timing loop help
Post by: Gore Motel on November 16, 2013
Thanks! Everything does seem to be working fine, although I haven't implemented anything graphics intensive yet.