{
ptc_cairo_01.pas
Example of animation using PTCPas and Cairo
Based upon <ptcpas>/examples/buffer.pp
}
program BufferExample;
{$MODE objfpc}
uses
ptc, cairo;
var
console: IPTCConsole;
format: IPTCFormat;
width, height: Integer;
pixels: PUint32 = nil;
(*
x, y, r, g, b: Integer;
i: Integer;
*)
sf: pcairo_surface_t;
cr: pcairo_t;
angle: double;
begin
try
try
{ create console }
console := TPTCConsoleFactory.CreateNew;
{ create format }
format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
{ open the console }
console.Open('PTCPas & Cairo example', 480, 480, format);
{ get console dimensions }
width := console.Width;
height := console.Height;
{ allocate a buffer of pixels }
pixels := GetMem(width * height * SizeOf(Uint32));
FillChar(pixels^, width * height * SizeOf(Uint32), 0);
sf := cairo_image_surface_create_for_data(pbyte(pixels), CAIRO_FORMAT_ARGB32, width, height, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width));
cr := cairo_create(sf);
cairo_scale(cr, width, height);
cairo_translate(cr, 1/2, 1/2);
angle := 0;
{ loop until a key is pressed }
while not console.KeyPressed do
begin
angle := angle + PI/500;
(*
{ draw random pixels }
for i := 1 to 100 do
begin
{ get random position }
x := Random(width);
y := Random(height);
{ get random color }
r := Random(256);
g := Random(256);
b := Random(256);
{ draw color [r,g,b] at position [x,y] }
pixels[x + y * width] := (r shl 16) or (g shl 8) or b;
end;
*)
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_paint(cr);
cairo_arc(cr, 0.0, 0.0, 1/2, 0.0, 2*PI);
cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
cairo_fill(cr);
cairo_arc(cr, 1/4 * Cos(angle), 1/4 * Sin(angle), 1/4, 0.0, 2*PI);
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_fill(cr);
{ load pixels to console }
console.Load(pixels, width, height, width * 4, format, TPTCPaletteFactory.CreateNew);
{ update console }
console.Update;
end;
cairo_destroy(cr);
cairo_surface_destroy(sf);
finally
{ free pixels buffer }
FreeMem(pixels);
if Assigned(console) then
console.close;
end;
except
on error: TPTCError do
{ report error }
error.report;
end;
end.