adaptive_skip.C

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 // Unix headers
00021 #include <sys/time.h>
00022 #include <unistd.h>
00023 
00024 // ANSI-C headers
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 
00028 #include <archon/util/time.H>
00029 #include <archon/util/adaptive_skip.H>
00030 
00031 
00032 namespace Archon
00033 {
00034   namespace Utilities
00035   {
00036     AdaptiveSkip::AdaptiveSkip(unsigned long millisBetweenOutTicks,
00037                                unsigned long checksPerOutTick):
00038       timeOfLastCheck(getTimeInMilliSeconds()),
00039       timeOfLastOutTick(timeOfLastCheck - millisBetweenOutTicks),
00040       millisBetweenOutTicks(millisBetweenOutTicks),
00041       checksPerOutTick(checksPerOutTick),
00042       skippedTicks(0),
00043       ticksPerCheck(1) {}
00044 
00045     unsigned long AdaptiveSkip::getTimeInMilliSeconds()
00046     {
00047       return Time::now().getMilliSeconds();
00048     }
00049 
00050     bool AdaptiveSkip::check()
00051     {
00052       // Estimate how many ticks to skip until next check
00053       const unsigned long now = getTimeInMilliSeconds();
00054       unsigned long timeSinceLastCheck = now - timeOfLastCheck;
00055       if(timeSinceLastCheck == 0) timeSinceLastCheck = 1;
00056       ticksPerCheck = int(double(ticksPerCheck) / timeSinceLastCheck *
00057                           millisBetweenOutTicks/checksPerOutTick);
00058       if(ticksPerCheck == 0) ticksPerCheck = 1;
00059       timeOfLastCheck = now;
00060       skippedTicks = 0;
00061 
00062       if(now - timeOfLastOutTick > millisBetweenOutTicks)
00063       {
00064         timeOfLastOutTick = now;
00065         return true;
00066       }
00067 
00068       return false;
00069     }
00070   }
00071 }

Generated on Sun Jul 30 22:55:44 2006 for Archon by  doxygen 1.4.4