Engine: - SdlContext and Window: RAII, throwing constructors, non-movable - FrameClock (real time, clamped dt) and SimClock (fixed 1/60 s steps, speed 0 = planning phase, backlog capped per frame) - Application owns them in lifetime order, exposes EngineContext, and runs the fixed-timestep loop; AppConfig::FromArgs parses --frames, --seed and --fullscreen - Temporary window-surface fill so Wayland maps the window until RenderDevice lands in step 2 Build: - Split into the siege_engine static lib and the siege_deck exe - check_engine_deps fails the build if Engine/ includes Game/ - ctest smoke test: siege_deck --frames 30 on the offscreen driver - siege_tests on Catch2 v3.15.3 (nixpkgs + CPM pin), with SimClock, FrameClock and AppConfig tests Docs: - Decision Log: error handling split, Catch2 for unit tests - Build Order step 1 done; Engine Overview, Main Loop and Time and README updated to match - AGENTS.md with build, conventions and gotchas for coding agents - .gitignore: .mcp.json (local CLion MCP port) and Obsidian's workspace.json (tab/pane state), now untracked Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
3.6 KiB
C++
125 lines
3.6 KiB
C++
//
|
|
// Created by darman on 9/24/26.
|
|
//
|
|
|
|
#include "Application.h"
|
|
|
|
#include <SDL3/SDL.h>
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <charconv>
|
|
#include <string_view>
|
|
#include <utility>
|
|
|
|
namespace SiegeDeck::Engine
|
|
{
|
|
namespace
|
|
{
|
|
std::expected<uint64, string> 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, string> 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
|
|
}
|
|
}
|