win32一个好用的游戏循环定时器
2016-01-21 15:16
288 查看
代码
定时器代码
#ifndef __CTimer_H__ #define __CTimer_H__ #include <windows.h> class CTimer{ private: LONGLONG m_CurrenntTime, m_LastTime, m_NextTime, m_FrameTime, //每帧有多少个时钟周期 m_PerfCountFreq; //定时器的时钟频率 double m_TimeElapsed, m_TimeScale; //定时器的时钟周期 float m_FPS; public: CTimer(); CTimer(float fps); void Start(); bool ReadyForNextFrame(); double GetTimeElapsed(){return m_TimeElapsed;} double TimeElapsed(); }; #endif //CTimer.cpp #include "CTimer.h" CTimer::CTimer():m_FPS(0), m_TimeElapsed(0.0f), m_FrameTime(0), m_LastTime(0), m_PerfCountFreq(0) { //获得定时器的时钟频率 QueryPerformanceFrequency((LARGE_INTEGER*) &m_PerfCountFreq); //时钟周期 m_TimeScale = 1.0f / m_PerfCountFreq; } CTimer::CTimer(float fps):m_FPS(fps), m_TimeElapsed(0.0f), m_LastTime(0), m_PerfCountFreq(0) { QueryPerformanceFrequency((LARGE_INTEGER*) &m_PerfCountFreq); m_TimeScale = 1.0f / m_PerfCountFreq; m_FrameTime = (LONGLONG)(m_PerfCountFreq / m_FPS); } void CTimer::Start(){ QueryPerformanceCounter((LARGE_INTEGER*) &m_LastTime); m_NextTime = m_LastTime + m_FrameTime; return; } bool CTimer::ReadyForNextFrame(){ if(!m_FPS){ MessageBox(NULL, "No FPS set in timer", "Doh", 0); return false; } QueryPerformanceCounter((LARGE_INTEGER*) &m_CurrentTime); if(m_CurrentTime > m_NextTime){ m_TimeElapsed = (m_CurrentTime - m_LastTime) * m_TimeScale; m_LastTime = m_CurrentTime; m_NextTime = m_CurrentTime + m_FrameTime; return true; } return false; } double CTimer::TimeElapsed(){ QueryPerformanceCounter((LARGE_INTEGER*) &m_CurrentTime); m_TimeElapsed = (m_CurrentTime - m_LastTime) * m_TimeScale; m_LastTime = m_CurrentTime; return m_TimeElapsed; }
定时器使用
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow){ WNDCLASSEX winclass; HWND hwnd; MSG msg; winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hinstance; winclass.hIcon = NULL; winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = NULL; winclass.lpszMenuName = NULL; winclass.lpszClassName = szWindowClassName; winclass.hIconSm = NULL; if(!RegisterClassEx(&winclass)){ MessageBox(NULL, "Error Registering Class!", "Error", 0); return 0; } if(!(hwnd = CreateWindowEx(NULL, szWindowClassName, szApplicationName, WS_OVERLAPPED | WS_VISIBLE | WS_CAPTION | WS_SYSMENU, GetSystemMetrics(SM_CXSCREEN)/2 - CParams::WindowWidth/2, GetSystemMetrics(SM_CYSCREEN)/2 - CParams::WindowHeight/2, CParams::WindowWidth, CParams::WindowHeight, NULL, NULL, hinstance, NULL))) { MessageBox(NULL, "Error Creating Window!", "Error", 0); return 0; } ShowWindow(hwnd, SW_SHOWDEFAULT); UpdateWindow(hwnd); //============游戏循环===================== //设置帧率 CTimer timer(CParams::iFramesPerSecond); //开始计时 timer.Start(); bool bDone = false; while(!bDone){ while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){ if(msg.message == WM_QUIT){ bDone = TRUE; }else{ TranslateMessage(&msg); DispatchMessage(&msg); } } //是否开始下一帧 if(timer.ReadyForNextFrame() || g_pController->FastRender()){ if(!g_pController->Update()) bDone = true; InvalidateRect(hwnd, NULL, NULL); UpdateWindow(hwnd); } } Cleanup(); UnregisterClass(szWindowClassName, winclass.hInstance); return 0; }
相关文章推荐
- win32下进程间通信(共享内存)实例分析
- WMI中的Win32_PingStatus类(ping命令实现)
- win32安装配置非安装版的MySQL
- 一个win32窗口创建示例
- iis Win32状态数值(sc-win32-status)说明
- nginx win32 版本静态文件测试 (Windows环境)
- C#利用win32 Api 修改本地系统时间、获取硬盘序列号
- C#通过WIN32 API实现嵌入程序窗体
- WIN32程序获取父进程ID的方法
- 实例分析一个简单的Win32程序
- VC程序在Win32环境下动态链接库(DLL)编程原理
- Win32应用程序(SDK)设计原理详解
- python使用win32com库播放mp3文件的方法
- WIN32 贪食蛇改进版,新增计分、等级及暂停功能
- WiFiAssistant 无线承载网络设置助手的开发历程
- Windows AppContainer 降权,隔离与安全
- Win32 调试接口设计与实现浅析
- 理解 Win32 "OutputDebugString"
- Win32串口编程
- VC++串口编程之基于Win32 API