comp.lang.ada
 help / color / mirror / Atom feed
From: Dr Nancy's Sweetie <kilroy@elvis.rowan.edu>
Subject: Re: On-Screen Elapsed Time Display?
Date: Tue, 15 Oct 2002 19:23:16 GMT
Date: 2002-10-15T19:23:16+00:00	[thread overview]
Message-ID: <EWZq9.19556$T_.459823@iad-read.news.verio.net> (raw)
In-Reply-To: 3DA72766.2010907@acm.org


In a previous article, I mentioned a game (in progress) for which there
needs to be an on-screen time display.  I wanted to (a) get the time,
and (b) get an interrupt every second or so to draw the new time on the
screen.  I'd come up with ways to do this by importing time() and
setitimer(), but that felt a lot like writing C instead of using Ada.

Jeffrey Carter suggested using "Ada.Calendar" and a separate task which
uses a "delay until".  That gives me two processes writing to the screen
at the same time, so some protecting needs to go on.

It works, so far as I can tell correctly, and so, for the benefit of the
next person who does a search at Google on some of the keywords that
appear in this post, here is what I came up with:


with Semaphores   ;
with Ada.Calendar ; use Ada.Calendar
-- other stuff deleted to get the guts of this post

   Screen_Semaphore : Semaphores.Binary_Semaphore_Type(True);

   task Timer_Task is -- for on-screen clock
      entry Start;
   end Timer_Task;
   task body Timer_Task is
      Game_Start  : Time;
      Now         : Time;
   begin

      accept Start; -- wait for screen to be painted first time

      Debug.Print("Timer Started");

      Game_Start := Clock;
      Timer_Started := True;
      loop
         exit when Timer_Stopped;

         Now := Clock;
         Area.Time := Integer(Now - Game_Start);
         Time_String := Make_Time_String(Area.Time);

         Screen_Semaphore.Wait;
         Set_Time_Window(Time_String);
         Screen_Semaphore.Signal;

         Update_Screen;

         delay 0.9; -- try to make sure the counter never skips
      end loop;
   end Timer_Task;


The thing which paints the screen doesn't start the timer until after
the screen is fully painted (that wouldn't be fair).  "Make_Time_String"
turns a number of seconds in 00:00:00 format, and "Set_Time_Window"
actually puts it in the right place on the screen.

"Area.Time" is an integer value in the player's data structure for how
long the game was in progress.  When the player wins, this will be
compared against the best times recorded by other players (as soon as I
write that part 8-).


Thanks for the help, and hopefully this may help the next person.


Darren Provine ! kilroy@elvis.rowan.edu ! http://www.rowan.edu/~kilroy
"I use not only all the brains I have, but all I can borrow as well."
                                                     -- Woodrow Wilson



      parent reply	other threads:[~2002-10-15 19:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-11 18:54 On-Screen Elapsed Time Display? Dr Nancy's Sweetie
2002-10-11 19:32 ` Jeffrey Carter
2002-10-14 21:36   ` Dr Nancy's Sweetie
2002-10-14 23:05     ` tmoran
2002-10-16  1:45       ` Dr Nancy's Sweetie
2002-10-15 19:23   ` Dr Nancy's Sweetie [this message]
replies disabled

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