From 5210448ac9cb782b18d659dc8412990c5872859c Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 1 Dec 2025 22:29:34 +0100 Subject: [PATCH] =?UTF-8?q?versi=C3=B3=20preliminar=20de=20text=20en=20pan?= =?UTF-8?q?talla?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 - source/game/escenes/escena_joc.cpp | 29 +++++++++++++++ source/game/escenes/escena_joc.hpp | 1 + source/utils/text_renderer.cpp | 60 ------------------------------ source/utils/text_renderer.hpp | 30 --------------- 5 files changed, 30 insertions(+), 91 deletions(-) delete mode 100644 source/utils/text_renderer.cpp delete mode 100644 source/utils/text_renderer.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c03243..eef6346 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,7 +51,6 @@ set(APP_SOURCES source/game/entities/nau.cpp source/game/entities/bala.cpp source/game/entities/enemic.cpp - source/utils/text_renderer.cpp ) # Configuración de SDL3 diff --git a/source/game/escenes/escena_joc.cpp b/source/game/escenes/escena_joc.cpp index 5ac5c5c..bac48f8 100644 --- a/source/game/escenes/escena_joc.cpp +++ b/source/game/escenes/escena_joc.cpp @@ -133,6 +133,9 @@ void EscenaJoc::dibuixar() { for (const auto &bala : bales_) { bala.dibuixar(); } + + // Dibuixar marcador + dibuixar_marcador(); } void EscenaJoc::processar_input(const SDL_Event &event) { @@ -182,3 +185,29 @@ void EscenaJoc::dibuixar_marges() const { Rendering::linea(sdl_.obte_renderer(), x1, y1, x1, y2, true); // Left Rendering::linea(sdl_.obte_renderer(), x2, y1, x2, y2, true); // Right } + +void EscenaJoc::dibuixar_marcador() { + // Text estàtic (hardcoded) + const std::string text = "SCORE: 00000 LIFE: 3 LEVEL: 01"; + + // Escala ajustada per cabre en 640px d'amplada + // Zona marcador: width = 640 px, height = 64 px + // Text: 34 caràcters → necessitem ~487 px amb escala 1.2 + // Altura caràcter: 20 * 1.2 = 24 px (37.5% de 64px) + const float escala = 1.2f; + const float spacing = 2.0f; + + // Calcular amplada total del text + float text_width = text_.get_text_width(text, escala, spacing); + + // Centrat horitzontal + float x = (Defaults::Zones::SCOREBOARD.w - text_width) / 2.0f; + + // Centrat vertical + // Altura del caràcter escalat: 20 * 1.2 = 24 px + // Marge superior: (64 - 24) / 2 = 20 px + float y = Defaults::Zones::SCOREBOARD.y + 20.0f; + + // Renderitzar + text_.render(text, {x, y}, escala, spacing); +} diff --git a/source/game/escenes/escena_joc.hpp b/source/game/escenes/escena_joc.hpp index 586ab21..2338b43 100644 --- a/source/game/escenes/escena_joc.hpp +++ b/source/game/escenes/escena_joc.hpp @@ -44,6 +44,7 @@ private: // Funcions privades void tocado(); void dibuixar_marges() const; // Dibuixar vores de la zona de joc + void dibuixar_marcador(); // Dibuixar marcador de puntuació }; #endif // ESCENA_JOC_HPP diff --git a/source/utils/text_renderer.cpp b/source/utils/text_renderer.cpp deleted file mode 100644 index f83bcfb..0000000 --- a/source/utils/text_renderer.cpp +++ /dev/null @@ -1,60 +0,0 @@ -// text_renderer.cpp - Implementació del renderitzador de text -// © 2025 Port a C++20 amb SDL3 - -#include "text_renderer.hpp" -#include "../core/graphics/shape_loader.hpp" -#include "../core/rendering/shape_renderer.hpp" -#include - -namespace Utils { - -void TextRenderer::render(SDL_Renderer* renderer, - const std::string& text, - int x, int y, - int spacing) { - int current_x = x; - - for (char c : text) { - std::string filename = get_char_filename(c); - auto shape = Graphics::ShapeLoader::load("font/" + filename); - - if (shape && shape->es_valida()) { - Punt pos = {current_x, y}; - Rendering::render_shape(renderer, shape, pos, 0.0f, 1.0f, true, 1.0f); - } - - current_x += spacing; - } -} - -int TextRenderer::calculate_width(const std::string& text, int spacing) { - return static_cast(text.length()) * spacing; -} - -std::string TextRenderer::get_char_filename(char c) { - // Números 0-9 - if (c >= '0' && c <= '9') - return std::string("char_") + c + ".shp"; - - // Letras A-Z (mayúsculas) - if (c >= 'A' && c <= 'Z') - return std::string("char_") + c + ".shp"; - - // Convertir minúsculas a mayúsculas - if (c >= 'a' && c <= 'z') - return std::string("char_") + char(c - 32) + ".shp"; - - // Caracteres especiales - switch (c) { - case ':': return "char_colon.shp"; - case '.': return "char_dot.shp"; - case '-': return "char_minus.shp"; - case ',': return "char_comma.shp"; - case '!': return "char_exclamation.shp"; - case '?': return "char_question.shp"; - case ' ': return "char_space.shp"; - default: return "char_question.shp"; // Fallback - } -} - -} // namespace Utils diff --git a/source/utils/text_renderer.hpp b/source/utils/text_renderer.hpp deleted file mode 100644 index c71da65..0000000 --- a/source/utils/text_renderer.hpp +++ /dev/null @@ -1,30 +0,0 @@ -// text_renderer.hpp - Renderitzador de text amb formes .shp -// © 2025 Port a C++20 amb SDL3 - -#pragma once -#include -#include "../core/types.hpp" - -// Forward declarations -struct SDL_Renderer; -namespace Graphics { class Shape; } - -namespace Utils { - -class TextRenderer { -public: - // Renderitza un string en la posició especificada - static void render(SDL_Renderer* renderer, - const std::string& text, - int x, int y, - int spacing = 22); - - // Calcula el ancho total de un string - static int calculate_width(const std::string& text, int spacing = 22); - -private: - // Mapea un caràcter a su nombre de archivo .shp - static std::string get_char_filename(char c); -}; - -} // namespace Utils