shithub: cstory

Download patch

ref: 1343e408b1de8dbdda956e26d56185e0b2dc32ab
parent: bce894210da1fecc16e1112dcacb62e39adf4364
parent: 5a2f98ece4c01ab02005ae9433447ab2781ce5d4
author: Gabriel Ravier <gabravier@gmail.com>
date: Tue May 14 04:15:43 EDT 2019

Merge branch 'master' into feature/makeFlagsMacros

--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
 	LDFLAGS = -s
 	FILENAME_DEF = CSE2
 else
-	CXXFLAGS = -O0 -g
+	CXXFLAGS = -Og -g3
 	FILENAME_DEF = CSE2d
 endif
 
--- a/msvc2003/devilution/comparer-config.toml
+++ b/msvc2003/devilution/comparer-config.toml
@@ -3,10 +3,78 @@
 address_offset = 0x400000
 
 [[func]]
+name = "ClearArmsData"
+addr = 0x401000
+
+[[func]]
+name = "ClearItemData"
+addr = 0x401030
+
+[[func]]
 name = "AddArmsData"
 addr = 0x401050
 
 [[func]]
+name = "SubArmsData"
+addr = 0x401160
+
+[[func]]
+name = "TradeArms"
+addr = 0x401220
+
+[[func]]
+name = "AddItemData"
+addr = 0x4012D0
+
+[[func]]
+name = "SubItemData"
+addr = 0x401330
+
+[[func]]
+name = "MoveCampCursor"
+addr = 0x4013C0
+
+[[func]]
+name = "PutCampObject"
+addr = 0x4016F0
+
+[[func]]
+name = "CampLoop"
+addr = 0x401D10
+
+[[func]]
+name = "CheckItem"
+addr = 0x401F20
+
+[[func]]
+name = "CheckArms"
+addr = 0x401F60
+
+[[func]]
+name = "UseArmsEnergy"
+addr = 0x401FA0
+
+[[func]]
+name = "ChargeArmsEnergy"
+addr = 0x402020
+
+[[func]]
+name = "FullArmsEnergy"
+addr = 0x402090
+
+[[func]]
+name = "RotationArms"
+addr = 0x4020E0
+
+[[func]]
+name = "RotationArmsRev"
+addr = 0x402190
+
+[[func]]
+name = "ChangeToFirstArms"
+addr = 0x402240
+
+[[func]]
 name = "InitBack"
 addr = 0x402270
 size = 0x100
@@ -288,6 +356,70 @@
 addr = 0x40AE30
 
 [[func]]
+name = "ActionStripper"
+addr = 0x40CF90
+
+[[func]]
+name = "PutStripper"
+addr = 0x40D010
+
+[[func]]
+name = "SetStripper"
+addr = 0x40D150
+
+[[func]]
+name = "RestoreStripper"
+addr = 0x40D240
+
+[[func]]
+name = "ActionIllust"
+addr = 0x40D2D0
+
+[[func]]
+name = "PutIllust"
+addr = 0x40D350
+
+[[func]]
+name = "ReloadIllust"
+addr = 0x40D3A0
+
+[[func]]
+name = "InitCreditScript"
+addr = 0x40D3E0
+
+[[func]]
+name = "ReleaseCreditScript"
+addr = 0x40D410
+
+[[func]]
+name = "StartCreditScript"
+addr = 0x40D440
+
+[[func]]
+name = "ActionCredit"
+addr = 0x40D5C0
+
+[[func]]
+name = "ActionCredit_Read"
+addr = 0x40D620
+
+[[func]]
+name = "GetScriptNumber"
+addr = 0x40DB00
+
+[[func]]
+name = "SetCreditIllust"
+addr = 0x40DB40
+
+[[func]]
+name = "CutCreditIllust"
+addr = 0x40DB60
+
+[[func]]
+name = "Scene_DownIsland"
+addr = 0x40DB70
+
+[[func]]
 name = "Call_Escape"
 addr = 0x40DD70
 
@@ -831,6 +963,30 @@
 addr = 0x41D550
 
 [[func]]
+name = "ClearPermitStage"
+addr = 0x41D610
+
+[[func]]
+name = "AddPermitStage"
+addr = 0x41D630
+
+[[func]]
+name = "SubPermitStage"
+addr = 0x41D6A0
+
+[[func]]
+name = "MoveStageSelectCursor"
+addr = 0x41D740
+
+[[func]]
+name = "PutStageSelectObject"
+addr = 0x41D840
+
+[[func]]
+name = "StageSelectLoop"
+addr = 0x41DA00
+
+[[func]]
 name = "ShootBullet_Frontia1"
 addr = 0x41DBD0
 
@@ -905,6 +1061,62 @@
 [[func]]
 name = "PutStar"
 addr = 0x4213B0
+
+[[func]]
+name = "InitTextScript2"
+addr = 0x4214E0
+
+[[func]]
+name = "EndTextScript"
+addr = 0x421570
+
+[[func]]
+name = "EncryptionBinaryData2"
+addr = 0x4215C0
+
+[[func]]
+name = "LoadTextScript2"
+addr = 0x421660
+
+[[func]]
+name = "LoadTextScript_Stage"
+addr = 0x421750
+
+[[func]]
+name = "GetTextScriptPath"
+addr = 0x4218E0
+
+[[func]]
+name = "GetTextScriptNo"
+addr = 0x421900
+
+[[func]]
+name = "StartTextScript"
+addr = 0x421990
+
+[[func]]
+name = "JumpTextScript"
+addr = 0x421AF0
+
+[[func]]
+name = "StopTextScript"
+addr = 0x421C50
+
+[[func]]
+name = "CheckNewLine"
+addr = 0x421C80
+
+[[func]]
+name = "SetNumberTextScript"
+addr = 0x421D10
+
+[[func]]
+name = "ClearTextLine"
+addr = 0x421E90
+
+[[func]]
+name = "PutTextScript"
+addr = 0x421F10
 
 [[func]]
 name = "TextScriptProc"
--- a/src/ArmsItem.cpp
+++ b/src/ArmsItem.cpp
@@ -129,10 +129,10 @@
 	}
 
 	if (i == ITEM_MAX)
-		return false;
+		return FALSE;
 
 	gItemData[i].code = code;
-	return true;
+	return TRUE;
 }
 
 BOOL SubItemData(long code)
@@ -270,6 +270,10 @@
 
 void PutCampObject()
 {
+	int i;
+	RECT rcArms;
+	RECT rcItem;
+
 	// Get rects
 	RECT rcPer = {72, 48, 80, 56};
 	RECT rcNone = {80, 48, 96, 56};
@@ -284,11 +288,10 @@
 	RECT rcBoxBottom = {0, 16, 244, 24};
 
 	// Draw box
-	int y;
 	PutBitmap3(&rcView, (WINDOW_WIDTH - 244) / 2, (WINDOW_HEIGHT - 224) / 2, &rcBoxTop, SURFACE_ID_TEXT_BOX);
-	for (y = 1; y < 18; y++)
-		PutBitmap3(&rcView, (WINDOW_WIDTH - 244) / 2, ((WINDOW_HEIGHT - 240) / 2) + (8 * (y + 1)), &rcBoxBody, SURFACE_ID_TEXT_BOX);
-	PutBitmap3(&rcView, (WINDOW_WIDTH - 244) / 2, ((WINDOW_HEIGHT - 240) / 2) + (8 * (y + 1)), &rcBoxBottom, SURFACE_ID_TEXT_BOX);
+	for (i = 1; i < 18; ++i)
+		PutBitmap3(&rcView, (WINDOW_WIDTH - 244) / 2, ((WINDOW_HEIGHT - 240) / 2) + (8 * (i + 1)), &rcBoxBody, SURFACE_ID_TEXT_BOX);
+	PutBitmap3(&rcView, (WINDOW_WIDTH - 244) / 2, ((WINDOW_HEIGHT - 240) / 2) + (8 * (i + 1)), &rcBoxBottom, SURFACE_ID_TEXT_BOX);
 
 	// Move titles
 	if (gCampTitleY > (WINDOW_HEIGHT - 208) / 2)
@@ -308,12 +311,11 @@
 		PutBitmap3(&rcView, 40 * gSelectedArms + (WINDOW_WIDTH - 224) / 2, (WINDOW_HEIGHT / 2) - 96, &rcCur1[1], SURFACE_ID_TEXT_BOX);
 
 	// Draw arms
-	for (int i = 0; i < ARMS_MAX; i++)
+	for (i = 0; i < ARMS_MAX; i++)
 	{
 		if (gArmsData[i].code == 0)
 			break;
 
-		RECT rcArms;
 		rcArms.left = 16 * (gArmsData[i].code % 16);
 		rcArms.right = rcArms.left + 16;
 		rcArms.top = 16 * (gArmsData[i].code / 16);
@@ -322,13 +324,13 @@
 		PutBitmap3(&rcView, 40 * i + (WINDOW_WIDTH - 224) / 2, (WINDOW_HEIGHT - 192) / 2, &rcArms, SURFACE_ID_ARMS_IMAGE);
 		PutBitmap3(&rcView, 40 * i + (WINDOW_WIDTH - 224) / 2, (WINDOW_HEIGHT - 128) / 2, &rcPer, SURFACE_ID_TEXT_BOX);
 		PutBitmap3(&rcView, 40 * i + (WINDOW_WIDTH - 224) / 2, (WINDOW_HEIGHT - 160) / 2, &rcLv, SURFACE_ID_TEXT_BOX);
-		PutNumber4(40 * i + (WINDOW_WIDTH - 224) / 2, (WINDOW_HEIGHT - 160) / 2, gArmsData[i].level, 0);
+		PutNumber4(40 * i + (WINDOW_WIDTH - 224) / 2, (WINDOW_HEIGHT - 160) / 2, gArmsData[i].level, FALSE);
 
 		// Draw ammo
 		if (gArmsData[i].max_num)
 		{
-			PutNumber4(40 * i + (WINDOW_WIDTH - 224) / 2, (WINDOW_HEIGHT - 144) / 2, gArmsData[i].num, 0);
-			PutNumber4(40 * i + (WINDOW_WIDTH - 224) / 2, (WINDOW_HEIGHT - 128) / 2, gArmsData[i].max_num, 0);
+			PutNumber4(40 * i + (WINDOW_WIDTH - 224) / 2, (WINDOW_HEIGHT - 144) / 2, gArmsData[i].num, FALSE);
+			PutNumber4(40 * i + (WINDOW_WIDTH - 224) / 2, (WINDOW_HEIGHT - 128) / 2, gArmsData[i].max_num, FALSE);
 		}
 		else
 		{
@@ -343,12 +345,11 @@
 	else
 		PutBitmap3(&rcView, 32 * (gSelectedItem % 6) + (WINDOW_WIDTH - 224) / 2, 16 * (gSelectedItem / 6) + (WINDOW_HEIGHT - 88) / 2, &rcCur2[1], SURFACE_ID_TEXT_BOX);
 
-	for (int i = 0; i < ITEM_MAX; i++)
+	for (i = 0; i < ITEM_MAX; i++)
 	{
 		if (gItemData[i].code == 0)
 			break;
 
-		RECT rcItem;
 		rcItem.left = 32 * (gItemData[i].code % 8);
 		rcItem.right = rcItem.left + 32;
 		rcItem.top = 16 * (gItemData[i].code / 8);
@@ -360,10 +361,12 @@
 
 int CampLoop()
 {
+	int arms_num;
+	char old_script_path[PATH_LENGTH];
+
 	RECT rcView = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
 
 	// Load the inventory script
-	char old_script_path[PATH_LENGTH];
 	GetTextScriptPath(old_script_path);
 
 	LoadTextScript2("ArmsItem.tsc");
@@ -373,7 +376,7 @@
 	gSelectedItem = 0;
 
 	// Run script
-	int arms_num = 0;
+	arms_num = 0;
 	for (; gArmsData[arms_num].code != 0;)
 		++arms_num;
 
@@ -388,7 +391,7 @@
 
 		if (gKeyTrg & KEY_ESCAPE)
 		{
-			switch (Call_Escape())
+			switch (Call_Escape(ghWnd))
 			{
 				case 0:
 					return 0;
@@ -430,7 +433,7 @@
 			}
 		}
 
-		if (!Flip_SystemTask())
+		if (!Flip_SystemTask(ghWnd))
 			return 0;
 	}
 
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -48,8 +48,10 @@
 
 #define FRAMERATE 20
 
-BOOL Flip_SystemTask()
+BOOL Flip_SystemTask(int hWnd)
 {
+	(void)hWnd;
+
 	while (true)
 	{
 		if (!SystemTask())
@@ -151,8 +153,10 @@
 	}
 }
 
-BOOL MakeSurface_Generic(int bxsize, int bysize, Surface_Ids surf_no)
+BOOL MakeSurface_Generic(int bxsize, int bysize, Surface_Ids surf_no, BOOL bSystem)
 {
+	(void)bSystem;
+
 	BOOL success = FALSE;
 
 #ifdef FIX_BUGS
@@ -251,7 +255,7 @@
 			}
 			else
 			{
-				if (create_surface == false || MakeSurface_Generic(surface->w, surface->h, surf_no))
+				if (create_surface == false || MakeSurface_Generic(surface->w, surface->h, surf_no, FALSE))
 				{
 					if (magnification == 1)
 					{
--- a/src/Draw.h
+++ b/src/Draw.h
@@ -52,7 +52,7 @@
 
 extern SURFACE surf[SURFACE_ID_MAX];
 
-BOOL Flip_SystemTask();
+BOOL Flip_SystemTask(int hWnd);
 BOOL StartDirectDraw(int lMagnification, int lColourDepth);
 void EndDirectDraw();
 void ReleaseSurface(int s);
@@ -60,7 +60,7 @@
 BOOL MakeSurface_Resource(const char *res, Surface_Ids surf_no);
 BOOL ReloadBitmap_File(const char *name, Surface_Ids surf_no);
 BOOL ReloadBitmap_Resource(const char *res, Surface_Ids surf_no);
-BOOL MakeSurface_Generic(int bxsize, int bysize, Surface_Ids surf_no);
+BOOL MakeSurface_Generic(int bxsize, int bysize, Surface_Ids surf_no, BOOL bSystem);
 void BackupSurface(Surface_Ids surf_no, RECT *rect);
 void PutBitmap3(RECT *rcView, int x, int y, RECT *rect, Surface_Ids surf_no);
 void PutBitmap4(RECT *rcView, int x, int y, RECT *rect, Surface_Ids surf_no);
--- a/src/Ending.cpp
+++ b/src/Ending.cpp
@@ -39,20 +39,27 @@
 // Draw casts
 void PutStripper()
 {
+	RECT rc;
+
 	for (int s = 0; s < MAX_STRIP; s++)
 	{
 		if (Strip[s].flag & 0x80)
 		{
 			// Draw text
-			RECT rc = {0, 16 * s, 320, 16 * s + 16};
-			PutBitmap3(&grcFull, (Strip[s].x + ((WINDOW_WIDTH - 320) << 8)) / 0x200, Strip[s].y / 0x200, &rc, SURFACE_ID_CREDIT_CAST);
+			rc.left = 0;
+			rc.right = 320;
+			rc.top = s * 0x10;
+			rc.bottom = rc.top + 0x10;
 
+			PutBitmap3(&grcFull, (Strip[s].x / 0x200) + ((WINDOW_WIDTH - 320) / 2), (Strip[s].y / 0x200), &rc, SURFACE_ID_CREDIT_CAST);
+
 			// Draw character
 			rc.left = 24 * (Strip[s].cast % 13);
 			rc.right = rc.left + 24;
 			rc.top = 24 * (Strip[s].cast / 13);
 			rc.bottom = rc.top + 24;
-			PutBitmap3(&grcFull, (Strip[s].x + ((WINDOW_WIDTH - 320) << 8)) / 0x200 - 24, Strip[s].y / 0x200 - 8, &rc, SURFACE_ID_CASTS);
+
+			PutBitmap3(&grcFull, (Strip[s].x / 0x200) + ((WINDOW_WIDTH - 320) / 2) - 24, (Strip[s].y / 0x200) - 8, &rc, SURFACE_ID_CASTS);
 		}
 	}
 }
@@ -60,34 +67,47 @@
 // Create a cast object
 void SetStripper(int x, int y, const char *text, int cast)
 {
-	for (int s = 0; s < MAX_STRIP; s++)
-	{
-		if (!(Strip[s].flag & 0x80))
-		{
-			// Initialize cast property
-			Strip[s].flag = 0x80;
-			Strip[s].x = x;
-			Strip[s].y = y;
-			Strip[s].cast = cast;
-			strcpy(Strip[s].str, text);
+	RECT rc;
+	int s;
 
-			// Draw text
-			RECT rc = {0, 16 * s, 320, 16 * s + 16};
-			CortBox2(&rc, 0, SURFACE_ID_CREDIT_CAST);
-			PutText2(0, 16 * s, text, RGB(0xFF, 0xFF, 0xFE), SURFACE_ID_CREDIT_CAST);
+	for (s = 0; s < MAX_STRIP; s++)
+		if (!(Strip[s].flag & 0x80))
 			break;
-		}
-	}
+
+	if (s == MAX_STRIP)
+		return;
+
+	// Initialize cast property
+	Strip[s].flag = 0x80;
+	Strip[s].x = x;
+	Strip[s].y = y;
+	Strip[s].cast = cast;
+	strcpy(Strip[s].str, text);
+
+	// Draw text
+	rc.left = 0;
+	rc.right = 320;
+	rc.top = s * 0x10;
+	rc.bottom = rc.top + 0x10;
+
+	CortBox2(&rc, 0, SURFACE_ID_CREDIT_CAST);
+	PutText2(0, rc.top, text, RGB(0xFF, 0xFF, 0xFE), SURFACE_ID_CREDIT_CAST);
 }
 
 // Regenerate cast text
 void RestoreStripper()
 {
+	RECT rc;
+
 	for (int s = 0; s < MAX_STRIP; s++)
 	{
 		if (Strip[s].flag & 0x80)
 		{
-			RECT rc = {0, 16 * s, 320, 16 * s + 16};
+			rc.left = 0;
+			rc.right = 320;
+			rc.top = s * 0x10;
+			rc.bottom = rc.top + 0x10;
+
 			CortBox2(&rc, 0, SURFACE_ID_CREDIT_CAST);
 			PutText2(0, rc.top, Strip[s].str, RGB(0xFF, 0xFF, 0xFE), SURFACE_ID_CREDIT_CAST);
 		}
@@ -121,8 +141,13 @@
 void PutIllust()
 {
 	RECT rcIllust = {0, 0, 160, 240};
+#if WINDOW_WIDTH != 320 || WINDOW_HEIGHT != 240
+	// Widescreen edit
 	RECT rcClip = {(WINDOW_WIDTH - 320) / 2, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
-	PutBitmap3(&rcClip, (Illust.x + ((WINDOW_WIDTH - 320) << 8)) / 0x200, (WINDOW_HEIGHT - 240) / 2, &rcIllust, SURFACE_ID_CREDITS_IMAGE);
+	PutBitmap3(&rcClip, (Illust.x / 0x200) + ((WINDOW_WIDTH - 320) / 2), (WINDOW_HEIGHT - 240) / 2, &rcIllust, SURFACE_ID_CREDITS_IMAGE);
+#else
+	PutBitmap3(&grcFull, (Illust.x / 0x200) + ((WINDOW_WIDTH - 320) / 2), (WINDOW_HEIGHT - 240) / 2, &rcIllust, SURFACE_ID_CREDITS_IMAGE);
+#endif
 }
 
 // Load illustration
@@ -151,8 +176,10 @@
 	}
 }
 
+const char *credit_script = "Credit.tsc";
+
 // Start playing credits
-bool StartCreditScript()
+BOOL StartCreditScript()
 {
 	// Clear previously existing credits data
 	if (Credit.pData)
@@ -163,22 +190,22 @@
 
 	// Open file
 	char path[PATH_LENGTH];
-	sprintf(path, "%s/%s", gDataPath, "Credit.tsc");
+	sprintf(path, "%s/%s", gDataPath, credit_script);
 
 	Credit.size = GetFileSizeLong(path);
 	if (Credit.size == -1)
-		return false;
+		return FALSE;
 
 	// Allocate buffer data
 	Credit.pData = (char*)malloc(Credit.size);
 	if (Credit.pData == NULL)
-		return false;
+		return FALSE;
 
 	FILE *fp = fopen(path, "rb");
 	if (fp == NULL)
 	{
-		printf("Couldn't open %s", path);
-		return false;
+		free(Credit.pData);
+		return FALSE;
 	}
 
 	// Read data
@@ -199,108 +226,144 @@
 
 	// Modify cliprect
 	grcGame.left = WINDOW_WIDTH / 2;
+#if WINDOW_WIDTH != 320 || WINDOW_HEIGHT != 240
 	// These three are non-vanilla: for wide/tallscreen support
 	grcGame.right = ((WINDOW_WIDTH - 320) / 2) + 320;
 	grcGame.top = (WINDOW_HEIGHT - 240) / 2;
 	grcGame.bottom = ((WINDOW_HEIGHT - 240) / 2) + 240;
+#endif
 
 	// Reload casts
 	if (!ReloadBitmap_File("casts", SURFACE_ID_CASTS))
-		return false;
+		return FALSE;
 
 	// Clear casts
 	memset(Strip, 0, sizeof(Strip));
-	return true;
+	return TRUE;
 }
 
 // Get number from text (4 digit)
 int GetScriptNumber(const char *text)
 {
-	return	1000 * text[0] - 48000 +
-			100 * text[1] - 4800 +
-			10 * text[2] - 480 +
-			text[3] - 48;
+	return (text[0] - '0') * 1000 +
+		(text[1] - '0') * 100 +
+		(text[2] - '0') * 10 +
+		text[3] - '0';
 }
 
 // Parse credits
 void ActionCredit_Read()
 {
-	while (Credit.offset < Credit.size)
+	int a, b, len;
+	char text[40];
+
+	while (1)
 	{
-		// Get character
-		unsigned char character = Credit.pData[Credit.offset];
+		if (Credit.offset >= Credit.size)
+			break;
 
-		int a, b, len;
-		switch (character)
+		switch (Credit.pData[Credit.offset])
 		{
 			case '[': // Create cast
 				// Get the range for the cast text
-				a = ++Credit.offset;
+				++Credit.offset;
 
+				a = Credit.offset;
+
 				while (Credit.pData[a] != ']')
 				{
 					if (IsShiftJIS(Credit.pData[a]))
 						a += 2;
 					else
-						a++;
+						a += 1;
 				}
 
 				len = a - Credit.offset;
 
 				// Copy the text to the cast text
-				char text[40];
-				memcpy(text, &Credit.pData[Credit.offset], a - Credit.offset);
+				memcpy(text, &Credit.pData[Credit.offset], len);
 				text[len] = 0;
 
 				// Get cast id
-				Credit.offset = a + 1;
-				len = GetScriptNumber(&Credit.pData[a + 1]);
+				Credit.offset = a;
+				len = GetScriptNumber(&Credit.pData[++Credit.offset]);
 
 				// Create cast object
-				SetStripper(Credit.start_x, (WINDOW_HEIGHT << 9) + 0x1000, text, len);
+				SetStripper(Credit.start_x, (WINDOW_HEIGHT * 0x200) + (8 * 0x200), text, len);
 
 				// Change offset
 				Credit.offset += 4;
 				return;
 
+			case '-': // Wait for X amount of frames
+				++Credit.offset;
+				Credit.wait = GetScriptNumber(&Credit.pData[Credit.offset]);
+				Credit.offset += 4;
+				Credit.mode = 2;
+				return;
+
+			case '+': // Change casts x-position
+				++Credit.offset;
+				Credit.start_x = GetScriptNumber(&Credit.pData[Credit.offset]) * 0x200;
+				Credit.offset += 4;
+				return;
+
+			case '/': // Stop credits
+				Credit.mode = 0;
+				return;
+
+			case '!': // Change music
+				++Credit.offset;
+				a = GetScriptNumber(&Credit.pData[Credit.offset]);
+				Credit.offset += 4;
+				ChangeMusic(a);
+				return;
+
+			case '~': // Start fading out music
+				++Credit.offset;
+				SetOrganyaFadeout();
+				return;
+
 			case 'j': // Jump to label
+				++Credit.offset;
+
 				// Get number
-				b = GetScriptNumber(&Credit.pData[++Credit.offset]);
+				b = GetScriptNumber(&Credit.pData[Credit.offset]);
 
 				// Change offset
 				Credit.offset += 4;
 
 				// Jump to specific label
-				while (Credit.offset < Credit.size)
+				if (1)
 				{
-					if (Credit.pData[Credit.offset] == 'l')
+					while (Credit.offset < Credit.size)
 					{
-						// What is this
-						a = GetScriptNumber(&Credit.pData[++Credit.offset]);
-						Credit.offset += 4;
-						if (b == a)
-							return;
+						if (Credit.pData[Credit.offset] == 'l')
+						{
+							// What is this
+							a = GetScriptNumber(&Credit.pData[++Credit.offset]);
+							Credit.offset += 4;
+							if (b == a)
+								break;
+						}
+						else if (IsShiftJIS(Credit.pData[Credit.offset]))
+						{
+							Credit.offset += 2;
+						}
+						else
+						{
+							++Credit.offset;
+						}
 					}
-					else if (IsShiftJIS(Credit.pData[Credit.offset]))
-					{
-						Credit.offset += 2;
-					}
-					else
-					{
-						++Credit.offset;
-					}
 				}
 
 				return;
 
-			case '~': // Start fading out music
+			case 'f': // Flag jump
 				++Credit.offset;
-				SetOrganyaFadeout();
-				return;
 
-			case 'f': // Flag jump
 				// Read numbers XXXX:YYYY
-				a = GetScriptNumber(&Credit.pData[++Credit.offset]);
+				a = GetScriptNumber(&Credit.pData[Credit.offset]);
 				Credit.offset += 5;
 				b = GetScriptNumber(&Credit.pData[Credit.offset]);
 				Credit.offset += 4;
@@ -316,7 +379,7 @@
 							a = GetScriptNumber(&Credit.pData[++Credit.offset]);
 							Credit.offset += 4;
 							if (b == a)
-								return;
+								break;
 						}
 						else if (IsShiftJIS(Credit.pData[Credit.offset]))
 						{
@@ -330,30 +393,11 @@
 				}
 				return;
 
-			case '+': // Change casts x-position
-				Credit.start_x = GetScriptNumber(&Credit.pData[++Credit.offset]) << 9;
-				Credit.offset += 4;
-				return;
-
-			case '-': // Wait for X amount of frames
-				Credit.wait = GetScriptNumber(&Credit.pData[++Credit.offset]);
-				Credit.offset += 4;
-				Credit.mode = 2;
-				return;
-
-			case '/': // Stop credits
-				Credit.mode = 0;
-				return;
-
-			case '!': // Change music
-				a = GetScriptNumber(&Credit.pData[++Credit.offset]);
-				Credit.offset += 4;
-				ChangeMusic(a);
-				return;
+			default:
+				// Progress through file
+				++Credit.offset;
+				break;
 		}
-
-		// Progress through file
-		++Credit.offset;
 	}
 }
 
@@ -360,17 +404,19 @@
 // Update credits
 void ActionCredit()
 {
-	if (Credit.offset < Credit.size)
+	if (Credit.offset >= Credit.size)
+		return;
+
+	// Update script, or if waiting, decrement the wait value
+	switch (Credit.mode)
 	{
-		// Update script, or if waiting, decrement the wait value
-		if (Credit.mode == 1)
-		{
+		case 1:
 			ActionCredit_Read();
-		}
-		else if (Credit.mode == 2 && --Credit.wait <= 0)
-		{
-			Credit.mode = 1;
-		}
+			break;
+
+		case 2:
+			if (--Credit.wait <= 0)
+				Credit.mode = 1;
 	}
 }
 
@@ -388,7 +434,7 @@
 }
 
 // Scene of the island falling
-int Scene_DownIsland(int mode)
+int Scene_DownIsland(int hWnd, int mode)
 {
 	// Setup background
 	RECT rc_frame = {(WINDOW_WIDTH - 160) / 2, (WINDOW_HEIGHT - 80) / 2, (WINDOW_WIDTH + 160) / 2, (WINDOW_HEIGHT + 80) / 2};
@@ -410,11 +456,13 @@
 		// Escape menu
 		if (gKey & 0x8000)
 		{
-			int escRet = Call_Escape();
-			if (escRet == 0)
-				return 0;
-			if (escRet == 2)
-				return 2;
+			switch (Call_Escape(hWnd))
+			{
+				case 0:
+					return 0;
+				case 2:
+					return 2;
+			}
 		}
 
 		switch (mode)
@@ -425,33 +473,27 @@
 				break;
 
 			case 1:
-				if (wait >= 350)
+				if (wait < 350)
 				{
-					if (wait >= 500)
-					{
-						if (wait >= 600)
-						{
-							// End scene
-							if (wait == 750)
-								wait = 900;
-						}
-						else
-						{
-							// Move down slow
-							sprite.y += 0xC;
-						}
-					}
-					else
-					{
-						// Move down slower
-						sprite.y += 0x19;
-					}
-				}
-				else
-				{
 					// Move down at normal speed
 					sprite.y += 0x33;
 				}
+				else if (wait < 500)
+				{
+					// Move down slower
+					sprite.y += 0x19;
+				}
+				else if (wait < 600)
+				{
+					// Move down slow
+					sprite.y += 0xC;
+				}
+				else if (wait == 750)
+				{
+					// End scene
+					wait = 900;
+				}
+
 				break;
 		}
 
@@ -464,7 +506,7 @@
 
 		// Draw window
 		PutFramePerSecound();
-		if (!Flip_SystemTask())
+		if (!Flip_SystemTask(hWnd))
 			return 0;
 	}
 
--- a/src/Ending.h
+++ b/src/Ending.h
@@ -1,5 +1,7 @@
 #pragma once
 
+#include "WindowsWrapper.h"
+
 struct CREDIT
 {
 	int size;
@@ -42,8 +44,8 @@
 void ReloadIllust(int a);
 void InitCreditScript();
 void ReleaseCreditScript();
-bool StartCreditScript();
+BOOL StartCreditScript();
 void ActionCredit();
 void SetCreditIllust(int a);
 void CutCreditIllust();
-int Scene_DownIsland(int mode);
+int Scene_DownIsland(int hWnd, int mode);
--- a/src/Escape.cpp
+++ b/src/Escape.cpp
@@ -7,7 +7,7 @@
 #include "KeyControl.h"
 #include "Main.h"
 
-int Call_Escape()
+int Call_Escape(int hWnd)
 {
 	RECT rc = {0, 128, 208, 144};
 
@@ -37,7 +37,7 @@
 		PutBitmap3(&grcFull, (WINDOW_WIDTH - 208) / 2, (WINDOW_HEIGHT - 16) / 2, &rc, SURFACE_ID_TEXT_BOX);
 		PutFramePerSecound();
 
-		if (!Flip_SystemTask())
+		if (!Flip_SystemTask(hWnd))
 		{
 			// Quit if window is closed
 			gKeyTrg = 0;
--- a/src/Escape.h
+++ b/src/Escape.h
@@ -1,3 +1,3 @@
 #pragma once
 
-int Call_Escape();
+int Call_Escape(int hWnd);
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -56,7 +56,7 @@
 	return min + rep_rand() % (max - min + 1);
 }
 
-void PutNumber4(int x, int y, int value, bool bZero)
+void PutNumber4(int x, int y, int value, BOOL bZero)
 {
 	// Define rects
 	RECT rcClient = grcFull;
@@ -141,7 +141,7 @@
 		// Escape menu
 		if (gKey & KEY_ESCAPE)
 		{
-			int escRet = Call_Escape();
+			int escRet = Call_Escape(ghWnd);
 			if (escRet == 0)
 				return 0;
 			if (escRet == 2)
@@ -189,11 +189,11 @@
 		if (tscRet == 2)
 			return 1;
 
-		PutMapName(false);
+		PutMapName(FALSE);
 		PutTextScript();
 		PutFramePerSecound();
 
-		if (!Flip_SystemTask())
+		if (!Flip_SystemTask(ghWnd))
 			return 0;
 
 		++gCounter;
@@ -204,7 +204,7 @@
 	{
 		CortBox(&grcGame, 0x000000);
 		PutFramePerSecound();
-		if (!Flip_SystemTask())
+		if (!Flip_SystemTask(ghWnd))
 			return 0;
 	}
 	return 2;
@@ -317,7 +317,7 @@
 	// Start loop
 	unsigned int wait = 0;
 
-	while (true)
+	while (1)
 	{
 		// Don't accept selection for 10 frames
 		if (wait < 10)
@@ -335,7 +335,7 @@
 
 		if (gKey & KEY_ESCAPE)
 		{
-			int escRet = Call_Escape();
+			int escRet = Call_Escape(ghWnd);
 			if (escRet == 0)
 				return 0;
 			if (escRet == 2)
@@ -365,10 +365,10 @@
 
 		int v1, v2, v3, v4;
 		GetCompileVersion(&v1, &v2, &v3, &v4);
-		PutNumber4((WINDOW_WIDTH - 40) / 2, WINDOW_HEIGHT - 24, v1, 0);
-		PutNumber4((WINDOW_WIDTH - 8) / 2, WINDOW_HEIGHT - 24, v2, 0);
-		PutNumber4((WINDOW_WIDTH + 24) / 2, WINDOW_HEIGHT - 24, v3, 0);
-		PutNumber4((WINDOW_WIDTH + 56) / 2, WINDOW_HEIGHT - 24, v4, 0);
+		PutNumber4((WINDOW_WIDTH - 40) / 2, WINDOW_HEIGHT - 24, v1, FALSE);
+		PutNumber4((WINDOW_WIDTH - 8) / 2, WINDOW_HEIGHT - 24, v2, FALSE);
+		PutNumber4((WINDOW_WIDTH + 24) / 2, WINDOW_HEIGHT - 24, v3, FALSE);
+		PutNumber4((WINDOW_WIDTH + 56) / 2, WINDOW_HEIGHT - 24, v4, FALSE);
 
 		// Draw main title
 		PutBitmap3(&grcGame, (WINDOW_WIDTH - 144) / 2, 40, &rcTitle, SURFACE_ID_TITLE);
@@ -420,7 +420,7 @@
 
 		PutFramePerSecound();
 
-		if (!Flip_SystemTask())
+		if (!Flip_SystemTask(ghWnd))
 			return 0;
 	}
 
@@ -433,7 +433,7 @@
 	{
 		CortBox(&grcGame, 0);
 		PutFramePerSecound();
-		if (!Flip_SystemTask())
+		if (!Flip_SystemTask(ghWnd))
 			return 0;
 	}
 
@@ -447,7 +447,7 @@
 
 	unsigned long color = GetCortBoxColor(RGB(0, 0, 0x20));
 
-	bool swPlay = true;
+	unsigned int swPlay = 1;
 
 	// Reset stuff
 	gCounter = 0;
@@ -471,7 +471,7 @@
 
 	if ((bContinue && LoadProfile(NULL)) || InitializeGame())
 	{
-		while (true)
+		while (1)
 		{
 			// Get pressed keys
 			GetTrg();
@@ -479,7 +479,7 @@
 			// Escape menu
 			if (gKey & KEY_ESCAPE)
 			{
-				int escRet = Call_Escape();
+				int escRet = Call_Escape(ghWnd);
 				if (escRet == 0)
 					return 0;
 				if (escRet == 2)
@@ -486,12 +486,12 @@
 					return 1;
 			}
 
-			if (swPlay & 1 && g_GameFlags & 1)
+			if (swPlay % 2 && g_GameFlags & 1)
 			{
 				if (g_GameFlags & 2)
-					ActMyChar(true);
+					ActMyChar(TRUE);
 				else
-					ActMyChar(false);
+					ActMyChar(FALSE);
 
 				ActStar();
 				ActNpChar();
@@ -515,9 +515,9 @@
 				ActFlash(frame_x, frame_y);
 
 				if (g_GameFlags & 2)
-					AnimationMyChar(true);
+					AnimationMyChar(TRUE);
 				else
-					AnimationMyChar(false);
+					AnimationMyChar(FALSE);
 			}
 
 			if (g_GameFlags & 8)
@@ -585,7 +585,7 @@
 					RotationArmsRev();
 			}
 
-			if (swPlay & 1)
+			if (swPlay % 2)
 			{
 				int tscRet = TextScriptProc();
 				if (tscRet == 0)
@@ -594,13 +594,13 @@
 					return 1;
 			}
 
-			PutMapName(false);
+			PutMapName(FALSE);
 			PutTimeCounter(16, 8);
 
 			if (g_GameFlags & 2)
 			{
-				PutMyLife(true);
-				PutArmsEnergy(true);
+				PutMyLife(TRUE);
+				PutArmsEnergy(TRUE);
 				PutMyAir((WINDOW_WIDTH - 80) / 2, (WINDOW_HEIGHT - 32) / 2);
 				PutActiveArmsList();
 			}
@@ -614,7 +614,7 @@
 			PutTextScript();
 
 			PutFramePerSecound();
-			if (!Flip_SystemTask())
+			if (!Flip_SystemTask(ghWnd))
 				break;
 			++gCounter;
 		}
@@ -623,7 +623,7 @@
 	return 0;
 }
 
-bool Game()
+BOOL Game()
 {
 	if (LoadGenericData())
 	{
@@ -655,9 +655,9 @@
 		}
 		else
 		{
-			return false;
+			return FALSE;
 		}
 	}
 
-	return true;
+	return TRUE;
 }
--- a/src/Game.h
+++ b/src/Game.h
@@ -1,9 +1,11 @@
 #pragma once
 
+#include "WindowsWrapper.h"
+
 extern int g_GameFlags;
 extern int gCounter;
 
 int Random(int min, int max);
-void PutNumber4(int x, int y, int value, bool bZero);
+void PutNumber4(int x, int y, int value, BOOL bZero);
 
-bool Game();
+BOOL Game();
--- a/src/Generic.cpp
+++ b/src/Generic.cpp
@@ -2,16 +2,18 @@
 
 #include <stdio.h>
 
+#include "WindowsWrapper.h"
+
 #include "CommonDefines.h"
 #include "Tags.h"
 
-bool GetCompileVersion(int *v1, int *v2, int *v3, int *v4)
+BOOL GetCompileVersion(int *v1, int *v2, int *v3, int *v4)
 {
 	*v1 = 1;
 	*v2 = 0;
 	*v3 = 0;
 	*v4 = 6;
-	return true;
+	return TRUE;
 }
 
 long GetFileSizeLong(const char *path)
@@ -29,7 +31,7 @@
 	return len;
 }
 
-bool CheckFileExists(const char *name)
+BOOL CheckFileExists(const char *name)
 {
 	char path[PATH_LENGTH];
 	sprintf(path, "%s/%s", gModulePath, name);
@@ -38,17 +40,17 @@
 	if (file)
 	{
 		fclose(file);
-		return true;
+		return TRUE;
 	}
 
-	return false;
+	return FALSE;
 }
 
-bool IsShiftJIS(unsigned char c)
+BOOL IsShiftJIS(unsigned char c)
 {
 	if (c > 0x80 && c < 0xA0)
-		return true;
+		return TRUE;
 	if (c < 0xE0 || c >= 0xF0)
-		return false;
-	return true;
+		return FALSE;
+	return TRUE;
 }
--- a/src/Generic.h
+++ b/src/Generic.h
@@ -1,6 +1,8 @@
 #pragma once
 
-bool GetCompileVersion(int *v1, int *v2, int *v3, int *v4);
+#include "WindowsWrapper.h"
+
+BOOL GetCompileVersion(int *v1, int *v2, int *v3, int *v4);
 long GetFileSizeLong(const char *path);
-bool CheckFileExists(const char *name);
-bool IsShiftJIS(unsigned char c);
+BOOL CheckFileExists(const char *name);
+BOOL IsShiftJIS(unsigned char c);
--- a/src/GenericLoad.cpp
+++ b/src/GenericLoad.cpp
@@ -197,16 +197,16 @@
 	}
 	else
 	{
-		MakeSurface_Generic(WINDOW_WIDTH, WINDOW_HEIGHT, SURFACE_ID_SCREEN_GRAB);
-		MakeSurface_Generic(WINDOW_WIDTH, WINDOW_HEIGHT, SURFACE_ID_LEVEL_BACKGROUND);
-		MakeSurface_Generic(WINDOW_WIDTH, WINDOW_HEIGHT, SURFACE_ID_MAP);
-		MakeSurface_Generic(WINDOW_WIDTH, WINDOW_HEIGHT, SURFACE_ID_CASTS);
-		MakeSurface_Generic(256, 256, SURFACE_ID_LEVEL_TILESET);
-		MakeSurface_Generic(160, 16, SURFACE_ID_ROOM_NAME);
-		MakeSurface_Generic(40, 240, SURFACE_ID_VALUE_VIEW);
-		MakeSurface_Generic(320, 240, SURFACE_ID_LEVEL_SPRITESET_1);
-		MakeSurface_Generic(320, 240, SURFACE_ID_LEVEL_SPRITESET_2);
-		MakeSurface_Generic(WINDOW_WIDTH, 16 * MAX_STRIP, SURFACE_ID_CREDIT_CAST);
+		MakeSurface_Generic(WINDOW_WIDTH, WINDOW_HEIGHT, SURFACE_ID_SCREEN_GRAB, TRUE);
+		MakeSurface_Generic(WINDOW_WIDTH, WINDOW_HEIGHT, SURFACE_ID_LEVEL_BACKGROUND, FALSE);
+		MakeSurface_Generic(WINDOW_WIDTH, WINDOW_HEIGHT, SURFACE_ID_MAP, TRUE);
+		MakeSurface_Generic(WINDOW_WIDTH, WINDOW_HEIGHT, SURFACE_ID_CASTS, FALSE);
+		MakeSurface_Generic(256, 256, SURFACE_ID_LEVEL_TILESET, FALSE);
+		MakeSurface_Generic(160, 16, SURFACE_ID_ROOM_NAME, FALSE);
+		MakeSurface_Generic(40, 240, SURFACE_ID_VALUE_VIEW, FALSE);
+		MakeSurface_Generic(320, 240, SURFACE_ID_LEVEL_SPRITESET_1, FALSE);
+		MakeSurface_Generic(320, 240, SURFACE_ID_LEVEL_SPRITESET_2, FALSE);
+		MakeSurface_Generic(WINDOW_WIDTH, 16 * MAX_STRIP, SURFACE_ID_CREDIT_CAST, FALSE);
 
 		pt_size = 0;
 		pt_size += MakePixToneObject(&gPtpTable[0], 2, 32);
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -32,6 +32,7 @@
 
 int gJoystickButtonTable[8];
 
+int ghWnd;	// Placeholder until we restore the WinAPI code
 bool gbUseJoystick = false;
 bool bFps = false;
 
@@ -61,7 +62,7 @@
 void PutFramePerSecound()
 {
 	if (bFps)
-		PutNumber4(WINDOW_WIDTH - 40, 8, GetFramePerSecound(), false);
+		PutNumber4(WINDOW_WIDTH - 40, 8, GetFramePerSecound(), FALSE);
 }
 
 int GetFramePerSecound()
@@ -349,7 +350,7 @@
 			PutBitmap3(&clip_rect, (WINDOW_WIDTH - 64) / 2, (WINDOW_HEIGHT - 8) / 2, &loading_rect, SURFACE_ID_LOADING);
 
 			// Draw to screen
-			if (Flip_SystemTask())
+			if (Flip_SystemTask(ghWnd))
 			{
 				// Initialize sound
 				InitDirectSound();
--- a/src/Main.h
+++ b/src/Main.h
@@ -1,4 +1,6 @@
 #pragma once
 
+extern int ghWnd;
+
 void PutFramePerSecound();
 int GetFramePerSecound();
--- a/src/MiniMap.cpp
+++ b/src/MiniMap.cpp
@@ -79,12 +79,16 @@
 int MiniMapLoop()
 {
 	int f;
+	int line;
+	unsigned char my_wait;
 
+	RECT rcMiniMap;
+	RECT rcView;
+
 	RECT my_rect = {0, 57, 1, 58};
 	int my_x = (gMC.x / 0x200 + 8) / 16;
 	int my_y = (gMC.y / 0x200 + 8) / 16;
 
-	RECT rcView;
 	for (f = 0; f <= 8; f++)
 	{
 		GetTrg();
@@ -91,7 +95,7 @@
 
 		if (gKey & KEY_ESCAPE)
 		{
-			switch (Call_Escape())
+			switch (Call_Escape(ghWnd))
 			{
 				case 0:
 					return 0;
@@ -107,15 +111,14 @@
 		rcView.top = (WINDOW_HEIGHT / 2) - gMap.length * f / 8 / 2;
 		rcView.bottom = (WINDOW_HEIGHT / 2) + gMap.length * f / 8 / 2;
 
-		PutMapName(true);
+		PutMapName(TRUE);
 		CortBox(&rcView, 0);
 
 		PutFramePerSecound();
-		if (!Flip_SystemTask())
+		if (!Flip_SystemTask(ghWnd))
 			return 0;
 	}
 
-	RECT rcMiniMap;
 	rcMiniMap.left = 0;
 	rcMiniMap.right = gMap.width;
 	rcMiniMap.top = 0;
@@ -125,9 +128,9 @@
 	rcView.bottom = --rcView.top + gMap.length + 2;
 	CortBox2(&rcMiniMap, 0, SURFACE_ID_MAP);
 
-	int line = 0;
-	unsigned char my_wait = 0;
-	while (true)
+	line = 0;
+	my_wait = 0;
+	while (1)
 	{
 		GetTrg();
 
@@ -136,7 +139,7 @@
 
 		if (gKey & KEY_ESCAPE)
 		{
-			switch (Call_Escape())
+			switch (Call_Escape(ghWnd))
 			{
 				case 0:
 					return 0;
@@ -161,13 +164,13 @@
 
 		PutBitmap3(&grcGame, rcView.left + 1, rcView.top + 1, &rcMiniMap, SURFACE_ID_MAP);
 
-		PutMapName(true);
+		PutMapName(TRUE);
 
 		if (++my_wait / 8 % 2)
 			PutBitmap3(&grcGame, my_x + rcView.left + 1, my_y + rcView.top + 1, &my_rect, SURFACE_ID_TEXT_BOX);
 
 		PutFramePerSecound();
-		if (!Flip_SystemTask())
+		if (!Flip_SystemTask(ghWnd))
 			return 0;
 	}
 
@@ -177,7 +180,7 @@
 
 		if (gKey & KEY_ESCAPE)
 		{
-			switch (Call_Escape())
+			switch (Call_Escape(ghWnd))
 			{
 				case 0:
 					return 0;
@@ -193,11 +196,11 @@
 		rcView.top = (WINDOW_HEIGHT / 2) - gMap.length * f / 8 / 2;
 		rcView.bottom = (WINDOW_HEIGHT / 2) + gMap.length * f / 8 / 2;
 
-		PutMapName(true);
+		PutMapName(TRUE);
 		CortBox(&rcView, 0);
 
 		PutFramePerSecound();
-		if (!Flip_SystemTask())
+		if (!Flip_SystemTask(ghWnd))
 			return 0;
 	}
 
--- a/src/MycParam.cpp
+++ b/src/MycParam.cpp
@@ -252,8 +252,8 @@
 	// Draw max ammo
 	if (gArmsData[gSelectedArms].max_num)
 	{
-		PutNumber4(gArmsEnergyX + 32, 16, gArmsData[gSelectedArms].num, 0);
-		PutNumber4(gArmsEnergyX + 32, 24, gArmsData[gSelectedArms].max_num, 0);
+		PutNumber4(gArmsEnergyX + 32, 16, gArmsData[gSelectedArms].num, FALSE);
+		PutNumber4(gArmsEnergyX + 32, 24, gArmsData[gSelectedArms].max_num, FALSE);
 	}
 	else
 	{
@@ -267,7 +267,7 @@
 
 	PutBitmap3(&rcView, gArmsEnergyX + 32, 24, &rcPer, SURFACE_ID_TEXT_BOX);
 	PutBitmap3(&rcView, gArmsEnergyX, 32, &rcLv, SURFACE_ID_TEXT_BOX);
-	PutNumber4(gArmsEnergyX - 8, 32, gArmsData[gSelectedArms].level, 0);
+	PutNumber4(gArmsEnergyX - 8, 32, gArmsData[gSelectedArms].level, FALSE);
 
 	SET_RECT(rcExpBox, 0, 72, 40, 80)
 	SET_RECT(rcExpVal, 0, 80, 0, 88)
@@ -366,7 +366,7 @@
 	PutBitmap3(&grcGame, 16, 40, &rcCase, SURFACE_ID_TEXT_BOX);
 	PutBitmap3(&grcGame, 40, 40, &rcBr, SURFACE_ID_TEXT_BOX);
 	PutBitmap3(&grcGame, 40, 40, &rcLife, SURFACE_ID_TEXT_BOX);
-	PutNumber4(8, 40, gMC.lifeBr, 0);
+	PutNumber4(8, 40, gMC.lifeBr, FALSE);
 }
 
 void PutMyAir(int x, int y)
@@ -383,7 +383,7 @@
 	{
 		// Draw how much air is left
 		if (gMC.air_get % 6 < 4)
-			PutNumber4(x + 32, y, gMC.air / 10, 0);
+			PutNumber4(x + 32, y, gMC.air / 10, FALSE);
 
 		// Draw "AIR" text
 		if (gMC.air % 30 > 10)
@@ -420,9 +420,9 @@
 		}
 
 		// Draw time
-		PutNumber4(x,		y, time_count / (60 * 50),	false);
-		PutNumber4(x + 20,	y, time_count / 50 % 60,	true);
-		PutNumber4(x + 32,	y, time_count / 5 % 10,		false);
+		PutNumber4(x,		y, time_count / (60 * 50),	FALSE);
+		PutNumber4(x + 20,	y, time_count / 50 % 60,	TRUE);
+		PutNumber4(x + 32,	y, time_count / 5 % 10,		FALSE);
 		PutBitmap3(&grcGame, x + 30, y, &rcTime[2], SURFACE_ID_TEXT_BOX);
 	}
 	else
--- a/src/Profile.cpp
+++ b/src/Profile.cpp
@@ -45,7 +45,7 @@
 {
 	PROFILE profile;
 	FILE *fp;
-	char *FLAG = "FLAG";
+	const char *FLAG = "FLAG";
 	char path[PATH_LENGTH];
 
 	//Get path
@@ -254,12 +254,12 @@
 	InitFlags();
 	if (!TransferStage(13, 200, 10, 8))
 	{
-		// TODO - restore this when hWnd is available
+		// TODO - restore this when ghWnd is available
 /*#if defined(NONPORTABLE) && defined(WINDOWS)
 #ifdef JAPANESE
-		MessageBoxA(hWnd, "�X�e�[�W�̓ǂݍ��݂Ɏ��s", "�G���[", MB_OK);
+		MessageBoxA(ghWnd, "�X�e�[�W�̓ǂݍ��݂Ɏ��s", "�G���[", MB_OK);
 #else
-		MessageBoxA(hWnd, "Failed to load stage", "Error", MB_OK);
+		MessageBoxA(ghWnd, "Failed to load stage", "Error", MB_OK);
 #endif
 #else*/
 #ifdef JAPANESE
--- a/src/SelStage.cpp
+++ b/src/SelStage.cpp
@@ -21,73 +21,82 @@
 	memset(gPermitStage, 0, 0x40);
 }
 
-bool AddPermitStage(int index, int event)
+BOOL AddPermitStage(int index, int event)
 {
-	for (int i = 0; i < 8; ++i)
+	int i = 0;
+	while (i < 8)
 	{
-		if (gPermitStage[i].index == 0 || gPermitStage[i].index == index)
-		{
-			gPermitStage[i].index = index;
-			gPermitStage[i].event = event;
-			return true;
-		}
+		if (gPermitStage[i].index == index)
+			break;
+
+		if (gPermitStage[i].index == 0)
+			break;
+
+		++i;
 	}
 
-	return false;
+	if (i == 8)
+		return FALSE;
+
+	gPermitStage[i].index = index;
+	gPermitStage[i].event = event;
+
+	return TRUE;
 }
 
-bool SubPermitStage(int index)
+BOOL SubPermitStage(int index)
 {
 	int i;
-	for (i = 0; i < 8 && gPermitStage[i].index != index; ++i);
+	for (i = 0; i < 8; ++i)
+		if (gPermitStage[i].index == index)
+			break;
 
 #ifdef FIX_BUGS
-	if (i != 8)
+	if (i == 8)
 #else
-	if (i != 32)
+	if (i == 32)
 #endif
-	{
-		int ia;
-		for (ia = i + 1; ia < 8; ++ia)
-		{
-			gPermitStage[ia - 1].index = gPermitStage[ia].index;
-			gPermitStage[ia - 1].event = gPermitStage[ia].event;
-		}
+		return FALSE;
 
-		gPermitStage[ia - 1].index = 0;
-		gPermitStage[ia - 1].event = 0;
-
-		return true;
+	for (i += 1; i < 8; ++i)
+	{
+		gPermitStage[i - 1] = gPermitStage[i];
 	}
 
-	return false;
+	gPermitStage[i - 1].index = 0;
+	gPermitStage[i - 1].event = 0;
+
+	return TRUE;
 }
 
 void MoveStageSelectCursor(void)
 {
-	int stage_num;
-	for (stage_num = 0; gPermitStage[stage_num].index != 0; ++stage_num);
+	int stage_num = 0;
+	while (gPermitStage[stage_num].index != 0)
+		++stage_num;
 
-	if (stage_num)
-	{
-		if (gKeyTrg & gKeyLeft)
-			--gSelectedStage;
+	int stage_x = (WINDOW_WIDTH - 40 * stage_num) / 2;	// Unused
 
-		if (gKeyTrg & gKeyRight)
-			++gSelectedStage;
+	if (stage_num == 0)
+		return;
 
-		if (gSelectedStage < 0)
-			gSelectedStage = stage_num - 1;
+	if (gKeyTrg & gKeyLeft)
+		--gSelectedStage;
 
-		if (stage_num - 1 < gSelectedStage)
-			gSelectedStage = 0;
+	if (gKeyTrg & gKeyRight)
+		++gSelectedStage;
 
-		if ((gKeyRight | gKeyLeft) & gKeyTrg)
-			StartTextScript(gPermitStage[gSelectedStage].index + 1000);
+	if (gSelectedStage < 0)
+		gSelectedStage = stage_num - 1;
 
-		if ((gKeyRight | gKeyLeft) & gKeyTrg)
-			PlaySoundObject(1, 1);
-	}
+	if (gSelectedStage > stage_num - 1)
+		gSelectedStage = 0;
+
+	if ((gKeyLeft | gKeyRight) & gKeyTrg)
+		StartTextScript(gPermitStage[gSelectedStage].index + 1000);
+
+	if ((gKeyLeft | gKeyRight) & gKeyTrg)
+		PlaySoundObject(1, 1);
 }
 
 void PutStageSelectObject(void)
@@ -103,25 +112,33 @@
 
 	RECT rcTitle1 = {80, 64, 144, 72};
 
+	int i;
+	int stage_num;
+	int stage_x;
+	RECT rcStage;
+
 	if (gStageSelectTitleY > (WINDOW_HEIGHT / 2) - 74)
 		--gStageSelectTitleY;
 
 	PutBitmap3(&rcView, (WINDOW_WIDTH / 2) - 32, gStageSelectTitleY, &rcTitle1, SURFACE_ID_TEXT_BOX);
 
-	int stage_num;
-	for (stage_num = 0; gPermitStage[stage_num].index; ++stage_num);
+	stage_num = 0;
+	while (gPermitStage[stage_num].index)
+		++stage_num;
 
 	++flash;
 
 	if (stage_num)
 	{
-		int stage_x = (WINDOW_WIDTH - 40 * stage_num) / 2;
+		stage_x = (WINDOW_WIDTH - 40 * stage_num) / 2;
 
 		PutBitmap3(&rcView, stage_x + 40 * gSelectedStage, (WINDOW_HEIGHT / 2) - 56, &rcCur[(flash >> 1) % 2], SURFACE_ID_TEXT_BOX);
 
-		for (int i = 0; i < 8 && gPermitStage[i].index; ++i)
+		for (i = 0; i < 8; ++i)
 		{
-			RECT rcStage;
+			if (gPermitStage[i].index == 0)
+				break;
+
 			rcStage.left = 32 * (gPermitStage[i].index % 8);
 			rcStage.right = rcStage.left + 32;
 			rcStage.top = 16 * (gPermitStage[i].index / 8);
@@ -145,26 +162,30 @@
 	gStageSelectTitleY = (WINDOW_HEIGHT / 2) - 66;
 	StartTextScript(gPermitStage[gSelectedStage].index + 1000);
 
-	do
+	for (;;)
 	{
 		GetTrg();
 
 		if (gKey & KEY_ESCAPE)
 		{
-			int escRet = Call_Escape();
-			if (escRet == 0)
-				return 0;
-			if (escRet == 2)
-				return 2;
+			switch (Call_Escape(ghWnd))
+			{
+				case 0:
+					return 0;
+				case 2:
+					return 2;
+			}
 		}
 
 		MoveStageSelectCursor();
 
-		int tscRet = TextScriptProc();
-		if (tscRet == 0)
-			return 0;
-		if (tscRet == 2)
-			return 2;
+		switch (TextScriptProc())
+		{
+			case 0:
+				return 0;
+			case 2:
+				return 2;
+		}
 
 #ifdef FIX_BUGS
 		PutBitmap4(&rcView, 0, 0, &rcView, SURFACE_ID_SCREEN_GRAB);
@@ -178,12 +199,9 @@
 		if (gKeyTrg & gKeyOk)
 		{
 			StopTextScript();
-			LoadTextScript_Stage(old_script_path);
-			*p_event = gPermitStage[gSelectedStage].event;
-			return 1;
+			break;
 		}
-
-		if (gKeyTrg & gKeyCancel)
+		else if (gKeyTrg & gKeyCancel)
 		{
 			StopTextScript();
 			LoadTextScript_Stage(old_script_path);
@@ -192,8 +210,12 @@
 		}
 
 		PutFramePerSecound();
+
+		if (!Flip_SystemTask(ghWnd))
+			return 0;
 	}
-	while (Flip_SystemTask());
 
-	return 0;
+	LoadTextScript_Stage(old_script_path);
+	*p_event = gPermitStage[gSelectedStage].event;
+	return 1;
 }
--- a/src/SelStage.h
+++ b/src/SelStage.h
@@ -1,5 +1,7 @@
 #pragma once
 
+#include "WindowsWrapper.h"
+
 struct PERMIT_STAGE
 {
 	int index;
@@ -9,8 +11,8 @@
 extern PERMIT_STAGE gPermitStage[8];
 
 void ClearPermitStage(void);
-bool AddPermitStage(int index, int event);
-bool SubPermitStage(int index);
+BOOL AddPermitStage(int index, int event);
+BOOL SubPermitStage(int index);
 void MoveStageSelectCursor(void);
 void PutStageSelectObject(void);
 int StageSelectLoop(int *p_event);
--- a/src/TextScr.cpp
+++ b/src/TextScr.cpp
@@ -21,6 +21,7 @@
 #include "Game.h"
 #include "Generic.h"
 #include "KeyControl.h"
+#include "Main.h"
 #include "Map.h"
 #include "MapName.h"
 #include "MiniMap.h"
@@ -64,7 +65,7 @@
 
 	//Create line surfaces
 	for (int i = 0; i < 4; i++)
-		MakeSurface_Generic(gRect_line.right, gRect_line.bottom, (Surface_Ids)(i + SURFACE_ID_TEXT_LINE1));
+		MakeSurface_Generic(gRect_line.right, gRect_line.bottom, (Surface_Ids)(i + SURFACE_ID_TEXT_LINE1), FALSE);
 
 	//Clear text
 	memset(text, 0, sizeof(text));
@@ -93,6 +94,8 @@
 void EncryptionBinaryData2(unsigned char *pData, int size)
 {
 	int val1;
+	int work;
+	int i;
 
 	int half = size / 2;
 	if (pData[half] == 0)
@@ -100,9 +103,9 @@
 	else
 		val1 = (pData[half] % 256) * -1;
 
-	for (int i = 0; i < size; i++)
+	for (i = 0; i < size; i++)
 	{
-		int work = pData[i];
+		work = pData[i];
 		work += val1;
 
 		if (i != half)
@@ -219,7 +222,7 @@
 
 	gTS.rcText.left = TEXT_LEFT;
 	gTS.rcText.top = WINDOW_HEIGHT - 56;
-	gTS.rcText.right = WINDOW_WIDTH + 108;
+	gTS.rcText.right = WINDOW_WIDTH - TEXT_LEFT;
 	gTS.rcText.bottom = gTS.rcText.top + 48;
 
 	/* This is present in the Linux port, but not the Windows version (1.0.0.6, at least)
@@ -336,6 +339,13 @@
 //Type a number into the text buffer
 void SetNumberTextScript(int index)
 {
+	int a;
+	int b;
+	int i;
+	BOOL bZero;
+	int offset;
+	char str[5];
+
 	//Get digit table
 	int table[3];
 	table[0] = 1000;
@@ -343,15 +353,13 @@
 	table[2] = 10;
 
 	//Get number to print
-	int a = gNumberTextScript[index];
-	int b;
+	a = gNumberTextScript[index];
 
-	char str[5];
-	BOOL bZero = false;
-	int offset = 0;
+	bZero = FALSE;
+	offset = 0;
 
 	//Trim leading zeroes
-	for (int i = 0; i < 3; i++)
+	for (i = 0; i < 3; i++)
 	{
 		if (a / table[i] || bZero != FALSE)
 		{
@@ -364,7 +372,7 @@
 	}
 
 	//Set last digit of string, and add null terminator
-	str[offset] = a + '0';
+	str[offset] = (char)a + '0';
 	str[offset + 1] = 0;
 
 	//Append number to line
@@ -404,6 +412,18 @@
 //Draw textbox and whatever else
 void PutTextScript()
 {
+	RECT rcFace;
+	RECT rcItemBox1;
+	RECT rcItemBox2;
+	RECT rcItemBox3;
+	RECT rcItemBox4;
+	RECT rcItemBox5;
+	int i;
+	RECT rect_yesno;
+	RECT rect_cur;
+	RECT rect;
+	int text_offset;
+
 	if (gTS.mode == 0)
 		return;
 
@@ -430,7 +450,6 @@
 		RECT rcFrame3 = {0, 16, 244, 24};
 
 		PutBitmap3(&grcFull, WINDOW_WIDTH / 2 - 122, gTS.rcText.top - 10, &rcFrame1, SURFACE_ID_TEXT_BOX);
-		int i;
 		for (i = 1; i < 7; i++)
 			PutBitmap3(&grcFull, WINDOW_WIDTH / 2 - 122, 8 * i + gTS.rcText.top - 10, &rcFrame2, SURFACE_ID_TEXT_BOX);
 		PutBitmap3(&grcFull, WINDOW_WIDTH / 2 - 122, 8 * i + gTS.rcText.top - 10, &rcFrame3, SURFACE_ID_TEXT_BOX);
@@ -437,7 +456,6 @@
 	}
 
 	//Draw face picture
-	RECT rcFace;
 	rcFace.left = 48 * (gTS.face % 6);
 	rcFace.top = 48 * (gTS.face / 6);
 	rcFace.right = rcFace.left + 48;
@@ -445,23 +463,22 @@
 
 	if (gTS.face_x < (TEXT_LEFT * 0x200))
 		gTS.face_x += 0x1000;
+
 	PutBitmap3(&gTS.rcText, gTS.face_x / 0x200, gTS.rcText.top - 3, &rcFace, SURFACE_ID_FACE);
 
 	//Draw text
-	int text_offset;
 	if (gTS.face)
 		text_offset = 56;
 	else
 		text_offset = 0;
 
-	for (int i = 0; i < 4; i++)
+	for (i = 0; i < 4; i++)
 		PutBitmap3(&gTS.rcText, text_offset + TEXT_LEFT, gTS.offsetY + gTS.ypos_line[i] + gTS.rcText.top, &gRect_line, (Surface_Ids)(i + SURFACE_ID_TEXT_LINE1));
 
 	//Draw NOD cursor
 	if ((gTS.wait_beam++ % 20 > 12) && gTS.mode == 2)
 	{
-		RECT rect;
-		rect.left = TEXT_LEFT + text_offset + 6 * gTS.p_write;
+		rect.left = TEXT_LEFT + 6 * gTS.p_write + text_offset;
 		rect.top = gTS.ypos_line[gTS.line % 4] + gTS.rcText.top + gTS.offsetY;
 		rect.right = rect.left + 5;
 		rect.bottom = rect.top + 11;
@@ -481,11 +498,11 @@
 	}
 
 	//Draw GIT
-	RECT rcItemBox1 = {0, 0, 72, 16};
-	RECT rcItemBox2 = {0, 8, 72, 24};
-	RECT rcItemBox3 = {240, 0, 244, 8};
-	RECT rcItemBox4 = {240, 8, 244, 16};
-	RECT rcItemBox5 = {240, 16, 244, 24};
+	SET_RECT(rcItemBox1, 0, 0, 72, 16)
+	SET_RECT(rcItemBox2, 0, 8, 72, 24)
+	SET_RECT(rcItemBox3, 240, 0, 244, 8)
+	SET_RECT(rcItemBox4, 240, 8, 244, 16)
+	SET_RECT(rcItemBox5, 240, 16, 244, 24)
 
 	if (gTS.item)
 	{
@@ -499,7 +516,6 @@
 		if (gTS.item_y < WINDOW_HEIGHT - 104)
 			++gTS.item_y;
 
-		RECT rect;
 		if (gTS.item < 1000)
 		{
 			rect.left = 16 * (gTS.item % 16);
@@ -519,12 +535,11 @@
 	}
 
 	//Draw Yes / No selection
-	RECT rect_yesno = {152, 48, 244, 80};
-	RECT rect_cur = {112, 88, 128, 104};
+	SET_RECT(rect_yesno, 152, 48, 244, 80)
+	SET_RECT(rect_cur, 112, 88, 128, 104)
 
 	if (gTS.mode == 6)
 	{
-		int i;
 		if (gTS.wait < 2)
 			i = (WINDOW_HEIGHT - 96) + (2 - gTS.wait) * 4;
 		else
@@ -539,10 +554,14 @@
 //Parse TSC
 int TextScriptProc()
 {
-	RECT rcSymbol = {64, 48, 72, 56};
-
 	BOOL bExit;
+	char c[3];
+	int w, x, y, z;
+	int i;
+	int length;
 
+	RECT rcSymbol = {64, 48, 72, 56};
+
 	switch (gTS.mode)
 	{
 		case 1: //PARSE
@@ -558,7 +577,6 @@
 			gTS.wait = 0;
 
 			//Parsing time
-			int w, x, y, z;
 			bExit = FALSE;
 
 			while (bExit == FALSE)
@@ -606,13 +624,13 @@
 					else if (IS_COMMAND('E','Q','+'))
 					{
 						z = GetTextScriptNo(gTS.p_read + 4);
-						EquipItem(z, true);
+						EquipItem(z, TRUE);
 						gTS.p_read += 8;
 					}
 					else if (IS_COMMAND('E','Q','-'))
 					{
 						z = GetTextScriptNo(gTS.p_read + 4);
-						EquipItem(z, false);
+						EquipItem(z, FALSE);
 						gTS.p_read += 8;
 					}
 					else if (IS_COMMAND('A','M','+'))
@@ -682,7 +700,7 @@
 							SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Failed to load stage", NULL);
 							#endif
 
-							//MessageBoxA(hWnd, "�X�e�[�W�̓ǂݍ��݂Ɏ��s", "�G���[", 0);
+							//MessageBoxA(ghWnd, "�X�e�[�W�̓ǂݍ��݂Ɏ��s", "�G���[", 0);
 							return 0;
 						}
 					}
@@ -695,12 +713,12 @@
 					}
 					else if (IS_COMMAND('H','M','C'))
 					{
-						ShowMyChar(false);
+						ShowMyChar(FALSE);
 						gTS.p_read += 4;
 					}
 					else if (IS_COMMAND('S','M','C'))
 					{
-						ShowMyChar(true);
+						ShowMyChar(TRUE);
 						gTS.p_read += 4;
 					}
 					else if (IS_COMMAND('F','L','+'))
@@ -731,7 +749,7 @@
 					{
 						g_GameFlags &= ~2;
 						g_GameFlags |= 1;
-						gMC.up = false;
+						gMC.up = FALSE;
 						gMC.shock = 0;
 						gTS.p_read += 4;
 					}
@@ -1207,7 +1225,7 @@
 						bExit = TRUE;
 						z = GetTextScriptNo(gTS.p_read + 4);
 
-						switch (Scene_DownIsland(z))
+						switch (Scene_DownIsland(ghWnd, z))
 						{
 							case 0:
 								return 0;
@@ -1224,7 +1242,6 @@
 					else
 					{
 						char str_0[0x40];
-
 						#ifdef JAPANESE
 						sprintf(str_0, "�s���̃R�[�h:<%c%c%c", gTS.data[gTS.p_read + 1], gTS.data[gTS.p_read + 2], gTS.data[gTS.p_read + 3]);
 						SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "�G���[", str_0, NULL);
@@ -1255,6 +1272,7 @@
 					else if (gTS.flags & 0x10)
 					{
 						//SAT/CAT/TUR printing
+						char str[72];
 						x = gTS.p_read;
 						//Break if reaches command, or new-line
 						while (gTS.data[x] != '<' && gTS.data[x] != '\r')
@@ -1267,8 +1285,7 @@
 						}
 
 						//Get text to copy
-						char str[72];
-						int length = x - gTS.p_read;
+						length = x - gTS.p_read;
 						memcpy(str, &gTS.data[gTS.p_read], length);
 						str[length] = 0;
 
@@ -1289,7 +1306,6 @@
 					else
 					{
 						//Get text to print
-						char c[3];
 						c[0] = gTS.data[gTS.p_read];
 
 						if (c[0] & 0x80)
@@ -1348,7 +1364,7 @@
 			break;
 
 		case 3: //NEW LINE
-			for (int i = 0; i < 4; i++)
+			for (i = 0; i < 4; i++)
 			{
 				gTS.ypos_line[i] -= 4;
 
--- a/src/TextScr.h
+++ b/src/TextScr.h
@@ -21,7 +21,7 @@
 
 	//Current positions (read position in buffer, x position in line)
 	unsigned int p_read;
-	unsigned int p_write;
+	int p_write;
 
 	//Current line to write to
 	int line;