adonthell-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Adonthell-commits] CVS: adonthell/src/base timer.cc, NONE, 1.1 timer.h,


From: Alexandre Courbot <address@hidden>
Subject: [Adonthell-commits] CVS: adonthell/src/base timer.cc, NONE, 1.1 timer.h, NONE, 1.1 Makefile.am, 1.2, 1.3
Date: Sun, 24 Aug 2003 15:38:05 -0400

Update of /cvsroot/adonthell/adonthell/src/base
In directory subversions:/tmp/cvs-serv18098/src/base

Modified Files:
        Makefile.am 
Added Files:
        timer.cc timer.h 
Log Message:
Added a basic timer class for synchronization.


--- NEW FILE ---
#include "base/timer.h"

namespace base
{
    timer::timer() : Slice(100), Lasttime(0), Frames_missed(0)
    {
        gettimeofday(&initial_time, NULL);
    }

    void timer::set_slice(unsigned long int sl)
    {
        Slice = sl;
    }

    unsigned long int timer::current_time() const
    {
        struct timeval tv;
        gettimeofday(&tv, NULL);
        return convert_timeval(tv);
    }

    void timer::sleep(unsigned long int msecs) const
    {
        unsigned char err;
        struct timespec req, rem;
        rem.tv_sec = msecs / 1000;
        rem.tv_nsec = (msecs % 1000) * 1000000;

        do
        {
            req = rem;
            err = nanosleep(&req, &rem);
        } while (err && (errno == EINTR));

        
    }

    void timer::update()
    {
        unsigned long int curtime = current_time();

        while (curtime - Lasttime < Slice)
        {
            //this->sleep(1);
            curtime = current_time();
        }
        
        Frames_missed = (curtime - Lasttime) / Slice - 1;
        Lasttime = curtime - (curtime - Lasttime) % Slice;
    }
}

--- NEW FILE ---
#include <iostream>
#include <sys/time.h>
#include <time.h>

namespace base
{
    class timer
    {
    public:
        timer();
        unsigned long int slice() const { return Slice; }
        unsigned long int current_time() const;
        unsigned long int frames_missed() const { return Frames_missed; }
        void sleep(unsigned long int msecs) const;

        void set_slice(unsigned long int sl);

        void update();

    private:
        unsigned long int convert_timeval (const struct timeval & tv) const
        { return ((tv.tv_sec - initial_time.tv_sec) * 1000 + (tv.tv_usec - 
initial_time.tv_usec) / 1000); }
        struct timeval initial_time;

        unsigned long int Slice;
        unsigned long int Lasttime;
        unsigned long int Frames_missed;
    };
}

Index: Makefile.am
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/base/Makefile.am,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** Makefile.am 24 Jul 2003 09:58:19 -0000      1.2
--- Makefile.am 24 Aug 2003 19:38:03 -0000      1.3
***************
*** 8,12 ****
        base.h \
        paths.h \
!       callback.h
  
  ## Main library
--- 8,13 ----
        base.h \
        paths.h \
!       callback.h \
!       timer.h
  
  ## Main library
***************
*** 16,20 ****
  libadonthell_base_la_SOURCES = \
        paths.cc \
!       callback.cc
  
  libadonthell_base_la_CXXFLAGS = -DPKGLIBDIR=\"$(pkglibdir)\"
--- 17,22 ----
  libadonthell_base_la_SOURCES = \
        paths.cc \
!       callback.cc \
!       timer.cc
  
  libadonthell_base_la_CXXFLAGS = -DPKGLIBDIR=\"$(pkglibdir)\"





reply via email to

[Prev in Thread] Current Thread [Next in Thread]