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
@@ -8,19 +8,24 @@
|
||||
|
||||
namespace ava2::input {
|
||||
|
||||
TouchLayout TouchLayout::ForScreen(float w, float h) {
|
||||
TouchLayout TouchLayout::ForScreen(float w, float h, float safeLeft, float safeTop, float safeRight, float safeBottom) {
|
||||
TouchLayout layout;
|
||||
layout.screen_w = w;
|
||||
layout.screen_h = h;
|
||||
layout.safe_left = safeLeft;
|
||||
layout.safe_top = safeTop;
|
||||
layout.safe_right = safeRight;
|
||||
layout.safe_bottom = safeBottom;
|
||||
const float unit = std::min(w, h);
|
||||
layout.joy_x = 0.0f;
|
||||
// The joystick area starts at the safe edge; the stick floats, so its bottom can stay wide.
|
||||
layout.joy_x = safeLeft;
|
||||
layout.joy_y = h * 0.45f;
|
||||
layout.joy_w = w * 0.40f;
|
||||
layout.joy_h = h * 0.55f;
|
||||
layout.joy_h = h * 0.55f - safeBottom;
|
||||
layout.joy_radius = unit * 0.12f;
|
||||
layout.attack_r = unit * 0.11f;
|
||||
layout.attack_x = w - layout.attack_r - unit * 0.06f;
|
||||
layout.attack_y = h - layout.attack_r - unit * 0.06f;
|
||||
layout.attack_x = w - safeRight - layout.attack_r - unit * 0.06f;
|
||||
layout.attack_y = h - safeBottom - layout.attack_r - unit * 0.06f;
|
||||
layout.tap_slop = unit * 0.02f;
|
||||
return layout;
|
||||
}
|
||||
|
||||
@@ -46,8 +46,13 @@ struct TouchLayout {
|
||||
float tap_slop = 14.0f; // movement that turns a tap into a camera drag
|
||||
std::uint64_t tap_ms = 300; // longer presses are not taps
|
||||
|
||||
// Default layout for a screen: joystick in the lower-left 40% x 55%, attack bottom right.
|
||||
static TouchLayout ForScreen(float w, float h);
|
||||
// Unobstructed-area insets (platform::SafeInsets); the HUD reads them through `touch`.
|
||||
float safe_left = 0.0f, safe_top = 0.0f, safe_right = 0.0f, safe_bottom = 0.0f;
|
||||
|
||||
// Default layout for a screen: joystick in the lower-left 40% x 55%, attack bottom right,
|
||||
// both kept inside the safe insets.
|
||||
static TouchLayout ForScreen(float w, float h, float safeLeft = 0.0f, float safeTop = 0.0f,
|
||||
float safeRight = 0.0f, float safeBottom = 0.0f);
|
||||
};
|
||||
|
||||
struct JoystickView {
|
||||
|
||||
+64
-57
@@ -38,6 +38,7 @@
|
||||
#include "eter_render/RaceData.h"
|
||||
#include "app/input/touch_router.h"
|
||||
#endif
|
||||
#include "app/platform/platform.h"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
@@ -176,58 +177,10 @@ int SDLKeyToDIK(SDL_Keycode key) {
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
#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& error) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Cannot initialize client data directory: %s", error.what());
|
||||
if (std::string error; !ava2::platform::EnterClientDataDirectory(&error)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s", error.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
#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 error;
|
||||
if (data) std::filesystem::current_path(data, error);
|
||||
if (!data || error) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Cannot enter client data directory: %s",
|
||||
data ? error.message().c_str() : SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
SDL_Log("Client data: %s", data);
|
||||
}
|
||||
#endif
|
||||
try {
|
||||
int frame_limit = 0, auto_slot = -1, fps_cap = -1, game_frames = 1, rendered_game_frames = 0;
|
||||
#ifdef __ANDROID__
|
||||
@@ -326,12 +279,8 @@ int main(int argc, char** argv) {
|
||||
#endif
|
||||
|
||||
#ifdef AVA2_HAS_PYTHON
|
||||
#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())
|
||||
ava2::CPythonSystem::SetConfigDirectory(internal);
|
||||
#endif
|
||||
if (const std::string config = ava2::platform::ConfigDirectory(); !config.empty())
|
||||
ava2::CPythonSystem::SetConfigDirectory(config);
|
||||
std::unique_ptr<ava2::PythonRuntime> python;
|
||||
if (!stdlib.empty() || !script_root.empty()) {
|
||||
python = std::make_unique<ava2::PythonRuntime>(stdlib, script_root);
|
||||
@@ -372,8 +321,12 @@ int main(int argc, char** argv) {
|
||||
SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "1");
|
||||
}
|
||||
#endif
|
||||
ava2::platform::Lifecycle::SetHints();
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) throw std::runtime_error(SDL_GetError());
|
||||
struct Quit { ~Quit(){ SDL_Quit(); } } quit;
|
||||
ava2::platform::Lifecycle lifecycle;
|
||||
lifecycle.Install();
|
||||
struct Unwatch { ava2::platform::Lifecycle& l; ~Unwatch(){ l.Uninstall(); } } unwatch{lifecycle};
|
||||
|
||||
std::unique_ptr<SDL_GPUDevice, decltype(&SDL_DestroyGPUDevice)> gpu(
|
||||
SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_MSL, std::getenv("AVA2_GPU_DEBUG") != nullptr, nullptr),
|
||||
@@ -733,7 +686,10 @@ int main(int argc, char** argv) {
|
||||
auto& touch_router = ava2::input::GetTouchRouter();
|
||||
if (touch_mode) {
|
||||
touch_router.SetSink(&touch_sink);
|
||||
touch_router.SetLayout(ava2::input::TouchLayout::ForScreen(float(screen_width), float(screen_height)));
|
||||
const auto safe = ava2::platform::QuerySafeInsets(window.get(), renderer.get());
|
||||
touch_router.SetLayout(ava2::input::TouchLayout::ForScreen(float(screen_width), float(screen_height),
|
||||
safe.left, safe.top, safe.right, safe.bottom));
|
||||
SDL_Log("Safe area insets: %.0f %.0f %.0f %.0f", safe.left, safe.top, safe.right, safe.bottom);
|
||||
}
|
||||
// Finger position in UI pixels (SDL gives it normalized to the window).
|
||||
auto finger_point = [&](const SDL_TouchFingerEvent& finger, float& x, float& y) {
|
||||
@@ -1309,6 +1265,14 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_EVENT_QUIT) running = false;
|
||||
#ifdef AVA2_HAS_PYTHON
|
||||
if (touch_mode && (event.type == SDL_EVENT_WINDOW_SAFE_AREA_CHANGED || event.type == SDL_EVENT_WINDOW_RESIZED)) {
|
||||
// The HUD reads the insets when it is built; the router keeps its button positions.
|
||||
const auto safe = ava2::platform::QuerySafeInsets(window.get(), renderer.get());
|
||||
auto layout = touch_router.GetLayout();
|
||||
layout.safe_left = safe.left; layout.safe_top = safe.top;
|
||||
layout.safe_right = safe.right; layout.safe_bottom = safe.bottom;
|
||||
touch_router.SetLayout(layout);
|
||||
}
|
||||
if (event.type == SDL_EVENT_WINDOW_FOCUS_LOST) {
|
||||
ava2::CancelPlayerMovement(); orbit_camera.EndDrag();
|
||||
key_move_fwd = key_move_back = key_turn_left = key_turn_right = false;
|
||||
@@ -1743,6 +1707,49 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
|
||||
#endif
|
||||
}
|
||||
|
||||
// platform/ lifecycle: save and go quiet in the background, keep the connection.
|
||||
switch (lifecycle.Poll()) {
|
||||
case ava2::platform::Lifecycle::Change::EnterBackground:
|
||||
SDL_Log("Entering background");
|
||||
#ifdef AVA2_HAS_PYTHON
|
||||
ava2::CancelPlayerMovement(); orbit_camera.EndDrag();
|
||||
key_move_fwd = key_move_back = key_turn_left = key_turn_right = false;
|
||||
if (touch_mode) touch_router.CancelAll();
|
||||
// The OS may kill a background app without another event.
|
||||
if (python) {
|
||||
ava2::CPythonSystem::Instance().SaveConfig();
|
||||
ava2::CPythonSystem::Instance().SaveInterfaceStatus();
|
||||
}
|
||||
#endif
|
||||
ava2::SoundManager::Instance().SetPaused(true);
|
||||
break;
|
||||
case ava2::platform::Lifecycle::Change::EnterForeground:
|
||||
SDL_Log("Entering foreground");
|
||||
ava2::SoundManager::Instance().SetPaused(false);
|
||||
// The first foreground frame must not see the whole pause as one frame.
|
||||
prev_frame_time = std::chrono::steady_clock::now();
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
if (lifecycle.TakeLowMemory()) SDL_Log("Low memory warning");
|
||||
if (lifecycle.IsTerminating()) running = false;
|
||||
if (lifecycle.IsBackground() && running) {
|
||||
// No rendering (the surface is gone on Android) and no game update; the network keeps
|
||||
// its keepalive and the queue drained. SDL itself waits up to 100 ms per poll on Android.
|
||||
#ifdef AVA2_HAS_PYTHON
|
||||
if (python) {
|
||||
if (!view_script.empty()) python->pump_network();
|
||||
ava2::PythonNetworkStream::Instance().ProcessRecvData();
|
||||
ava2::pump_python_net();
|
||||
if (ava2::PythonNetworkStream::Instance().IsQuitRequested()) running = false;
|
||||
}
|
||||
#endif
|
||||
#ifndef __ANDROID__
|
||||
SDL_Delay(50);
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef AVA2_HAS_PYTHON
|
||||
if (python && !view_script.empty()) {
|
||||
if (auto_connect && !requested) { requested = true; connect(); if (!local_error.empty()) throw std::runtime_error(local_error); }
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
// Platform layer for the client host (docs/MOBILE-ARCHITECTURE.md, platform/): data and config
|
||||
// directories, application lifecycle (background / foreground / low memory / terminate) and the
|
||||
// screen safe area. Keeps the per-platform #ifdefs out of the main loop.
|
||||
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
|
||||
struct SDL_Window;
|
||||
struct SDL_Renderer;
|
||||
|
||||
namespace ava2::platform {
|
||||
|
||||
// Enters the client root the legacy code reads relative paths from: on macOS the per-user data
|
||||
// directory that links the bundle Resources (config/chat writes stay outside the signed app); on
|
||||
// Android the app's external files directory (android/push_assets.py). Elsewhere a no-op.
|
||||
// False with *error set when the directory cannot be entered.
|
||||
bool EnterClientDataDirectory(std::string* error);
|
||||
|
||||
// Where metin2.cfg / interface.cfg are written when the working directory is not writable
|
||||
// (Android internal storage); empty keeps the working directory.
|
||||
std::string ConfigDirectory();
|
||||
|
||||
// Background/foreground handling. SDL delivers the app events from the OS thread on mobile
|
||||
// (SDL_AddEventWatch), so the watch only records them; the main loop applies them with Poll().
|
||||
// Android runs with SDL_ANDROID_BLOCK_ON_PAUSE=0 so the loop keeps pumping the network while
|
||||
// paused (SDL then waits up to 100 ms per poll).
|
||||
class Lifecycle {
|
||||
public:
|
||||
enum class Change { None, EnterBackground, EnterForeground };
|
||||
|
||||
// Sets the hints (before SDL_Init) and installs the event watch (after SDL_Init).
|
||||
static void SetHints();
|
||||
void Install();
|
||||
void Uninstall();
|
||||
|
||||
// Records one app event (called by the watch; public for tests).
|
||||
void OnEvent(unsigned int sdlEventType);
|
||||
|
||||
// The next background/foreground transition since the last call (background wins when both
|
||||
// happened, so a quick pause-resume still saves the config once).
|
||||
Change Poll();
|
||||
bool IsBackground() const { return m_background.load(); }
|
||||
// True once per SDL_EVENT_LOW_MEMORY.
|
||||
bool TakeLowMemory() { return m_lowMemory.exchange(false); }
|
||||
bool IsTerminating() const { return m_terminating.load(); }
|
||||
|
||||
private:
|
||||
std::atomic<bool> m_background{false}, m_enteredBackground{false}, m_enteredForeground{false};
|
||||
std::atomic<bool> m_lowMemory{false}, m_terminating{false};
|
||||
bool m_installed = false;
|
||||
};
|
||||
|
||||
// Insets of the unobstructed area (notch, rounded corners, gesture bar) in logical UI pixels.
|
||||
struct SafeInsets {
|
||||
float left = 0.0f, top = 0.0f, right = 0.0f, bottom = 0.0f;
|
||||
bool operator==(const SafeInsets& o) const {
|
||||
return left == o.left && top == o.top && right == o.right && bottom == o.bottom;
|
||||
}
|
||||
};
|
||||
|
||||
// Maps a window-space safe rectangle into a letterboxed logical presentation of logicalW x
|
||||
// logicalH inside a windowW x windowH window (SDL_LOGICAL_PRESENTATION_LETTERBOX). Insets that
|
||||
// fall into the letterbox bars are 0; results are clamped to the logical screen.
|
||||
SafeInsets MapSafeInsets(float windowW, float windowH, float safeX, float safeY, float safeW, float safeH,
|
||||
float logicalW, float logicalH);
|
||||
|
||||
// SDL_GetWindowSafeArea mapped through the renderer's logical presentation.
|
||||
SafeInsets QuerySafeInsets(SDL_Window* window, SDL_Renderer* renderer);
|
||||
|
||||
} // namespace ava2::platform
|
||||
@@ -87,6 +87,12 @@ bool SoundManager::Initialize() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void SoundManager::SetPaused(bool paused) {
|
||||
if (m_deviceId == 0) return;
|
||||
if (paused) SDL_PauseAudioDevice(m_deviceId);
|
||||
else SDL_ResumeAudioDevice(m_deviceId);
|
||||
}
|
||||
|
||||
void SoundManager::Destroy() {
|
||||
if (!m_initialized) return;
|
||||
m_initialized = false;
|
||||
|
||||
@@ -26,6 +26,8 @@ public:
|
||||
void Destroy();
|
||||
bool IsInitialized() const { return m_initialized; }
|
||||
bool HasAudioDevice() const { return m_deviceId != 0; }
|
||||
// Application background (platform::Lifecycle): stops the device without dropping streams.
|
||||
void SetPaused(bool paused);
|
||||
|
||||
// Frame update for music fades, 3D stream lifetimes, and looping
|
||||
void Update(float dt = 0.016f);
|
||||
|
||||
@@ -54,6 +54,14 @@ PyObject* touch_get_layout(PyObject*, PyObject*) {
|
||||
l.joy_x, l.joy_y, l.joy_w, l.joy_h, l.joy_radius);
|
||||
}
|
||||
|
||||
// (left, top, right, bottom) insets of the unobstructed screen area in UI pixels (notch,
|
||||
// rounded corners, gesture bar); 0 on desktop.
|
||||
PyObject* touch_get_safe_area(PyObject*, PyObject*) {
|
||||
const auto& l = GetTouchRouter().GetLayout();
|
||||
return Py_BuildValue("(iiii)", int(l.safe_left + 0.5f), int(l.safe_top + 0.5f),
|
||||
int(l.safe_right + 0.5f), int(l.safe_bottom + 0.5f));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void inittouch() {
|
||||
@@ -64,6 +72,7 @@ void inittouch() {
|
||||
{"SetAttackButton", touch_set_attack_button, METH_VARARGS, nullptr},
|
||||
{"SetJoystickArea", touch_set_joystick_area, METH_VARARGS, nullptr},
|
||||
{"GetLayout", touch_get_layout, METH_NOARGS, nullptr},
|
||||
{"GetSafeArea", touch_get_safe_area, METH_NOARGS, nullptr},
|
||||
{nullptr, nullptr, 0, nullptr}
|
||||
};
|
||||
Py_InitModule("touch", methods);
|
||||
|
||||
Reference in New Issue
Block a user