📄 pfwintime.cpp
字号:
/////////////////////////////////////////////////////////////////////////////////////////////////////// PowerFish core library// Copyright (C) 1997-2001 Camilla Drefvenborg <elmindreda@home.se>//// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA/////////////////////////////////////////////////////////////////////////////////////////////////////#include <PfBase.h>#include <PfTime.h>#include <windows.h>#include "PfWinTime.h"///////////////////////////////////////////////////////////////////////////////////////////////////// PfWinTimer constructors ------------------------------------------------------------------------PfWinTimer::PfWinTimer(void){ m_bCreated = false; m_bStarted = false; m_dBaseTime = 0.0; m_dCurTime = 0.0; m_dPrevTime = 0.0; m_dInvFrequency = 0.0;}PfWinTimer::~PfWinTimer(void){ Destroy();}// PfWinTimer methods -----------------------------------------------------------------------------bool PfWinTimer::Create(void){ Destroy(); __int64 qwFrequency; if (!QueryPerformanceFrequency((LARGE_INTEGER*) &qwFrequency)) return false; if (!qwFrequency) return false; m_dInvFrequency = 1.0 / (double) qwFrequency; return m_bCreated = true;}void PfWinTimer::Destroy(void){ Stop(); m_bCreated = false; m_bStarted = false; m_dBaseTime = 0.0; m_dCurTime = 0.0; m_dPrevTime = 0.0; m_dInvFrequency = 0.0;}// PfWinTimer interface methods -------------------------------------------------------------------void PfWinTimer::Start(void){ if (!m_bCreated) return; Stop(); __int64 qwCounter; QueryPerformanceCounter((LARGE_INTEGER*) &qwCounter); m_dBaseTime = (double) qwCounter; m_dPrevTime = m_dBaseTime; m_dCurTime = m_dBaseTime; m_bStarted = true;}void PfWinTimer::Stop(void){ if (!m_bStarted) return; m_bStarted = false; m_dBaseTime = 0.0; m_dCurTime = 0.0; m_dPrevTime = 0.0; m_dInvFrequency = 0.0;}float PfWinTimer::CalcDeltaTime(void){ if (!m_bStarted) return 0.f; __int64 qwCounter; QueryPerformanceCounter((LARGE_INTEGER*) &qwCounter); m_dPrevTime = m_dCurTime; m_dCurTime = (double) qwCounter; return (float) ((m_dCurTime - m_dPrevTime) * m_dInvFrequency);}float PfWinTimer::CalcTotalTime(void){ if (!m_bStarted) return 0.f; __int64 qwCounter; QueryPerformanceCounter((LARGE_INTEGER*) &qwCounter); return (float) (((double) qwCounter - m_dBaseTime) * m_dInvFrequency);}// PfWinTimer accessors ---------------------------------------------------------------------------bool PfWinTimer::IsCreated(void) const{ return m_bCreated;}// PfWinTimer interface accessors -----------------------------------------------------------------bool PfWinTimer::IsStarted(void) const{ return m_bStarted;}float PfWinTimer::GetDeltaTime(void) const{ return (float) m_dCurTime;}float PfWinTimer::GetTotalTime(void) const{ if (!m_bStarted) return 0.f; return (float) ((m_dCurTime - m_dBaseTime) * m_dInvFrequency);}///////////////////////////////////////////////////////////////////////////////////////////////////PfTimer* PfCreateTimer(void){ PfWinTimer* pTimer = new PfWinTimer(); if (!pTimer->Create()) { delete pTimer; pTimer = NULL; return NULL; } return pTimer;}const char* PfGetTimeName(void){ // NOTE: potential buffer overflow! // TODO: fix this bug! static char szTimeName[512]; if (!GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, "HH\':\'mm\':\'ss", szTimeName, sizeof(szTimeName))) return NULL; return szTimeName;}const char* PfGetDateName(void){ // NOTE: potential buffer overflow! // TODO: fix this bug! static char szDateName[512]; if (!GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, "yyyy\'-\'MM\'-\'dd", szDateName, sizeof(szDateName))) return NULL; return szDateName;}///////////////////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -