From a65544e8b3fa7be03f86d7c1581c99ed814d97e1 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sun, 8 Mar 2026 22:36:10 +0100 Subject: [PATCH] fix: png_shape ja carrega de resources.pack Amb tots els fixos anteriors, el app de macos ja funciona correctament --- CMakeLists.txt | 5 +++++ source/engine.cpp | 2 +- source/shapes/png_shape.cpp | 30 ++++++++++++++++------------- source/shapes_mgr/shape_manager.cpp | 2 +- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7631f6a..9579360 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,3 +59,8 @@ set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAK # Enlazar las bibliotecas necesarias target_link_libraries(${PROJECT_NAME} ${LINK_LIBS}) + +# Tool: pack_resources +add_executable(pack_resources tools/pack_resources.cpp source/resource_pack.cpp) +target_include_directories(pack_resources PRIVATE ${CMAKE_SOURCE_DIR}/source) +set_target_properties(pack_resources PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tools") diff --git a/source/engine.cpp b/source/engine.cpp index c406400..69f8e1d 100644 --- a/source/engine.cpp +++ b/source/engine.cpp @@ -1829,7 +1829,7 @@ void Engine::activateShapeInternal(ShapeType type) { active_shape_ = std::make_unique(); break; case ShapeType::PNG_SHAPE: - active_shape_ = std::make_unique((getResourcesDirectory() + "/data/shapes/jailgames.png").c_str()); + active_shape_ = std::make_unique("shapes/jailgames.png"); break; default: active_shape_ = std::make_unique(); // Fallback diff --git a/source/shapes/png_shape.cpp b/source/shapes/png_shape.cpp index d337d0a..09ef7b2 100644 --- a/source/shapes/png_shape.cpp +++ b/source/shapes/png_shape.cpp @@ -1,6 +1,7 @@ #include "png_shape.hpp" #include "defines.hpp" #include "external/stb_image.h" +#include "resource_manager.hpp" #include #include #include @@ -20,26 +21,29 @@ PNGShape::PNGShape(const char* png_path) { next_idle_time_ = PNG_IDLE_TIME_MIN + (rand() % 1000) / 1000.0f * (PNG_IDLE_TIME_MAX - PNG_IDLE_TIME_MIN); } -bool PNGShape::loadPNG(const char* path) { - std::cout << "[PNGShape] Intentando cargar: " << path << std::endl; - int width, height, channels; - unsigned char* data = stbi_load(path, &width, &height, &channels, 1); // Forzar 1 canal (grayscale) - - if (!data) { - std::cerr << "[PNGShape] ERROR al cargar PNG: " << stbi_failure_reason() << std::endl; +bool PNGShape::loadPNG(const char* resource_key) { + std::cout << "[PNGShape] Cargando recurso: " << resource_key << std::endl; + unsigned char* file_data = nullptr; + size_t file_size = 0; + if (!ResourceManager::loadResource(resource_key, file_data, file_size)) { + std::cerr << "[PNGShape] ERROR: recurso no encontrado: " << resource_key << std::endl; + return false; + } + int width, height, channels; + unsigned char* pixels = stbi_load_from_memory(file_data, static_cast(file_size), + &width, &height, &channels, 1); + delete[] file_data; + if (!pixels) { + std::cerr << "[PNGShape] ERROR al decodificar PNG: " << stbi_failure_reason() << std::endl; return false; } - image_width_ = width; image_height_ = height; pixel_data_.resize(width * height); - - // Convertir a mapa booleano (true = pĂ­xel blanco/visible, false = negro/transparente) for (int i = 0; i < width * height; i++) { - pixel_data_[i] = (data[i] > 128); // Umbral: >128 = blanco + pixel_data_[i] = (pixels[i] > 128); } - - stbi_image_free(data); + stbi_image_free(pixels); return true; } diff --git a/source/shapes_mgr/shape_manager.cpp b/source/shapes_mgr/shape_manager.cpp index 507ab87..7f43985 100644 --- a/source/shapes_mgr/shape_manager.cpp +++ b/source/shapes_mgr/shape_manager.cpp @@ -269,7 +269,7 @@ void ShapeManager::activateShapeInternal(ShapeType type) { active_shape_ = std::make_unique(); break; case ShapeType::PNG_SHAPE: - active_shape_ = std::make_unique((getResourcesDirectory() + "/data/shapes/jailgames.png").c_str()); + active_shape_ = std::make_unique("shapes/jailgames.png"); break; default: active_shape_ = std::make_unique(); // Fallback