1. with Ada.Real_Time;                     use Ada.Real_Time; 
  2. with Ada.Streams;                       use Ada.Streams; 
  3.  
  4. package Support.Real_Time is 
  5.  
  6.     -- A simple generic timer used to calculate elapsed time. 
  7.     type Timer_Type is limited private; 
  8.  
  9.     -- Return the elapsed time counted by the timer. 
  10.     function Elapsed( t : Timer_Type ) return Time_Span; 
  11.  
  12.     -- Returns True if the timer has been started and isn't paused. 
  13.     function Is_Running( t : Timer_Type ) return Boolean; 
  14.  
  15.     -- Pauses the timer. Elapsed time will not accumulate until the timer 
  16.     -- is started again. 
  17.     procedure Pause( t : in out Timer_Type ); 
  18.  
  19.     -- Clears elapsed time and starts the timer again. 
  20.     procedure Restart( t : in out Timer_Type ); 
  21.  
  22.     -- Starts or resumes the timer. This will have no effect if the timer is 
  23.     -- already running. 
  24.     procedure Start( t : in out Timer_Type ); 
  25.  
  26.     ---------------------------------------------------------------------------- 
  27.  
  28.     -- A timer used specifically for calculating frame rates. 
  29.     type Frame_Timer is limited private; 
  30.  
  31.     -- Returns true if the rate has been updated since the last check. Note that 
  32.     -- this doesn't mean the rate has changed in value, it means it has been 
  33.     -- recalculated. 
  34.     function Is_Updated( t : Frame_Timer ) return Boolean; 
  35.  
  36.     -- Returns the current frame rate. The value returned is FPS. 
  37.     function Rate( t : access Frame_Timer ) return Natural; 
  38.  
  39.     -- Returns the current frame rate. 
  40.     procedure Rate( t : in out Frame_Timer; fps : out Natural ); 
  41.  
  42.     -- Sets the minimum time to collect ticks before updating the current rate 
  43.     procedure Set_Update_Delay( t : in out Frame_Timer; ts : Time_Span ); 
  44.  
  45.     -- Notifies the timer of a complete frame. 
  46.     procedure Tick( t : in out Frame_Timer ); 
  47.  
  48.     ---------------------------------------------------------------------------- 
  49.  
  50.     -- Returns a string representation of 'ts' with the nearest applicable units. 
  51.     -- Example: "10[ms]", "10[s]", "10[min]" 
  52.     function Format( ts : Time_Span ) return String; 
  53.     pragma Postcondition( Format'Result'Length > 0 ); 
  54.  
  55.     -- Returns 'ts' as a Long_Float, based in seconds. 
  56.     function To_Float( ts : Time_Span ) return Long_Float; 
  57.  
  58.     -- Returns 'ts' rounded to the nearest microsecond. 
  59.     function To_Microseconds( ts : Time_Span ) return Natural; 
  60.  
  61.     -- Returns 'ts' rounded to the nearest millisecond. 
  62.     function To_Milliseconds( ts : Time_Span ) return Natural; 
  63.  
  64.     -- Returns 'ts' rounded to the nearest minute. 
  65.     function To_Minutes( ts : Time_Span ) return Natural; 
  66.  
  67.     -- Returns 'ts' rounded to the nearest second. 
  68.     function To_Seconds( ts : Time_Span ) return Natural; 
  69.  
  70.     -- Returns a string representation of Time 't' as a floating point number 
  71.     -- of seconds since the epoch (typically machine boot), with three digits 
  72.     -- to the right of the decimal. 
  73.     function To_String( t : Time ) return String; 
  74.  
  75.     -- Returns a string representation of 'ts' as a floating point number in 
  76.     -- seconds, with 'precision' number of digits to the right of the decimal. 
  77.     function To_String( ts : Time_Span; precision : Natural := 3 ) return String; 
  78.     pragma Postcondition( To_String'Result'Length > 0 ); 
  79.  
  80.     ---------------------------------------------------------------------------- 
  81.  
  82.     -- Reads a Time record from a stream. 
  83.     function Time_Input( stream : access Root_Stream_Type'Class ) return Time; 
  84.  
  85.     -- Writes a Time record to a stream. 
  86.     procedure Time_Output( stream : access Root_Stream_Type'Class; t : Time ); 
  87.  
  88. private 
  89.  
  90.     type Timer_Type is limited 
  91.         record 
  92.             started : Time := Time_First;   -- start time, changes when unpaused 
  93.             paused  : Time := Time_First;   -- time of pause (if paused) 
  94.             running : Boolean := False;     -- time is running 
  95.         end record; 
  96.  
  97.     type Frame_Timer is limited 
  98.         record 
  99.             ticks       : Natural := 0;               -- ticks since last update 
  100.             updateDelay : Time_Span := Seconds( 1 );  -- minimum time between updates 
  101.             lastUpdate  : Time := Clock;              -- time of last update 
  102.             updated     : Boolean := True;            -- updated since last check 
  103.             fps         : Natural := 0;               -- calculated fps 
  104.         end record; 
  105.  
  106. end Support.Real_Time;