From e2ead2cc5e0918181941ad9128efe04f6e3e4c1a Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 14 Jun 2021 04:06:34 +0200 Subject: [PATCH] Port engine to Android Set the font path for Android Do not check for FileExists on CharisSILB.ttf for Android, FileExists will always fail as it doesn't detect files located in the APK port --- CMakeLists.txt | 31 +++++++++++++++----- Source/DiabloUI/fonts.cpp | 2 +- Source/init.cpp | 14 +++++++-- Source/main.cpp | 4 +++ Source/storm/storm_file_wrapper.h | 2 +- android-project/.gitignore | 2 ++ android-project/app/src/main/assets/.gitkeep | 0 7 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 android-project/app/src/main/assets/.gitkeep diff --git a/CMakeLists.txt b/CMakeLists.txt index 6196a2679..a739e873c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -187,6 +187,17 @@ if(NINTENDO_3DS) include(n3ds_defs) endif() +if(ANDROID) + set(ASAN OFF) + set(UBSAN OFF) + set(NONET ON) + set(TTF_FONT_DIR \"\") + + file( + COPY "${DevilutionX_SOURCE_DIR}/Packaging/resources/CharisSILB.ttf" + DESTINATION "${DevilutionX_SOURCE_DIR}/android-project/app/src/main/assets") +endif() + if(PIE) set(CMAKE_POSITION_INDEPENDENT_CODE TRUE) endif() @@ -426,13 +437,6 @@ set(libdevilutionx_SRCS Source/storm/storm_svid.cpp Source/miniwin/misc_msg.cpp) -set(devilutionxbin_SRCS - Source/main.cpp - Source/devilutionx.exe.manifest - Packaging/macOS/AppIcon.icns - Packaging/resources/CharisSILB.ttf - Packaging/windows/devilutionx.rc) - if(USE_SDL1) list(APPEND libdevilutionx_SRCS Source/utils/sdl2_to_1_2_backports.cpp) endif() @@ -515,7 +519,18 @@ if(RUN_TESTS) endif() add_library(libdevilutionx OBJECT ${libdevilutionx_SRCS}) -add_executable(${BIN_TARGET} WIN32 MACOSX_BUNDLE ${devilutionxbin_SRCS}) +if (ANDROID) + add_library(${BIN_TARGET} SHARED Source/main.cpp) +else() + add_executable(${BIN_TARGET} + WIN32 + MACOSX_BUNDLE + Source/main.cpp + Source/devilutionx.exe.manifest + Packaging/macOS/AppIcon.icns + Packaging/resources/CharisSILB.ttf + Packaging/windows/devilutionx.rc) +endif() target_link_libraries(${BIN_TARGET} PRIVATE libdevilutionx) # Copy the font and devilutionx.mpq to the build directory to it works from the build directory diff --git a/Source/DiabloUI/fonts.cpp b/Source/DiabloUI/fonts.cpp index acc48f567..dd5c66f3c 100644 --- a/Source/DiabloUI/fonts.cpp +++ b/Source/DiabloUI/fonts.cpp @@ -63,7 +63,7 @@ void LoadTtfFont() } std::string ttfFontPath = paths::TtfPath() + paths::TtfName(); -#ifdef __linux__ +#if defined(__linux__) && !defined(__ANDROID__) if (!FileExists(ttfFontPath.c_str())) { ttfFontPath = "/usr/share/fonts/truetype/" + paths::TtfName(); } diff --git a/Source/init.cpp b/Source/init.cpp index e18dbeabc..18a1dcdbc 100644 --- a/Source/init.cpp +++ b/Source/init.cpp @@ -139,16 +139,24 @@ void init_archives() { std::vector paths; paths.reserve(5); +#ifdef __ANDROID__ + paths.push_back(std::string(getenv("EXTERNAL_STORAGE")) + "/devilutionx/"); +#else paths.push_back(paths::BasePath()); +#endif paths.push_back(paths::PrefPath()); if (paths[0] == paths[1]) paths.pop_back(); -#ifdef __linux__ +#ifdef __ANDROID__ + if (getenv("SECONDARY_STORAGE") != nullptr) + paths.emplace_back(std::string(getenv("SECONDARY_STORAGE")) + "/devilutionx/"); + if (getenv("EXTERNAL_SDCARD_STORAGE") != nullptr) + paths.emplace_back(std::string(getenv("EXTERNAL_SDCARD_STORAGE")) + "/devilutionx/"); +#elif defined(__linux__) paths.emplace_back("/usr/share/diasurgical/devilutionx/"); paths.emplace_back("/usr/local/share/diasurgical/devilutionx/"); -#endif -#ifdef __3DS__ +#elif defined(__3DS__) paths.emplace_back("romfs:/"); #endif diff --git a/Source/main.cpp b/Source/main.cpp index 41b0833cf..258613052 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -18,7 +18,11 @@ extern "C" const char *__asan_default_options() } #endif +#ifdef __ANDROID__ +int SDL_main(int argc, char **argv) +#else int main(int argc, char **argv) +#endif { #ifdef __SWITCH__ switch_enable_network(); diff --git a/Source/storm/storm_file_wrapper.h b/Source/storm/storm_file_wrapper.h index dbf2ee495..909241ddf 100644 --- a/Source/storm/storm_file_wrapper.h +++ b/Source/storm/storm_file_wrapper.h @@ -1,6 +1,6 @@ /** A pointer to a Storm file as a `FILE *`. Only available on some platforms. */ #pragma once -#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) +#if (defined(__linux__) && !defined(__ANDROID__)) || defined(__FreeBSD__) || defined(__DragonFly__) #include #include "miniwin/miniwin.h" diff --git a/android-project/.gitignore b/android-project/.gitignore index 59dc52887..939b2d812 100644 --- a/android-project/.gitignore +++ b/android-project/.gitignore @@ -10,3 +10,5 @@ build/ # Local configuration file (sdk path, etc) /local.properties + +/app/src/main/assets/CharisSILB.ttf diff --git a/android-project/app/src/main/assets/.gitkeep b/android-project/app/src/main/assets/.gitkeep new file mode 100644 index 000000000..e69de29bb