Implement M9.2 native client lifecycle and immutable render boundary

This commit is contained in:
dingo
2026-10-01 16:58:46 -04:00
parent d487d86ea3
commit 04c7715646
24 changed files with 827 additions and 34 deletions
+160
View File
@@ -0,0 +1,160 @@
#include "evo/client/application.hpp"
#include "render/sdl_renderer.hpp"
#define SDL_MAIN_HANDLED
#include <SDL3/SDL_main.h>
#include <cmath>
#include <optional>
#include <utility>
namespace evo::client {
namespace {
bool runtime_owned = false; // Process main-thread-only contract; no hidden threads.
struct Runtime {
bool acquired{false};
~Runtime() {
if (acquired) {
SDL_Quit();
runtime_owned = false;
}
}
};
} // namespace
struct Application::Impl {
Runtime runtime;
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> window{nullptr, SDL_DestroyWindow};
detail::Renderer renderer{nullptr, SDL_DestroyRenderer};
std::string renderer_name;
WindowState state;
std::optional<Error> failure;
std::expected<void, Error> refresh() {
if (!SDL_GetWindowSize(window.get(), &state.logical_width, &state.logical_height) ||
!SDL_GetWindowSizeInPixels(window.get(), &state.pixel_width, &state.pixel_height))
return std::unexpected(Error::surface);
state.content_scale = SDL_GetWindowDisplayScale(window.get());
if (!std::isfinite(state.content_scale) || state.content_scale <= 0 ||
state.logical_width < 0 || state.logical_height < 0 || state.pixel_width < 0 ||
state.pixel_height < 0 || state.logical_width > 32768 || state.logical_height > 32768 ||
state.pixel_width > 32768 || state.pixel_height > 32768)
return std::unexpected(Error::surface);
return {};
}
};
Application::Application(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
Application::~Application() = default;
WindowState Application::state() const noexcept {
auto result = impl_->state;
result.failed = impl_->failure.has_value();
return result;
}
std::expected<std::unique_ptr<Application>, Error> Application::open(const Config& config) {
if (config.width <= 0 || config.height <= 0 || config.width > 32768 || config.height > 32768 ||
config.renderer.size() > 64 || config.renderer.find('\0') != std::string::npos)
return std::unexpected(Error::invalid_config);
if (runtime_owned || SDL_WasInit(0) != 0)
return std::unexpected(Error::already_open);
auto impl = std::make_unique<Impl>();
SDL_SetMainReady();
impl->runtime.acquired = true;
runtime_owned = true;
if (!SDL_Init(SDL_INIT_VIDEO))
return std::unexpected(Error::initialization);
const auto flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY |
(config.hidden ? SDL_WINDOW_HIDDEN : 0);
impl->window.reset(SDL_CreateWindow("Aeternalis Evolution - engineering client", config.width,
config.height, flags));
if (!impl->window)
return std::unexpected(Error::window);
impl->renderer_name = config.renderer;
auto renderer = detail::make_renderer(impl->window.get(), impl->renderer_name);
if (!renderer)
return std::unexpected(renderer.error());
impl->renderer = std::move(*renderer);
const auto window_flags = SDL_GetWindowFlags(impl->window.get());
impl->state.focused = (window_flags & SDL_WINDOW_INPUT_FOCUS) != 0;
impl->state.minimized = (window_flags & SDL_WINDOW_MINIMIZED) != 0;
impl->state.hidden = (window_flags & SDL_WINDOW_HIDDEN) != 0;
if (auto result = impl->refresh(); !result)
return std::unexpected(result.error());
return std::unique_ptr<Application>{new Application{std::move(impl)}};
}
std::expected<Step, Error> Application::step(const render::Frame& frame) {
auto& p = *impl_;
if (p.failure)
return std::unexpected(*p.failure);
if (p.state.closed)
return Step::closed;
const auto id = SDL_GetWindowID(p.window.get());
bool reset = false;
SDL_Event event{};
for (int count = 0; count < 256 && SDL_PollEvent(&event); ++count) {
if (event.type == SDL_EVENT_QUIT)
p.state.closed = true;
if (event.type >= SDL_EVENT_WINDOW_FIRST && event.type <= SDL_EVENT_WINDOW_LAST &&
event.window.windowID == id) {
switch (event.type) {
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
p.state.closed = true;
break;
case SDL_EVENT_WINDOW_FOCUS_GAINED:
p.state.focused = true;
break;
case SDL_EVENT_WINDOW_FOCUS_LOST:
p.state.focused = false;
break;
case SDL_EVENT_WINDOW_MINIMIZED:
p.state.minimized = true;
break;
case SDL_EVENT_WINDOW_RESTORED:
case SDL_EVENT_WINDOW_MAXIMIZED:
p.state.minimized = false;
break;
case SDL_EVENT_WINDOW_HIDDEN:
p.state.hidden = true;
break;
case SDL_EVENT_WINDOW_SHOWN:
p.state.hidden = false;
break;
default:
break;
}
}
if (event.type >= SDL_EVENT_RENDER_TARGETS_RESET &&
event.type <= SDL_EVENT_RENDER_DEVICE_LOST && event.render.windowID == id) {
if (event.type == SDL_EVENT_RENDER_DEVICE_LOST)
p.failure = Error::device_lost;
else if (event.type == SDL_EVENT_RENDER_DEVICE_RESET)
reset = true;
}
}
if (p.failure)
return std::unexpected(*p.failure);
if (p.state.closed)
return Step::closed;
if (reset) {
p.renderer.reset();
auto renderer = detail::make_renderer(p.window.get(), p.renderer_name);
if (!renderer) {
p.failure = renderer.error();
return std::unexpected(*p.failure);
}
p.renderer = std::move(*renderer);
}
if (auto result = p.refresh(); !result) {
p.failure = result.error();
return std::unexpected(*p.failure);
}
if (p.state.hidden || p.state.minimized || p.state.logical_width == 0 ||
p.state.logical_height == 0 || p.state.pixel_width == 0 || p.state.pixel_height == 0)
return Step::paused;
if (auto result = detail::draw(p.renderer.get(), frame, p.state); !result) {
p.failure = result.error();
return std::unexpected(*p.failure);
}
if (!SDL_RenderPresent(p.renderer.get())) {
p.failure = Error::draw;
return std::unexpected(*p.failure);
}
return Step::rendered;
}
} // namespace evo::client
+17
View File
@@ -0,0 +1,17 @@
#include "evo/render/frame.hpp"
#include <cmath>
namespace evo::render {
std::expected<Frame, FrameError> Frame::create(std::span<const Rectangle> rectangles,
Color background) {
if (rectangles.size() > max_rectangles)
return std::unexpected(FrameError::too_many_primitives);
for (const auto& r : rectangles) {
if (!std::isfinite(r.x) || !std::isfinite(r.y) || !std::isfinite(r.width) ||
!std::isfinite(r.height) || std::abs(r.x) > 32768 || std::abs(r.y) > 32768 ||
r.width < 0 || r.height < 0 || r.width > 32768 || r.height > 32768)
return std::unexpected(FrameError::invalid_geometry);
}
return Frame{rectangles, background};
}
} // namespace evo::render
+32
View File
@@ -0,0 +1,32 @@
#include "sdl_renderer.hpp"
namespace evo::client::detail {
std::expected<Renderer, Error> make_renderer(SDL_Window* window, const std::string& name) {
Renderer result{SDL_CreateRenderer(window, name.empty() ? nullptr : name.c_str()),
SDL_DestroyRenderer};
if (!result)
return std::unexpected(Error::renderer);
if (!SDL_SetRenderDrawBlendMode(result.get(), SDL_BLENDMODE_BLEND))
return std::unexpected(Error::renderer);
return result;
}
std::expected<void, Error> draw(SDL_Renderer* renderer, const render::Frame& frame,
const WindowState& state) {
const auto bg = frame.background();
if (!SDL_SetRenderScale(
renderer,
static_cast<float>(state.pixel_width) / static_cast<float>(state.logical_width),
static_cast<float>(state.pixel_height) / static_cast<float>(state.logical_height)) ||
!SDL_SetRenderDrawColor(renderer, bg.red, bg.green, bg.blue, bg.alpha) ||
!SDL_RenderClear(renderer))
return std::unexpected(Error::draw);
for (const auto& r : frame.rectangles()) {
const SDL_FRect box{r.x, r.y, r.width, r.height};
if (!SDL_SetRenderDrawColor(renderer, r.color.red, r.color.green, r.color.blue,
r.color.alpha) ||
!SDL_RenderFillRect(renderer, &box))
return std::unexpected(Error::draw);
}
return {};
}
} // namespace evo::client::detail
+12
View File
@@ -0,0 +1,12 @@
#pragma once
#include "evo/client/application.hpp"
#include <SDL3/SDL.h>
#include <memory>
namespace evo::client::detail {
using Renderer = std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)>;
[[nodiscard]] std::expected<Renderer, Error> make_renderer(SDL_Window* window,
const std::string& name);
[[nodiscard]] std::expected<void, Error> draw(SDL_Renderer* renderer, const render::Frame& frame,
const WindowState& state);
} // namespace evo::client::detail