Menu

[r127]: / trunk / src / examples / framecounter.pas  Maximize  Restore  History

Download this file

115 lines (92 with data), 3.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
unit FrameCounter;
(* Defines a class to help counting frames and render a FPS label. *)
(* Copyright (c) 2012, 2025 Guillermo Martínez J.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*)
interface
uses
allegro5, a3dge;
type
(* The frame counter.
Add it to the Application state list and assign to the Global state.
*)
TFPSCounter = class (TGameState)
private
fBlack, fWhite: ALLEGRO_COLOR;
fFrameCount, fFPS, fTime, fPositionXFPS, fPositionCopy: Integer;
public
(* Initialize the counter. *)
procedure Enter (aEntity: TObject); override;
(* Updates the counter. *)
procedure Update (aEntity: TObject); override;
(* Renders the counter label on screen. *)
procedure Render; override;
end;
implementation
uses
al5Base, al5font, al5strings,
{$IfDef FPC}
GL;
{$Else}
OpenGL;
{$EndIf}
(*
* TFPSCounter
*************************************************************************)
procedure TFPSCounter.Enter(aEntity: TObject);
begin
fFrameCount := 0; fFPS := 0; fTime := 0;
fPositionXFPS := a3dge.Application.Display.Width - 64;
fPositionCopy := (a3dge.Application.Display.Width div 2) - (Length (CopyStr) * 8 div 2);
fBlack := al_map_rgb (0, 0, 0);
fWhite := al_map_rgb (255, 255, 255)
end;
procedure TFPSCounter.Update (aEntity: TObject);
begin
Inc (fTime);
if fTime >= a3dge.FPS then
begin
fFPS := fFrameCount;
fFrameCount := 0;
fTime := 0
end
end;
procedure TFPSCounter.Render;
procedure PrintText (const aText: AL_STR; const aX, aY: Integer); inline;
begin
al_draw_text (
a3dge.Application.SysFont, fBlack,
aX + 1, aY + 1, 0,
aText
);
al_draw_text (
a3dge.Application.SysFont, fWhite,
aX, aY, 0,
aText
)
end;
begin
Inc (fFrameCount);
{ To render text using Allegro. }
a3dge.Application.World3D.DisableOpenGLContext;
a3dge.Application.Display.SetOrthographicProjection;
PrintText (CopyStr, fPositionCopy, 0);
PrintText (al_str_format ('FPS %d', [fFPS]), fPositionXFPS, 8);
{ Restore render mode. }
a3dge.Application.World3D.SetOpenGLContext;
a3dge.Application.World3D.Camera.SetPerspectiveProjection
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.