Tutorials

Andorra 2D - Compiling with Lazarus

Every beginning is difficult
To compile Andorra 2D with Lazarus, some particular issues have to be attended. Once those are taken care of, nothing stands in the way to use Andorra 2D even in Linux. This tutorial requires a working version of Lazarus as well as some knowledge in using it. Therefore, if any problems should occur with Lazarus that are not directly related to Andorra 2D (such as "Lazarus complains that 'Unit Interfaces could not be found', what should I do?"), please do not address them to me. Honest as I am, I have to admit I have not been working with Lazarus a lot myself, so if you have any more errors or suggestions for improvements, do not hesitate to inform me of them.


Compiling the plugin
The first difficulty to overcome is the fact that plugin DLLs compiled with Delphi are not compatible with Lazarus, which is quite logical as the internal Delphi compiler and the Free Pascal Compiler (which is used by Lazarus) are two different programmes, which were developed independently from one another. As a consequence of this, the class structure created by Free Pascal has become incompatible to Delphi's, but Andorra 2D exchanges objects between the library and the host application, which inevitably results in access violations.
We avoid this problem simply by recompiling the plugin library with Lazarus. For this, we open the file "AndorraOGL.lpi" in the directory "src/dll/opengl/" with Lazarus. However, before we can simply compile this file, we have to set up the search paths and the output directories accordingly.
To do this, we enter the path to the OpenGL header and the path to the Andorra 2D surce files via "Project->Compiler settings->Paths->Other unit files->...". If there should be any problems with unit files that cannot be found, just adding the "path templates" can help. Especially in Linux Andorra 2D only wanted to compile with those.

Apart from this, "../../dcu/" should be entered in "unit output path" in the paths dialogue and "../../../bin/" in "Project->Project settings->Target file name". Now we can compile the library with a click on "Start->Recompile all". If you are lucky enough to get the message "Project 'AndorraOGLLaz' built successfully. ;)", you should also make sure that the plugin was really recompiled. FOr this, take a look at the target directory and check the change date of "AndorraOGLLaz.dll".
As you may have noticed, the file name of the Lazarus DLL differs from that of the Delphi DLL to avoid confusion.
If everything went well until now, there is not much in the way of taking a trip to the world of Andorra 2D with the help of Lazarus now.


Our Lazarus Andorra 2D project
Now we want to create a simple Andorra 2D project with Lazarus. For that, we create a new "Application" project with "File->New" and add the Andorra 2D search path just like we did when compiling the OpenGL plugin. Also, it might be necessary to add the package "LazOpenGLContext" to your project when using another widget set than "Win32/Win64". (in Linux, for example)

About windows systems and device contexts
This necessity has the following background: When using the normal Windows widget set, Andorra 2D can also forward a handle (the inventory number of our window, in a manner of speaking) to the plugin library. After that, the plugin handles creating the according device context itself. (Which is done with a few lines in Windows) This is the area our graphics card or the used window graphics system can draw on. However, the device context has to be managed by the window system in use. In Linux, a multitude of window systems exists. (some of which can be selected as a widget set in Lazarus) Now the plugin would need to have a context creation routine for each of these systems, which would of course be an enormous effort. Luckily, the busy developers of Lazarus have thought of something for that, too: The component "TOpenGLControl", which creates a device context adapted to our selected window system and operating system for our application, which the plugin can draw on directly. Alternative windows frameworks, such as the GLFW framework work the same way. There is more information about this in the tutorial "About the Andorra 2D window frameworks".


Some code...
Now we just need some code to test Andorra 2D with Lazarus:

Delphi-Code:
procedure TForm1.FormCreate(Sender: TObject);
begin
  AdDraw := TAdDraw.Create(self);
  AdDraw.DllName := 'AndorraOGLLaz.dll';
  Application.OnIdle := @Idle;
  AdDraw.Initialize;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  AdDraw.Free;
end;

procedure TForm1.Idle(Sender: TObject; var Done:boolean);
begin
  AdDraw.ClearSurface(0);
  AdDraw.BeginScene;
  with AdDraw.Canvas do
  begin
    Font := AdDraw.Fonts.GenerateFont('Arial', 24, []);
    TextOut(0,0,'Andorra 2D...');
    Font := AdDraw.Fonts.GenerateFont('Arial', 10, []);
    TextOut(0,30,'...works with Lazarus too');
  end;
  AdDraw.EndScene;
  AdDraw.Flip;
end;

To prevent problems, it has proven useful to replace the line

Delphi-Code:
{$mode objfpc}{$H+}

at the beginning of the unit with this one:

Delphi-Code:
{$MODE DELPHI}

Copyright and licence
(c) by Andreas Stöckel, February 2008
Translation by Manuel Eberl

The content of this tutorial subjects to the GNU Licence for Free Documentation.


This page was generated with the help of the following PHP-Scripts: GeSHi a free PHP Syntax highlighter and StringParser_BBCode a free BBCode Parser.