Paleta UI compartida (sega-cube.pal) i constants semàntiques UiColor (v0.7.1)

This commit is contained in:
JailDesigner committed 2026-04-19 09:30:29 +02:00
1 parent e6f3c888e6
commit bee9b650db
12 files changed
+396 -19

No files matched your search

+8 -7
View File
@@ -8,14 +8,11 @@
#include "core/rendering/text.hpp"
#include "core/resources/resource_cache.hpp"
#include "game/options.hpp"
#include "game/ui/ui_palette.hpp"
#include "utils/defines.hpp"
namespace {
// La paleta de 8bithud.gif només té 2 entrades (0=negre, 1=blanc); per a
// poder colorar el text lliurement caldrà una paleta compartida, com al
// projecte_2026. De moment ens quedem amb blanc.
constexpr Uint8 TEXT_COLOR = 1;
constexpr int TEXT_SIZE = 8;
constexpr int PADDING_V = (RenderInfo::HEIGHT - TEXT_SIZE) / 2;
@@ -35,10 +32,14 @@ auto RenderInfo::get() -> RenderInfo& {
RenderInfo::~RenderInfo() = default;
void RenderInfo::init() {
// El fitxer s'indexa al Cache perquè també el puguen reutilitzar altres
// HUDs que vulguen la mateixa font sense recarregar-la.
// Paleta compartida de UI: qualsevol HUD que la vulga només ha de cridar
// Cache::getPalette("ui") i aplicar-la a la seua surface. El .gif propi
// de la font (2 colors) es descarta a favor d'aquesta.
Resource::Cache::get().loadPalette("ui", "data/palettes/sega-cube.pal");
Resource::Cache::get().loadSurface("8bithud.gif", "data/font/8bithud.gif");
auto font_surface = Resource::Cache::get().getSurface("8bithud.gif");
font_surface->setPalette(Resource::Cache::get().getPalette("ui"));
text_ = std::make_unique<Text>(font_surface, "data/font/8bithud.fnt");
// Restaura l'estat guardat a config.yaml (video.render_info).
@@ -93,7 +94,7 @@ void RenderInfo::render() {
static_cast<int>(y_) + PADDING_V,
line,
1,
TEXT_COLOR);
UiColor::RENDER_INFO_TEXT);
}
void RenderInfo::toggle() {
-8
View File
@@ -63,14 +63,6 @@ auto Surface::loadSurface(const std::string& file_path) -> SurfaceData {
return {static_cast<int>(w), static_cast<int>(h), pixels};
}
void Surface::loadPalette(const std::string& file_path) {
palette_ = loadPaletteFromGif(file_path);
}
void Surface::loadPalette(const Palette& palette) {
palette_ = palette;
}
void Surface::clear(Uint8 color) {
const std::size_t TOTAL = static_cast<std::size_t>(surface_data_->width) * static_cast<std::size_t>(surface_data_->height);
std::fill(surface_data_->data.get(), surface_data_->data.get() + TOTAL, color);
-3
View File
@@ -47,9 +47,6 @@ class Surface {
static auto loadSurface(const std::string& file_path) -> SurfaceData;
void loadPalette(const std::string& file_path);
void loadPalette(const Palette& palette);
// Vobla la regió indicada al framebuffer ARGB del joc usant la paleta propia.
void render(int x, int y, const SDL_FRect* src_rect = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE) const;
// Igual que render() però substituint els píxels amb valor source_color per
+65
View File
@@ -0,0 +1,65 @@
#include "core/resources/palette_loader.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <sstream>
#include <stdexcept>
#include <string>
#include "core/rendering/surface.hpp"
#include "core/resources/resource_helper.hpp"
namespace Resource::PaletteLoader {
auto parseJasc(const std::vector<std::uint8_t>& bytes) -> Palette {
std::string content(bytes.begin(), bytes.end());
std::istringstream stream(content);
std::string line;
// Capçalera: "JASC-PAL" / "0100" / count.
std::getline(stream, line);
if (!line.starts_with("JASC-PAL")) {
throw std::runtime_error("PaletteLoader: fitxer no és JASC-PAL");
}
std::getline(stream, line); // versió, ignorada per ara
std::getline(stream, line);
int declared_count = 0;
try {
declared_count = std::stoi(line);
} catch (...) {
throw std::runtime_error("PaletteLoader: count JASC-PAL invàlid");
}
Palette palette{};
const std::size_t MAX = std::min(static_cast<std::size_t>(declared_count), palette.size());
std::size_t i = 0;
while (i < MAX && std::getline(stream, line)) {
if (line.empty()) { continue; }
std::istringstream ls(line);
int r = 0;
int g = 0;
int b = 0;
ls >> r >> g >> b;
palette[i] = 0xFF000000U | (static_cast<std::uint32_t>(r & 0xFF) << 16) | (static_cast<std::uint32_t>(g & 0xFF) << 8) | static_cast<std::uint32_t>(b & 0xFF);
++i;
}
return palette;
}
auto loadFromFile(const std::string& file_path) -> Palette {
auto ext = std::filesystem::path(file_path).extension().string();
std::ranges::transform(ext, ext.begin(), [](unsigned char c) { return std::tolower(c); });
auto bytes = Resource::Helper::loadFile(file_path);
if (bytes.empty()) {
throw std::runtime_error("PaletteLoader: no s'ha pogut llegir " + file_path);
}
if (ext == ".pal") { return parseJasc(bytes); }
if (ext == ".gif") { return loadPaletteFromGif(file_path); }
throw std::runtime_error("PaletteLoader: format de paleta no suportat: " + ext);
}
} // namespace Resource::PaletteLoader
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include "core/rendering/surface.hpp"
namespace Resource::PaletteLoader {
// Detecta el format pel sufix i retorna una Palette (256 entrades ARGB).
// Actualment suporta:
// - ".pal" → JASC-PAL versió 0100 (plaintext "R G B" per línia).
// - ".gif" → paleta global del GIF (reutilitza el decoder existent).
// Cridar-lo amb un altre sufix llança std::runtime_error.
auto loadFromFile(const std::string& file_path) -> Palette;
// Parser pur del format JASC-PAL, útil si ja es té el contingut en memòria
// (p.ex. des d'un resources.pack). No fa I/O.
auto parseJasc(const std::vector<std::uint8_t>& bytes) -> Palette;
} // namespace Resource::PaletteLoader
+16
View File
@@ -4,6 +4,7 @@
#include <iostream>
#include <stdexcept>
#include "core/resources/palette_loader.hpp"
#include "core/resources/resource_helper.hpp"
#include "utils/utils.hpp"
@@ -28,6 +29,12 @@ namespace Resource {
printWithDots("Animation : ", getFileName(file_path), "[ LOADED ]");
}
void Cache::loadPalette(const std::string& name, const std::string& file_path) {
auto palette = PaletteLoader::loadFromFile(file_path);
palettes_.emplace_back(PaletteResource{.name = name, .palette = palette});
printWithDots("Palette : ", getFileName(file_path), "[ LOADED ]");
}
auto Cache::getSurface(const std::string& name) -> std::shared_ptr<Surface> {
auto it = std::ranges::find_if(surfaces_, [&name](const auto& s) { return s.name == name; });
if (it == surfaces_.end()) {
@@ -46,4 +53,13 @@ namespace Resource {
return *it;
}
auto Cache::getPalette(const std::string& name) -> const Palette& {
auto it = std::ranges::find_if(palettes_, [&name](const auto& p) { return p.name == name; });
if (it == palettes_.end()) {
std::cerr << "Cache: palette not found: " << name << '\n';
throw std::runtime_error("Palette not found: " + name);
}
return it->palette;
}
} // namespace Resource
+5
View File
@@ -25,9 +25,13 @@ namespace Resource {
void loadSurface(const std::string& name, const std::string& file_path);
// Llig el YAML d'animació en cru i el registra amb el nom donat.
void loadAnimation(const std::string& name, const std::string& file_path);
// Carrega una paleta (.pal JASC o .gif) i la registra amb el nom donat.
// Pensat per a paletes compartides (UI, fonts, tilesets reciclats).
void loadPalette(const std::string& name, const std::string& file_path);
auto getSurface(const std::string& name) -> std::shared_ptr<Surface>;
auto getAnimationData(const std::string& name) -> const AnimationResource&;
auto getPalette(const std::string& name) -> const Palette&;
private:
Cache() = default;
@@ -35,6 +39,7 @@ namespace Resource {
std::vector<SurfaceResource> surfaces_;
std::vector<AnimationResource> animations_;
std::vector<PaletteResource> palettes_;
};
} // namespace Resource
+5
View File
@@ -16,3 +16,8 @@ struct AnimationResource {
std::string name;
std::vector<std::uint8_t> yaml_data;
};
struct PaletteResource {
std::string name;
Palette palette;
};
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include <SDL3/SDL.h>
// Constants semàntiques que mapegen cada element de la UI al seu color dins
// de la paleta compartida (registre "ui" a Resource::Cache, carregada des de
// data/palettes/sega-cube.pal). Canviar l'aparença d'un component només
// requereix canviar la seua constant ací — no cal buscar l'índex pel codi.
namespace UiColor {
// Color per al text de l'overlay de debug RenderInfo (F3).
constexpr Uint8 RENDER_INFO_TEXT = 9;
} // namespace UiColor
+1 -1
View File
@@ -3,7 +3,7 @@
namespace Texts {
constexpr const char* WINDOW_TITLE = "© 2026 Aventures en Egipte — JailDesigner";
constexpr const char* COPYRIGHT = "© 2026 JailDesigner";
constexpr const char* VERSION = "0.7.0"; // Single source of truth — Makefile el grep
constexpr const char* VERSION = "0.7.1"; // Single source of truth — Makefile el grep
} // namespace Texts
namespace GameCanvas {