| 1 |
{ |
| 2 |
Copyright 2021-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Measure loading time of various images. } |
| 17 |
unit GameViewSpeedTest; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleVectors, CastleUIControls, CastleControls, CastleKeysMouse; |
| 23 |
|
| 24 |
type |
| 25 |
TViewSpeedTest = class(TCastleView) |
| 26 |
published |
| 27 |
{ Components designed using CGE editor. |
| 28 |
These fields will be automatically initialized at Start. } |
| 29 |
ButtonGoView: TCastleButton; |
| 30 |
LabelTestInfo: TCastleLabel; |
| 31 |
private |
| 32 |
const |
| 33 |
TestImages: array [0..11] of String = ( |
| 34 |
'castle-data:/images/bricks2.jpg', |
| 35 |
'castle-data:/images/crown.gif', |
| 36 |
'castle-data:/images/metal_decal.dds', |
| 37 |
'castle-data:/images/sky_red_back.bmp', |
| 38 |
'castle-data:/images/sky_red_left.ppm', |
| 39 |
'castle-data:/images/sky_red_right.rgbe', |
| 40 |
'castle-data:/images/test_texture_grayscale.png', |
| 41 |
'castle-data:/images/test_texture.png', |
| 42 |
'castle-data:/images/texture_alpha.png', |
| 43 |
'castle-data:/images/test_texture.tga', |
| 44 |
'castle-data:/images/test_texture.xpm', |
| 45 |
'castle-data:/images/rgb-reference.ktx' |
| 46 |
); |
| 47 |
|
| 48 |
var |
| 49 |
TestIndex: Cardinal; |
| 50 |
|
| 51 |
procedure ClickGoView(Sender: TObject); |
| 52 |
procedure NextTest(Sender: TObject); |
| 53 |
public |
| 54 |
constructor Create(AOwner: TComponent); override; |
| 55 |
procedure Start; override; |
| 56 |
end; |
| 57 |
|
| 58 |
var |
| 59 |
ViewSpeedTest: TViewSpeedTest; |
| 60 |
|
| 61 |
implementation |
| 62 |
|
| 63 |
uses SysUtils, |
| 64 |
CastleImages, CastleTimeUtils, CastleUriUtils, |
| 65 |
GameViewMain; |
| 66 |
|
| 67 |
constructor TViewSpeedTest.Create(AOwner: TComponent); |
| 68 |
begin |
| 69 |
inherited; |
| 70 |
DesignUrl := 'castle-data:/gameviewspeedtest.castle-user-interface'; |
| 71 |
end; |
| 72 |
|
| 73 |
procedure TViewSpeedTest.Start; |
| 74 |
begin |
| 75 |
inherited; |
| 76 |
|
| 77 |
ButtonGoView.OnClick := {$ifdef FPC}@{$endif} ClickGoView; |
| 78 |
|
| 79 |
TestIndex := 0; |
| 80 |
|
| 81 |
LabelTestInfo.Text.Clear; |
| 82 |
LabelTestInfo.Text.Append('Testing image loading times:'); |
| 83 |
LabelTestInfo.Text.Append(''); // add empty line before the rest of tests |
| 84 |
|
| 85 |
{ We use WaitForRenderAndCall to show the speed test results gradually. |
| 86 |
It doesn't really improve the functionality, but it looks better. } |
| 87 |
WaitForRenderAndCall({$ifdef FPC}@{$endif} NextTest); |
| 88 |
end; |
| 89 |
|
| 90 |
procedure TViewSpeedTest.ClickGoView(Sender: TObject); |
| 91 |
begin |
| 92 |
Container.View := ViewMain; |
| 93 |
end; |
| 94 |
|
| 95 |
procedure TViewSpeedTest.NextTest(Sender: TObject); |
| 96 |
const |
| 97 |
TestsCountDefault = 100; |
| 98 |
TestsCountXpm = 5; // XPM loading is *very* slow, smaller amount of tests is enough |
| 99 |
var |
| 100 |
Img: TEncodedImage; |
| 101 |
I, TestsCount: Integer; |
| 102 |
TimeStart: TTimerResult; |
| 103 |
Seconds: TFloatTime; |
| 104 |
ImgInfo: String; |
| 105 |
begin |
| 106 |
TimeStart := Timer; |
| 107 |
if ExtractFileExt(TestImages[TestIndex]) = '.xpm' then |
| 108 |
TestsCount := TestsCountXpm |
| 109 |
else |
| 110 |
TestsCount := TestsCountDefault; |
| 111 |
for I := 1 to TestsCount do |
| 112 |
begin |
| 113 |
Img := LoadEncodedImage(TestImages[TestIndex]); |
| 114 |
if I = 1 then |
| 115 |
ImgInfo := Format('%s, %dx%d', [ |
| 116 |
Img.ClassName, |
| 117 |
Img.Width, |
| 118 |
Img.Height |
| 119 |
]); |
| 120 |
FreeAndNil(Img); |
| 121 |
end; |
| 122 |
Seconds := TimeStart.ElapsedTime; |
| 123 |
|
| 124 |
LabelTestInfo.Text.Append(Format('%s: Loaded in %f secs (%d times). Image loaded as %s', [ |
| 125 |
UriCaption(TestImages[TestIndex]), |
| 126 |
Seconds, |
| 127 |
TestsCount, |
| 128 |
ImgInfo |
| 129 |
])); |
| 130 |
|
| 131 |
Inc(TestIndex); |
| 132 |
if TestIndex <= High(TestImages) then |
| 133 |
WaitForRenderAndCall({$ifdef FPC}@{$endif} NextTest) |
| 134 |
else |
| 135 |
begin |
| 136 |
LabelTestInfo.Text.Append(''); |
| 137 |
LabelTestInfo.Text.Append('Tests finished.'); |
| 138 |
end; |
| 139 |
end; |
| 140 |
|
| 141 |
end. |