program DVDScreensaver;
{$WARN 5027 off : Local variable "$1" is assigned but never used}
uses
raylib, math;
const
ScreenWidth = 800;
ScreenHeight = 600;
CellW = 4;
CellH = 4;
SpaceX = CellW + 4;
SpaceY = CellH + 4;
Effet_Amp = 4;
var
tt: Single = 0;
tex: TTexture2D;
running: Boolean = True;
gridWidth, gridHeight: Integer;
posX, posY: Single;
speedX, speedY: Single;
currentColor: TColor;
rotation: Single;
logoSize: Single;
procedure LoadResources;
begin
if FileExists('image2.png') then
begin
tex := LoadTexture('image2.png');
gridWidth := tex.width div CellW;
gridHeight := tex.height div CellH;
posX := 0;//ScreenWidth / 2;
posY := 0;//ScreenHeight / 2;
speedX := 120;
speedY := 120;
currentColor := WHITE;
rotation := 0;
logoSize := 1.0;
end
else
begin
TraceLog(LOG_WARNING, 'Texture file image2.png not found');
running := False;
end;
end;
procedure UnloadResources;
begin
UnloadTexture(tex);
end;
function GetRandomColor: TColor;
begin
case GetRandomValue(0, 6) of
0: Result := RED;
1: Result := GREEN;
2: Result := BLUE;
3: Result := YELLOW;
4: Result := PURPLE;
5: Result := ORANGE;
else Result := SKYBLUE;
end;
end;
procedure UpdateDVDLogo(dt: Single);
begin
posX := posX + speedX * dt;
posY := posY + speedY * dt;
if (posX < 0) or (posX > ScreenWidth - gridWidth * SpaceX) then
begin
speedX := -speedX;
currentColor := GetRandomColor;
end;
if (posY < 0) or (posY > ScreenHeight - gridHeight * SpaceY) then
begin
speedY := -speedY;
currentColor := GetRandomColor;
end;
rotation := rotation + 20 * dt;
if rotation > 360 then rotation := rotation - 360;
logoSize := 0.9 + 0.1 * Sin(tt * 2);
end;
procedure DrawDVDLogo;
var
x, y: Integer;
x0, y0: Single;
SourceRect, DestRect: TRectangle;
origin: TVector2;
begin
origin.x := (gridWidth * SpaceX) / 2;
origin.y := (gridHeight * SpaceY) / 2;
for x := 0 to gridWidth - 1 do
for y := 0 to gridHeight - 1 do
begin
x0 := posX + x * SpaceX + Effet_Amp * Cos(4 * tt + 0.3 * y);
y0 := posY + y * SpaceY + (x / 6 + 2) * Sin(3 * tt + 0.2 * x);
SourceRect := RectangleCreate(
(x * CellW) mod tex.width,
(y * CellH) mod tex.height,
CellW,
CellH
);
DestRect := RectangleCreate(
x0,
y0,
CellW * logoSize,
CellH * logoSize
);
DrawTexturePro(
tex,
SourceRect,
DestRect,
Vector2Create(CellW/2 * logoSize, CellH/2 * logoSize),
rotation,
currentColor
);
end;
end;
procedure UpdateAndDraw;
var
dt: Single;
begin
dt := GetFrameTime();
tt += 0.025;
UpdateDVDLogo(dt);
ClearBackground(DARKGRAY);
DrawDVDLogo;
DrawText(TextFormat('Position: %.1f, %.1f', posX, posY), 10, 10, 20, LIGHTGRAY);
DrawText(TextFormat('Speed: %.1f, %.1f', speedX, speedY), 10, 30, 20, LIGHTGRAY);
end;
begin
InitWindow(ScreenWidth, ScreenHeight, 'DVD Logo Animation');
SetTargetFPS(60);
SetRandomSeed(Trunc(GetTime()*1000));
LoadResources;
while running and not WindowShouldClose do
begin
BeginDrawing;
UpdateAndDraw;
EndDrawing;
end;
UnloadResources;
CloseWindow;
end.