versió preliminar de text en pantalla
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 <iostream>
|
||||
|
||||
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<int>(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
|
||||
@@ -1,30 +0,0 @@
|
||||
// text_renderer.hpp - Renderitzador de text amb formes .shp
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include <string>
|
||||
#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
|
||||
Reference in New Issue
Block a user