1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| type
TDisplayEngine = class(TObject)
private
FActive: Boolean;
function SwitchBitmap(NewBitmap: TBitmap): TBitmap;
function GetDrawBitmap: TBitmap;
public
class procedure DrawTo(ACanvas: TCanvas);
property Active: Boolean read FActive;
property DrawBitmap: TBitmap read GetDrawBitmap;
end;
var
_DisplayEngine: TDisplayEngine = nil; // Créer dans TDisplayEngine.Start, c'est une sorte de singleton car on ne peut pas instancier TDisplayEngine directement, on n'utilise que les méthodes de classes
class procedure TDisplayEngine.DrawTo(ACanvas: TCanvas);
begin
if Assigned(_DisplayEngine) and _DisplayEngine.Active then
begin
ACanvas.Draw(0, 0, _DisplayEngine.DrawBitmap);
_DisplayEngine.DrawBitmap.Free(); // On Libère l'image utilisée
end;
end;
function TDisplayEngine.SwitchBitmap(NewBitmap: TBitmap): TBitmap;
begin
FDrawBitmapLock.Acquire();
try
Result := FDrawBitmap;
FDrawBitmap := NewBitmap;
finally
FDrawBitmapLock.Release();
end;
end;
function TDisplayEngine.GetDrawBitmap: TBitmap;
begin
FDrawBitmapLock.Acquire();
try
Result := FDrawBitmap;
FDrawBitmap := nil;
finally
FDrawBitmapLock.Release();
end;
end;
procedure TDisplayEngine.TreatActionDraw;
var
DrawingBitmap: TBitmap;
iCol, iRow: Integer;
begin
DrawingBitmap := TBitmap.Create();
DrawingBitmap.Height := _DisplayEngine.FHeight;
DrawingBitmap.Width := _DisplayEngine.FWidth;
DrawingBitmap.BlaBlaBla ... DrawLine, Rect, FillRect, CopyRect, ...
_DisplayEngine.SwitchBitmap(DrawingBitmap).Free(); // Cela libère l'ancienne image, une nouvelle prend a pris sa place, si elle n'a pas été utilisé entre temps ...
end;
procedure TDisplayEngine.Execute;
var
Action: TDisplayAction;
begin
while not Terminated do
begin
Action := PopAction();
case Action.ActionType of
tatNone : TreatActionNone();
tatDraw: TreatActionDraw();
...
end;
Sleep(1);
end;
end; |
Partager