Files
AVA2/tests/platform_test.cpp
T
shenleiandClaude Opus 5.5 b79fbce0b0 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>
2026-10-12 20:45:07 +09:00

89 lines
3.7 KiB
C++

// platform/ (mobile step 2): lifecycle transitions, safe-area mapping into the letterboxed UI and
// the touch layout kept inside the safe insets.
#include "app/platform/platform.h"
#include "app/input/touch_router.h"
#include <SDL3/SDL.h>
#include <cassert>
#include <cmath>
#include <iostream>
namespace {
using ava2::platform::Lifecycle;
using ava2::platform::MapSafeInsets;
using ava2::platform::SafeInsets;
bool Near(float a, float b) { return std::fabs(a - b) < 0.01f; }
void TestLifecycle() {
Lifecycle l;
assert(l.Poll() == Lifecycle::Change::None && !l.IsBackground());
// Android sends WILL and DID together; one transition.
l.OnEvent(SDL_EVENT_WILL_ENTER_BACKGROUND);
l.OnEvent(SDL_EVENT_DID_ENTER_BACKGROUND);
assert(l.IsBackground());
assert(l.Poll() == Lifecycle::Change::EnterBackground);
assert(l.Poll() == Lifecycle::Change::None);
l.OnEvent(SDL_EVENT_WILL_ENTER_FOREGROUND);
l.OnEvent(SDL_EVENT_DID_ENTER_FOREGROUND);
assert(!l.IsBackground());
assert(l.Poll() == Lifecycle::Change::EnterForeground);
assert(l.Poll() == Lifecycle::Change::None);
// A pause and resume between two frames still reports both, background first.
l.OnEvent(SDL_EVENT_DID_ENTER_BACKGROUND);
l.OnEvent(SDL_EVENT_DID_ENTER_FOREGROUND);
assert(l.Poll() == Lifecycle::Change::EnterBackground);
assert(l.Poll() == Lifecycle::Change::EnterForeground);
assert(!l.IsBackground());
// Foreground without background is ignored.
l.OnEvent(SDL_EVENT_DID_ENTER_FOREGROUND);
assert(l.Poll() == Lifecycle::Change::None);
assert(!l.TakeLowMemory());
l.OnEvent(SDL_EVENT_LOW_MEMORY);
assert(l.TakeLowMemory() && !l.TakeLowMemory());
assert(!l.IsTerminating());
l.OnEvent(SDL_EVENT_TERMINATING);
assert(l.IsTerminating());
}
void TestSafeInsets() {
// Full safe area: no insets.
SafeInsets none = MapSafeInsets(1280, 720, 0, 0, 1280, 720, 1280, 720);
assert(none == SafeInsets{});
// 20:9 phone (2400x1080 window points) showing a 16:9 UI 1280x720 letterboxed: scale 1.5,
// bars of 240 px left/right. A 100 px notch on the left lies inside the bar -> no inset.
SafeInsets bar = MapSafeInsets(2400, 1080, 100, 0, 2300, 1080, 1280, 720);
assert(bar == SafeInsets{});
// Same screen with a UI as wide as the window (2400x1080 logical = 1:1): the notch shows.
SafeInsets wide = MapSafeInsets(2400, 1080, 100, 0, 2240, 1040, 2400, 1080);
assert(Near(wide.left, 100) && Near(wide.top, 0) && Near(wide.right, 60) && Near(wide.bottom, 40));
// Scaled: 1600x720 UI in a 2400x1080 window (scale 1.5); a 150 px cutout is 100 UI px.
SafeInsets scaled = MapSafeInsets(2400, 1080, 150, 0, 2250, 1035, 1600, 720);
assert(Near(scaled.left, 100) && Near(scaled.right, 0) && Near(scaled.bottom, 30));
// Degenerate input.
assert(MapSafeInsets(0, 0, 0, 0, 0, 0, 1280, 720) == SafeInsets{});
}
void TestTouchLayout() {
using ava2::input::TouchLayout;
const TouchLayout plain = TouchLayout::ForScreen(1600, 720);
const TouchLayout safe = TouchLayout::ForScreen(1600, 720, 100, 0, 60, 30);
assert(Near(safe.safe_left, 100) && Near(safe.safe_right, 60) && Near(safe.safe_bottom, 30));
assert(Near(safe.joy_x, plain.joy_x + 100));
assert(Near(safe.attack_x, plain.attack_x - 60) && Near(safe.attack_y, plain.attack_y - 30));
assert(Near(safe.attack_r, plain.attack_r));
// The attack circle stays inside the safe area.
assert(safe.attack_x + safe.attack_r <= 1600 - 60 && safe.attack_y + safe.attack_r <= 720 - 30);
assert(safe.joy_y + safe.joy_h <= 720 - 30 + 0.01f);
}
} // namespace
int main() {
TestLifecycle();
TestSafeInsets();
TestTouchLayout();
std::cout << "Platform lifecycle and safe area insets behave\n";
return 0;
}