From e3758a15a6430e7ab7ce9821c0d9e19bbf7a1e87 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Tue, 5 Nov 2019 08:45:44 -0800 Subject: Memory / Main are now cleaned up --- App/Main.cpp | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'App') diff --git a/App/Main.cpp b/App/Main.cpp index b018892..5adf22d 100644 --- a/App/Main.cpp +++ b/App/Main.cpp @@ -17,11 +17,10 @@ public: }; #define EXE_NAME L"witness64_d3d11.exe" -#define PROC_ATTACH 0x401 +#define HEARTBEAT 0x401 #define RANDOMIZE_READY 0x402 #define RANDOMIZING 0403 #define RANDOMIZE_DONE 0x404 -#define CHECK_NEWGAME 0x405 // Globals HWND g_hwnd; @@ -40,24 +39,25 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) PostQuitMessage(0); } else if (message == WM_COMMAND || message == WM_TIMER) { switch (LOWORD(wParam)) { - case PROC_ATTACH: - if (!g_witnessProc->Initialize(EXE_NAME)) { - SetTimer(g_hwnd, PROC_ATTACH, 1000, NULL); // Retry in 1s - } else { - g_randomizer = std::make_shared(g_witnessProc); - PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL); - SetTimer(g_hwnd, CHECK_NEWGAME, 1000, NULL); // Start checking for new game - } - break; - case CHECK_NEWGAME: - if (g_witnessProc) { - // It shouldn't be possible to pause earlier than this, so subsequent checks will fail. - if (g_witnessProc->GetCurrentFrame() < 80) { // New game + case HEARTBEAT: + SetTimer(g_hwnd, HEARTBEAT, 1000, NULL); + // Potential improvement: Change this call to be part of the HEARTBEAT message. + switch (g_witnessProc->Heartbeat(EXE_NAME)) { + case ProcStatus::NotRunning: + // Shut down randomizer, wait for startup + if (g_randomizer) g_randomizer = nullptr; + break; + case ProcStatus::Running: + if (!g_randomizer) { + g_randomizer = std::make_shared(g_witnessProc); + PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL); + } + break; + case ProcStatus::NewGame: // This status will fire only once per new game SetWindowText(g_seed, L""); PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL); + break; } - SetTimer(g_hwnd, CHECK_NEWGAME, 1000, NULL); // Continue checking for new game - } break; case RANDOMIZE_READY: SetWindowText(g_randomizerStatus, L"Randomize"); @@ -65,6 +65,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) break; case RANDOMIZING: if (!g_randomizer) break; + EnableWindow(g_randomizerStatus, FALSE); SetWindowText(g_randomizerStatus, L"Randomizing..."); std::thread([]{ g_randomizer->Randomize(); @@ -74,8 +75,6 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) case RANDOMIZE_DONE: SetWindowText(g_randomizerStatus, L"Randomized!"); break; - default: - break; } } return DefWindowProc(hwnd, message, wParam, lParam); @@ -127,9 +126,9 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance CreateLabel(390, 15, 90, L"Version: " VERSION_STR); g_seed = CreateText(10, 10, 100); - g_randomizerStatus = CreateButton(120, 10, 100, L"Randomize", RANDOMIZING); + g_randomizerStatus = CreateButton(120, 10, 110, L"Randomize", RANDOMIZING); EnableWindow(g_randomizerStatus, FALSE); - PostMessage(g_hwnd, WM_COMMAND, PROC_ATTACH, NULL); + PostMessage(g_hwnd, WM_COMMAND, HEARTBEAT, NULL); ShowWindow(g_hwnd, nCmdShow); UpdateWindow(g_hwnd); -- cgit 1.4.1