// // Created by darman on 9/24/26. // #include "Application.h" #include #include #include #include #include namespace SiegeDeck::Engine { namespace { std::expected ParseUInt(std::string_view flag, std::string_view value) { uint64 result = 0; auto [end, ec] = std::from_chars(value.data(), value.data() + value.size(), result); if (ec != std::errc() || end != value.data() + value.size()) return std::unexpected(std::format("{} expects a non-negative integer, got '{}'", flag, value)); return result; } } std::expected AppConfig::FromArgs(int argc, char* argv[]) { AppConfig config; for (int i = 1; i < argc; ++i) { std::string_view arg = argv[i]; if (arg == "--fullscreen") { config.Fullscreen = true; continue; } if (arg != "--frames" && arg != "--seed") return std::unexpected(std::format("unknown argument '{}'", arg)); if (i + 1 >= argc) return std::unexpected(std::format("{} expects a value", arg)); auto value = ParseUInt(arg, argv[++i]); if (!value) return std::unexpected(value.error()); if (arg == "--frames") config.MaxFrames = *value; else config.Seed = *value; } return config; } Application::Application(AppConfig appConfig) : config(std::move(appConfig)), sdl(config.Title, config.Version), window(sdl, WindowDesc { .Title = config.Title, .Width = config.Width, .Height = config.Height, .Fullscreen = config.Fullscreen, }), context { .App = *this, .MainWindow = window, .Frame = frameClock, .Sim = simClock, } { } Application::~Application() = default; int Application::Run() { while (running) { frameClock.Tick(); HandleEvents(); // scenes.Update(frameClock.Dt()) goes here: UI, card hand, camera. simClock.Accumulate(frameClock.Dt()); while (simClock.ConsumeStep()) { // scenes.FixedUpdate(simClock.StepDt()) goes here: movement, targeting, damage. } DrawFrame(simClock.Alpha()); if (config.MaxFrames && frameClock.FrameCount() >= *config.MaxFrames) Quit(); } spdlog::info("Ran {} frames, {} sim steps in {:.2f} s", frameClock.FrameCount(), simClock.StepIndex(), frameClock.Time()); return 0; } void Application::HandleEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_EVENT_QUIT) Quit(); } } void Application::DrawFrame(double /*alpha*/) { // Wayland doesn't map a window until a frame is presented. Stand-in for RenderDevice's // clear + present; a window surface can't be combined with SDL_GPU, so this goes in step 2. SDL_Surface* surface = SDL_GetWindowSurface(window.Handle()); SDL_FillSurfaceRect(surface, nullptr, SDL_MapSurfaceRGB(surface, 24, 26, 32)); SDL_UpdateWindowSurface(window.Handle()); SDL_Delay(16); // no vsync yet } }