### Author Topic: Delphi + OpenGL - Timing loop help  (Read 1674 times)

0 Members and 1 Guest are viewing this topic.

#### Gore Motel

• C= 64
• Posts: 45
• Karma: 18
##### Delphi + OpenGL - Timing loop help
« on: November 10, 2013 »
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:
Code: [Select]
`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:
Code: [Select]
`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:
Code: [Select]
`  // 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:

Code: [Select]
`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;`

#### Hotshot

• Posts: 2114
• Karma: 91
##### Re: Delphi + OpenGL - Timing loop help
« Reply #1 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

#### Rbz

• Founder Member
• Posts: 2721
• Karma: 485
##### Re: Delphi + OpenGL - Timing loop help
« Reply #2 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:

Code: [Select]
`  // 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;`
Challenge Trophies Won:

#### Gore Motel

• C= 64
• Posts: 45
• Karma: 18
##### Re: Delphi + OpenGL - Timing loop help
« Reply #3 on: November 16, 2013 »
Thanks! Everything does seem to be working fine, although I haven't implemented anything graphics intensive yet.