Here is a fun graphical effect. Start a new game project and paste in the following code (remember to keep the unit name). The effect is known as Archimedes spiral, although we play with the numbers to create a strobe like effect.
[sourcecode language=”delphi”]
unit ArchSpiral;
interface
uses w3system, w3components, w3ctrls, w3application,
w3game, w3gameapp, w3graphics, w3components;
type
TApplication = class(TW3CustomGameApplication)
private
{ Private methods }
FaValue: Float;
FbValue: Float;
protected
{ protected methods }
procedure ApplicationStarting; override;
procedure ApplicationClosing; override;
procedure PaintView(Canvas: TW3Canvas); override;
end;
implementation
procedure TApplication.ApplicationStarting;
begin
inherited;
FaValue := 5.0;
FbValue := 1.0;
//Initialize refresh interval, set this to 1 for optimal speed
GameView.Delay:=20;
//Start the redraw-cycle with framecounter active
//Note: the framecounter impacts rendering speed. Disable
//the framerate for maximum speed (false)
GameView.StartSession(true);
end;
procedure TApplication.ApplicationClosing;
begin
GameView.EndSession;
inherited;
end;
// Note: In a real live game you would try to cache as much
// info as you can. Typical tricks are:
// 1: Only get the width/height when resized
// 2: Pre-calculate strings, especially RGB/RGBA values
// 3: Only redraw what has changed, avoid a full repaint
// The code below is just to get you started
procedure TApplication.PaintView(Canvas:TW3Canvas);
var
cx,cy: Float;
i: Integer;
angle: Float;
x,y: Float;
begin
fbValue := fbValue + 0.0009;
// Clear background
Canvas.FillStyle := ‘rgba(0,0,99,0.3)’;
Canvas.FillRectF(0, 0, GameView.Width, GameView.Height);
// Draw our framerate on the screen
Canvas.Font := ’10pt verdana’;
Canvas.FillStyle := ‘rgb(255,255,255)’;
Canvas.FillTextF(‘FPS:’ + IntToStr(GameView.FrameRate),10,20,MAX_INT);
cx := GameView.Width div 2;
cy := GameView.Height div 2;
Canvas.MoveToF(cx,cy);
Canvas.BeginPath;
for i := 0 to 719 do
begin
angle := fbValue * i;
x := cx + (FaValue + FbValue * angle) * cos(angle);
y := cy + (FaValue + FbValue * angle) * sin(angle);
Canvas.LinetoF(x,y);
end;
Canvas.StrokeStyle := ColorToWebStr(clWhite);
Canvas.Stroke;
end;
end.
[/sourcecode]
Demo and code
You can download the project source code here: ArchSpiral.zip
You can find an online demo here: ArchSpiral demo
(Or simply scan the QR code below with your phone)