From 593315eb53602ddeff22cfd2e46b6d727808a681 Mon Sep 17 00:00:00 2001
From: Gabriel Ravier <gabravier@gmail.com>
Date: Thu, 16 Apr 2020 00:11:03 +0200
Subject: [PATCH 01/11] CMakeLists: Made build work for cross-compile with
 mingw

Signed-off-by: Gabriel Ravier <gabravier@gmail.com>
---
 CMakeLists.txt | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d00e9089..f1c626e5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -245,11 +245,11 @@ set_target_properties(CSE2 PROPERTIES
 ################
 
 # Link libraries
-target_link_libraries(CSE2 PRIVATE ddraw.lib dsound.lib version.lib shlwapi.lib imm32.lib winmm.lib dxguid.lib)
+target_link_libraries(CSE2 PRIVATE ddraw dsound version shlwapi imm32 winmm dxguid gdi32)
 
-# Newer MSVC is missing `dinput.lib`
+# Newer MSVC is missing `dinput.lib`, we need to use `dinput8.lib`
 if(MSVC AND MSVC_VERSION GREATER_EQUAL 1500)
-	target_link_libraries(CSE2 PRIVATE dinput8.lib)
+	target_link_libraries(CSE2 PRIVATE dinput8)
 else()
-	target_link_libraries(CSE2 PRIVATE dinput.lib)
+	target_link_libraries(CSE2 PRIVATE dinput)
 endif()

From 181ecc3ec24a5623dfbf4fc83618e55787b88422 Mon Sep 17 00:00:00 2001
From: Gabriel Ravier <gabravier@gmail.com>
Date: Thu, 16 Apr 2020 00:29:43 +0200
Subject: [PATCH 02/11] CMakeLists: Reverted random comment change from
 593315eb

Signed-off-by: Gabriel Ravier <gabravier@gmail.com>
---
 CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f1c626e5..06876693 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -247,7 +247,7 @@ set_target_properties(CSE2 PROPERTIES
 # Link libraries
 target_link_libraries(CSE2 PRIVATE ddraw dsound version shlwapi imm32 winmm dxguid gdi32)
 
-# Newer MSVC is missing `dinput.lib`, we need to use `dinput8.lib`
+# Newer MSVC is missing `dinput.lib`
 if(MSVC AND MSVC_VERSION GREATER_EQUAL 1500)
 	target_link_libraries(CSE2 PRIVATE dinput8)
 else()

From b8c0581ee475dbc848fe6e7fbc15d9aea7425c8f Mon Sep 17 00:00:00 2001
From: Clownacy <Clownacy@users.noreply.github.com>
Date: Thu, 16 Apr 2020 13:17:26 +0100
Subject: [PATCH 03/11] Found the original Config.cpp variable names

Thank you, Mac port...
---
 src/Config.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/Config.cpp b/src/Config.cpp
index dfc02224..ffcc11a5 100644
--- a/src/Config.cpp
+++ b/src/Config.cpp
@@ -7,8 +7,8 @@
 #include "Config.h"
 #include "Main.h"
 
-static const char* const config_filename = "Config.dat";	// Not the original name
-static const char* const config_magic = "DOUKUTSU20041206";	// Not the original name
+static const char* const gConfigName = "Config.dat";
+static const char* const gProof = "DOUKUTSU20041206";
 
 BOOL LoadConfigData(CONFIG *conf)
 {
@@ -17,7 +17,7 @@ BOOL LoadConfigData(CONFIG *conf)
 
 	// Get path
 	char path[MAX_PATH];
-	sprintf(path, "%s\\%s", gModulePath, config_filename);
+	sprintf(path, "%s\\%s", gModulePath, gConfigName);
 
 	// Open file
 	FILE *fp = fopen(path, "rb");
@@ -31,7 +31,7 @@ BOOL LoadConfigData(CONFIG *conf)
 	fclose(fp);
 
 	// Check if version is not correct, and return if it failed
-	if (fread_result != 1 || strcmp(conf->proof, config_magic))
+	if (fread_result != 1 || strcmp(conf->proof, gProof))
 	{
 		memset(conf, 0, sizeof(CONFIG));
 		return FALSE;

From b83b7f25fcdecbb7812e3e00a45a106b6dfe3426 Mon Sep 17 00:00:00 2001
From: Clownacy <Clownacy@users.noreply.github.com>
Date: Thu, 16 Apr 2020 13:27:25 +0100
Subject: [PATCH 04/11] Found the real name for DummiedOutLogFunction..?

---
 src/Draw.cpp | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/Draw.cpp b/src/Draw.cpp
index d391a0ce..ab5f9c52 100644
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -730,7 +730,9 @@ void CortBox2(const RECT *rect, unsigned long col, SurfaceID surf_no)
 	surf[surf_no]->Blt(&dst_rect, 0, 0, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
 }
 
-BOOL DummiedOutLogFunction(int unknown)
+// Dummied-out log function
+// According to the Mac port, its name really is just "out".
+BOOL out(int unknown)
 {
 	char unknown2[0x100];
 	int unknown3;
@@ -762,14 +764,14 @@ int RestoreSurfaces(void)	// Guessed function name - this doesn't exist in the L
 	{
 		++surfaces_regenerated;
 		frontbuffer->Restore();
-		DummiedOutLogFunction(0x66);
+		out(0x66);
 	}
 
 	if (backbuffer->IsLost() == DDERR_SURFACELOST)
 	{
 		++surfaces_regenerated;
 		backbuffer->Restore();
-		DummiedOutLogFunction(0x62);
+		out(0x62);
 	}
 
 	for (s = 0; s < SURFACE_ID_MAX; ++s)
@@ -780,7 +782,7 @@ int RestoreSurfaces(void)	// Guessed function name - this doesn't exist in the L
 			{
 				++surfaces_regenerated;
 				surf[s]->Restore();
-				DummiedOutLogFunction(0x30 + s);
+				out(0x30 + s);
 
 				if (!surface_metadata[s].bSystem)
 				{

From 47740fe88d11ba487dfffc1927bf588a5bb542dd Mon Sep 17 00:00:00 2001
From: Clownacy <Clownacy@users.noreply.github.com>
Date: Thu, 16 Apr 2020 13:31:32 +0100
Subject: [PATCH 05/11] Note some weird code

---
 src/Frame.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/Frame.cpp b/src/Frame.cpp
index 920e6230..bf735ace 100644
--- a/src/Frame.cpp
+++ b/src/Frame.cpp
@@ -41,7 +41,7 @@ void MoveFrame3(void)
 		--gFrame.quake;
 	}
 
-	// This code exists in the Linux port (v1.0.0.4), but not the Windows version (v1.0.0.6)
+	// This code exists in the Linux port (v1.0.0.4), but not the Windows version (v1.0.0.6) or the Mac port
 /*	if (gFrame.x / 0x200 < 0)
 		gFrame.x = 0;
 	if (gFrame.y / 0x200 < 0)

From 8a9f70875cd90965da70a3d210951f66c797ed65 Mon Sep 17 00:00:00 2001
From: Clownacy <Clownacy@users.noreply.github.com>
Date: Thu, 16 Apr 2020 13:48:15 +0100
Subject: [PATCH 06/11] More authentic variable/function names

---
 devilution/comparer-config.toml |  4 ++--
 src/Main.cpp                    | 33 +++++++++++++++++----------------
 src/Main.h                      |  2 +-
 3 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/devilution/comparer-config.toml b/devilution/comparer-config.toml
index 010b3f1d..1039c25b 100644
--- a/devilution/comparer-config.toml
+++ b/devilution/comparer-config.toml
@@ -442,7 +442,7 @@ name = "CortBox2"
 addr = 0x40CA80
 
 [[func]]
-name = "DummiedOutLogFunction"
+name = "out"
 addr = 0x40CB30
 
 [[func]]
@@ -791,7 +791,7 @@ name = "PutFramePerSecound"
 addr = 0x412370
 
 [[func]]
-name = "GetFramePerSecound"
+name = "CountFramePerSecound"
 addr = 0x4123A0
 
 [[func]]
diff --git a/src/Main.cpp b/src/Main.cpp
index a8806bc8..40a5cbfa 100644
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -51,6 +51,7 @@ static const char *lpWindowName = "\x93\xB4\x8C\x41\x95\xA8\x8C\xEA";	// '洞窟
 static const char *lpWindowName = "Cave Story ~ Doukutsu Monogatari";
 #endif
 
+// The original name for this function is unknown
 void SetWindowName(HWND hWnd)
 {
 	char window_name[0x100];
@@ -64,36 +65,36 @@ void PutFramePerSecound(void)
 {
 	if (bFps)
 	{
-		const unsigned long fps = GetFramePerSecound();
+		const unsigned long fps = CountFramePerSecound();
 		PutNumber4(WINDOW_WIDTH - 40, 8, fps, FALSE);
 	}
 }
 
-unsigned long GetFramePerSecound(void)
+unsigned long CountFramePerSecound(void)
 {
-	unsigned long current_tick;
-	static BOOL need_new_base_tick = TRUE;
-	static unsigned long frames_this_second;
-	static unsigned long current_frame;
-	static unsigned long base_tick;
+	unsigned long current_tick;	// The original name for this variable is unknown
+	static BOOL first = TRUE;
+	static unsigned long max_count;
+	static unsigned long count;
+	static unsigned long wait;
 
-	if (need_new_base_tick)
+	if (first)
 	{
-		base_tick = GetTickCount();
-		need_new_base_tick = FALSE;
+		wait = GetTickCount();
+		first = FALSE;
 	}
 
 	current_tick = GetTickCount();
-	++current_frame;
+	++count;
 
-	if (base_tick + 1000 <= current_tick)
+	if (wait + 1000 <= current_tick)
 	{
-		base_tick += 1000;
-		frames_this_second = current_frame;
-		current_frame = 0;
+		wait += 1000;
+		max_count = count;
+		count = 0;
 	}
 
-	return frames_this_second;
+	return max_count;
 }
 
 // TODO - Inaccurate stack frame
diff --git a/src/Main.h b/src/Main.h
index 09808682..9c2f0656 100644
--- a/src/Main.h
+++ b/src/Main.h
@@ -9,6 +9,6 @@ extern HWND ghWnd;
 extern BOOL bFullscreen;
 
 void PutFramePerSecound(void);
-unsigned long GetFramePerSecound(void);
+unsigned long CountFramePerSecound(void);
 
 BOOL SystemTask(void);

From 4c910dbee6b3f420d5166c59cf14375b3c82a6ab Mon Sep 17 00:00:00 2001
From: Clownacy <Clownacy@users.noreply.github.com>
Date: Thu, 16 Apr 2020 13:52:00 +0100
Subject: [PATCH 07/11] Correct variable name

I must have guessed this one, and not left a comment saying it was
fake... dammit.
---
 src/Main.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/Main.cpp b/src/Main.cpp
index 40a5cbfa..de57e29c 100644
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -34,7 +34,7 @@ BOOL gbUseJoystick = FALSE;
 int gJoystickButtonTable[8];
 
 static BOOL bActive = TRUE;
-static BOOL bFps = FALSE;
+static BOOL bFPS = FALSE;
 
 static HANDLE hObject;
 static HANDLE hMutex;
@@ -63,7 +63,7 @@ void SetWindowName(HWND hWnd)
 // Framerate stuff
 void PutFramePerSecound(void)
 {
-	if (bFps)
+	if (bFPS)
 	{
 		const unsigned long fps = CountFramePerSecound();
 		PutNumber4(WINDOW_WIDTH - 40, 8, fps, FALSE);
@@ -472,7 +472,7 @@ LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lPar
 			DrawMenuBar(hWnd);
 
 			if (IsKeyFile("fps"))
-				bFps = TRUE;
+				bFPS = TRUE;
 
 			if (!bFullscreen)
 				LoadWindowRect(hWnd, "window.rect", FALSE);

From 8f49276d5e4f52967419c952a7edb9ea2d36cf94 Mon Sep 17 00:00:00 2001
From: Clownacy <Clownacy@users.noreply.github.com>
Date: Thu, 16 Apr 2020 14:44:53 +0100
Subject: [PATCH 08/11] Note some not-so-authentic variable names

---
 src/Draw.cpp | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/Draw.cpp b/src/Draw.cpp
index ab5f9c52..cf59ac55 100644
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -67,6 +67,7 @@ void SetClientOffset(int width, int height)
 
 BOOL Flip_SystemTask(HWND hWnd)
 {
+	// TODO - Not the original variable names
 	static DWORD timePrev;
 	static DWORD timeNow;
 
@@ -89,7 +90,7 @@ BOOL Flip_SystemTask(HWND hWnd)
 	else
 		timePrev += FRAMERATE;
 
-	static RECT dst_rect;
+	static RECT dst_rect;	// TODO - Not the original vaiable name
 	GetWindowRect(hWnd, &dst_rect);
 	dst_rect.left += client_x;
 	dst_rect.top += client_y;
@@ -537,12 +538,12 @@ BOOL MakeSurface_Generic(int bxsize, int bysize, SurfaceID surf_no, BOOL bSystem
 
 void BackupSurface(SurfaceID surf_no, const RECT *rect)
 {
-	static DDBLTFX ddbltfx;
+	static DDBLTFX ddbltfx;	// TODO - Not the original variable name
 
 	memset(&ddbltfx, 0, sizeof(DDBLTFX));
 	ddbltfx.dwSize = sizeof(DDBLTFX);
 
-	static RECT scaled_rect;
+	static RECT scaled_rect;	// TODO - Not the original variable name
 	scaled_rect.left = rect->left * magnification;
 	scaled_rect.top = rect->top * magnification;
 	scaled_rect.right = rect->right * magnification;
@@ -698,12 +699,12 @@ unsigned long GetCortBoxColor(COLORREF col)
 
 void CortBox(const RECT *rect, unsigned long col)
 {
-	static DDBLTFX ddbltfx;
+	static DDBLTFX ddbltfx;	// TODO - Not the original variable name
 	memset(&ddbltfx, 0, sizeof(DDBLTFX));
 	ddbltfx.dwSize = sizeof(DDBLTFX);
 	ddbltfx.dwFillColor = col;
 
-	static RECT dst_rect;
+	static RECT dst_rect;	// TODO - Not the original variable name
 	dst_rect.left = rect->left * magnification;
 	dst_rect.top = rect->top * magnification;
 	dst_rect.right = rect->right * magnification;
@@ -714,12 +715,12 @@ void CortBox(const RECT *rect, unsigned long col)
 
 void CortBox2(const RECT *rect, unsigned long col, SurfaceID surf_no)
 {
-	static DDBLTFX ddbltfx;
+	static DDBLTFX ddbltfx;	// TODO - Not the original variable name
 	memset(&ddbltfx, 0, sizeof(DDBLTFX));
 	ddbltfx.dwSize = sizeof(DDBLTFX);
 	ddbltfx.dwFillColor = col;
 
-	static RECT dst_rect;
+	static RECT dst_rect;	// TODO - Not the original variable name
 	dst_rect.left = rect->left * magnification;
 	dst_rect.top = rect->top * magnification;
 	dst_rect.right = rect->right * magnification;
@@ -732,7 +733,7 @@ void CortBox2(const RECT *rect, unsigned long col, SurfaceID surf_no)
 
 // Dummied-out log function
 // According to the Mac port, its name really is just "out".
-BOOL out(int unknown)
+static BOOL out(int unknown)
 {
 	char unknown2[0x100];
 	int unknown3;
@@ -748,7 +749,8 @@ BOOL out(int unknown)
 	return TRUE;
 }
 
-int RestoreSurfaces(void)	// Guessed function name - this doesn't exist in the Linux port
+// TODO - Probably not the original variable name (this is an educated guess)
+int RestoreSurfaces(void)
 {
 	int s;
 	RECT rect;

From ff45da6a0ecd990e67c626335ae4a6efc58ca5e2 Mon Sep 17 00:00:00 2001
From: Clownacy <Clownacy@users.noreply.github.com>
Date: Thu, 16 Apr 2020 14:45:08 +0100
Subject: [PATCH 09/11] Mark a static function as static

Both the Linux and Mac debug data indicate this is static (they both
prefix static function names with an extra underscore)
---
 src/NpChar.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/NpChar.cpp b/src/NpChar.cpp
index 46c314da..b4f914d9 100644
--- a/src/NpChar.cpp
+++ b/src/NpChar.cpp
@@ -26,12 +26,7 @@ int gSuperYpos;
 
 const char *gPassPixEve = "PXE";
 
-void InitNpChar(void)
-{
-	memset(gNPC, 0, sizeof(gNPC));
-}
-
-void SetUniqueParameter(NPCHAR *npc)
+static void SetUniqueParameter(NPCHAR *npc)
 {
 	int code = npc->code_char;
 	npc->surf = (SurfaceID)gNpcTable[code].surf;
@@ -50,6 +45,11 @@ void SetUniqueParameter(NPCHAR *npc)
 	npc->view.bottom = gNpcTable[code].view.bottom * 0x200;
 }
 
+void InitNpChar(void)
+{
+	memset(gNPC, 0, sizeof(gNPC));
+}
+
 BOOL LoadEvent(const char *path_event)
 {
 	int i, n;

From 18986336024c6c9c3631adb3a4d8f94e53bc62a6 Mon Sep 17 00:00:00 2001
From: Clownacy <Clownacy@users.noreply.github.com>
Date: Thu, 16 Apr 2020 14:05:44 +0100
Subject: [PATCH 10/11] Found a name for `nameless_flag`!

---
 src/Organya.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/Organya.cpp b/src/Organya.cpp
index 51070b16..f93394e2 100644
--- a/src/Organya.cpp
+++ b/src/Organya.cpp
@@ -760,7 +760,7 @@ BOOL QuitMMTimer();
 /*■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■*/
 UINT ExactTime   = 13;	// 最小精度 (Minimum accuracy)
 UINT TimerID;
-BOOL nameless_flag;
+BOOL bTimer;
 
 /*■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■*/
 // タイマー精度を設定する。 (Set timer accuracy.)
@@ -809,7 +809,7 @@ BOOL StartTimer(DWORD dwTimer)
 	if (ret != TIMERR_NOERROR)
 		return FALSE;
 
-	nameless_flag = TRUE;
+	bTimer = TRUE;
 
 	return TRUE;
 }
@@ -843,7 +843,7 @@ BOOL QuitMMTimer(void)
 {
 	MMRESULT ret;
 
-	if (!nameless_flag)
+	if (!bTimer)
 		return FALSE;
 
 	if(TimerID != TIMERR_NOERROR)
@@ -859,7 +859,7 @@ BOOL QuitMMTimer(void)
 	if (ret != TIMERR_NOERROR)
 		return FALSE;
 
-	nameless_flag = FALSE;
+	bTimer = FALSE;
 
 	return TRUE;
 }

From c52812664e563190db5e2c377a9db4334c255e12 Mon Sep 17 00:00:00 2001
From: Clownacy <Clownacy@users.noreply.github.com>
Date: Sun, 19 Apr 2020 22:37:37 +0100
Subject: [PATCH 11/11] Fix bosses flicking across the screen when hurt

The Wii U's wacky PowerPC architecture highlighted a cool
platform-dependency: the boss rendering code only properly works on
platforms where `char` is signed by default.

Granted, it's possible Pixel explicitly marked this variable as
signed. Who knows. Either way, CSE2 didn't, and now that's been
fixed.
---
 src/Boss.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/Boss.cpp b/src/Boss.cpp
index 32abed07..365ec440 100644
--- a/src/Boss.cpp
+++ b/src/Boss.cpp
@@ -35,7 +35,7 @@ void InitBossChar(int code)
 
 void PutBossChar(int fx, int fy)
 {
-	char a = 0;
+	signed char a = 0;
 	int b;
 
 	int side;