int main(int argc, char* argv[]) { // Get application path g_ApplicationExePath.append(argv[0]); // Calculate INI file path g_ApplicationIniPath = g_ApplicationExePath; g_ApplicationIniPath.resize(g_ApplicationIniPath.find_last_of(_T('.'))); g_ApplicationIniPath.append(_T(".ini")); if (! InitInstance()) return 255; Emulator_Start(); // The main loop Uint32 ticksLast; Uint32 ticksLastFps = SDL_GetTicks(); int frames = 0; while (!g_okQuit) { ticksLast = SDL_GetTicks(); // Time at frame start SDL_Event evt; while (SDL_PollEvent(&evt)) { if (evt.type == SDL_QUIT) { g_okQuit = TRUE; break; } else { if (evt.type == SDL_KEYDOWN || evt.type == SDL_KEYUP || evt.type == SDL_JOYBUTTONDOWN || evt.type == SDL_JOYBUTTONUP) { Main_OnKeyJoyEvent(evt); } } } if (g_okEmulatorRunning) { Emulator_SystemFrame(); Main_DrawScreen(); frames++; } // Delay Uint32 ticksNew = SDL_GetTicks(); Uint32 ticksElapsed = ticksNew - ticksLast; g_LastDelay = 0; if (ticksElapsed < FRAME_TICKS) { g_LastDelay = FRAME_TICKS - ticksElapsed; SDL_Delay(FRAME_TICKS - ticksElapsed); } ticksLast = ticksNew; if (ticksLast - ticksLastFps > 1000) //DEBUG: FPS calculation { g_LastFps = frames; frames = 0; ticksLastFps += 1000; } } Emulator_Stop(); DoneInstance(); return 0; }
int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); #ifdef _DEBUG _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF); int n = 0; _CrtSetBreakAlloc(n); #endif g_hInst = hInstance; // Store instance handle in our global variable LARGE_INTEGER nFrameStartTime; nFrameStartTime.QuadPart = 0; // Initialize global strings LoadString(g_hInst, IDS_APP_TITLE, g_szTitle, MAX_LOADSTRING); LoadString(g_hInst, IDC_NEMIGABTL, g_szWindowClass, MAX_LOADSTRING); MainWindow_RegisterClass(); // Perform application initialization if (! InitInstance(hInstance, nCmdShow)) return FALSE; HACCEL hAccelTable = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_NEMIGABTL)); LARGE_INTEGER nPerformanceFrequency; ::QueryPerformanceFrequency(&nPerformanceFrequency); // Main message loop MSG msg; for (;;) { ::QueryPerformanceCounter(&nFrameStartTime); if (!g_okEmulatorRunning) ::Sleep(20); else { if (Emulator_IsBreakpoint()) Emulator_Stop(); else { Emulator_SystemFrame(); ScreenView_RedrawScreen(); } } // Process all queue while (::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )) { if (msg.message == WM_QUIT) goto endprog; if (::TranslateAccelerator(g_hwnd, hAccelTable, &msg)) continue; ::TranslateMessage(&msg); ::DispatchMessage(&msg); } if (g_okEmulatorRunning && !Settings_GetSound()) { if (Settings_GetRealSpeed() == 0) ::Sleep(1); // We should not consume 100% of CPU else { // Slow down to 25 frames per second LARGE_INTEGER nFrameFinishTime; // Frame start time ::QueryPerformanceCounter(&nFrameFinishTime); LONGLONG nTimeElapsed = (nFrameFinishTime.QuadPart - nFrameStartTime.QuadPart) * 1000 / nPerformanceFrequency.QuadPart; LONGLONG nFrameDelay = 1000 / 25 - 1; // 1000 millisec / 25 = 40 millisec if (Settings_GetRealSpeed() == 0x7ffe) // Speed 25% nFrameDelay = 1000 / 25 * 4 - 1; else if (Settings_GetRealSpeed() == 0x7fff) // Speed 50% nFrameDelay = 1000 / 25 * 2 - 1; else if (Settings_GetRealSpeed() == 2) // Speed 200% nFrameDelay = 1000 / 25 / 2 - 1; if (nTimeElapsed > 0 && nTimeElapsed < nFrameDelay) { LONG nTimeToSleep = (LONG)(nFrameDelay - nTimeElapsed); ::Sleep((DWORD)nTimeToSleep); } } } } endprog: DoneInstance(); #ifdef _DEBUG if (_CrtDumpMemoryLeaks()) ::MessageBeep(MB_ICONEXCLAMATION); #endif return (int) msg.wParam; }
int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); #ifdef _DEBUG _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF); int n = 0; _CrtSetBreakAlloc(n); #endif g_hInst = hInstance; // Store instance handle in our global variable LARGE_INTEGER nFrameStartTime; nFrameStartTime.QuadPart = 0; // Initialize global strings LoadString(g_hInst, IDS_APP_TITLE, g_szTitle, MAX_LOADSTRING); LoadString(g_hInst, IDC_UKNCBTL, g_szWindowClass, MAX_LOADSTRING); MainWindow_RegisterClass(); // Perform application initialization if (! InitInstance(hInstance, nCmdShow)) return FALSE; HACCEL hAccelTable = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_UKNCBTL)); LARGE_INTEGER nPerformanceFrequency; ::QueryPerformanceFrequency(&nPerformanceFrequency); // Main message loop MSG msg; for (;;) { ::QueryPerformanceCounter(&nFrameStartTime); if (!g_okEmulatorRunning) ::Sleep(20); else { if (Emulator_IsBreakpoint()) Emulator_Stop(); else { if (Emulator_SystemFrame()) { ScreenView_RedrawScreen(); if (g_hAnimatedScreenshot != INVALID_HANDLE_VALUE) ScreenView_SaveApngFrame(g_hAnimatedScreenshot); } } } // Process all queue while (::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )) { if (msg.message == WM_QUIT) goto endprog; if (::TranslateAccelerator(g_hwnd, hAccelTable, &msg)) continue; ::TranslateMessage(&msg); ::DispatchMessage(&msg); } if (g_okEmulatorRunning && !Settings_GetSound()) Sleep(1); // We should not consume 100% of CPU //// Time bomb for perfomance analysis //if (Emulator_GetUptime() >= 300) // 5 minutes // ::PostQuitMessage(0); } endprog: DoneInstance(); #ifdef _DEBUG if (_CrtDumpMemoryLeaks()) ::MessageBeep(MB_ICONEXCLAMATION); #endif return (int) msg.wParam; }