v0.3.0: display abstraction, map-browser menu, SAGE terrain tiling/blends

- ra3.client::display: shared interactive loops; SDL and Vulkan backends
  implement only the primitives (init/present/poll_event/window_size/
  key_down/present_terrain). ra3.display picks the backend.
- Menu: maps by localized name (gamestrings.csf), red/gold theme, hover
  highlight, mouse + keyboard, wheel scroll, fullscreen and FPS/vsync
  options, loading progress bar.
- Terrain: continuous tile sampling via a texture array (REPEAT, uv =
  cell/(2*cellSize)) removes per-cell grid seams; SAGE blend ramp for
  material transitions; FPS label + top-right minimap overlays.
- Skip the skirmish sim for map views; reuse the Vulkan texture; no idle
  terrain redraw.
This commit is contained in:
EnderTheCoder
2026-09-20 02:20:39 +08:00
parent 702b4e29aa
commit b6d619254d
66 changed files with 37661 additions and 406 deletions
+12 -10
View File
@@ -3,19 +3,21 @@ export module ra3.ui;
import std;
export import ra3.core;
import ra3.render;
import ra3.client;
/**
* Fallback viewer used when the build has no SDL3. It never opens a window;
* the caller should write an offscreen image instead.
* Fallback SDL backend used when the build has no SDL3. It never opens a window;
* `init` fails so the caller can fall back to an offscreen image.
*/
export namespace ra3::ui {
/** Window parameters (mirrors the SDL backend). */
struct viewer_options {
std::string title = "OpenRA3";
int width = 1024;
int height = 768;
class sdl_display final : public ra3::client::display {
public:
[[nodiscard]] auto init(const ra3::client::display_options &) -> bool override { return false; }
[[nodiscard]] auto present(const ra3::render::image &, const ra3::render::view_rect &, bool) -> bool override { return false; }
[[nodiscard]] auto poll_event(ra3::render::ui_event &) -> bool override { return false; }
[[nodiscard]] auto window_size() const -> std::pair<int, int> override { return {0, 0}; }
[[nodiscard]] auto key_down(ra3::render::ui_key) const -> bool override { return false; }
auto shutdown() -> void override {}
[[nodiscard]] auto name() const -> std::string_view override { return "sdl(null)"; }
};
/** Always returns false: this build has no windowing backend. */
[[nodiscard]] inline auto run_viewer(const ra3::render::image &, const viewer_options &) -> bool { return false; }
}
+158 -66
View File
@@ -7,85 +7,177 @@ export module ra3.ui;
import std;
export import ra3.core;
import ra3.render;
import ra3.client;
/**
* A minimal SDL3 window for viewing a rendered scene.
* The SDL3 presentation backend.
*
* Controls: drag with the left mouse button to pan, wheel to zoom, Esc/close
* to quit. When no display is available `run_viewer` returns false so the
* caller can fall back to an offscreen image.
* SDL3 is the platform layer: it owns the window, the input, and (here) a
* low-end `SDL_Renderer` blit path. Only the low-level display primitives live
* in this module; the interactive loops are shared in `ra3::client::display`.
* The Vulkan backend implements the same interface.
*/
export namespace ra3::ui {
/** Window parameters. */
struct viewer_options {
std::string title = "OpenRA3";
int width = 1024;
int height = 768;
};
/**
* Show `scene` in a window until the user quits.
*
* @return true if the window ran; false if video init or window creation
* failed (typically no display).
* An SDL_Renderer-backed display: cheap, dependency-light, and used as the
* fallback when Vulkan is unavailable.
*/
[[nodiscard]] inline auto run_viewer(const ra3::render::image &scene, const viewer_options &options) -> bool {
if (scene.empty()) return false;
if (!SDL_Init(SDL_INIT_VIDEO)) return false;
class sdl_display final : public ra3::client::display {
public:
sdl_display() = default;
sdl_display(const sdl_display &) = delete;
auto operator=(const sdl_display &) -> sdl_display & = delete;
~sdl_display() override { this->shutdown(); }
SDL_Window *window = SDL_CreateWindow(options.title.c_str(), options.width, options.height, SDL_WINDOW_RESIZABLE);
if (window == nullptr) {
SDL_Quit();
return false;
[[nodiscard]] auto init(const ra3::client::display_options &options) -> bool override {
if (!SDL_Init(SDL_INIT_VIDEO)) return false;
this->fps_limit_ = options.fps_limit;
window_ = SDL_CreateWindow(options.title.c_str(), options.width, options.height,
SDL_WINDOW_RESIZABLE | (options.fullscreen ? SDL_WINDOW_FULLSCREEN : 0));
if (window_ == nullptr) {
SDL_Quit();
return false;
}
SDL_RaiseWindow(window_);
renderer_ = SDL_CreateRenderer(window_, nullptr);
if (renderer_ == nullptr) {
this->shutdown();
return false;
}
SDL_SetRenderVSync(renderer_, options.fps_limit == 0 ? 1 : 0);
SDL_StartTextInput(window_);
return true;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, nullptr);
if (renderer == nullptr) {
SDL_DestroyWindow(window);
SDL_Quit();
return false;
}
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, static_cast<int>(scene.width()),
static_cast<int>(scene.height()));
if (texture == nullptr) {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return false;
}
SDL_UpdateTexture(texture, nullptr, scene.data(), static_cast<int>(scene.width()) * 4);
SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
float zoom = 1.0F;
float pan_x = 0.0F;
float pan_y = 0.0F;
bool running = true;
SDL_Event event;
while (running) {
[[nodiscard]] auto present(const ra3::render::image &frame, const ra3::render::view_rect &dest, bool changed) -> bool override {
if (frame.empty() || renderer_ == nullptr) return false;
if (changed || texture_ == nullptr || frame.width() != texture_w_ || frame.height() != texture_h_) {
if (texture_ != nullptr) SDL_DestroyTexture(texture_);
texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, static_cast<int>(frame.width()),
static_cast<int>(frame.height()));
if (texture_ == nullptr) return false;
texture_w_ = frame.width();
texture_h_ = frame.height();
SDL_SetTextureScaleMode(texture_, SDL_SCALEMODE_LINEAR);
SDL_UpdateTexture(texture_, nullptr, frame.data(), static_cast<int>(frame.width()) * 4);
}
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
SDL_RenderClear(renderer_);
const SDL_FRect dst{dest.x, dest.y, dest.w, dest.h};
SDL_RenderTexture(renderer_, texture_, nullptr, &dst);
SDL_RenderPresent(renderer_);
return true;
}
[[nodiscard]] auto poll_event(ra3::render::ui_event &out) -> bool override {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) {
running = false;
} else if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_ESCAPE) {
running = false;
} else if (event.type == SDL_EVENT_MOUSE_WHEEL) {
zoom *= event.wheel.y > 0.0F ? 1.1F : (1.0F / 1.1F);
zoom = std::clamp(zoom, 0.1F, 20.0F);
} else if (event.type == SDL_EVENT_MOUSE_MOTION && (event.motion.state & SDL_BUTTON_LMASK) != 0U) {
pan_x += event.motion.xrel;
pan_y += event.motion.yrel;
switch (event.type) {
case SDL_EVENT_QUIT:
out = {};
out.type = ra3::render::ui_event_type::quit;
return true;
case SDL_EVENT_KEY_DOWN:
if (const auto key = map_key(event.key.key); key != ra3::render::ui_key::none) {
out = {};
out.type = ra3::render::ui_event_type::key;
out.key = key;
return true;
}
break;
case SDL_EVENT_TEXT_INPUT:
if (event.text.text[0] != '\0') {
out = {};
out.type = ra3::render::ui_event_type::text;
out.character = event.text.text[0];
return true;
}
break;
case SDL_EVENT_MOUSE_MOTION:
out = {};
out.type = ra3::render::ui_event_type::mouse_move;
out.x = event.motion.x;
out.y = event.motion.y;
out.dx = event.motion.xrel;
out.dy = event.motion.yrel;
out.left = (event.motion.state & SDL_BUTTON_LMASK) != 0U;
return true;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
out = {};
out.type = ra3::render::ui_event_type::mouse_button;
out.x = event.button.x;
out.y = event.button.y;
out.left = event.button.button == SDL_BUTTON_LEFT;
return true;
case SDL_EVENT_MOUSE_WHEEL:
out = {};
out.type = ra3::render::ui_event_type::wheel;
out.wheel = event.wheel.y;
return true;
default:
break;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
const SDL_FRect dst{pan_x, pan_y, static_cast<float>(scene.width()) * zoom, static_cast<float>(scene.height()) * zoom};
SDL_RenderTexture(renderer, texture, nullptr, &dst);
SDL_RenderPresent(renderer);
return false;
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return true;
}
[[nodiscard]] auto window_size() const -> std::pair<int, int> override {
int w = 0;
int h = 0;
SDL_GetWindowSizeInPixels(window_, &w, &h);
return {w, h};
}
[[nodiscard]] auto key_down(ra3::render::ui_key key) const -> bool override {
const bool *keys = SDL_GetKeyboardState(nullptr);
if (keys == nullptr) return false;
switch (key) {
case ra3::render::ui_key::up: return keys[SDL_SCANCODE_W] || keys[SDL_SCANCODE_UP];
case ra3::render::ui_key::down: return keys[SDL_SCANCODE_S] || keys[SDL_SCANCODE_DOWN];
case ra3::render::ui_key::left: return keys[SDL_SCANCODE_A] || keys[SDL_SCANCODE_LEFT];
case ra3::render::ui_key::right: return keys[SDL_SCANCODE_D] || keys[SDL_SCANCODE_RIGHT];
default: return false;
}
}
auto shutdown() -> void override {
if (window_ != nullptr) SDL_StopTextInput(window_);
if (texture_ != nullptr) SDL_DestroyTexture(texture_);
if (renderer_ != nullptr) SDL_DestroyRenderer(renderer_);
if (window_ != nullptr) SDL_DestroyWindow(window_);
SDL_Quit();
texture_ = nullptr;
renderer_ = nullptr;
window_ = nullptr;
texture_w_ = 0;
texture_h_ = 0;
}
[[nodiscard]] auto name() const -> std::string_view override { return "sdl"; }
private:
[[nodiscard]] static auto map_key(SDL_Keycode key) -> ra3::render::ui_key {
using ra3::render::ui_key;
switch (key) {
case SDLK_UP: return ui_key::up;
case SDLK_DOWN: return ui_key::down;
case SDLK_LEFT: return ui_key::left;
case SDLK_RIGHT: return ui_key::right;
case SDLK_PAGEUP: return ui_key::page_up;
case SDLK_PAGEDOWN: return ui_key::page_down;
case SDLK_RETURN:
case SDLK_KP_ENTER: return ui_key::confirm;
case SDLK_ESCAPE: return ui_key::cancel;
case SDLK_TAB: return ui_key::tab;
case SDLK_BACKSPACE: return ui_key::backspace;
default: return ui_key::none;
}
}
SDL_Window *window_ = nullptr;
SDL_Renderer *renderer_ = nullptr;
SDL_Texture *texture_ = nullptr;
ra3::core::uint32 texture_w_ = 0;
ra3::core::uint32 texture_h_ = 0;
};
}