Recent

Author Topic: Jerky animation (windows)  (Read 2879 times)

hedgehog

  • Jr. Member
  • **
  • Posts: 82
Re: Jerky animation (windows)
« Reply #15 on: January 05, 2026, 01:13:33 pm »
This is an impressive demonstration of OpenGL's capabilities.
On my computer, the process took up 5% of the CPU and 6% of the GPU.

These are quite respectable results.

I have a question:
If OpenGL can perform frame rate synchronization, then perhaps this capability can be added to LCL?

Roland57

  • Hero Member
  • *****
  • Posts: 553
    • msegui.net
PTCPas & Cairo example
« Reply #16 on: January 11, 2026, 08:59:15 am »
Here is something else I tried: an animation using PTCPas and Cairo.

It is based on an example shipped with PTCPas. I wasn't sure how I could connect the pixel buffer to a Cairo surface, but fortunately the first thing I tried worked.  :)

The result is very satisfying (to my eyes), but the program uses 60% of the CPU. The Smoothris demo uses 80%. Maybe we could make a report to PTCPas author about this.

Code: Pascal  [Select][+][-]
  1. {
  2.   ptc_cairo_01.pas
  3.   Example of animation using PTCPas and Cairo
  4.   Based upon <ptcpas>/examples/buffer.pp
  5. }
  6.  
  7. program BufferExample;
  8.  
  9. {$MODE objfpc}
  10.  
  11. uses
  12.   ptc, cairo;
  13.  
  14. var
  15.   console: IPTCConsole;
  16.   format: IPTCFormat;
  17.   width, height: Integer;
  18.   pixels: PUint32 = nil;
  19.   (*
  20.   x, y, r, g, b: Integer;
  21.   i: Integer;
  22.   *)
  23.   sf: pcairo_surface_t;
  24.   cr: pcairo_t;
  25.   angle: double;
  26.  
  27. begin
  28.   try
  29.     try
  30.       { create console }
  31.       console := TPTCConsoleFactory.CreateNew;
  32.  
  33.       { create format }
  34.       format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  35.  
  36.       { open the console }
  37.       console.Open('PTCPas & Cairo example', 480, 480, format);
  38.      
  39.       { get console dimensions }
  40.       width := console.Width;
  41.       height := console.Height;
  42.  
  43.       { allocate a buffer of pixels }
  44.       pixels := GetMem(width * height * SizeOf(Uint32));
  45.       FillChar(pixels^, width * height * SizeOf(Uint32), 0);
  46.      
  47.       sf := cairo_image_surface_create_for_data(pbyte(pixels), CAIRO_FORMAT_ARGB32, width, height, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width));
  48.       cr := cairo_create(sf);
  49.      
  50.       cairo_scale(cr, width, height);
  51.       cairo_translate(cr, 1/2, 1/2);
  52.      
  53.       angle := 0;
  54.      
  55.       { loop until a key is pressed }
  56.       while not console.KeyPressed do
  57.       begin
  58.         angle := angle + PI/500;
  59.         (*
  60.         { draw random pixels }
  61.         for i := 1 to 100 do
  62.         begin
  63.           { get random position }
  64.           x := Random(width);
  65.           y := Random(height);
  66.  
  67.           { get random color }
  68.           r := Random(256);
  69.           g := Random(256);
  70.           b := Random(256);
  71.  
  72.           { draw color [r,g,b] at position [x,y] }
  73.           pixels[x + y * width] := (r shl 16) or (g shl 8) or b;
  74.         end;
  75.         *)
  76.        
  77.         cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
  78.         cairo_paint(cr);
  79.        
  80.         cairo_arc(cr, 0.0, 0.0, 1/2, 0.0, 2*PI);
  81.         cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
  82.         cairo_fill(cr);
  83.        
  84.         cairo_arc(cr, 1/4 * Cos(angle), 1/4 * Sin(angle), 1/4, 0.0, 2*PI);
  85.         cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
  86.         cairo_fill(cr);
  87.        
  88.         { load pixels to console }
  89.         console.Load(pixels, width, height, width * 4, format, TPTCPaletteFactory.CreateNew);
  90.  
  91.         { update console }
  92.         console.Update;
  93.       end;
  94.      
  95.       cairo_destroy(cr);
  96.       cairo_surface_destroy(sf);
  97.      
  98.     finally
  99.       { free pixels buffer }
  100.       FreeMem(pixels);
  101.       if Assigned(console) then
  102.         console.close;
  103.     end;
  104.   except
  105.     on error: TPTCError do
  106.       { report error }
  107.       error.report;
  108.   end;
  109. end.




My projects are on Codeberg.

Fred vS

  • Hero Member
  • *****
  • Posts: 3776
    • StrumPract is the musicians best friend
Re: PTCPas & Cairo example
« Reply #17 on: January 11, 2026, 08:23:07 pm »
The result is very satisfying (to my eyes), but the program uses 60% of the CPU. The Smoothris demo uses 80%. Maybe we could make a report to PTCPas author about this.

Hello Roland.

WoW once again.  ;)

But on my Noble Ubuntu system, the program uses 1.9% of the processor, which seems very reasonable to me... (see picture).
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Roland57

  • Hero Member
  • *****
  • Posts: 553
    • msegui.net
Re: PTCPas & Cairo example
« Reply #18 on: January 12, 2026, 07:19:29 am »
But on my Noble Ubuntu system, the program uses 1.9% of the processor

Interesting to know. Thank you for the feedback Fred.
My projects are on Codeberg.

hedgehog

  • Jr. Member
  • **
  • Posts: 82
Re: Jerky animation (windows)
« Reply #19 on: January 26, 2026, 04:21:13 pm »
It looks like Delphi has solved the problem of jerky animation.
They implemented the Display Link Service.

Quote from the Embarcadero website:

Quote
Starting with RAD Studio 13.0, the Display Link Service for FMX is now the central engine for all application animations.
This frame-accurate system leverages the display’s actual refresh rate to synchronize and drive all UI updates and animations. <...> the service ensures that animation processing is tightly aligned with VSync events, resulting in smoother and more consistent visuals across all platforms.
https://docwiki.embarcadero.com/RADStudio/Florence/en/FireMonkey_Animation_Effects

Is it possible to do this in Lazarus?

 

TinyPortal © 2005-2018