From d58c0303e9cbce32b0201e2da64462d8319acc99 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 18 Apr 2026 12:22:19 +0200 Subject: [PATCH] =?UTF-8?q?surface:=20hallazgo=201=20=E2=80=94=20SurfaceDa?= =?UTF-8?q?ta::width/height=20de=20float=20a=20int?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Las dimensiones en píxeles son enteros por naturaleza. Convertidos los miembros y constructores a int, y ajustados getWidth()/getHeight() para devolver int. Eliminados los static_cast(...->width/height) y static_cast(surface->getWidth/getHeight()) redundantes que sólo existían para compensar el tipo erróneo. Los callers que inicializan SDL_FRect directamente con getWidth/getHeight requieren static_cast explícito (sprite.cpp, animated_sprite.cpp, notifier.cpp, title.cpp) por las reglas de narrowing de list-init. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../core/rendering/sprite/animated_sprite.cpp | 2 +- .../core/rendering/sprite/dissolve_sprite.cpp | 12 ++-- source/core/rendering/sprite/sprite.cpp | 2 +- source/core/rendering/surface.cpp | 64 +++++++++---------- source/core/rendering/surface.hpp | 16 ++--- source/game/editor/mini_map.cpp | 4 +- source/game/editor/tile_picker.cpp | 4 +- source/game/scenes/title.cpp | 2 +- source/game/ui/notifier.cpp | 2 +- 9 files changed, 54 insertions(+), 54 deletions(-) diff --git a/source/core/rendering/sprite/animated_sprite.cpp b/source/core/rendering/sprite/animated_sprite.cpp index baec551..0e9d44e 100644 --- a/source/core/rendering/sprite/animated_sprite.cpp +++ b/source/core/rendering/sprite/animated_sprite.cpp @@ -219,7 +219,7 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr surface, SDL_FRect pos) : MovingSprite(std::move(surface), pos) { // animations_ queda buit (protegit per el guard de animate()) if (surface_) { - clip_ = {.x = 0, .y = 0, .w = surface_->getWidth(), .h = surface_->getHeight()}; + clip_ = {.x = 0, .y = 0, .w = static_cast(surface_->getWidth()), .h = static_cast(surface_->getHeight())}; } } diff --git a/source/core/rendering/sprite/dissolve_sprite.cpp b/source/core/rendering/sprite/dissolve_sprite.cpp index 40c4ada..afe5459 100644 --- a/source/core/rendering/sprite/dissolve_sprite.cpp +++ b/source/core/rendering/sprite/dissolve_sprite.cpp @@ -35,8 +35,8 @@ auto DissolveSprite::computePixelRank(int col, int row, int frame_h, DissolveDir DissolveSprite::DissolveSprite(std::shared_ptr surface, SDL_FRect pos) : AnimatedSprite(std::move(surface), pos) { if (surface_) { - const int W = static_cast(surface_->getWidth()); - const int H = static_cast(surface_->getHeight()); + const int W = surface_->getWidth(); + const int H = surface_->getHeight(); surface_display_ = std::make_shared(W, H); surface_display_->setTransparentColor(surface_->getTransparentColor()); surface_display_->clear(surface_->getTransparentColor()); @@ -47,8 +47,8 @@ DissolveSprite::DissolveSprite(std::shared_ptr surface, SDL_FRect pos) DissolveSprite::DissolveSprite(const AnimationResource& data) : AnimatedSprite(data) { if (surface_) { - const int W = static_cast(surface_->getWidth()); - const int H = static_cast(surface_->getHeight()); + const int W = surface_->getWidth(); + const int H = surface_->getHeight(); surface_display_ = std::make_shared(W, H); surface_display_->setTransparentColor(surface_->getTransparentColor()); // Inicialitza tots els píxels com a transparents @@ -75,8 +75,8 @@ void DissolveSprite::rebuildDisplaySurface() { auto src_data = surface_->getSurfaceData(); auto dst_data = surface_display_->getSurfaceData(); - const int SRC_W = static_cast(src_data->width); - const int DST_W = static_cast(dst_data->width); + const int SRC_W = src_data->width; + const int DST_W = dst_data->width; const Uint8 TRANSPARENT = surface_->getTransparentColor(); // Esborra frame anterior si ha canviat diff --git a/source/core/rendering/sprite/sprite.cpp b/source/core/rendering/sprite/sprite.cpp index 32ed45b..bbb683a 100644 --- a/source/core/rendering/sprite/sprite.cpp +++ b/source/core/rendering/sprite/sprite.cpp @@ -19,7 +19,7 @@ Sprite::Sprite() = default; Sprite::Sprite(std::shared_ptr surface) : surface_(std::move(surface)), - pos_{0.0F, 0.0F, surface_->getWidth(), surface_->getHeight()}, + pos_{0.0F, 0.0F, static_cast(surface_->getWidth()), static_cast(surface_->getHeight())}, clip_(pos_) {} // Muestra el sprite por pantalla diff --git a/source/core/rendering/surface.cpp b/source/core/rendering/surface.cpp index 10cb601..7b040ed 100644 --- a/source/core/rendering/surface.cpp +++ b/source/core/rendering/surface.cpp @@ -129,7 +129,7 @@ auto Surface::loadSurface(const std::string& file_path) -> SurfaceData { // Crear y devolver directamente el objeto SurfaceData printWithDots("Surface : ", file_path.substr(file_path.find_last_of("\\/") + 1), "[ LOADED ]"); - return {static_cast(w), static_cast(h), pixels}; + return {static_cast(w), static_cast(h), pixels}; } // Carga una paleta desde un archivo @@ -149,7 +149,7 @@ void Surface::setColor(int index, Uint32 color) { // Rellena la superficie con un color void Surface::clear(Uint8 color) { // NOLINT(readability-convert-member-functions-to-static) - const size_t TOTAL_PIXELS = surface_data_->width * surface_data_->height; + const size_t TOTAL_PIXELS = static_cast(surface_data_->width) * static_cast(surface_data_->height); Uint8* data_ptr = surface_data_->data.get(); std::fill(data_ptr, data_ptr + TOTAL_PIXELS, color); } @@ -165,19 +165,19 @@ void Surface::putPixel(int x, int y, Uint8 color) { // NOLINT(readability-conve } // Obtiene el color de un pixel de la surface_data -auto Surface::getPixel(int x, int y) -> Uint8 { return surface_data_->data.get()[x + (y * static_cast(surface_data_->width))]; } +auto Surface::getPixel(int x, int y) -> Uint8 { return surface_data_->data.get()[x + (y * surface_data_->width)]; } // Dibuja un rectangulo relleno void Surface::fillRect(const SDL_FRect* rect, Uint8 color) { // NOLINT(readability-convert-member-functions-to-static) // Limitar los valores del rectángulo al tamaño de la superficie float x_start = std::max(0.0F, rect->x); float y_start = std::max(0.0F, rect->y); - float x_end = std::min(rect->x + rect->w, surface_data_->width); - float y_end = std::min(rect->y + rect->h, surface_data_->height); + float x_end = std::min(rect->x + rect->w, static_cast(surface_data_->width)); + float y_end = std::min(rect->y + rect->h, static_cast(surface_data_->height)); // Rellenar fila a fila con memset (memoria contigua por fila) Uint8* data_ptr = surface_data_->data.get(); - const int SURF_WIDTH = static_cast(surface_data_->width); + const int SURF_WIDTH = surface_data_->width; const int ROW_WIDTH = static_cast(x_end) - static_cast(x_start); for (int y = static_cast(y_start); y < static_cast(y_end); ++y) { std::memset(data_ptr + (y * SURF_WIDTH) + static_cast(x_start), color, ROW_WIDTH); @@ -189,12 +189,12 @@ void Surface::drawRectBorder(const SDL_FRect* rect, Uint8 color) { // NOLINT(re // Limitar los valores del rectángulo al tamaño de la superficie float x_start = std::max(0.0F, rect->x); float y_start = std::max(0.0F, rect->y); - float x_end = std::min(rect->x + rect->w, surface_data_->width); - float y_end = std::min(rect->y + rect->h, surface_data_->height); + float x_end = std::min(rect->x + rect->w, static_cast(surface_data_->width)); + float y_end = std::min(rect->y + rect->h, static_cast(surface_data_->height)); // Dibujar bordes horizontales con memset (líneas contiguas en memoria) Uint8* data_ptr = surface_data_->data.get(); - const int SURF_WIDTH = static_cast(surface_data_->width); + const int SURF_WIDTH = surface_data_->width; const int ROW_WIDTH = static_cast(x_end) - static_cast(x_start); std::memset(data_ptr + (static_cast(y_start) * SURF_WIDTH) + static_cast(x_start), color, ROW_WIDTH); std::memset(data_ptr + ((static_cast(y_end) - 1) * SURF_WIDTH) + static_cast(x_start), color, ROW_WIDTH); @@ -223,8 +223,8 @@ void Surface::drawLine(float x1, float y1, float x2, float y2, Uint8 color) { / const int SX = (ix1 < IX2) ? 1 : -1; const int SY = (iy1 < IY2) ? 1 : -1; - const int SURF_W = static_cast(surface_data_->width); - const int SURF_H = static_cast(surface_data_->height); + const int SURF_W = surface_data_->width; + const int SURF_H = surface_data_->height; Uint8* data_ptr = surface_data_->data.get(); int err = DX - DY; @@ -253,14 +253,14 @@ void Surface::render(int x, int y, SDL_FRect* src_rect, SDL_FlipMode flip) { // // Determina la región de origen (clip) a renderizar float sx = (src_rect != nullptr) ? src_rect->x : 0; float sy = (src_rect != nullptr) ? src_rect->y : 0; - float w = (src_rect != nullptr) ? src_rect->w : surface_data_->width; - float h = (src_rect != nullptr) ? src_rect->h : surface_data_->height; + float w = (src_rect != nullptr) ? src_rect->w : static_cast(surface_data_->width); + float h = (src_rect != nullptr) ? src_rect->h : static_cast(surface_data_->height); // Limitar la región para evitar accesos fuera de rango (origen y destino) - w = std::min(w, surface_data_->width - sx); - h = std::min(h, surface_data_->height - sy); - w = std::min(w, surface_data_dest->width - x); - h = std::min(h, surface_data_dest->height - y); + w = std::min(w, static_cast(surface_data_->width) - sx); + h = std::min(h, static_cast(surface_data_->height) - sy); + w = std::min(w, static_cast(surface_data_dest->width - x)); + h = std::min(h, static_cast(surface_data_dest->height - y)); // Renderiza píxel por píxel aplicando el flip si es necesario const Uint8* src_ptr = surface_data_->data.get(); @@ -288,7 +288,7 @@ void Surface::render(int x, int y, SDL_FRect* src_rect, SDL_FlipMode flip) { // } // Helper para calcular coordenadas con flip -void Surface::calculateFlippedCoords(int ix, int iy, float sx, float sy, float w, float h, SDL_FlipMode flip, int& src_x, int& src_y) { +void Surface::calculateFlippedCoords(int ix, int iy, int sx, int sy, int w, int h, SDL_FlipMode flip, int& src_x, int& src_y) { src_x = (flip == SDL_FLIP_HORIZONTAL) ? (sx + w - 1 - ix) : (sx + ix); src_y = (flip == SDL_FLIP_VERTICAL) ? (sy + h - 1 - iy) : (sy + iy); } @@ -418,14 +418,14 @@ static auto computeFadeDensity(int screen_y, int fade_h, int canvas_height) -> f void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height, SDL_FRect* src_rect) const { const int SX = (src_rect != nullptr) ? static_cast(src_rect->x) : 0; const int SY = (src_rect != nullptr) ? static_cast(src_rect->y) : 0; - const int SW = (src_rect != nullptr) ? static_cast(src_rect->w) : static_cast(surface_data_->width); - const int SH = (src_rect != nullptr) ? static_cast(src_rect->h) : static_cast(surface_data_->height); + const int SW = (src_rect != nullptr) ? static_cast(src_rect->w) : surface_data_->width; + const int SH = (src_rect != nullptr) ? static_cast(src_rect->h) : surface_data_->height; auto surface_data_dest = Screen::get()->getRendererSurface()->getSurfaceData(); for (int row = 0; row < SH; row++) { const int SCREEN_Y = y + row; - if (SCREEN_Y < 0 || SCREEN_Y >= static_cast(surface_data_dest->height)) { + if (SCREEN_Y < 0 || SCREEN_Y >= surface_data_dest->height) { continue; } @@ -433,11 +433,11 @@ void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height for (int col = 0; col < SW; col++) { const int SCREEN_X = x + col; - if (SCREEN_X < 0 || SCREEN_X >= static_cast(surface_data_dest->width)) { + if (SCREEN_X < 0 || SCREEN_X >= surface_data_dest->width) { continue; } - const Uint8 COLOR = surface_data_->data[((SY + row) * static_cast(surface_data_->width)) + (SX + col)]; + const Uint8 COLOR = surface_data_->data[((SY + row) * surface_data_->width) + (SX + col)]; if (COLOR == static_cast(transparent_color_)) { continue; } @@ -446,7 +446,7 @@ void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height continue; // Pixel tapat per la zona de fade } - surface_data_dest->data[SCREEN_X + (SCREEN_Y * static_cast(surface_data_dest->width))] = sub_palette_[COLOR]; + surface_data_dest->data[SCREEN_X + (SCREEN_Y * surface_data_dest->width)] = sub_palette_[COLOR]; } } } @@ -455,14 +455,14 @@ void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height, Uint8 source_color, Uint8 target_color, SDL_FRect* src_rect) const { const int SX = (src_rect != nullptr) ? static_cast(src_rect->x) : 0; const int SY = (src_rect != nullptr) ? static_cast(src_rect->y) : 0; - const int SW = (src_rect != nullptr) ? static_cast(src_rect->w) : static_cast(surface_data_->width); - const int SH = (src_rect != nullptr) ? static_cast(src_rect->h) : static_cast(surface_data_->height); + const int SW = (src_rect != nullptr) ? static_cast(src_rect->w) : surface_data_->width; + const int SH = (src_rect != nullptr) ? static_cast(src_rect->h) : surface_data_->height; auto surface_data_dest = Screen::get()->getRendererSurface()->getSurfaceData(); for (int row = 0; row < SH; row++) { const int SCREEN_Y = y + row; - if (SCREEN_Y < 0 || SCREEN_Y >= static_cast(surface_data_dest->height)) { + if (SCREEN_Y < 0 || SCREEN_Y >= surface_data_dest->height) { continue; } @@ -470,11 +470,11 @@ void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height for (int col = 0; col < SW; col++) { const int SCREEN_X = x + col; - if (SCREEN_X < 0 || SCREEN_X >= static_cast(surface_data_dest->width)) { + if (SCREEN_X < 0 || SCREEN_X >= surface_data_dest->width) { continue; } - const Uint8 COLOR = surface_data_->data[((SY + row) * static_cast(surface_data_->width)) + (SX + col)]; + const Uint8 COLOR = surface_data_->data[((SY + row) * surface_data_->width) + (SX + col)]; if (COLOR == static_cast(transparent_color_)) { continue; } @@ -484,7 +484,7 @@ void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height } const Uint8 OUT_COLOR = (COLOR == source_color) ? target_color : sub_palette_[COLOR]; - surface_data_dest->data[SCREEN_X + (SCREEN_Y * static_cast(surface_data_dest->width))] = OUT_COLOR; + surface_data_dest->data[SCREEN_X + (SCREEN_Y * surface_data_dest->width)] = OUT_COLOR; } } } @@ -493,8 +493,8 @@ void Surface::renderWithVerticalFade(int x, int y, int fade_h, int canvas_height void Surface::toARGBBuffer(Uint32* buffer) const { if (!surface_data_ || !surface_data_->data || (buffer == nullptr)) { return; } - const int WIDTH = static_cast(surface_data_->width); - const int HEIGHT = static_cast(surface_data_->height); + const int WIDTH = surface_data_->width; + const int HEIGHT = surface_data_->height; const Uint8* src = surface_data_->data.get(); // Obtenemos el tamaño de la paleta para evitar accesos fuera de rango diff --git a/source/core/rendering/surface.hpp b/source/core/rendering/surface.hpp index 6bb26a7..572847d 100644 --- a/source/core/rendering/surface.hpp +++ b/source/core/rendering/surface.hpp @@ -22,8 +22,8 @@ auto readPalFile(const std::string& file_path) -> Palette; struct SurfaceData { std::shared_ptr data; // Usa std::shared_ptr para gestión automática - float width; // Ancho de la imagen - float height; // Alto de la imagen + int width; // Ancho de la imagen + int height; // Alto de la imagen // Constructor por defecto SurfaceData() @@ -32,13 +32,13 @@ struct SurfaceData { height(0) {} // Constructor que inicializa dimensiones y asigna memoria - SurfaceData(float w, float h) - : data(std::shared_ptr(new Uint8[static_cast(w * h)](), std::default_delete())), + SurfaceData(int w, int h) + : data(std::shared_ptr(new Uint8[static_cast(w) * static_cast(h)](), std::default_delete())), width(w), height(h) {} // Constructor para inicializar directamente con datos - SurfaceData(float w, float h, std::shared_ptr pixels) + SurfaceData(int w, int h, std::shared_ptr pixels) : data(std::move(pixels)), width(w), height(h) {} @@ -131,8 +131,8 @@ class Surface { [[nodiscard]] auto getSurfaceData() const -> std::shared_ptr { return surface_data_; } // Obtien ancho y alto - [[nodiscard]] auto getWidth() const -> float { return surface_data_->width; } - [[nodiscard]] auto getHeight() const -> float { return surface_data_->height; } + [[nodiscard]] auto getWidth() const -> int { return surface_data_->width; } + [[nodiscard]] auto getHeight() const -> int { return surface_data_->height; } // Color transparente [[nodiscard]] auto getTransparentColor() const -> Uint8 { return transparent_color_; } @@ -147,7 +147,7 @@ class Surface { private: // Helper para calcular coordenadas con flip - static void calculateFlippedCoords(int ix, int iy, float sx, float sy, float w, float h, SDL_FlipMode flip, int& src_x, int& src_y); + static void calculateFlippedCoords(int ix, int iy, int sx, int sy, int w, int h, SDL_FlipMode flip, int& src_x, int& src_y); // Helper para copiar un pixel si no es transparente void copyPixelIfNotTransparent(Uint8* dest_data, int dest_x, int dest_y, int dest_width, int src_x, int src_y) const; diff --git a/source/game/editor/mini_map.cpp b/source/game/editor/mini_map.cpp index aaae8da..ea2c2be 100644 --- a/source/game/editor/mini_map.cpp +++ b/source/game/editor/mini_map.cpp @@ -39,9 +39,9 @@ void MiniMap::buildTileColorTable(const std::string& tileset_name) { auto tileset = Resource::Cache::get()->getSurface(tileset_name); if (!tileset) { return; } - tileset_width_ = static_cast(tileset->getWidth()) / Tile::SIZE; + tileset_width_ = tileset->getWidth() / Tile::SIZE; tileset_transparent_ = tileset->getTransparentColor(); - int tileset_height = static_cast(tileset->getHeight()) / Tile::SIZE; + int tileset_height = tileset->getHeight() / Tile::SIZE; int total_tiles = tileset_width_ * tileset_height; tile_colors_.resize(total_tiles, 0); diff --git a/source/game/editor/tile_picker.cpp b/source/game/editor/tile_picker.cpp index 5d2df2e..e4bd205 100644 --- a/source/game/editor/tile_picker.cpp +++ b/source/game/editor/tile_picker.cpp @@ -26,8 +26,8 @@ void TilePicker::open(const std::string& tileset_name, int current_tile, int bg_ // Calcular dimensiones del tileset en tiles (teniendo en cuenta spacing de entrada) int src_cell = Tile::SIZE + spacing_in_; - tileset_width_ = static_cast(tileset_->getWidth()) / src_cell; - tileset_height_ = static_cast(tileset_->getHeight()) / src_cell; + tileset_width_ = tileset_->getWidth() / src_cell; + tileset_height_ = tileset_->getHeight() / src_cell; // Corregir si el último tile cabe sin spacing if (tileset_width_ == 0 && tileset_->getWidth() >= Tile::SIZE) { tileset_width_ = 1; } if (tileset_height_ == 0 && tileset_->getHeight() >= Tile::SIZE) { tileset_height_ = 1; } diff --git a/source/game/scenes/title.cpp b/source/game/scenes/title.cpp index bdfc002..cb38d8f 100644 --- a/source/game/scenes/title.cpp +++ b/source/game/scenes/title.cpp @@ -502,7 +502,7 @@ void Title::createCheevosTexture() { // NOLINT(readability-convert-member-funct // Crea el sprite para el listado de logros (usa la zona del menu) cheevos_sprite_ = std::make_unique(cheevos_surface_, (GameCanvas::WIDTH - cheevos_surface_->getWidth()) / 2, MENU_ZONE_Y, cheevos_surface_->getWidth(), cheevos_surface_->getHeight()); - cheevos_surface_view_ = {.x = 0, .y = 0, .w = cheevos_surface_->getWidth(), .h = CHEEVOS_TEXTURE_VIEW_HEIGHT}; + cheevos_surface_view_ = {.x = 0, .y = 0, .w = static_cast(cheevos_surface_->getWidth()), .h = CHEEVOS_TEXTURE_VIEW_HEIGHT}; cheevos_sprite_->setClip(cheevos_surface_view_); } diff --git a/source/game/ui/notifier.cpp b/source/game/ui/notifier.cpp index 11321d4..a58af8c 100644 --- a/source/game/ui/notifier.cpp +++ b/source/game/ui/notifier.cpp @@ -225,7 +225,7 @@ void Notifier::show(std::vector texts, const Style& style, int icon else if (SHAPE == Shape::SQUARED) { n.surface->clear(style.bg_color); - SDL_FRect squared_rect = {.x = 0, .y = 0, .w = n.surface->getWidth(), .h = n.surface->getHeight()}; + SDL_FRect squared_rect = {.x = 0, .y = 0, .w = static_cast(n.surface->getWidth()), .h = static_cast(n.surface->getHeight())}; n.surface->drawRectBorder(&squared_rect, style.border_color); }