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