this slowpoke moves

Draw Snowfall (30fps)

uses ExtCtrls, ComCtrls

type
   pScanArray = ^TScanArray;
   TScanArray = array[0..4095] of integer;

var
  Form1: TForm1;
  Buffer : TBitmap;
  
//

function Max(const A,B : integer) : integer;
begin
  if B > A then
    result := B
  else
    result := A;
end;

function CreateGray(const B : byte) : integer;
begin
  result := B + (B shl 8) + (B shl 16);
end;

procedure FX_Snow(const SnowCount, GroundColor : integer; const ShowScan : boolean; Bitmap : Tbitmap);
var N,
	  X,Y,
	  CB  : integer;
	PSA0,
	PSA1,
	PSA2 : pScanArray;
	XOK1,
	YOK1,
  XOK2,
	YOK2 : boolean;
	Col0,
	Col1,
	Col2 : integer;
begin
  Bitmap.Canvas.Brush.Color := GroundColor;
  Bitmap.Canvas.FillRect(Rect(0,0,Bitmap.Width,Bitmap.Height));
  if SnowCount <= 0 then
     exit;
  for N := 0 to Snowcount do begin
      X := Random(Bitmap.Width);
      Y := Random(Bitmap.Height);
      CB:= Random(256);
      XOK1 := X > 0;
      XOK2 := X < Bitmap.Width-1;
      YOK1 := Y > 0;
      YOK2 := Y < Bitmap.Height-1;
	  
      if (YOK1 and YOK2) and (XOK1 and XOK2) then begin
         Col0 := CreateGray(CB);
         PSA0 := Bitmap.ScanLine[Y];
         PSA0[X]   := Col0;
         if not ShowScan then begin
            if CB > 0 then begin

               Col1 := CreateGray(max(CB-(CB div 5),0));
               Col2 := CreateGRay(max(CB-(CB div 3),0));
            end else begin
               Col1 := 0;
               Col2 := 0;
            end;

            PSA1 := Bitmap.ScanLine[Y-1];
            PSA2 := Bitmap.ScanLine[Y+1];
            PSA1[X-1] := Col2;
            PSA1[X]   := Col1;
            PSA1[X+1] := Col2;
            PSA0[X-1] := Col1;
            PSA0[X+1] := Col1;
            PSA2[X-1] := Col2;
            PSA2[X]   := Col1;
            PSA2[X+1] := Col2;
         end;
      end;
   end;
   if ShowScan then
      for Y := 0 to Bitmap.height-1 do
	      
          if (Y mod 2) = 0 then begin
             PSA0 := Bitmap.ScanLine[Y];
             for X := 0 to Bitmap.Width-1 do
                 PSA0[X] := GroundColor;
          end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
  Buffer             := Tbitmap.Create;
  Buffer.PixelFormat := pf32bit;
  Buffer.Width       := PaintBox1.Width+8;
  Buffer.Height      := PaintBox1.Height+8;
  Timer1.Enabled  := true;
end;

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

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Buffer.Width  := PaintBox1.Width+8;
  Buffer.Height := PaintBox1.Height+8;
  FX_Snow(TrackBar1.Position shl 2, $121212, CheckBox1.Checked, Buffer);
  PaintBox1.Canvas.Draw(-4,-4,Buffer);
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  Label2.Caption := Format('Bruit (%d) :',[TrackBar1.Position shl 2]);
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  case ComboBox1.ItemIndex of
    0 : Timer1.Interval := 1000 div 12; 
    1 : Timer1.Interval := 1000 div 20; 
    2 : Timer1.Interval := 1000 div 23; 
    3 : Timer1.Interval := 1000 div 25; 
    4 : Timer1.Interval := 1000 div 30; 
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate