No announcement yet.

Question about TTimer for a better understanding

  • Filter
  • Time
  • Show
Clear All
new posts

  • lifepower
    There are two general approaches at creating applications with Afterwarp:

    1. Repaint only when needed. This is typical to CAD applications. If you are using TRenderingControl (a small component both for Delphi and Lazarus that is created at run-time on the form), then you just have to call TRenderingControl.Invalidate to schedule a repaint event. In this case, you would call TTimer.Execute inside ApplicationIdle event, which will call TTimer.OnTimer event - simply call TTimer.Process there to invoke TTimer.OnProcess a fixed number of times per second. It would be useful to set TTimer.MaxFrameRate to something like 120 or 200, to avoid using 100% of CPU by just doing timer calculations. Same approach would be used in a dedicated server, where you'll call TTimer.Execute as fast as possible, but call TTimer.Process(True), so if your processing speed is slower than 60 FPS, it will not try to overrun the timer; similarly, in this case, set MaxFrameRate to limit CPU usage.

    Please note that approach (1) is most power efficient, especially on laptops and mobiles and it works on all platforms. The only exception is when you use Lazarus LCL on Linux - there seems to be no way to prevent flickering when repainting the form, likely something to do with LCL itself. In this case, you can use Afterwarp without LCL by using "TApplication" component from Afterwarp, which will manage application window and all its events for you and it will work flawlessly.

    2. Repaint continuously. This is typical to desktop games. For mobile games, it'd still be better to try approach 1, or reduce TTimer.Speed to 30, to increase battery life. In this case, you call TTimer.Execute as fast as possible, e.g. from ApplicationIdle event. This would invoke OnTimer event, where you should perform the rendering. After issuing all rendering calls and before TDevice.EndScene, you should call TTimer.Process to invoke OnProcess event fixed times per second. The idea is that while GPU is still busy rendering the scene, you can do CPU calculations in parallel.

    I have attached Cube example for FreePascal/Lazarus that illustrates how to use TTimer as in case (2).
    Attached Files

    Leave a comment:

  • Zimond
    started a topic Question about TTimer for a better understanding

    Question about TTimer for a better understanding

    I saw that all the examples only use the TTimer for measuring the framerate while the application basicly runs at full speed without a process loop at all. So I specified an onTimer and onProcess event (after I found out that the procedures need TTimerevents to work) and I wanted to move the rendering part to the onTimer loop as it always has been in Asphyre or PXL. But that didnt really work. I had to leave the render stuff within the ApplicationOnIdle procedure and the onTimer event only has 1 line "ftimer.process". (Tested the process loop with a counter and that works with the correct speed)

    To my surprise this already limited the maximum framerate as it should (I set it to 120) even though the rendercall happens in the ApplicationOnIdle prod. Is this now the correct way of using the timer? So what the MaxFramerate property does is keeping the application busy (non idle) and when it let go the OnIdle event kicks in to render a frame until the Ftimer.execute call?