Add platform layer: lifecycle, data dirs, safe area (mobile step 2)
Move the macOS/Android data and config directory code out of main.cpp into src/app/platform. Handle background/foreground: save config and interface status, pause audio, cancel touch input and skip rendering while keeping the network pumped. Map the window safe area through the letterboxed presentation into TouchLayout and expose touch.GetSafeArea() so the mobile HUD stays clear of notches and gesture bars. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
260d421bde
commit
b79fbce0b0
@@ -0,0 +1,149 @@
|
||||
#include "platform.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace ava2::platform {
|
||||
|
||||
bool EnterClientDataDirectory(std::string* error) {
|
||||
#if defined(__APPLE__)
|
||||
// Keep legacy Python's relative config/chat writes outside the signed application.
|
||||
try {
|
||||
std::filesystem::path base = std::filesystem::path(SDL_GetBasePath()).lexically_normal();
|
||||
if (base.filename().empty()) base = base.parent_path();
|
||||
// SDL on macOS returns the bundle's Resources directory by default.
|
||||
if (base.filename() == "Resources" && base.parent_path().filename() == "Contents")
|
||||
base = base.parent_path() / "MacOS";
|
||||
const auto resources = base / "../Resources";
|
||||
if (base.filename() == "MacOS" && std::filesystem::is_directory(resources)) {
|
||||
std::filesystem::path data;
|
||||
if (const char* overridePath = std::getenv("AVA2_USER_DATA_DIR")) data = overridePath;
|
||||
else {
|
||||
char* preferences = SDL_GetPrefPath("AVA2", "Client");
|
||||
if (!preferences) throw std::runtime_error(SDL_GetError());
|
||||
data = preferences; SDL_free(preferences);
|
||||
}
|
||||
std::filesystem::create_directories(data);
|
||||
for (const auto& entry : std::filesystem::directory_iterator(resources)) {
|
||||
const auto name = entry.path().filename();
|
||||
const auto extension = name.extension().string();
|
||||
if (name == "lib" || extension == ".cfg" || extension == ".inf" || extension == ".chat") continue;
|
||||
const auto link = data / name;
|
||||
const auto target = std::filesystem::canonical(entry.path());
|
||||
if (std::filesystem::is_symlink(link) && std::filesystem::read_symlink(link) != target)
|
||||
std::filesystem::remove(link);
|
||||
if (!std::filesystem::exists(link)) std::filesystem::create_symlink(target, link);
|
||||
}
|
||||
std::filesystem::current_path(data);
|
||||
SDL_Log("Client user data: %s", data.string().c_str());
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
if (error) *error = std::string("Cannot initialize client data directory: ") + e.what();
|
||||
return false;
|
||||
}
|
||||
#elif defined(__ANDROID__)
|
||||
// The APK holds code only. The flattened client root (assets/loose + python-root + python27.zip,
|
||||
// android/push_assets.py) lives in the app's external files directory; the legacy code reads
|
||||
// paths relative to the working directory.
|
||||
const char* data = SDL_GetAndroidExternalStoragePath();
|
||||
std::error_code ec;
|
||||
if (data) std::filesystem::current_path(data, ec);
|
||||
if (!data || ec) {
|
||||
if (error) *error = std::string("Cannot enter client data directory: ") + (data ? ec.message() : SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
SDL_Log("Client data: %s", data);
|
||||
#else
|
||||
(void)error;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string ConfigDirectory() {
|
||||
#ifdef __ANDROID__
|
||||
// metin2.cfg / interface.cfg live in the client's working directory on Windows; the APK's
|
||||
// working directory is not writable, so they go to the app's internal storage instead.
|
||||
if (const char* internal = SDL_GetAndroidInternalStoragePath()) return internal;
|
||||
#endif
|
||||
return {};
|
||||
}
|
||||
|
||||
namespace {
|
||||
bool SDLCALL LifecycleWatch(void* userdata, SDL_Event* event) {
|
||||
static_cast<Lifecycle*>(userdata)->OnEvent(event->type);
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void Lifecycle::SetHints() {
|
||||
// Keep the loop (and the game connection) alive while paused; rendering and sound stop.
|
||||
SDL_SetHint(SDL_HINT_ANDROID_BLOCK_ON_PAUSE, "0");
|
||||
}
|
||||
|
||||
void Lifecycle::Install() {
|
||||
if (m_installed) return;
|
||||
m_installed = SDL_AddEventWatch(LifecycleWatch, this);
|
||||
}
|
||||
|
||||
void Lifecycle::Uninstall() {
|
||||
if (!m_installed) return;
|
||||
SDL_RemoveEventWatch(LifecycleWatch, this);
|
||||
m_installed = false;
|
||||
}
|
||||
|
||||
void Lifecycle::OnEvent(unsigned int type) {
|
||||
switch (type) {
|
||||
case SDL_EVENT_WILL_ENTER_BACKGROUND:
|
||||
case SDL_EVENT_DID_ENTER_BACKGROUND:
|
||||
if (!m_background.exchange(true)) m_enteredBackground = true;
|
||||
break;
|
||||
case SDL_EVENT_WILL_ENTER_FOREGROUND:
|
||||
case SDL_EVENT_DID_ENTER_FOREGROUND:
|
||||
if (m_background.exchange(false)) m_enteredForeground = true;
|
||||
break;
|
||||
case SDL_EVENT_LOW_MEMORY: m_lowMemory = true; break;
|
||||
case SDL_EVENT_TERMINATING: m_terminating = true; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
Lifecycle::Change Lifecycle::Poll() {
|
||||
if (m_enteredBackground.exchange(false)) {
|
||||
// A resume that already happened is reported on the next poll.
|
||||
return Change::EnterBackground;
|
||||
}
|
||||
if (m_enteredForeground.exchange(false)) return Change::EnterForeground;
|
||||
return Change::None;
|
||||
}
|
||||
|
||||
SafeInsets MapSafeInsets(float windowW, float windowH, float safeX, float safeY, float safeW, float safeH,
|
||||
float logicalW, float logicalH) {
|
||||
SafeInsets out;
|
||||
if (windowW <= 0.0f || windowH <= 0.0f || logicalW <= 0.0f || logicalH <= 0.0f) return out;
|
||||
// Letterbox: uniform scale, centred.
|
||||
const float scale = std::min(windowW / logicalW, windowH / logicalH);
|
||||
const float offX = (windowW - logicalW * scale) * 0.5f;
|
||||
const float offY = (windowH - logicalH * scale) * 0.5f;
|
||||
auto clampInset = [](float v, float limit) { return std::clamp(v, 0.0f, limit); };
|
||||
out.left = clampInset((safeX - offX) / scale, logicalW);
|
||||
out.top = clampInset((safeY - offY) / scale, logicalH);
|
||||
out.right = clampInset((offX + logicalW * scale - (safeX + safeW)) / scale, logicalW);
|
||||
out.bottom = clampInset((offY + logicalH * scale - (safeY + safeH)) / scale, logicalH);
|
||||
return out;
|
||||
}
|
||||
|
||||
SafeInsets QuerySafeInsets(SDL_Window* window, SDL_Renderer* renderer) {
|
||||
int ww = 0, wh = 0, lw = 0, lh = 0;
|
||||
SDL_Rect safe{};
|
||||
SDL_RendererLogicalPresentation mode;
|
||||
if (!window || !renderer || !SDL_GetWindowSize(window, &ww, &wh) || !SDL_GetWindowSafeArea(window, &safe) ||
|
||||
!SDL_GetRenderLogicalPresentation(renderer, &lw, &lh, &mode) || lw <= 0 || lh <= 0)
|
||||
return {};
|
||||
return MapSafeInsets(float(ww), float(wh), float(safe.x), float(safe.y), float(safe.w), float(safe.h),
|
||||
float(lw), float(lh));
|
||||
}
|
||||
|
||||
} // namespace ava2::platform
|
||||
Reference in New Issue
Block a user