Ca introducere va pot spune ca, in amintirea vremurilor din liceu cand programam in Pascal in modul 13h, m-am decis sa-mi programez un mic foc (la care sa ma incalzesc de Craciun
) in Delphi.Singura problema e ca accesul la pixelii unui Canvas este destul de incet, motiv pentru care m-am multumit cu un foc mic (100x100 or so), pana cand mi-am adus aminte de ScanLines ... si atunci am ajuns la concluzia ca un foc mai serios n-ar strica ... ScanLines FTW

Ceva in genul asta, mai exact


Sursa programului (doar fisierul .PAS) arata in felul urmator:
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, ExtCtrls, StdCtrls, ShellAPI;
- type
- TRGB32 = record
- B, G, R, A: Byte;
- end;
- TRGB32Array = packed array[0 .. MaxInt div SizeOf(TRGB32)-1] of TRGB32;
- PRGB32Array = ^TRGB32Array;
- TForm1 = class(TForm)
- imgFire: TImage;
- Timer1: TTimer;
- Button1: TButton;
- Panel1: TPanel;
- procedure Timer1Timer(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure Panel1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- procedure ALittleSomething;
- procedure InitLowerLinesScanlines;
- procedure DrawFlameScanlines;
- end;
- var
- Form1: TForm1;
- lBmp: TBitmap;
- implementation
- {$R *.dfm}
- procedure TForm1.InitLowerLinesScanlines;
- var
- i, j: Integer;
- Line: PRGB32Array;
- begin
- for j := imgFire.Height - 4 to imgFire.Height - 1 do
- begin
- Line := imgFire.Picture.Bitmap.ScanLine[j];
- for i := 0 to imgFire.Width - 1 do
- begin
- Line[i].B := Random(100) + 40;
- Line[i].G := Random(100) + 60;
- Line[i].R := Random(50) + 200;
- Line[i].A := 0;
- end;
- end;
- imgFire.Invalidate;
- end;
- procedure TForm1.DrawFlameScanlines;
- var
- i, j: Integer;
- Line, bLine, aLine: PRGB32Array;
- begin
- for j := imgFire.Height - 3 downto 0 do
- begin
- Line := imgFire.Picture.Bitmap.ScanLine[j];
- bLine := imgFire.Picture.Bitmap.ScanLine[j + 1];
- if (j > 10) then
- aLine := imgFire.Picture.Bitmap.ScanLine[j - 1];
- for i := 0 to imgFire.Width - 1 do
- begin
- Line[i].B := (Line[i - 2].B + Line[i + 2].B + bLine[i].B) div 3;
- if (Line[i].B > 5) then
- Dec(Line[i].B, Random(4));
- Line[i].G := (Line[i - 2].G + Line[i + 2].G + bLine[i].G) div 3;
- if (Line[i].G > 5) then
- Dec(Line[i].G, Random(3));
- Line[i].R := (Line[i - 1].R + Line[i + 1].R + bLine[i].R) div 3;
- if (Line[i].R > 5) then
- Dec(Line[i].R, Random(2));
- if ((i * j) mod (Random(50) + 5) = 0) then
- begin
- if (Line[i].B < 200) then
- Inc(Line[i].B, Random(10));
- if (Line[i].G < 200) then
- Inc(Line[i].G, Random(10));
- if (Line[i].R < 200) then
- Inc(Line[i].R, Random(10));
- end;
- if ((i + j) mod (Random(2) + 2) = 0) and (j > 10) then
- begin
- aLine[i] := Line[i];
- end;
- end;
- end;
- end;
- procedure TForm1.Timer1Timer(Sender: TObject);
- begin
- DrawFlameScanlines;
- InitLowerLinesScanlines;
- Randomize;
- end;
- procedure TForm1.ALittleSomething;
- var
- x,y : Integer;
- Line : PRGB32Array;
- begin
- with imgFire.Picture.Bitmap do
- begin
- PixelFormat := pf32bit;
- Width := imgFire.Width;
- Height := imgFire.Height;
- for y := 0 to Height - 1 do
- begin
- Line := Scanline[y];
- for x := 0 to Width - 1 do
- begin
- Line[x].B := 0;
- Line[x].G := 0;
- Line[x].R := x xor y;
- Line[x].A := 0;
- end;
- end;
- end;
- imgFire.Invalidate;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Randomize;
- Panel1.Color := Self.Color;
- Self.ClientWidth := imgFire.Left + imgFire.Width + imgFire.Left;
- Self.ClientHeight := imgFire.Top + imgFire.Height + imgFire.Left div 2;
- ALittleSomething;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- Timer1.Enabled := True;
- end;
- procedure TForm1.Panel1Click(Sender: TObject);
- begin
- ShellExecute(0, 'open', PAnsiChar('http://www.bitcell.info'), nil, nil, 0);
- end;
- end.
Executabilul si sursele complete le puteti downloada de mai jos.
Bafta & happy holiday programming !
Welcome to BitCell. Click here to register !
L-ai putea face sa mearga si in full-screen? Ar fi interesant de vazut daca ar merge OK si asa...

