Recent

Author Topic: BGRABitmap - how to switch rendered graphic?  (Read 3553 times)

Dibo

  • Hero Member
  • *****
  • Posts: 1045
BGRABitmap - how to switch rendered graphic?
« on: February 18, 2012, 08:03:43 pm »
Hi,

Where TBGRABitmap store rendered data? Example should explain what I want to get:
Code: Pascal  [Select]
  1. var
  2.   bgra: TBGRABitmap;
  3.   a, b: TRenderedData;
  4. begin
  5.   // Rendering first data
  6.   bgra.TextOut(1, 1, 'Some text', ColorToBGRA(ColorToRGB(clBlack)));
  7.  
  8.   // Move first rendered data to cache
  9.   a := bgra.RenderedData;
  10.  
  11.   // Clear/reset bgrabitmap;
  12.   bgra.Clear;  
  13.  
  14.   // Rendering second data
  15.   bgra.TextOut(1, 1, 'Some another text', ColorToBGRA(ColorToRGB(clBlack)));
  16.  
  17.   // And remember it in cache
  18.   b := bgra.RenderedData;
  19.   b.Clear;
  20.  
  21.   // Now draw first data in canvas
  22.   bgra.RenderedData := a;
  23.   bgra.Draw(Canvas,1,1);
  24.  
  25.   // ... and draw second data
  26.   bgra.RenderedData := b;
  27.   bgra.Draw(Canvas,1,1);
  28. end;
  29.  

Of course this types and methods are fictitious, but they showing what I want to get. I want use ONLY one instance of TBGRABitmap as a drawer and replace data in runtime. Can I somehow store raw rendered data in buffer and switch pointer to it in BGRABitmap?

Regards

circular

  • Hero Member
  • *****
  • Posts: 3047
    • Personal webpage
Re: BGRABitmap - how to switch rendered graphic?
« Reply #1 on: February 19, 2012, 05:16:45 pm »
Well you cannot change the data pointer, because it is registered to the operating system to be a part of memory that contains an image.

What you can do is to copy pixel data to a buffer, and use this buffer, for example :
Code: [Select]
var bgra: BGRABitmap;
   buffer1, buffer2: Pointer;
Begin
   ...
   bgra.TextOut(...)
   
   getmem(buffer1, bgra.NbPixels* sizeof(TBGRAPixel) );
   move(bgra.Data^, buffer1^, bgra.NbPixels* sizeof(TBGRAPixel) );
   
   bgra.Clear;
   bgra.TextOut(...)

   getmem(buffer2, bgra.NbPixels* sizeof(TBGRAPixel) );
   move(bgra.Data^, buffer2^, bgra.NbPixels* sizeof(TBGRAPixel) );

   ...

   move(buffer1^, bgra.Data^, bgra.NbPixels* sizeof(TBGRAPixel) );
   bgra.InvalidateBitmap;
   bgra.Draw(...)

   move(buffer2^, bgra.Data^, bgra.NbPixels* sizeof(TBGRAPixel) );
   bgra.InvalidateBitmap;
   bgra.Draw(...)

   freemem(buffer1);
   freemem(buffer2);
end;

If do not want to copy the data to draw it, you can use the following method :
BGRABitmapDraw(ACanvas: TCanvas; Rect: TRect; AData: Pointer;
  VerticalFlip: boolean; AWidth, AHeight: integer; Opaque: boolean);

So in this case, after creating buffer1 and buffer2, you can draw it like that :
Code: [Select]
BGRABitmapDraw(Canvas, Rect(0,0,bgra.Width,bgra.Height), Buffer1, bgra.LineOrder=riloBottomToTop, bgra.Width,bgra.Height, False);

BGRABitmapDraw(Canvas, Rect(0,0,bgra.Width,bgra.Height), Buffer2, bgra.LineOrder=riloBottomToTop, bgra.Width,bgra.Height, False);

You can also use DataDrawOpaque or DataDrawTransparent function of your bgra instance.
« Last Edit: February 19, 2012, 05:20:18 pm by circular »
Conscience is the debugger of the mind