From 80036808f199eddb627c13648194d0fe8ced80be Mon Sep 17 00:00:00 2001 From: Joel Leclerc Date: Sun, 29 Apr 2012 15:28:51 -0600 Subject: [PATCH] Moved the gettime functions to gettime.cpp and gettime.h --- src/CMakeLists.txt | 4 +-- src/gettime.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++ src/gettime.h | 39 +++++++++++++++++++++++++++ src/main.cpp | 60 ----------------------------------------- 4 files changed, 108 insertions(+), 62 deletions(-) create mode 100644 src/gettime.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8e1619a..5c7b719 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -204,6 +204,8 @@ set(common_SRCS sha1.cpp base64.cpp ban.cpp + gettime.cpp + main.cpp ) # This gives us the icon @@ -257,13 +259,11 @@ set(minetest_SRCS filecache.cpp tile.cpp game.cpp - main.cpp ) # Server sources set(minetestserver_SRCS ${common_SRCS} - main.cpp ) include_directories( diff --git a/src/gettime.cpp b/src/gettime.cpp new file mode 100644 index 0000000..3c73064 --- /dev/null +++ b/src/gettime.cpp @@ -0,0 +1,67 @@ +/* +BlockPlanet +Copyright (C) 2012 MiJyn, Joel Leclerc +Licensed under GPLv3 + + +Based on: +Minetest-c55 +Copyright (C) 2010-2011 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "porting.h" +#include "gettime.h" + + +/* + gettime.h implementation +*/ + +#ifdef SERVER + +u32 getTimeMs() +{ + /* Use imprecise system calls directly (from porting.h) */ + return porting::getTimeMs(); +} + +#else + +// A precise irrlicht one +u32 IrrlichtTimeGetter::getTime() +{ + if(m_device == NULL) + { + return 0; + } + return m_device->getTimer()->getRealTime(); +} + +// Not so precise one which works without irrlicht +u32 SimpleTimeGetter::getTime() +{ + return porting::getTimeMs(); +} + +u32 getTimeMs() +{ + if(g_timegetter == NULL) + return 0; + return g_timegetter->getTime(); +} + +#endif diff --git a/src/gettime.h b/src/gettime.h index b195526..69dfdee 100644 --- a/src/gettime.h +++ b/src/gettime.h @@ -51,5 +51,44 @@ inline std::string getTimestamp() return cs; } +#ifdef SERVER + +u32 getTimeMs(); + +#else + +// A small helper class +class TimeGetter +{ +public: + virtual u32 getTime() = 0; +}; + +// A precise irrlicht one +class IrrlichtTimeGetter: public TimeGetter +{ +public: + IrrlichtTimeGetter(IrrlichtDevice *device): + m_device(device) + {} + u32 getTime(); +private: + IrrlichtDevice *m_device; +}; +// Not so precise one which works without irrlicht +class SimpleTimeGetter: public TimeGetter +{ +public: + u32 getTime(); +}; + +// A pointer to a global instance of the time getter +// TODO: why? +TimeGetter *g_timegetter = NULL; + +u32 getTimeMs(); + +#endif + #endif diff --git a/src/main.cpp b/src/main.cpp index e9eab47..21e8da9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -119,66 +119,6 @@ bool noMenuActive() MainGameCallback *g_gamecallback = NULL; #endif -/* - gettime.h implementation -*/ - -#ifdef SERVER - -u32 getTimeMs() -{ - /* Use imprecise system calls directly (from porting.h) */ - return porting::getTimeMs(); -} - -#else - -// A small helper class -class TimeGetter -{ -public: - virtual u32 getTime() = 0; -}; - -// A precise irrlicht one -class IrrlichtTimeGetter: public TimeGetter -{ -public: - IrrlichtTimeGetter(IrrlichtDevice *device): - m_device(device) - {} - u32 getTime() - { - if(m_device == NULL) - return 0; - return m_device->getTimer()->getRealTime(); - } -private: - IrrlichtDevice *m_device; -}; -// Not so precise one which works without irrlicht -class SimpleTimeGetter: public TimeGetter -{ -public: - u32 getTime() - { - return porting::getTimeMs(); - } -}; - -// A pointer to a global instance of the time getter -// TODO: why? -TimeGetter *g_timegetter = NULL; - -u32 getTimeMs() -{ - if(g_timegetter == NULL) - return 0; - return g_timegetter->getTime(); -} - -#endif - class StderrLogOutput: public ILogOutput { public: