about summary refs log tree commit diff stats
path: root/App/Main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'App/Main.cpp')
-rw-r--r--App/Main.cpp41
1 files changed, 20 insertions, 21 deletions
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:
17}; 17};
18 18
19#define EXE_NAME L"witness64_d3d11.exe" 19#define EXE_NAME L"witness64_d3d11.exe"
20#define PROC_ATTACH 0x401 20#define HEARTBEAT 0x401
21#define RANDOMIZE_READY 0x402 21#define RANDOMIZE_READY 0x402
22#define RANDOMIZING 0403 22#define RANDOMIZING 0403
23#define RANDOMIZE_DONE 0x404 23#define RANDOMIZE_DONE 0x404
24#define CHECK_NEWGAME 0x405
25 24
26// Globals 25// Globals
27HWND g_hwnd; 26HWND g_hwnd;
@@ -40,24 +39,25 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
40 PostQuitMessage(0); 39 PostQuitMessage(0);
41 } else if (message == WM_COMMAND || message == WM_TIMER) { 40 } else if (message == WM_COMMAND || message == WM_TIMER) {
42 switch (LOWORD(wParam)) { 41 switch (LOWORD(wParam)) {
43 case PROC_ATTACH: 42 case HEARTBEAT:
44 if (!g_witnessProc->Initialize(EXE_NAME)) { 43 SetTimer(g_hwnd, HEARTBEAT, 1000, NULL);
45 SetTimer(g_hwnd, PROC_ATTACH, 1000, NULL); // Retry in 1s 44 // Potential improvement: Change this call to be part of the HEARTBEAT message.
46 } else { 45 switch (g_witnessProc->Heartbeat(EXE_NAME)) {
47 g_randomizer = std::make_shared<Randomizer>(g_witnessProc); 46 case ProcStatus::NotRunning:
48 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL); 47 // Shut down randomizer, wait for startup
49 SetTimer(g_hwnd, CHECK_NEWGAME, 1000, NULL); // Start checking for new game 48 if (g_randomizer) g_randomizer = nullptr;
50 } 49 break;
51 break; 50 case ProcStatus::Running:
52 case CHECK_NEWGAME: 51 if (!g_randomizer) {
53 if (g_witnessProc) { 52 g_randomizer = std::make_shared<Randomizer>(g_witnessProc);
54 // It shouldn't be possible to pause earlier than this, so subsequent checks will fail. 53 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL);
55 if (g_witnessProc->GetCurrentFrame() < 80) { // New game 54 }
55 break;
56 case ProcStatus::NewGame: // This status will fire only once per new game
56 SetWindowText(g_seed, L""); 57 SetWindowText(g_seed, L"");
57 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL); 58 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL);
59 break;
58 } 60 }
59 SetTimer(g_hwnd, CHECK_NEWGAME, 1000, NULL); // Continue checking for new game
60 }
61 break; 61 break;
62 case RANDOMIZE_READY: 62 case RANDOMIZE_READY:
63 SetWindowText(g_randomizerStatus, L"Randomize"); 63 SetWindowText(g_randomizerStatus, L"Randomize");
@@ -65,6 +65,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
65 break; 65 break;
66 case RANDOMIZING: 66 case RANDOMIZING:
67 if (!g_randomizer) break; 67 if (!g_randomizer) break;
68 EnableWindow(g_randomizerStatus, FALSE);
68 SetWindowText(g_randomizerStatus, L"Randomizing..."); 69 SetWindowText(g_randomizerStatus, L"Randomizing...");
69 std::thread([]{ 70 std::thread([]{
70 g_randomizer->Randomize(); 71 g_randomizer->Randomize();
@@ -74,8 +75,6 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
74 case RANDOMIZE_DONE: 75 case RANDOMIZE_DONE:
75 SetWindowText(g_randomizerStatus, L"Randomized!"); 76 SetWindowText(g_randomizerStatus, L"Randomized!");
76 break; 77 break;
77 default:
78 break;
79 } 78 }
80 } 79 }
81 return DefWindowProc(hwnd, message, wParam, lParam); 80 return DefWindowProc(hwnd, message, wParam, lParam);
@@ -127,9 +126,9 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance
127 126
128 CreateLabel(390, 15, 90, L"Version: " VERSION_STR); 127 CreateLabel(390, 15, 90, L"Version: " VERSION_STR);
129 g_seed = CreateText(10, 10, 100); 128 g_seed = CreateText(10, 10, 100);
130 g_randomizerStatus = CreateButton(120, 10, 100, L"Randomize", RANDOMIZING); 129 g_randomizerStatus = CreateButton(120, 10, 110, L"Randomize", RANDOMIZING);
131 EnableWindow(g_randomizerStatus, FALSE); 130 EnableWindow(g_randomizerStatus, FALSE);
132 PostMessage(g_hwnd, WM_COMMAND, PROC_ATTACH, NULL); 131 PostMessage(g_hwnd, WM_COMMAND, HEARTBEAT, NULL);
133 132
134 ShowWindow(g_hwnd, nCmdShow); 133 ShowWindow(g_hwnd, nCmdShow);
135 UpdateWindow(g_hwnd); 134 UpdateWindow(g_hwnd);