shithub: cstory

Download patch

ref: a6ac6787d3d884b4bc9e58a8c385da25d0413fe7
parent: 1f179f9287ef7c10c9cbdaebb2d8f0f260d57a18
author: Clownacy <[email protected]>
date: Sun May 5 10:03:16 EDT 2019

Embed the fonts in the EXE

Now CSE2.exe should be drop-in replacement for Doukutsu.exe, with
no extra files needed.

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -255,6 +255,13 @@
 	target_compile_definitions(CSE2 PRIVATE _CRT_SECURE_NO_WARNINGS)	# Shut up those stupid warnings
 endif()
 
+# Decide the embedded font
+if(JAPANESE)
+	list(APPEND RESOURCES "FONT/msgothic.ttc")
+elseif(NOT WIN32)
+	list(APPEND RESOURCES "FONT/cour.ttf")
+endif()
+
 # Magic to convert resources to header files
 add_executable(bin2h "src/misc/bin2h.c")
 if(MSVC)
--- a/Makefile
+++ b/Makefile
@@ -183,8 +183,13 @@
 
 ifeq ($(JAPANESE), 1)
 	RESOURCES += BITMAP/pixel_jp.bmp
+	RESOURCES += FONT/msgothic.ttc
 else
 	RESOURCES += BITMAP/pixel.bmp
+
+	ifneq ($(WINDOWS), 1)
+		RESOURCES += FONT/cour.ttf
+	endif
 endif
 
 ifneq ($(WINDOWS), 1)
binary files a/build_en/font/cour.ttf /dev/null differ
binary files a/build_jp/font/msgothic.ttc /dev/null differ
binary files /dev/null b/res/FONT/cour.ttf differ
binary files /dev/null b/res/FONT/msgothic.ttc differ
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -525,9 +525,6 @@
 
 	HFONT hfont = CreateFontA(fontHeight, fontWidth, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, charset, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE, font_name);
 
-	if (hfont == NULL)
-		hfont = CreateFontA(fontHeight, fontWidth, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, charset, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE, NULL);
-
 	if (hfont != NULL)
 	{
 		HDC hdc = CreateCompatibleDC(NULL);
@@ -539,7 +536,7 @@
 
 			if (size != GDI_ERROR)
 			{
-				buffer = new unsigned char[size];
+				buffer = (unsigned char*)malloc(size);
 
 				if (data_size != NULL)
 					*data_size = size;
@@ -546,7 +543,7 @@
 
 				if (GetFontData(hdc, 0, 0, buffer, size) != size)
 				{
-					delete[] buffer;
+					free(buffer);
 					buffer = NULL;
 				}
 			}
@@ -575,34 +572,40 @@
 		fontWidth = 5 * magnification;
 		fontHeight = 10 * magnification;
 //	}
-	
+
+	size_t data_size;
 #ifdef WINDOWS
 	// Actually use the font Config.dat specifies
-	size_t data_size;
-	unsigned char *data = GetFontFromWindows(&data_size, font_name, fontWidth, fontHeight);
-
-	if (data != NULL)
+	unsigned char *data;
+	data = GetFontFromWindows(&data_size, font_name, fontWidth, fontHeight);
+	if (data)
 	{
 		gFont = LoadFontFromData(data, data_size, fontWidth, fontHeight);
+		free(data);
+	}
 
-		delete[] data;
+	if (gFont)
+		return;
 
-		if (gFont)
-			return;
+#ifndef JAPANESE
+	// Fall back on a default font
+	data = GetFontFromWindows(&data_size, "Courier New", fontWidth, fontHeight);
+	if (data)
+	{
+		gFont = LoadFontFromData(data, data_size, fontWidth, fontHeight);
+		free(data);
 	}
+
+	if (gFont)
+		return;
 #endif
-	// Fall back on the built-in fonts
+#endif
+	// Fall back on the built-in font
 	(void)font_name;
+	const unsigned char *res_data = FindResource("DEFAULT_FONT", "FONT", &data_size);
 
-	//Open Font.ttf
-	char path[PATH_LENGTH];
-#ifdef JAPANESE
-	sprintf(path, "%s/font/msgothic.ttc", gModulePath);
-#else
-	sprintf(path, "%s/font/cour.ttf", gModulePath);
-#endif
-
-	gFont = LoadFont(path, fontWidth, fontHeight);
+	if (res_data != NULL)
+		gFont = LoadFontFromData(res_data, data_size, fontWidth, fontHeight);
 }
 
 void PutText(int x, int y, const char *text, uint32_t color)
--- a/src/Resource.cpp
+++ b/src/Resource.cpp
@@ -74,6 +74,14 @@
 #include "Resource/CURSOR/CURSOR_IKA.bmp.h"
 #include "Resource/CURSOR/CURSOR_NORMAL.bmp.h"
 
+#ifdef JAPANESE
+#include "Resource/FONT/msgothic.ttc.h"
+#else
+#ifndef WINDOWS
+#include "Resource/FONT/cour.ttf.h"
+#endif
+#endif
+
 static const struct
 {
 	const char *type;
@@ -156,6 +164,13 @@
 	{"CURSOR", "CURSOR_NORMAL", rCURSOR_NORMAL, sizeof(rCURSOR_NORMAL)},
 	{"CURSOR", "CURSOR_IKA", rCURSOR_IKA, sizeof(rCURSOR_IKA)},
 
+#ifdef JAPANESE
+	{"FONT", "DEFAULT_FONT", rmsgothic, sizeof(rmsgothic)},
+#else
+#ifndef WINDOWS
+	{"FONT", "DEFAULT_FONT", rcour, sizeof(rcour)},
+#endif
+#endif
 };
 
 const unsigned char* FindResource(const char *name, const char *type, size_t *size)