Hello!
I'm trying to learn OpenGL programming with Delphi by following
NeHe's tutorials, using Mike Lischke's OpenGL 1.2 header.
I'm at tutorial 4,
Rotation, which explains how to rotate objects around an axis. I watched Martti Nurmikari's presentation
The basics of demo programming, paying particular attention to his explanation on implementing the timing loop. He presents the following solution, at around 17:55 in the video:

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;