Skip to content

Commit

Permalink
Added loading screen to sprites example
Browse files Browse the repository at this point in the history
  • Loading branch information
sysrpl committed Feb 12, 2015
1 parent 28b984b commit a3aa7e3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/sprites/sprites.cat.pas
Expand Up @@ -43,7 +43,7 @@ constructor TRunningCat.Create(World: TWorld);
begin
Textures := TTextures.Create;
Textures.Generate(1);
Textures.Load(WebGet(CatFile), TexCat);
Textures.Load(WebLoad(CatFile), TexCat);
end;
Inc(TexturesCount);
Texture := Textures[TexCat];
Expand Down
4 changes: 3 additions & 1 deletion examples/sprites/sprites.lpi
Expand Up @@ -19,6 +19,9 @@
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
Expand All @@ -43,7 +46,6 @@
<Unit1>
<Filename Value="sprites.cat.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="Sprites.Cat"/>
</Unit1>
<Unit2>
<Filename Value="sprites.web.pas"/>
Expand Down
41 changes: 35 additions & 6 deletions examples/sprites/sprites.lpr
Expand Up @@ -10,6 +10,7 @@
Bare.Geometry,
Bare.Graphics,
Bare.Example,
Bare.Networking.Web,
Bare.Interop.OpenGL,
Sprites.Web,
Sprites.Cat;
Expand All @@ -27,6 +28,10 @@ TSpriteWindow = class(TWorldWindow)
FRunningCat: TRunningCat;
FShow3D: Boolean;
FShowStart: Float;
FUrl: string;
FUrlCounter: Integer;
procedure WebProgress(Sender: TObject; var Args: TWebProgressArgs);
function WebDownload(const Url: string): TStream;
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure RenderInitialize; override;
Expand All @@ -46,6 +51,28 @@ procedure TSpriteWindow.CreateParams(var Params: TCreateParams);
Params.Multithreaded := True;
end;

procedure TSpriteWindow.WebProgress(Sender: TObject; var Args: TWebProgressArgs);
begin
{ To to increase download performance we only print progress every four notifications }
Inc(FUrlCounter);
if FUrlCounter mod 4 <> 0 then
Exit;
World.Update;
if Args.ContentLength > 0 then
Font.Write(Format('Loading %s %d% complete', [FUrl,
Round((Args.ReadLength / Args.ContentLength) * 100)]), 1, 1, 0)
else
Font.Write(Format('Loading %s (%d bytes read)', [FUrl, Args.ReadLength]), 1, 1, 0);
SwapBuffers;
end;

function TSpriteWindow.WebDownload(const Url: string): TStream;
begin
{ When we download show the user the download progress }
FUrl := Url;
Result := WebGet(Url, WebProgress);
end;

procedure TSpriteWindow.RenderInitialize;
const
{ Location to download resources }
Expand All @@ -69,18 +96,20 @@ procedure TSpriteWindow.RenderInitialize;
{ If we draw something, let's use a 2 pixel wide yellow pen }
Pen.Color := clYellow;
Pen.Width := 2;
{ This is our loading progress }
WebLoad := WebDownload;
{ Load a looping audio track from the internet into bank zero }
Audio.Banks[0].Looping := True;
Audio.Samples.Add(WebGet(MusicUrl)).Play;
Audio.Samples.Add(WebLoad(MusicUrl)).Play;
{ Make room for our textures }
Textures.Generate(TexGround + 1);
{ Load a some textures from the internet }
Textures.Load(WebGet(TexBrokenUrl), TexBroken);
Textures.Load(WebGet(TexSkyUrl), TexSky);
Textures.Load(WebGet(TexCloudsUrl), TexClouds);
Textures.Load(WebGet(TexMountainsUrl), TexMountains);
Textures.Load(WebLoad(TexBrokenUrl), TexBroken);
Textures.Load(WebLoad(TexSkyUrl), TexSky);
Textures.Load(WebLoad(TexCloudsUrl), TexClouds);
Textures.Load(WebLoad(TexMountainsUrl), TexMountains);
Textures.Magnify[TexMountains] := filterNearest;
Textures.Load(WebGet(TexGroundUrl), TexGround);
Textures.Load(WebLoad(TexGroundUrl), TexGround);
Textures.Magnify[TexGround] := filterNearest;
{ If you create it }
FBroken := TBackgroudSprite.Create(World);
Expand Down
15 changes: 12 additions & 3 deletions examples/sprites/sprites.web.pas
Expand Up @@ -8,17 +8,21 @@ interface
Bare.System,
Bare.Networking.Web;

function WebGet(const Url: string): TStream;
var
WebLoad: function(const Url: string): TStream of object;

function WebGet(const Url: string; OnProgress: TWebProgressEvent = nil): TStream;

implementation

var
WebStream: TStream;

function WebGet(const Url: string): TStream;
function WebGet(const Url: string; OnProgress: TWebProgressEvent = nil): TStream;
const
BaseUrl = 'http://download.baregame.org/';
var
Client: TWebClient;
S: string;
begin
WebStream.Free;
Expand All @@ -28,7 +32,12 @@ function WebGet(const Url: string): TStream;
WebStream.LoadFromFile(S)
else
begin
Bare.Networking.Web.WebGet(BaseUrl + Url, WebStream);
Client := TWebClient.Create;
try
Client.OnProgress := OnProgress;
finally
Client.Get(BaseUrl + Url, WebStream);
end;
DirCreate(FileExtractPath(S));
WebStream.SaveToFile(S);
end;
Expand Down
3 changes: 3 additions & 0 deletions source/bare.interop.sdl2.pas
Expand Up @@ -13,6 +13,8 @@

interface

{.$define static}

{$ifdef static}
{$define libsdl2 := external}
{$else}
Expand Down Expand Up @@ -2317,6 +2319,7 @@ function SDL_RWClose(ctx: PSDL_RWops): LongInt;
implementation

{$ifdef static}
{$linklib SDL2}
{$endif}

{ Macros from all headers }
Expand Down

0 comments on commit a3aa7e3

Please sign in to comment.