From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a1bb0c0c18c89737 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-15 12:29:30 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.cwix.com!sjc-peer.news.verio.net!news.verio.net!iad-read.news.verio.net.POSTED!not-for-mail From: Dr Nancy's Sweetie Subject: Re: On-Screen Elapsed Time Display? Newsgroups: comp.lang.ada References: <3DA72766.2010907@acm.org> Organization: Rowan College of New Jersey User-Agent: tin/1.4.6-20020816 ("Aerials") (UNIX) (SunOS/5.8 (sun4u)) Message-ID: Date: Tue, 15 Oct 2002 19:23:16 GMT NNTP-Posting-Host: 150.250.64.69 X-Complaints-To: abuse@verio.net X-Trace: iad-read.news.verio.net 1034709796 150.250.64.69 (Tue, 15 Oct 2002 19:23:16 GMT) NNTP-Posting-Date: Tue, 15 Oct 2002 19:23:16 GMT Xref: archiver1.google.com comp.lang.ada:29818 Date: 2002-10-15T19:23:16+00:00 List-Id: 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