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
+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