delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/29/22:33:24

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Allegro game timing.
Date: Wed, 29 Jan 1997 22:14:03 +0000
Organization: None
Distribution: world
Message-ID: <9SMsj8Aru87yEwuf@talula.demon.co.uk>
References: <Pine DOT SUN DOT 3 DOT 95 DOT 970128162301 DOT 26911C-100000 AT ernie DOT eecs DOT uic DOT edu>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
Lines: 45
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Andrew Deren writes:
>my_timer++;
>//i want some event to occur every 6th irritation
>if (my_timer % 6 == 0) {do some stuff;}
>//other stuff every 11th
>if (my_timer % 11 == 0) {some other stuff;}
>
>Is there some way in Allegro that I can achieve the same thing, I tried
>similar thing setting timer to 10ms, and then using timer%6, but this
>failed in some cases because the timer var is being updated by an
>interrupt and the program was missing some irritations of timer%6.

I'd use two timer callbacks and two counter variables. Set the timers
going at the required speed (one to go off every 60 msec, one every
110), and in the callbacks, increment the timer variables. If you
initialise the counters to zero, in your main game loop you can check
them and see if they have changed since the last iteration. A simple
control loop could then look something like:

   if (timer1 > 0) {
      timer1--;
      do some stuff;
   }

   if (timer2 > 0) {
      timer2--;
      do some other stuff;
   }

That raises the question of what to do if the processing takes longer
than the entire available time period, eg. timer1 is being incremented
by the callback more often than you are getting around to decrementing
it. You can easily detect if this happening, because the value will
start getting bigger rather than hovering between zero and one - what to
do about it really depends on your program.

When implementing this sort of semaphore system with global variables
that are updated by interrupt handlers, it's very important that you
declare the variables as volatile - otherwise the optimiser is liable to
do screwy things to them that will break your program...

/*
 *  Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
 *  Ghoti: 'gh' as in 'enough', 'o' as in 'women', and 'ti' as in 'nation'.
 */

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019