Windows: Embed default font as a resource

After this, embedded MTLc3m.ttf is used unless the -fontfile option is
specified.
This commit is contained in:
kichikuou
2020-05-24 22:01:32 +09:00
parent b11ddd35c8
commit a0af5ef10c
8 changed files with 69 additions and 44 deletions
+1 -3
View File
@@ -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
$<$<CONFIG:Debug>:_DEBUG_CONSOLE>
DEFAULT_FONT_PATH="${DEFAULT_FONT_PATH}"
RESOURCE_PATH="${RESOURCE_PATH}")
target_include_directories(system3 PRIVATE . sys)
+5
View File
@@ -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
+6
View File
@@ -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
+25
View File
@@ -0,0 +1,25 @@
#ifdef _WIN32
#include <windows.h>
#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
}
+1 -1
View File
@@ -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;
+30 -12
View File
@@ -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++) {
+1
View File
@@ -49,6 +49,7 @@ private:
SDL_Surface* hBmpDest; // DIBSection 24bpp (最終出力先)
// フォント
SDL_RWops* rw_font;
TTF_Font* hFont16;
TTF_Font* hFont24;
TTF_Font* hFont32;
-28
View File
@@ -4,38 +4,10 @@
[ DRI ]
*/
#ifdef _WIN32
#include <windows.h>
#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];