[fpc-pascal] AggPas / PTCGraph dynamic memory allocation

James Richters james at productionautomation.net
Sat Nov 11 02:48:43 CET 2017

I've been trying to figure out how to dynamically allocate memory to be used as a screen buffer to display AggPas generated images with PTCGraph.     I don't want it hard coded because I do not know what resolution monitor is going to be used.   Here is where I am at:  

I can define a static variable like this:
   RGB_Width = 2;
TGraphBitmapBuffer=packed record
  reserved:  LongInt; 
  data: 	array[0.. (Buffer_Width+1) * (Buffer_Height+1)] of word;   // I don't know why I need the +1's but it won't work without them

   graph_buffer: TGraphBitmapBuffer;

Then I attach it to AggPas with:
  agg^.attach(@graph_buffer.data, graph_buffer.width, graph_buffer.height, - graph_buffer.width * RGB_Width);

Then I put it on the screen with:

I should point out that I like to do  this:
Original_screen:=graph_buffer;  to save my screen then put new stuff on it,  then just copy back the original and putimage it back when I'm done.  So I need a solution that will allow me to do this.  I also don't always use the whole screen,  sometimes I just want to getimage a little area, and putimage that back... but I don't know the size of these areas... they are calculated based on the resolution of the monitor and various settings and configurations.

Here is where I am at with my attempt to make the memory dynamically allocated:

I can use a dynamic method with PTCGraph with getimage and putimage
But if I allocate memory this way, while it works fine with getimage and putimage, I can't figure out how to use it with AggPas because the first 3 words are not part of the bitmap data...  It seems like there should be a way to make this work but the solution eludes me.
I've been trying to come up with a way to do *something* like this:

   graph_buffer: Pointer;
  Max_X, Max_Y: Word;

Max_X:=1920;  //these will be detected by PTCGraph... 
Size := ImageSize(0, 0, Max_X-1, Max_Y-1);
GetMem(graph_buffer, Size);   
GetImage(0, 0, Max_X-1, Max_Y-1, graph_buffer^);

agg^.attach(@graph_buffer[12], Max_X-1,Max_Y-1, -(Max_X-1) * RGB_Width);  //graph_buffer[12] is to skip over the 3 longints 4 bytes each


The problem is with Agg^.attac()  of course now graph_buffer isn't an array anymore...  so what I have won't work and I haven' figured out how to attach AggPas  at a position where the bitmap data is so I can use PutImage and still have the Longints that were put in by GetImage.

Any advice is greatly appreciated


More information about the fpc-pascal mailing list