Split the frame loop out of main.cpp (app_loop) for SDL main callbacks

main.cpp moves to app_loop.cpp; its body becomes RunClient, a C++20
coroutine suspended at the top of each frame, driven by ava2::app::AppLoop
(Start/Iterate/QueueEvent/Finish/Run). The new main.cpp uses SDL main
callbacks (AVA2_MAIN_CALLBACKS, default on except Android) or the classic
loop. Screenshots are byte-identical through both drivers; 54/54 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 22:28:50 +09:00
co-authored by Claude Opus 5.5
parent 64d25e3571
commit f973b24abc
6 changed files with 2851 additions and 2614 deletions
+13 -2
View File
@@ -357,13 +357,24 @@ if(AVA2_BUILD_HOST)
target_link_libraries(platform_test PRIVATE ava2_platform ava2_input) target_link_libraries(platform_test PRIVATE ava2_platform ava2_input)
add_test(NAME ava2.platform COMMAND platform_test) add_test(NAME ava2.platform COMMAND platform_test)
endif() endif()
# SDL main callbacks drive src/app/app_loop.cpp (required on iOS); Android keeps the classic loop.
if(ANDROID) if(ANDROID)
add_library(main SHARED src/app/main.cpp) set(AVA2_MAIN_CALLBACKS_DEFAULT OFF)
else()
set(AVA2_MAIN_CALLBACKS_DEFAULT ON)
endif()
option(AVA2_MAIN_CALLBACKS "Drive the client through SDL_AppInit/SDL_AppIterate" ${AVA2_MAIN_CALLBACKS_DEFAULT})
set(AVA2_HOST_SOURCES src/app/main.cpp src/app/app_loop.cpp)
if(ANDROID)
add_library(main SHARED ${AVA2_HOST_SOURCES})
set(AVA2_HOST main) set(AVA2_HOST main)
else() else()
add_executable(ava2_client src/app/main.cpp) add_executable(ava2_client ${AVA2_HOST_SOURCES})
set(AVA2_HOST ava2_client) set(AVA2_HOST ava2_client)
endif() endif()
if(AVA2_MAIN_CALLBACKS)
target_compile_definitions(${AVA2_HOST} PRIVATE AVA2_MAIN_CALLBACKS)
endif()
target_include_directories(${AVA2_HOST} PRIVATE src src/app src/game src/network src/scripting) target_include_directories(${AVA2_HOST} PRIVATE src src/app src/game src/network src/scripting)
target_link_libraries(${AVA2_HOST} PRIVATE SDL3::SDL3 ava2_protocol ava2_render ava2_platform) target_link_libraries(${AVA2_HOST} PRIVATE SDL3::SDL3 ava2_protocol ava2_render ava2_platform)
if(AVA2_EMBED_PYTHON) if(AVA2_EMBED_PYTHON)
+1 -1
View File
@@ -75,7 +75,7 @@
``` ```
最终都调用原版已有的移动和攻击接口,游戏逻辑不感知输入设备差异。多指同时操作(摇杆 + 攻击 + 镜头)由此支持。 最终都调用原版已有的移动和攻击接口,游戏逻辑不感知输入设备差异。多指同时操作(摇杆 + 攻击 + 镜头)由此支持。
- `app_loop` 从 `main.cpp` 抽出,便于 iOS 改用 SDL 回调式主循环;可在做 iOS 时再拆。 - `app_loop` 从 `main.cpp` 抽出,便于 iOS 改用 SDL 回调式主循环;可在做 iOS 时再拆。(已完成:`src/app/app_loop.{h,cpp}`,主体为每帧挂起一次的协程;`main.cpp` 只选择 SDL 回调或经典循环。)
### Python 界面层 ### Python 界面层
+25
View File
@@ -1378,3 +1378,28 @@ pick-up refusals, attack speed); straight-line walking only, the stuck
watchdog is the only obstacle handling; party loot rules ignored (only own / watchdog is the only obstacle handling; party loot rules ignored (only own /
unowned drops); no desktop hotkey (F9 is taken by the preview mode); unowned drops); no desktop hotkey (F9 is taken by the preview mode);
English labels, no locale strings; does not run in `--preview-data`. English labels, no locale strings; does not run in `--preview-data`.
### 2026-10-12 Mobile step 7a — app_loop split
- `src/app/main.cpp` moved to `src/app/app_loop.cpp` (history kept). The
former `main()` body is now `RunClient`, a C++20 coroutine that suspends
at the top of every frame (`co_yield`), so start-up, frame body and
teardown stay one function with all their locals in the coroutine frame.
`ava2::app::AppLoop` (`app_loop.h`) drives it: `Start` (start-up up to the
first frame), `Iterate` (one frame), `QueueEvent`, `Finish` (lets a running
client leave the loop and tear down), `Run` (classic loop).
- The new `src/app/main.cpp` only picks the driver: SDL main callbacks
(`AVA2_MAIN_CALLBACKS`, default ON except Android; iOS requires it because
UIKit owns the main thread) or a classic `main` loop (Android for now).
With callbacks, SDL_AppEvent queues events and the frame's event pass reads
the queue instead of `SDL_PollEvent`.
- iOS: no software FPS cap by default (SDL_AppIterate follows the display).
Verified: 54/54 desktop tests; `ava2_client --phone 2400x1080@2.75
--safe-insets 50,0,0,24 --phase game --preview-data --frames 150
--screenshot` gives a byte-identical BMP with the callbacks and with the
classic loop (`-DAVA2_MAIN_CALLBACKS=OFF`) compared to before the split;
bad arguments still exit 1.
Outstanding: Android stays on the classic loop until callbacks are tried on
a device; the frame body is still one function (state not yet split into
classes).
+2718
View File
File diff suppressed because it is too large Load Diff
+57
View File
@@ -0,0 +1,57 @@
#pragma once
// Client frame loop (docs/MOBILE-ARCHITECTURE.md, app_loop), split out of main.cpp so one source
// serves both ways SDL can drive an app:
// - SDL's main callbacks (main.cpp with AVA2_MAIN_CALLBACKS): SDL_AppInit -> Start,
// SDL_AppIterate -> Iterate, SDL_AppEvent -> QueueEvent, SDL_AppQuit -> Finish. Required on
// iOS, where UIKit owns the main thread and calls SDL_AppIterate once per display refresh.
// - a classic main loop (Run): the frame polls SDL for events itself.
//
// Start-up, the per-frame body and teardown stay one function (RunClient in app_loop.cpp). It is a
// C++20 coroutine that suspends at the top of every frame, so the renderer, scene, actors and the
// lambdas that capture them live in the coroutine frame between callbacks.
#include <SDL3/SDL_events.h>
#include <coroutine>
#include <deque>
namespace ava2::app {
class AppLoop {
public:
struct Frame; // RunClient's coroutine type (app_loop.cpp)
AppLoop() = default;
~AppLoop();
AppLoop(const AppLoop&) = delete;
AppLoop& operator=(const AppLoop&) = delete;
// Runs the start-up (arguments, SDL, Python, scene) up to the first frame. queuedEvents: events
// arrive through QueueEvent (main callbacks) instead of SDL_PollEvent. False when the client
// already finished (bad arguments, failed start-up); ExitCode() tells how.
bool Start(int argc, char** argv, bool queuedEvents);
// One frame; false once the client has finished.
bool Iterate();
// Called from SDL_AppEvent; the next frame's event pass reads it.
void QueueEvent(const SDL_Event& event);
// Lets a still-running client leave its loop and tear down (SDL_AppQuit), then returns the
// exit code. Safe to call more than once.
int Finish();
int ExitCode() const { return m_exitCode; }
// Classic main loop: Start, Iterate until done, Finish.
int Run(int argc, char** argv);
// For the frame body: the next event of this frame, and whether the host asked it to stop.
bool PollEvent(SDL_Event* event);
bool QuitRequested() const { return m_quit; }
private:
bool Resume();
std::coroutine_handle<> m_frame;
std::deque<SDL_Event> m_events;
bool m_queuedEvents = false, m_quit = false, m_inFrame = false;
int m_exitCode = 0;
};
} // namespace ava2::app
+37 -2611
View File
File diff suppressed because it is too large Load Diff