From b625916b1804874414a87c212bb018d0883ae70b Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 25 Oct 2025 13:10:41 +0200 Subject: [PATCH] afegit executable path a les rutes --- src/defines.hpp | 59 ++++++++++++++++++++++++++++++++++++++++++++----- src/main.cpp | 11 +++++---- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/defines.hpp b/src/defines.hpp index 6cfda60..f971191 100644 --- a/src/defines.hpp +++ b/src/defines.hpp @@ -1,5 +1,8 @@ #pragma once +#include +#include + // Nombre de la aplicación constexpr const char* APP_NAME = "Shadertoy"; @@ -7,11 +10,55 @@ constexpr const char* APP_NAME = "Shadertoy"; constexpr int WINDOW_WIDTH = 800; constexpr int WINDOW_HEIGHT = 800; -// Rutas -#ifdef MACOS_BUNDLE - constexpr const char* SHADERS_FOLDER = "../Resources/shaders"; - constexpr const char* DEFAULT_SHADER = "../Resources/shaders/test.frag.glsl"; +// Includes específicos por plataforma para obtener la ruta del ejecutable +#ifdef _WIN32 +#include +#elif defined(__APPLE__) +#include +#include #else - constexpr const char* SHADERS_FOLDER = "shaders"; - constexpr const char* DEFAULT_SHADER = "shaders/test.frag.glsl"; +#include +#include #endif + +// Función auxiliar para obtener la ruta del directorio del ejecutable +inline std::string getExecutableDirectory() { +#ifdef _WIN32 + char buffer[MAX_PATH]; + GetModuleFileNameA(NULL, buffer, MAX_PATH); + std::filesystem::path exe_path(buffer); + return exe_path.parent_path().string(); +#elif defined(__APPLE__) + char buffer[PATH_MAX]; + uint32_t size = sizeof(buffer); + if (_NSGetExecutablePath(buffer, &size) == 0) { + std::filesystem::path exe_path(buffer); + return exe_path.parent_path().string(); + } + return "."; +#else + // Linux y otros Unix + char buffer[PATH_MAX]; + ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1); + if (len != -1) { + buffer[len] = '\0'; + std::filesystem::path exe_path(buffer); + return exe_path.parent_path().string(); + } + return "."; +#endif +} + +// Función auxiliar para obtener la ruta del directorio de recursos +inline std::string getResourcesDirectory() { + std::string exe_dir = getExecutableDirectory(); + +#ifdef MACOS_BUNDLE + // En macOS Bundle: ejecutable está en Contents/MacOS/, recursos en Contents/Resources/ + std::filesystem::path resources_path = std::filesystem::path(exe_dir) / ".." / "Resources"; + return resources_path.string(); +#else + // En desarrollo o releases normales: recursos están junto al ejecutable + return exe_dir; +#endif +} diff --git a/src/main.cpp b/src/main.cpp index ef9c8dd..d32479d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -283,7 +283,7 @@ int main(int argc, char** argv) { if (a == "-F" || a == "--fullscreen") { fullscreenFlag = true; continue; } if (shaderPath.empty()) shaderPath = a; } - if (shaderPath.empty()) shaderPath = DEFAULT_SHADER; + if (shaderPath.empty()) shaderPath = "test.frag.glsl"; Options_video.fullscreen = fullscreenFlag; // Inicializar SDL3 @@ -326,12 +326,15 @@ int main(int argc, char** argv) { return -1; } + // Obtener directorio de recursos + std::string resources_dir = getResourcesDirectory(); + // Determinar carpeta de shaders std::filesystem::path shaderFile(shaderPath); if (shaderFile.has_parent_path()) { shaders_directory_ = shaderFile.parent_path(); } else { - shaders_directory_ = SHADERS_FOLDER; + shaders_directory_ = std::filesystem::path(resources_dir) / "shaders"; } // Escanear carpeta de shaders @@ -358,9 +361,9 @@ int main(int argc, char** argv) { } } - // Si no se encuentra, intentar con DEFAULT_SHADER + // Si no se encuentra, intentar con shader por defecto (test.frag.glsl) if (!found_shader) { - std::filesystem::path default_path(DEFAULT_SHADER); + std::filesystem::path default_path = std::filesystem::path(resources_dir) / "shaders" / "test.frag.glsl"; for (size_t i = 0; i < shader_list_.size(); ++i) { if (shader_list_[i] == default_path) { initial_index = i;