Skip to content

Commit

Permalink
Android port (#179)
Browse files Browse the repository at this point in the history
Set the font path for Android

The font is located in the package resources so no path should be

Do not check for FileExists on CharisSILB.ttf for Android (#2087)

FileExists will always fail as it doesn't detect files located in the
APK
  • Loading branch information
AJenbo committed May 30, 2021
1 parent 0d5d8cb commit 4cfe1da
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
49 changes: 39 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ if(N3DS)
include(n3ds_defs)
endif()

if(ANDROID)
set(ASAN OFF)
set(UBSAN OFF)
set(NONET ON)
set(TTF_FONT_DIR \"\")
endif()

if(PIE)
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
endif()
Expand Down Expand Up @@ -231,7 +238,11 @@ if(NOT fmt_FOUND)
add_subdirectory(3rdParty/libfmt)
endif()

if(USE_SDL1)
if(ANDROID)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/android-project/SDL2/ ${CMAKE_CURRENT_SOURCE_DIR}/android-project/SDL2)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/android-project/SDL2_mixer/ ${CMAKE_CURRENT_SOURCE_DIR}/android-project/SDL2_mixer)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/android-project/SDL2_ttf/ ${CMAKE_CURRENT_SOURCE_DIR}/android-project/SDL2_ttf)
elseif(USE_SDL1)
find_package(SDL REQUIRED)
find_package(SDL_ttf REQUIRED)
include_directories(${SDL_INCLUDE_DIR})
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -706,7 +721,21 @@ else()
target_link_libraries(libdevilutionx PUBLIC "$<${ASAN_GENEX}:-fsanitize=address;-fsanitize-recover=address>")
endif()

if(USE_SDL1)
if (ANDROID)
target_include_directories(libdevilutionx PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/android-project/app/libs/include
${CMAKE_CURRENT_SOURCE_DIR}/android-project/SDL2/include

# SDL_internal.h is located in src required by SDL_android_main.c bridge
${CMAKE_CURRENT_SOURCE_DIR}/android-project/SDL2/src

${CMAKE_CURRENT_SOURCE_DIR}/android-project/SDL2_mixer
${CMAKE_CURRENT_SOURCE_DIR}/android-project/SDL2_ttf)
target_link_libraries(libdevilutionx PRIVATE
SDL2
SDL2_ttf
SDL2_mixer)
elseif(USE_SDL1)
target_link_libraries(libdevilutionx PUBLIC
${SDL_TTF_LIBRARY}
${SDL_LIBRARY})
Expand Down
2 changes: 1 addition & 1 deletion Source/DiabloUI/fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 4 additions & 0 deletions Source/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ void init_archives()
{
std::vector<std::string> paths;
paths.reserve(5);
#ifdef __ANDROID__
paths.push_back(getenv("EXTERNAL_STORAGE"));
#else
paths.push_back(paths::BasePath());
#endif
paths.push_back(paths::PrefPath());
if (paths[0] == paths[1])
paths.pop_back();
Expand Down
4 changes: 4 additions & 0 deletions Source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Source/storm/storm_file_wrapper.h
Original file line number Diff line number Diff line change
@@ -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 <cstdio>

#include "miniwin/miniwin.h"
Expand Down

0 comments on commit 4cfe1da

Please sign in to comment.