with Ada.Real_Time; use Ada.Real_Time;
with Ada.Streams; use Ada.Streams;
package Support.Real_Time is
-- A simple generic timer used to calculate elapsed time.
type Timer_Type is limited private;
-- Return the elapsed time counted by the timer.
function Elapsed( t : Timer_Type ) return Time_Span;
-- Returns True if the timer has been started and isn't paused.
function Is_Running( t : Timer_Type ) return Boolean;
-- Pauses the timer. Elapsed time will not accumulate until the timer
-- is started again.
procedure Pause( t : in out Timer_Type );
-- Clears elapsed time and starts the timer again.
procedure Restart( t : in out Timer_Type );
-- Starts or resumes the timer. This will have no effect if the timer is
-- already running.
procedure Start( t : in out Timer_Type );
----------------------------------------------------------------------------
-- A timer used specifically for calculating frame rates.
type Frame_Timer is limited private;
-- Returns true if the rate has been updated since the last check. Note that
-- this doesn't mean the rate has changed in value, it means it has been
-- recalculated.
function Is_Updated( t : Frame_Timer ) return Boolean;
-- Returns the current frame rate. The value returned is FPS.
function Rate( t : access Frame_Timer ) return Natural;
-- Returns the current frame rate.
procedure Rate( t : in out Frame_Timer; fps : out Natural );
-- Sets the minimum time to collect ticks before updating the current rate
procedure Set_Update_Delay( t : in out Frame_Timer; ts : Time_Span );
-- Notifies the timer of a complete frame.
procedure Tick( t : in out Frame_Timer );
----------------------------------------------------------------------------
-- Returns a string representation of 'ts' with the nearest applicable units.
-- Example: "10[ms]", "10[s]", "10[min]"
function Format( ts : Time_Span ) return String;
pragma Postcondition( Format'Result'Length > 0 );
-- Returns 'ts' as a Long_Float, based in seconds.
function To_Float( ts : Time_Span ) return Long_Float;
-- Returns 'ts' rounded to the nearest microsecond.
function To_Microseconds( ts : Time_Span ) return Natural;
-- Returns 'ts' rounded to the nearest millisecond.
function To_Milliseconds( ts : Time_Span ) return Natural;
-- Returns 'ts' rounded to the nearest minute.
function To_Minutes( ts : Time_Span ) return Natural;
-- Returns 'ts' rounded to the nearest second.
function To_Seconds( ts : Time_Span ) return Natural;
-- Returns a string representation of Time 't' as a floating point number
-- of seconds since the epoch (typically machine boot), with three digits
-- to the right of the decimal.
function To_String( t : Time ) return String;
-- Returns a string representation of 'ts' as a floating point number in
-- seconds, with 'precision' number of digits to the right of the decimal.
function To_String( ts : Time_Span; precision : Natural := 3 ) return String;
pragma Postcondition( To_String'Result'Length > 0 );
----------------------------------------------------------------------------
-- Reads a Time record from a stream.
function Time_Input( stream : access Root_Stream_Type'Class ) return Time;
-- Writes a Time record to a stream.
procedure Time_Output( stream : access Root_Stream_Type'Class; t : Time );
private
type Timer_Type is limited
record
started : Time := Time_First; -- start time, changes when unpaused
paused : Time := Time_First; -- time of pause (if paused)
running : Boolean := False; -- time is running
end record;
type Frame_Timer is limited
record
ticks : Natural := 0; -- ticks since last update
updateDelay : Time_Span := Seconds( 1 ); -- minimum time between updates
lastUpdate : Time := Clock; -- time of last update
updated : Boolean := True; -- updated since last check
fps : Natural := 0; -- calculated fps
end record;
end Support.Real_Time;