[fpc-pascal] Implementing AggPas with PtcGraph

Stefan V. Pantazi svpantazi at gmail.com
Sat Jun 3 03:16:31 CEST 2017


I literally just sent a reply with the solution. I had to guess the 
width and height were longints. Your message confirmed it. I attached an 
updated version of the test program, the old one had a few typos.

Thank you for your work on the ptcpas, I found it very useful for my 
projects.

On 06/02/2017 08:39 PM, Nikolay Nikolov wrote:
>
>
> On 05/31/2017 02:51 PM, James Richters wrote:
>> I was doing some tests with Putpixel and that seems to be a word in
>> the format of RGBA with 4 bits each.    I would think putimage would
>> use the same format, but I haven't tested that yet.
>>
>> I'm still a bit confused by putimage, since it only has an X and Y
>> startpoint, how do you define the height and width of the bitmap?
>> Getimage() specifies a rectangle, and imagesize() figures out how much
>> memory you need, but I just don't what defines the size and shape of
>> the image to putimage.
> The structure, used by putimage is as follows:
>
> 3 longints (12 bytes):
> - image width
> - image height
> - reserved
>
> followed by width*height 16-bit words. If you're a using a 16-bit color
> mode (that's the highest supported - it's a limitation of the graph unit
> include files, which ptcgraph reuses from the fpc sources), the color
> format is rgb565.
>
> Nikolay
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
-------------- next part --------------
{
  This is a console application demo. It uses the Agg2D object,
  which has a much friendlier API, to do all the drawing.
  The drawing buffer is then displayed in a window using ptcgraph and 16 bit RGB565 color format.

  Requires modification of Agg2D object constructor to use the 16 bit RGB565 color format.
  Uses a GraphBitmapBuffer type that includes contains the width and height of the image per the info at this URL:

http://pascal.net.ru/PutImage+(en)
[...]
"BitMap is an untyped parameter that contains the height and width of the region, and the bit image that will be put onto the screen."
[...]
}

program console_aggpas_2;

{$mode objfpc}{$H+}

uses
  ptcgraph,
  ptccrt,
  agg_2D,
  agg_basics;

const
  IMAGE_WIDTH = 800;
  IMAGE_HEIGHT = 600;
  RGBA_WIDTH =2; //16bit 565 format
  LINE_COUNT = 30;
  {$IFDEF Unix}
  FontFile = '../../arial.ttf';
  {$ENDIF}
  {$IFDEF Windows}
  FontFile = 'Arial';
  {$ENDIF}

type

TGraphBitmapBuffer=packed record
  width,
  height:	longint;
  data: 	array[0..2*IMAGE_WIDTH*IMAGE_HEIGHT-1] of byte;
end;

var
  gd,gm : smallint;
  graph_buffer: TGraphBitmapBuffer;

procedure DrawStuff(agg: Agg2D_ptr);
var
  i: Integer;
  x, y, px, py, d: Double;
  c1, c2: Color;
begin
  // draw a full screen graph with grid
  agg^.clearAll(0, 0, 0);
  agg^.lineColor(0, 0, 0, 255);
  agg^.lineWidth(3);
  agg^.rectangle(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
  agg^.lineWidth(1);
  agg^.lineColor(0, 155, 0, 255);
  agg^.rectangle(10, 10, 50, 50);
//  agg^.font(fontfile, 16);
  d := IMAGE_WIDTH / LINE_COUNT;
  agg^.lineColor(0, 0, 0, 100);
  agg^.lineWidth(1);
  for i := 1 to LINE_COUNT - 1 do
  begin
    x := i * d;
    agg^.line(x, 0, x, IMAGE_HEIGHT);
  end;
  for i := 1 to trunc(IMAGE_HEIGHT / d) do
  begin
    y := i * d;
    agg^.line(0, y, IMAGE_WIDTH, y);
  end;
  x := 0;
  y := IMAGE_HEIGHT / 2;
  px := x;
  py := y;
  agg^.lineColor(255, 0, 0, 200);
  agg^.fillColor(0, 0, 0, 200);
  agg^.lineWidth(1);
  for i := 0 to LINE_COUNT - 1 do
  begin
    x := x + d;
    y := y + Random(Round(IMAGE_HEIGHT / 3)) - IMAGE_HEIGHT / 6;
    if y < 0 then
      y := IMAGE_HEIGHT / 6;
    if y >= IMAGE_HEIGHT then
      y := IMAGE_HEIGHT - IMAGE_HEIGHT / 6;
    agg^.line(px, py, x, y);
//    agg^.text(x, y, char_ptr(IntToStr(i) + ' point'));
    px := x;
    py := y;
  end;

  // Star shape
  agg^.LineCap(CapRound);
  agg^.LineWidth(5);
  agg^.LineColor($32 ,$cd ,$32 );
  c1.Construct(0, 0 , 255, 200);
  c2.Construct(0, 0, 255, 50);
  agg^.FillLinearGradient(100, 100, 150, 150, c1, c2);
  agg^.Star(100 ,150 ,30 ,70 ,55 ,5 );

  // Draw Arc from 45 degrees to 270 degrees
  agg^.LineColor($4C, $6C, $9C);
  agg^.LineWidth(5 );
  agg^.Arc(300 ,320 ,80 ,50 ,Deg2Rad(45 ) ,Deg2Rad(270 ) );
end;


procedure DrawAndDisplay;
var
  agg: Agg2D_ptr;
begin
//agg draw
  New(agg, Construct);
  agg^.attach(@graph_buffer.data, IMAGE_WIDTH, IMAGE_HEIGHT, -(IMAGE_WIDTH * RGBA_WIDTH));
  DrawStuff(agg);
  Dispose(agg, Destruct); // not necessary to keep it after rendering is finished

//display on ptc surface
	graph_buffer.width:=IMAGE_WIDTH;
	graph_buffer.height:=IMAGE_HEIGHT;
  ptcgraph.PutImage(0,0,graph_buffer,NormalPut);
  ptcgraph.Rectangle(10,10,100,100);
  ptcgraph.PieSlice(100,100,0,25,30);
  ptcgraph.OutTextXY(80,80,'It works!');
end;


begin
  gd:=d16Bit;
  gm:=m800x600;
  //Windowtitle:='ptcgraph';
  ptcgraph.Initgraph(gd,gm,'');
  Randomize;
  DrawAndDisplay;
  ReadKey;
end.



More information about the fpc-pascal mailing list