www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/02/11:34:38

Message-ID: <372B8FDE.8A1D08BC@jps.net>
Date: Sat, 01 May 1999 16:35:58 -0700
From: Dennis Yelle <dennis51 AT jps DOT net>
X-Mailer: Mozilla 4.51 [en] (Win95; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Simple timer question...
References: <eQKZuWASpyK3Ew2w AT zaynar DOT demon DOT co DOT uk> <19990501144106 DOT 24950 DOT 00001763 AT ng-fi1 DOT aol DOT com>
NNTP-Posting-Host: 209.239.208.36
X-Original-NNTP-Posting-Host: 209.239.208.36
X-Trace: 2 May 1999 00:17:57 -0700, 209.239.208.36
Lines: 69
X-Original-NNTP-Posting-Host: 209.63.224.240
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

SIowTalker wrote:
> 
> make a function like this
> 
> #include <go32.h>
> unsigned long QueryTimer(void)
> {
>   unsigned long clock = _farpeekl(_dos_ds, 0x0046C);
>   return(clock);
> }
> 
> then so each game loop takes at least 100 milliseconds, you save the time
> setting in the beggining of the game loop. Then at the end you get the time
> again and test if the difference between the two times is less than 100, if it
> is...it waits. Here's an example:
> 
> start_time = TimerQuery()
> // do game processing
> while((start_time-TimerQuery())<100) { }
> 
> Hope that helps...
> -Chad

If you do that you will be off by a factor of about 55,
because the counter at _farpeekl(_dos_ds, 0x0046C) only
advances about 18.2 times per second.
Also, that counter gets reset at midnight, so if you
are unlucky enough to start that loop shortly before
midnight, it will stay there forever.

This will work much better:

#include <dos.h>
#include <go32.h>
#include <sys/farptr.h>
#include <stdio.h>

void show_time()
{
  struct dostime_t dt;
  _dos_gettime( &dt);
  printf( "%02d:%02d:%02d.%02d",
          dt.hour, dt.minute, dt.second, dt.hsecond);
}

int main()
{
  unsigned long counter = 0;
  printf( "Starting at "); show_time(); printf( "\n");
  unsigned long clock = _farpeekl(_dos_ds, 0x0046C);
  for(int i=2; i>0; ) { // Work for 2 dos clock ticks, about 0.1 sec.
    //
    counter++;  // Let's see how much work we can get done in 0.1 sec.
    //
    if ( clock != _farpeekl(_dos_ds, 0x0046C)) {
      clock = _farpeekl(_dos_ds, 0x0046C);
      i--;
    }
  }
  printf( "  Ending at "); show_time(); printf( "\n");
  printf( "counter is %lu\n", counter);
  return 0;
}

Dennis Yelle

-- 
Want to get paid for using the internet?
If so, go to: http://alladvantage.com/go.asp?refid=BAL536

- Raw text -


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