changed copyright years in source files
[fegdk.git] / core / code / system / f_timer.cpp
blob21a0aa341cd40233e04b53055596cd2663354e1b
1 /*
2 fegdk: FE Game Development Kit
3 Copyright (C) 2001-2008 Alexey "waker" Yakovenko
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 Alexey Yakovenko
20 waker@users.sourceforge.net
23 #include "pch.h"
24 #include "f_timer.h"
25 #ifndef _WIN32
26 #include <sys/time.h>
27 #endif
29 namespace fe
32 static float processTime;
34 void
35 initProcessTime (void)
37 #ifdef _WIN32
38 processTime = timeGetTime ()*0.001f;
39 #else
40 struct timeval tv;
41 gettimeofday (&tv, NULL);
42 processTime = tv.tv_sec + tv.tv_usec * 0.000001;
43 #endif
46 #ifdef _WIN32
47 float
48 getProcessTime (void)
50 return timeGetTime ()*0.001f - processTime;
52 #else
53 float getProcessTime (void)
55 struct timeval tv;
56 gettimeofday (&tv, NULL);
57 return tv.tv_sec + tv.tv_usec * 0.000001- processTime;
59 #endif
61 void sleep (int ms)
65 timer::timer (void)
67 mbPaused = false;
68 mPrevTime = 0;
69 mCurrentTime = 0;
70 mSysPrevTime = 0;
71 mSysCurrentTime = 0;
72 mStartTime = getProcessTime ();
75 timer::~timer ()
79 void timer::update (void)
81 // update timer
82 mSysPrevTime = mSysCurrentTime;
83 mSysCurrentTime = getProcessTime () - mStartTime;
85 mPrevTime = mCurrentTime;
86 if (!mbPaused)
87 mCurrentTime += mSysCurrentTime - mSysPrevTime;
90 float timer::getTime (void) const
92 return mCurrentTime;
95 float timer::getTimePassed (void) const
97 return mCurrentTime - mPrevTime;
100 float timer::getSysTime (void) const
102 return mSysCurrentTime;
105 float timer::getSysTimePassed () const
107 return mSysCurrentTime - mSysPrevTime;
110 void timer::pause (bool onoff)
112 mbPaused = onoff;
113 update ();
116 bool timer::isPaused (void) const
118 return mbPaused;