// Run the demo void Demo::Run() { // Get the instance Demo *p = Demo::privGetInstance(); p->running = true; p->privReset(); // Main message loop MSG msg = { 0 }; // Game Loop until an exit while (p->running) { // Check messages if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); if (WM_QUIT == msg.message) { p->Quit(); continue; } } else { // called once per frame, update data, transformations, etc p->Update(); // called once per frame, only do rendering here p->Draw(); } } };
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE, LPSTR, int show) { Demo *demo = new Demo(hinst,show); using namespace Gdiplus; GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); MSG msg = {0}; const unsigned framerate = 1000/60; // 60 FPS unsigned last_frame_time = timeGetTime(); while (IsWindow(demo->Window())) { // care and feeding of window if (PeekMessage(&msg,demo->Window(),0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } // draw and sleep(?) unsigned current_frame_time = timeGetTime(); if (current_frame_time - last_frame_time >= framerate) // Lock to 60 FPS. { demo->Draw(double(current_frame_time - last_frame_time) / 1000); last_frame_time = current_frame_time; } Sleep(1); } delete demo; GdiplusShutdown(gdiplusToken); return msg.wParam; }