LCL Start - MrShoor/AvalancheProject GitHub Wiki
In order to make a project in LCL, one would need to download Avalanche and AvalancheUtils packages. They should be compiled (no need to rebuild Lazarus IDE) and used by (added to list of Used Packages) of the project.
Avalanche only supports Windows at this point.
Start
Create a New Application project. A project with a single form is what we need.
Open "Project Options" and select "Application".
On the right side find "For Windows" section and uncheck "Use manifest file to enable themes"
Theme drawing might come in conflict with GAPI drawing (especially OpenGL)
Units to be used
The following units needs to be added to uses clause
uses
...
,avTypes, avRes, mutils;
- avTypes - in case, apiDX11 is desired to be used
- avRes - this is where TavMainRender
- mutils - for Vec() function and TVec4 declaration (in the color related code below)
Initialization and Release
Declare a TavMainRender object. In this example it would be a field in TForm.
TForm1 = class(TForm)
...
public
{ public declarations }
FMain : TavMainRender; // doesn't have to be a member of TForm, but ok for this example
end;
Create both FormCreate and FormDestroy events and assign their bodies as following.
procedure TForm1.FormCreate(Sender: TObject);
begin
// TavMainRender doesn't follow FPC, TComponent structure. Thus the parent passed should be nil (in this case)
FMain := TavMainRender.Create(Nil); // creating the object
FMain.Window := Self.Handle; // assigning system Window handle. (it should be created by this time
FMain.Init3D(); // initializing 3D GAPI. By default it would select OpenGL
// you can call it like this: FMain.Init3D(apiDX11); for Direct X 11
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FMain.Free; // release and clean
FMain:=nil;
end;
Rendering
of emptiness. As mention earlier Paint event would be used, thus it's a good time to assign FormPaint event.
procedure TForm1.FormPaint(Sender: TObject);
begin
if not Assigned(fMain) then Exit; // in case paint event comes before or after FMain is assigned
FMain.Bind; // binding the rendering context
try
FMain.Present; // swapping the buffers, presenting the contents (which is now blank)
// to the window
finally
FMain.Unbind; // releasing the rendering context
end;
end;
Rendering Emptiness of Color
TavMainRender provide Clear method, to fill out the current rendering target (in our case it's the backbuffer) if a certain color. It also allows to clear other buffers (such as depth buffer, etc..). In this example we simply want to fill out the back buffer with a certain color, before showing it on the screen.
The first parameter accepts TVec4 describing the color. The components of the vector describe color components in the following order: R(ed),G(reen), B(lue) and A(lpha). There's a utility function available Vec() (with many overloaded options to pass parameters)
We add the Clear() method call right before the call to Present() method.
procedure TForm1.FormPaint(Sender: TObject);
begin
if not Assigned(fMain) then Exit;
FMain.Bind;
try
FMain.Clear( Vec( 0, 0.75, 0.5, 1)); // Red, Green, Blue and Alpha resulting in greenish fill color
FMain.Present;
finally
FMain.Unbind;
end;
end;