comp.lang.ada
 help / color / mirror / Atom feed
From: "Luke A. Guest" <laguest@n_o_p_o_r_k_a_n_d_h_a_m.abyss2.demon.co.uk>
Subject: Re: Ada, games and frame rate calculation
Date: Wed, 16 Feb 2005 23:03:20 +0000
Date: 2005-02-16T23:03:20+00:00	[thread overview]
Message-ID: <pan.2005.02.16.23.03.19.256588@n_o_p_o_r_k_a_n_d_h_a_m.abyss2.demon.co.uk> (raw)
In-Reply-To: mailman.147.1108592182.527.comp.lang.ada@ada-france.org

On Wed, 16 Feb 2005 17:16:06 -0500, Stephen Leake wrote:

> "Luke A. Guest" <laguest@n_o_p_o_r_k_a_n_d_h_a_m.abyss2.demon.co.uk> writes:
> 
>> Hi,
>> 
>> I'm currently designing a game engine in Ada and was wondering about using
>> the Ada.Real_Time facilities (especially the timing).
> 
> If you are using GNAT on Windows, there is no difference between
> Ada.Real_Time and Ada.Calendar. On other compilers and operating
> systems, there may be a difference.

It's portable, so yeah there will be a windaz build. My main development
platform is Linux. I'm surprised that the win32 is identical to Calendar,
well not that surprised ;-) just as long as I can get milliseconds from it
then that'll be fine. Although, I would've thought they'd have used the
performance counters for the Ada.Real_Time package!

>> Now, normally you get the current time calculate how much time has
>> passed since the last time it was called. These values are used to
>> implement frame rate (FPS) display and to animated models. 
> 
> I'm a little confused by this. A typical loop should be:
> 
> declare
>    use Ada.Real_Time; -- or Calendar
>    Next_Time  : Time; 
>    Frame_Time : Time_Span := To_Time_Span (0.0333); 
> begin
>    loop
>       Next_Time := Next_Time + Frame_Time;
>       delay until Next_Time;
>       --  do stuff here
>    end loop;
> end;

You don't have to delay...what's confusing? It's a game engine, you want
it to run as fast as possible, but by utilising certain Ada language
features, you get some extra nice functionality that's portable.
 
>> So ultimately, I need to get access to the internals (I think).
> 
> No, that should never be necessary.

Well, not at the bit level no ;-) I have been told and I've verified it,
that I can use Duration and use that to get the string value to display.

>> So, as an example, I have my Ada.Real_Time.Time type which is my FPS, I
>> now have to display this on screen, so I need to determine the actual
>> text from the value. How do I do this?
> 
> Use Split and To_Duration:
> 
> declare
>     seconds : seconds_Count;
>     fraction : time_span;
> begin
>     split (Next_time, seconds, fraction); put_Line (seconds_count'image
>     (Seconds) & "." & duration'image
>       (to_duration (fraction)));
> end;
> 
> That's not a very clean format, but you get the idea.

Well, here's my current FPS calculation:


  procedure CalculateFPS is

    CurrentTime         : Float := Float(SDL.Timer.GetTicks) / 1000.0;
    ElapsedTime         : Float := CurrentTime - LastElapsedTime;
    FramesPerSecond     : String(1 .. 10);
    MillisecondPerFrame : String(1 .. 10);

    package Float_InOut is new Text_IO.Float_IO(Float);
    use Float_InOut;

  begin

    FrameCount := FrameCount + 1;

    if ElapsedTime > 1.0 then

      FPS := Float(FrameCount) / ElapsedTime;

      Put(FramesPerSecond, FPS, Aft => 2, Exp => 0);
      Put(MillisecondPerFrame, 1000.0 / FPS, Aft => 2, Exp => 0);

      SDL.Video.WM_Set_Caption_Title(Example.GetTitle & "  " & FramesPerSecond
& " fps  " & MillisecondPerFrame & " ms/frame");

      LastElapsedTime := CurrentTime;
      FrameCount      := 0;

    end if;

  end CalculateFPS;

Now that uses SDL, but ultimately that's not portable and I won't be using
SDL in the long run and cannot depend on it.

Luke.




  reply	other threads:[~2005-02-16 23:03 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-16 21:32 Ada, games and frame rate calculation Luke A. Guest
2005-02-16 22:16 ` Stephen Leake
2005-02-16 23:03   ` Luke A. Guest [this message]
2005-02-17  0:55     ` Stephen Leake
2005-02-17  2:33       ` tmoran
2005-02-17  8:39       ` Dmitry A. Kazakov
2005-02-17 23:23     ` Randy Brukardt
2005-02-19 14:48       ` Simon Wright
2005-02-17  0:06   ` Jeffrey Carter
2005-02-17  2:33     ` tmoran
2005-02-17 22:08       ` Simon Wright
2005-02-18  0:06       ` Jeffrey Carter
2005-02-18  5:30         ` tmoran
2005-02-19  0:03           ` Jeffrey Carter
2005-02-19  0:45             ` tmoran
2005-02-19 22:19               ` Jeffrey Carter
2005-02-18  9:04         ` Adrien Plisson
2005-02-18  9:19           ` Vinzent 'Gadget' Hoefler
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox