delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/24/22:01:36

From: jromano AT slate DOT Mines DOT EDU (Jean-Luc Romano)
Newsgroups: comp.os.msdos.djgpp
Subject: clock() function
Date: 25 Jan 1998 01:01:57 GMT
Organization: Colorado School of Mines
Lines: 64
Message-ID: <6ae2u5$ls2$1@herald.Mines.EDU>
NNTP-Posting-Host: slate.mines.edu
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I've got a problem...

   If I want to write a game that uses an continually repeating
event loop, I would also want to include some items that do not
change state repeatedly (that is, as fast as the buffer is
displayed on the screen), but rather every second or so.

   For example, if I want to change the background color every
second, I would think to insert an if statement that uses the 
clock() function to see if one second has elapsed.  If it has,
the background color gets updated, and the variable that records
the last time of "if statement execution" contains the current time
to be read the next time the if statement is executed.  If this
sounds too confusing, look at my code at the bottom to help you 
figure out what I mean.

   I've tried that code in one of my programs, but it does not work
like I'd want it to.  When I run it, the screen gets cleared almost
instantly.  It get cleared (as best as I can tell) about 15 to 20
times a second.

   I have to mention that I am not familiar with the
initialise_timer() routine at all, so I do not know if that routine
has anything to do with what I am trying to accomplish.

   Can anyone tell me if there is a better way of checking to see
if a certain amount of time has elapsed?

   Thanks.

   Jean-Luc Romano



/* Here is that sample bit of code... */

#include <stdio.h>
#include <time.h>
#include <allegro.h>

int color;
clock_t old_time = clock();

while (1)  /* event loop */
{
   .
   .
   .
  if (clock() - old_time > CLOCKS_PER_SEC) /* Has one second passed yet? */
  {
     clear_to_color(screen, color);
     color++;
     color %= 16;  /* color resets to 0 when it hits 16 */
     old_time = clock();  /* old_time resets to the current time value */
  }
   .
   .
   .
}

/* End of code */


- Raw text -


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