From a0af5ef10caf486c41e00177ff7a62ec0c9344ef Mon Sep 17 00:00:00 2001 From: kichikuou Date: Sun, 24 May 2020 21:54:50 +0900 Subject: [PATCH] Windows: Embed default font as a resource After this, embedded MTLc3m.ttf is used unless the -fontfile option is specified. --- src/CMakeLists.txt | 4 +--- src/common.h | 5 +++++ src/res3/Script1.rc | 6 ++++++ src/resource.cpp | 25 +++++++++++++++++++++++++ src/sdlmain.cpp | 2 +- src/sys/ags.cpp | 42 ++++++++++++++++++++++++++++++------------ src/sys/ags.h | 1 + src/sys/dri.cpp | 28 ---------------------------- 8 files changed, 69 insertions(+), 44 deletions(-) create mode 100644 src/resource.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1cf3635..efb9586 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,7 +17,6 @@ if (ANDROID) android/mako.cpp ) target_link_libraries(system3 PRIVATE SDL2 SDL2_ttf) - set(DEFAULT_FONT_PATH fonts/) elseif (EMSCRIPTEN) add_executable(system3) @@ -58,7 +57,6 @@ else() # NOT (ANDROID OR EMSCRIPTEN) target_sources(system3 PRIVATE linux/nact_linux.cpp linux/mako.cpp) target_link_libraries(system3 PRIVATE PkgConfig::SDL2MIXER) set(RESOURCE_PATH ${CMAKE_INSTALL_PREFIX}/share/system3/) - set(DEFAULT_FONT_PATH ${CMAKE_INSTALL_PREFIX}/share/system3/fonts/) install(TARGETS system3 RUNTIME DESTINATION bin) install(DIRECTORY ../resources/ DESTINATION share/system3 @@ -69,6 +67,7 @@ endif() target_sources(system3 PRIVATE sdlmain.cpp fileio.cpp + resource.cpp utfsjis.cpp texthook.cpp sys/ags.cpp @@ -94,6 +93,5 @@ target_sources(system3 PRIVATE target_compile_definitions(system3 PRIVATE $<$:_DEBUG_CONSOLE> - DEFAULT_FONT_PATH="${DEFAULT_FONT_PATH}" RESOURCE_PATH="${RESOURCE_PATH}") target_include_directories(system3 PRIVATE . sys) diff --git a/src/common.h b/src/common.h index 6351098..175936a 100644 --- a/src/common.h +++ b/src/common.h @@ -21,6 +21,8 @@ #define SDL_Delay emscripten_sleep #endif +#define FONT_RESOURCE_NAME "MTLc3m.ttf" + // type definition typedef uint8_t uint8; typedef uint16_t uint16; @@ -63,4 +65,7 @@ inline void strcpy_s(char* dst, size_t n, const char* src) #endif // !WIN32 +// resource.cpp +SDL_RWops* open_resource(const char* name, const char* type); + #endif diff --git a/src/res3/Script1.rc b/src/res3/Script1.rc index 51a671c..bd8e8b6 100644 --- a/src/res3/Script1.rc +++ b/src/res3/Script1.rc @@ -87,6 +87,12 @@ END // remains consistent on all systems. IDI_ICON1 ICON DISCARDABLE "icon1.ico" +///////////////////////////////////////////////////////////////////////////// +// +// FONTS +// +MTLc3m.ttf FONTS DISCARDABLE "../../resources/fonts/MTLc3m.ttf" + ///////////////////////////////////////////////////////////////////////////// // // MDA diff --git a/src/resource.cpp b/src/resource.cpp new file mode 100644 index 0000000..e93d001 --- /dev/null +++ b/src/resource.cpp @@ -0,0 +1,25 @@ +#ifdef _WIN32 +#include +#undef ERROR +#endif +#include "common.h" + +SDL_RWops* open_resource(const char* name, const char* type) { +#ifdef _WIN32 + // On Windows, read from resource. + HINSTANCE hInst = GetModuleHandle(NULL); + HRSRC hRes = FindResource(hInst, name, type); + HGLOBAL hGlobal = LoadResource(hInst, hRes); + if (!hGlobal) { + WARNING("Cannot load resource %s (type: %s)", name, type); + return NULL; + } + return SDL_RWFromConstMem(LockResource(hGlobal), SizeofResource(hInst, hRes)); +#else + // On Android, read from APK assets. + // On other platforms, read from a file under RESOURCE_PATH. + char path[_MAX_PATH]; + sprintf_s(path, _MAX_PATH, "%s%s/%s", RESOURCE_PATH, type, name); + return SDL_RWFromFile(path, "rb"); +#endif +} diff --git a/src/sdlmain.cpp b/src/sdlmain.cpp index bbe9664..609228a 100644 --- a/src/sdlmain.cpp +++ b/src/sdlmain.cpp @@ -26,7 +26,7 @@ int main(int argc, char *argv[]) #endif g_window = SDL_CreateWindow("Scenario Decoder SYSTEM3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 400, flags); - const char* font_file = DEFAULT_FONT_PATH "MTLc3m.ttf"; + const char* font_file = NULL; const char* playlist = NULL; const char* game_id = NULL; const char* save_dir = NULL; diff --git a/src/sys/ags.cpp b/src/sys/ags.cpp index 1c50674..c66eb48 100644 --- a/src/sys/ags.cpp +++ b/src/sys/ags.cpp @@ -60,13 +60,28 @@ AGS::AGS(NACT* parent, const char* fontfile) : nact(parent) // フォント TTF_Init(); - hFont16 = TTF_OpenFont(fontfile, 16); - hFont24 = TTF_OpenFont(fontfile, 24); - hFont32 = TTF_OpenFont(fontfile, 32); - hFont48 = TTF_OpenFont(fontfile, 48); - hFont64 = TTF_OpenFont(fontfile, 64); - if (!hFont16 || !hFont24 || !hFont32 || !hFont48 || !hFont64) { - parent->fatal("Cannot open font file %s", fontfile); + if (fontfile) { + rw_font = SDL_RWFromFile(fontfile, "rb"); + if (!rw_font) + parent->fatal("Cannot open font file %s", fontfile); + } else { + rw_font = open_resource(FONT_RESOURCE_NAME, "fonts"); + if (!rw_font) + parent->fatal("Cannot open default font"); + } + if (rw_font) { + hFont16 = TTF_OpenFontRW(rw_font, 0, 16); + SDL_RWseek(rw_font, 0, SEEK_SET); + hFont24 = TTF_OpenFontRW(rw_font, 0, 24); + SDL_RWseek(rw_font, 0, SEEK_SET); + hFont32 = TTF_OpenFontRW(rw_font, 0, 32); + SDL_RWseek(rw_font, 0, SEEK_SET); + hFont48 = TTF_OpenFontRW(rw_font, 0, 48); + SDL_RWseek(rw_font, 0, SEEK_SET); + hFont64 = TTF_OpenFontRW(rw_font, 0, 64); + if (!hFont16 || !hFont24 || !hFont32 || !hFont48 || !hFont64) { + parent->fatal("TTF_OpenFontRW failed: %s", TTF_GetError()); + } } // カーソル初期化 @@ -342,11 +357,14 @@ AGS::~AGS() } // フォント開放 - TTF_CloseFont(hFont16); - TTF_CloseFont(hFont24); - TTF_CloseFont(hFont32); - TTF_CloseFont(hFont48); - TTF_CloseFont(hFont64); + if (rw_font) { + TTF_CloseFont(hFont16); + TTF_CloseFont(hFont24); + TTF_CloseFont(hFont32); + TTF_CloseFont(hFont48); + TTF_CloseFont(hFont64); + SDL_RWclose(rw_font); + } // surface開放 for(int i = 0; i < 3; i++) { diff --git a/src/sys/ags.h b/src/sys/ags.h index 79b8fe3..d398d5f 100644 --- a/src/sys/ags.h +++ b/src/sys/ags.h @@ -49,6 +49,7 @@ private: SDL_Surface* hBmpDest; // DIBSection 24bpp (最終出力先) // フォント + SDL_RWops* rw_font; TTF_Font* hFont16; TTF_Font* hFont24; TTF_Font* hFont32; diff --git a/src/sys/dri.cpp b/src/sys/dri.cpp index f744dc9..6033e0a 100644 --- a/src/sys/dri.cpp +++ b/src/sys/dri.cpp @@ -4,38 +4,10 @@ [ DRI ] */ -#ifdef _WIN32 -#include -#undef ERROR -#endif #include "dri.h" #include "crc32.h" #include "../fileio.h" -namespace { - -SDL_RWops* open_resource(const char* name, const char* type) { -#ifdef _WIN32 - // On Windows, read from resource. - HINSTANCE hInst = GetModuleHandle(NULL); - HRSRC hRes = FindResource(hInst, name, type); - HGLOBAL hGlobal = LoadResource(hInst, hRes); - if (!hGlobal) { - WARNING("Cannot load resource %s (type: %s)", name, type); - return NULL; - } - return SDL_RWFromConstMem(LockResource(hGlobal), SizeofResource(hInst, hRes)); -#else - // On Android, read from APK assets. - // On other platforms, read from a file under RESOURCE_PATH. - char path[_MAX_PATH]; - sprintf_s(path, _MAX_PATH, "%s%s/%s", RESOURCE_PATH, type, name); - return SDL_RWFromFile(path, "rb"); -#endif -} - -} // namespace - uint8* DRI::load(const char* file_name, int page, int* size) { char tmp_name[_MAX_PATH];