| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- //
- // Copyright (c) 2008-2014 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "CoreEvents.h"
- #include "Profiler.h"
- #include "Timer.h"
- #include <ctime>
- #ifdef WIN32
- #include <windows.h>
- #include <mmsystem.h>
- #else
- #include <sys/time.h>
- #include <unistd.h>
- #endif
- #include "DebugNew.h"
- namespace Urho3D
- {
- bool HiresTimer::supported(false);
- long long HiresTimer::frequency(1000);
- Time::Time(Context* context) :
- Object(context),
- frameNumber_(0),
- timeStep_(0.0f),
- timerPeriod_(0)
- {
- #ifdef WIN32
- LARGE_INTEGER frequency;
- if (QueryPerformanceFrequency(&frequency))
- {
- HiresTimer::frequency = frequency.QuadPart;
- HiresTimer::supported = true;
- }
- #else
- HiresTimer::frequency = 1000000;
- HiresTimer::supported = true;
- #endif
- }
- Time::~Time()
- {
- SetTimerPeriod(0);
- }
- void Time::BeginFrame(float timeStep)
- {
- ++frameNumber_;
- if (!frameNumber_)
- ++frameNumber_;
-
- timeStep_ = timeStep;
-
- Profiler* profiler = GetSubsystem<Profiler>();
- if (profiler)
- profiler->BeginFrame();
-
- {
- PROFILE(BeginFrame);
-
- // Frame begin event
- using namespace BeginFrame;
-
- VariantMap& eventData = GetEventDataMap();
- eventData[P_FRAMENUMBER] = frameNumber_;
- eventData[P_TIMESTEP] = timeStep_;
- SendEvent(E_BEGINFRAME, eventData);
- }
- }
- void Time::EndFrame()
- {
- {
- PROFILE(EndFrame);
-
- // Frame end event
- SendEvent(E_ENDFRAME);
- }
-
- Profiler* profiler = GetSubsystem<Profiler>();
- if (profiler)
- profiler->EndFrame();
- }
- void Time::SetTimerPeriod(unsigned mSec)
- {
- #ifdef WIN32
- if (timerPeriod_ > 0)
- timeEndPeriod(timerPeriod_);
-
- timerPeriod_ = mSec;
-
- if (timerPeriod_ > 0)
- timeBeginPeriod(timerPeriod_);
- #endif
- }
- float Time::GetElapsedTime()
- {
- return elapsedTime_.GetMSec(false) / 1000.0f;
- }
- unsigned Time::GetSystemTime()
- {
- #ifdef WIN32
- unsigned currentTime = timeGetTime();
- #else
- struct timeval time;
- gettimeofday(&time, NULL);
- unsigned currentTime = time.tv_sec * 1000 + time.tv_usec / 1000;
- #endif
-
- return currentTime;
- }
- String Time::GetTimeStamp()
- {
- time_t sysTime;
- time(&sysTime);
- const char* dateTime = ctime(&sysTime);
- return String(dateTime).Replaced("\n", "");
- }
- void Time::Sleep(unsigned mSec)
- {
- #ifdef WIN32
- ::Sleep(mSec);
- #else
- usleep(mSec * 1000);
- #endif
- }
- Timer::Timer()
- {
- Reset();
- }
- unsigned Timer::GetMSec(bool reset)
- {
- #ifdef WIN32
- unsigned currentTime = timeGetTime();
- #else
- struct timeval time;
- gettimeofday(&time, NULL);
- unsigned currentTime = time.tv_sec * 1000 + time.tv_usec / 1000;
- #endif
-
- unsigned elapsedTime = currentTime - startTime_;
- if (reset)
- startTime_ = currentTime;
-
- return elapsedTime;
- }
- void Timer::Reset()
- {
- #ifdef WIN32
- startTime_ = timeGetTime();
- #else
- struct timeval time;
- gettimeofday(&time, NULL);
- startTime_ = time.tv_sec * 1000 + time.tv_usec / 1000;
- #endif
- }
- HiresTimer::HiresTimer()
- {
- Reset();
- }
- long long HiresTimer::GetUSec(bool reset)
- {
- long long currentTime;
-
- #ifdef WIN32
- if (supported)
- {
- LARGE_INTEGER counter;
- QueryPerformanceCounter(&counter);
- currentTime = counter.QuadPart;
- }
- else
- currentTime = timeGetTime();
- #else
- struct timeval time;
- gettimeofday(&time, NULL);
- currentTime = time.tv_sec * 1000000LL + time.tv_usec;
- #endif
-
- long long elapsedTime = currentTime - startTime_;
-
- // Correct for possible weirdness with changing internal frequency
- if (elapsedTime < 0)
- elapsedTime = 0;
-
- if (reset)
- startTime_ = currentTime;
-
- return (elapsedTime * 1000000LL) / frequency;
- }
- void HiresTimer::Reset()
- {
- #ifdef WIN32
- if (supported)
- {
- LARGE_INTEGER counter;
- QueryPerformanceCounter(&counter);
- startTime_ = counter.QuadPart;
- }
- else
- startTime_ = timeGetTime();
- #else
- struct timeval time;
- gettimeofday(&time, NULL);
- startTime_ = time.tv_sec * 1000000LL + time.tv_usec;
- #endif
- }
- }
|