MyGUI  3.2.0
MyGUI_Timer.cpp
Go to the documentation of this file.
00001 
00006 /*
00007     This file is part of MyGUI.
00008 
00009     MyGUI is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     MyGUI is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 #include "MyGUI_Precompiled.h"
00023 #include "MyGUI_Timer.h"
00024 
00025 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC
00026 #   include <windows.h>
00027 #   pragma comment(lib, "winmm.lib")
00028 #else
00029 #   include <sys/time.h>
00030 #endif
00031 
00032 namespace MyGUI
00033 {
00034 
00035     Timer::Timer() :
00036         mTimeStart(0)
00037     {
00038     }
00039 
00040     void Timer::reset()
00041     {
00042         mTimeStart = getCurrentMilliseconds();
00043     }
00044 
00045     unsigned long Timer::getMilliseconds()
00046     {
00047         return getCurrentMilliseconds() - mTimeStart;
00048     }
00049 
00050     unsigned long Timer::getCurrentMilliseconds()
00051     {
00052 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC
00053         /*
00054         We do this because clock() is not affected by timeBeginPeriod on Win32.
00055         QueryPerformanceCounter is a little overkill for the amount of precision that
00056         I consider acceptable. If someone submits a patch that replaces this code
00057         with QueryPerformanceCounter, I wouldn't complain. Until then, timeGetTime
00058         gets the results I'm after. -EMS
00059 
00060         See: http://www.geisswerks.com/ryan/FAQS/timing.html
00061         And: http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q274323&
00062         */
00063         return timeGetTime();
00064 #else
00065         struct timeval now;
00066         gettimeofday(&now, NULL);
00067         return (now.tv_sec) * 1000 + (now.tv_usec) / 1000;
00068         //return ( unsigned long )(( double )( clock() ) / (( double )CLOCKS_PER_SEC / 1000.0 ) );
00069 #endif
00070     }
00071 
00072 } // namespace MyGUI