unit view.main;
{$mode objfpc}{$H+}
interface
(* Copyright ©2026 Benny Christensen a.k.a. cdbc, *)
(* released as PUBLIC DOMAIN - Use at your own risk! *)
uses
Classes, ExtCtrls,StdCtrls,SysUtils, Forms, Controls, Graphics, Dialogs,
cdbc.istreams;
type
{ TfrmMain }
TfrmMain = class(TForm)
Bevel1: TBevel;
btnClose: TButton;
btnMagic: TButton;
gbxResult: TGroupBox;
pnlImg: TPanel;
pnlTop: TPanel;
procedure btnCloseClick(Sender: TObject);
procedure btnMagicClick(Sender: TObject);
procedure pnlImgPaint(Sender: TObject);
private
fArrayImg: array[0..2] of IMemoryStream;
fBmp: TBitmap;
fDrawing: boolean;
procedure DrawByIndex(const anIdx: ptrint);
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
end;
var
frmMain: TfrmMain;
implementation
{$R *.lfm}
resourcestring
rsCaption = '(O)ff (S)creen (P)ainting';
{ TfrmMain }
procedure TfrmMain.btnCloseClick(Sender: TObject);
begin
Close;
end;
procedure TfrmMain.btnMagicClick(Sender: TObject);
const lidx: ptrint = 0; { static variable }
begin
DrawByIndex(lidx);
inc(lidx);
if lidx > 2 then lidx:= 0;
end;
procedure TfrmMain.pnlImgPaint(Sender: TObject);
begin
if fDrawing then exit; { first we check our /lock/, a missed beat doesn't kill us }
pnlImg.Canvas.CopyMode:= cmSrcCopy; { actually the default behaviour }
/// pnlImg.Canvas.StretchDraw(rect(0,0,315,234),fBmp); { fixed image-sizes regardsless of form-size }
{ if the user resizes the form, we scale with the new size :o) }
pnlImg.Canvas.StretchDraw(rect(0,0,pnlImg.Width,pnlImg.Height),fBmp);
end;
procedure TfrmMain.DrawByIndex(const anIdx: ptrint);
begin
fDrawing:= true; { we don't want the canvas blitting while we're drawing }
fBmp.Clear; { not entirely sure, this is necessary, but hey... }
fBmp.LoadFromStream(fArrayImg[anIdx].AsTStream); { preloaded to streamarray }
fArrayImg[anIdx].Position:= 0; { reset after use, or we'll see a black rect }
fDrawing:= false; { release our lock and notify panel about a redraw needed }
pnlImg.Invalidate; { sends a WM_PAINT message to our panel }
end;
procedure TfrmMain.AfterConstruction;
begin
inherited AfterConstruction; { here we'll preload the images - for speed :o) }
fArrayImg[0]:= CreMemStreamFromFile('img'+PathDelim+'316x235_Novigrad_Castle.bmp');
fArrayImg[0].Position:= 0; { muy importante! }
fArrayImg[1]:= CreMemStreamFromFile('img'+PathDelim+'316x235_Novigrad_Sunset.bmp');
fArrayImg[1].Position:= 0; { muy importante! }
fArrayImg[2]:= CreMemStreamFromFile('img'+PathDelim+'316x235_Novigrad_Wauw.bmp');
fArrayImg[2].Position:= 0; { muy importante! }
fBmp:= TBitmap.Create; { create once and reuse :: -much- quicker \o/ }
fBmp.SetSize(316,235); { i've created the photos, so i know :: otherwise... }
Caption:= rsCaption;
end;
procedure TfrmMain.BeforeDestruction;
begin
fArrayImg[0]:= nil; fArrayImg[1]:= nil; fArrayImg[2]:= nil; { com-objects }
fBmp.Clear; fBmp.Free; { class }
inherited BeforeDestruction;
end;
end.