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