Recent

Author Topic: Cross platform graphics  (Read 6553 times)

jbreits

  • New Member
  • *
  • Posts: 15
    • http://www.breittechnologies.com
Cross platform graphics
« on: February 07, 2007, 10:40:46 pm »
I'm trying to use the AggPas library to do some graphics stuff.  The files and examples all compile fine within FPC and Lazarus (if {$mode delphi} is on).  However, the AggPas example projects are not anything close to how I would write an application in Lazarus.  I was hoping to encapsulate some or all of the AggPas code into a component that would be much more user friendly to us Lazarus users.  

However, I have some questions about what I can do as far as keeping it platform neutral.

In one of the examples showing how to integrate the AggPas code with a Delphi form there is some code like this:

Code: [Select]

var
  mem_dc      : HDC;
  buf         : pointer;
  bmp,
  temp        : HBitmap;
   bmp_info    : TBitmapInfo;    

....
  mem_dc:=CreateCompatibleDC(Canvas.Handle);

  bmp_info.bmiHeader.biSize  :=sizeof(TBITMAPINFOHEADER );
  bmp_info.bmiHeader.biWidth :=Width;
  bmp_info.bmiHeader.biHeight:=Height;
  bmp_info.bmiHeader.biPlanes:=1;

  bmp_info.bmiHeader.biBitCount     :=32;
  bmp_info.bmiHeader.biCompression  :=BI_RGB;
  bmp_info.bmiHeader.biSizeImage    :=0;
  bmp_info.bmiHeader.biXPelsPerMeter:=0;
  bmp_info.bmiHeader.biYPelsPerMeter:=0;
  bmp_info.bmiHeader.biClrUsed      :=0;
  bmp_info.bmiHeader.biClrImportant :=0;

  buf:=NIL;
  bmp:=CreateDIBSection (mem_dc, bmp_info, DIB_RGB_COLORS, buf, 0, 0);
  temp:=SelectObject(mem_dc ,bmp );



It then goes on to use the Agg redering code as normal.  It copmiles and runs fine on my XP machine, but I'm wondering how much of this is platform specific.  Can HDC and CreateCompatibleDC be used, or are they specific to Windows? The Developing With Graphics page seems to show that DCs can be used.

What about CreateDIBSection and SelectObject?  Those look like WinAPI calls to me.

Any thoughts?

JBreits[/url]
--
JBreits

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Cross platform graphics
« Reply #1 on: February 08, 2007, 04:46:01 pm »
Quote from: "jbreits"
It copmiles and runs fine on my XP machine, but I'm wondering how much of this is platform specific.  Can HDC and CreateCompatibleDC be used, or are they specific to Windows?


CreateCompatibleDC, CreateDIBSection and SelectObject are Windows API calls. To make integrating Delphi code easier, lazarus provides cross-platform implementations of several winapi functions, including CreateCompatibleDC and SelectObject, but not CreateDIBSection AFAIK

That implementation is a simple call to a winapi on windows, and on gtk, qt, etc, it's implemented using the native api.

Nevertheless, it's not a perfect implementation, and IMHO you should avoid using this, and instead use LCL components, which would be TCanvas, TBitmap, TFont, TBrush, etc, for drawing.

It's easy to see if your code is cross-platform, just don't use the Windows unit, which only exists on windows. If you need winapi functions add the LclIntf unit instead.

Of course you can write platform specific code that is enclosed by a IFDEF Windows

jbreits

  • New Member
  • *
  • Posts: 15
    • http://www.breittechnologies.com
Cross platform graphics
« Reply #2 on: February 08, 2007, 11:09:43 pm »
Thanks for the reply.  I was able change the code so now I am using just Lazarus stuff (Winows not in the uses).  The AGGPAS library will render everything to a simple memory location (I used an array).  But I now need to get that array onto a TCanvas quickly.  It works fine to loop through each pixel and do Canvas.Pixels[x,y] := MyColor;  But this is really really slow.  

Is there anyway to copy the entire array of values into the canvas's pixels?  Or better yet, is there a way I could get a pointer to the canvas's pixels array so I could have AGGPAS render directly to that?

Thanks,
JBreits
--
JBreits

OnixJr

  • Full Member
  • ***
  • Posts: 172
    • http://www.brlazarus.kit.net
Cross platform graphics
« Reply #3 on: February 09, 2007, 04:42:58 am »
Quote
lazarus provides cross-platform implementations of several winapi functions


sekel: where are this "cross-plataform implementations"? in some unit in special?

thx...
Portal Lazarus Brasil - http://lazaruspascal.codigolivre.org.br/portal.php
Lazarus BOOK (in portuguese) - http://lazarus-book.blogspot.com
<hipernetjr@yahoo.com.br> - Curitiba/Brazil

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Cross platform graphics
« Reply #4 on: February 09, 2007, 11:55:03 am »
Quote from: "jbreits"

Is there anyway to copy the entire array of values into the canvas's pixels?  Or better yet, is there a way I could get a pointer to the canvas's pixels array so I could have AGGPAS render directly to that?


I've done somthing in this area.
http://www.theo.ch/lazarus/op1.zip
Hope it helps a bit.
Or read here: http://wiki.lazarus.freepascal.org/Developing_with_Graphics

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Cross platform graphics
« Reply #5 on: February 09, 2007, 03:17:38 pm »
Quote from: "OnixJr"
sekel: where are this "cross-plataform implementations"? in some unit in special?


Two units: LCLIntf and LCLType

those units will call the widgetset specific code, which for gtk is on:

lazarus/lcl/interfaces/gtk/gtkwinapi.inc

and several other units, like gtkwinapih.inc

And similarly for win32, qt, etc, etc

 

TinyPortal © 2005-2018