3 Commits

5 changed files with 503 additions and 199 deletions

8
.gitignore vendored
View File

@@ -5,7 +5,6 @@
/CMakeFiles/ /CMakeFiles/
/CMakeCache.txt /CMakeCache.txt
/cmake_install.cmake /cmake_install.cmake
/Makefile
/*.cmake /*.cmake
/*.obj /*.obj
/*.o /*.o
@@ -29,9 +28,16 @@ third_party/**/build/
third_party/**/CMakeFiles/ third_party/**/CMakeFiles/
third_party/**/CMakeCache.txt third_party/**/CMakeCache.txt
# Release artifacts
shadertoy_release/
*.zip
*.tar.gz
*.dmg
# IDEs and editors # IDEs and editors
.vscode/ .vscode/
.idea/ .idea/
.claude/
*.suo *.suo
*.user *.user
*.userprefs *.userprefs

View File

@@ -1,143 +1,77 @@
cmake_minimum_required(VERSION 3.14) # CMakeLists.txt
project(shadertoy_sdl3 LANGUAGES C CXX)
cmake_minimum_required(VERSION 3.10)
project(shadertoy VERSION 1.00)
# Establecer estándar de C++
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED True)
# Default to Release for single-config generators when none specified # Establece la política CMP0072 para indicar cómo se debe seleccionar la implementación de OpenGL.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) # En este caso, se elige la opción "GLVND", que utiliza bibliotecas modernas y modulares (libOpenGL, libGLX),
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) # en lugar de la biblioteca OpenGL clásica (libGL). Esto mejora la compatibilidad con drivers recientes
endif() # y evita ambigüedades cuando se encuentran múltiples implementaciones de OpenGL en el sistema.
cmake_policy(SET CMP0072 NEW)
set(OpenGL_GL_PREFERENCE GLVND)
# Paths # --- LISTA EXPLÍCITA DE FUENTES ---
set(GLAD_SRC "${CMAKE_CURRENT_SOURCE_DIR}/third_party/glad/src/glad.c") set(APP_SOURCES
if(NOT EXISTS "${GLAD_SRC}")
message(FATAL_ERROR "glad.c no encontrado en: ${GLAD_SRC}")
endif()
# glad library
add_library(glad_src STATIC "${GLAD_SRC}")
target_include_directories(glad_src PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/third_party/glad/include
)
set_target_properties(glad_src PROPERTIES LINKER_LANGUAGE C)
# Executable
add_executable(shadertoy_sdl3
src/main.cpp src/main.cpp
) )
target_include_directories(shadertoy_sdl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/glad/include)
# SDL3 # Fuentes de librerías de terceros
find_package(SDL3 REQUIRED) set(EXTERNAL_SOURCES
target_link_libraries(shadertoy_sdl3 PRIVATE glad_src SDL3::SDL3) third_party/glad/src/glad.c
)
# Platform-specific flags and linkers (try to mimic your Makefile) # Configuración de SDL3
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}")
# --- AÑADIR EJECUTABLE ---
add_executable(${PROJECT_NAME} ${APP_SOURCES} ${EXTERNAL_SOURCES})
# --- DIRECTORIOS DE INCLUSIÓN ---
target_include_directories(${PROJECT_NAME} PUBLIC
"${CMAKE_SOURCE_DIR}/src"
"${CMAKE_SOURCE_DIR}/third_party/glad/include"
)
# Enlazar la librería SDL3
target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3)
# --- CONFIGURACIÓN PLATAFORMAS Y COMPILADOR ---
# Configuración de flags de compilación
target_compile_options(${PROJECT_NAME} PRIVATE -Wall)
target_compile_options(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:-Os -ffunction-sections -fdata-sections>)
# Definir _DEBUG en modo Debug
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:DEBUG>:_DEBUG>)
# Configuración específica para cada plataforma
if(WIN32) if(WIN32)
target_compile_definitions(shadertoy_sdl3 PRIVATE WINDOWS_BUILD) target_compile_definitions(${PROJECT_NAME} PRIVATE WINDOWS_BUILD)
if(MSVC) target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32 mingw32 opengl32)
# MSVC: reasonable defaults; adjust as needed elseif(APPLE)
target_compile_options(shadertoy_sdl3 PRIVATE /W3 /permissive-) target_compile_definitions(${PROJECT_NAME} PRIVATE MACOS_BUILD)
# Link to system libraries (MSVC uses system libs automatically for system includes) target_compile_options(${PROJECT_NAME} PRIVATE -Wno-deprecated)
target_link_libraries(shadertoy_sdl3 PRIVATE opengl32) set(CMAKE_OSX_ARCHITECTURES "arm64")
else() elseif(UNIX AND NOT APPLE)
# MinGW / GNU on Windows target_compile_definitions(${PROJECT_NAME} PRIVATE LINUX_BUILD)
target_compile_options(shadertoy_sdl3 PRIVATE -std=c++17 -Wall -Os -ffunction-sections -fdata-sections)
target_link_options(shadertoy_sdl3 PRIVATE "-Wl,--gc-sections" "-Wl,-Bstatic" "-Wl,-Bdynamic")
target_link_libraries(shadertoy_sdl3 PRIVATE -lmingw32 -lws2_32 -lSDL3 -lopengl32)
endif() endif()
else()
# POSIX platforms # Configuración común para OpenGL
target_compile_options(shadertoy_sdl3 PRIVATE -std=c++17 -Wall -Os) if(NOT WIN32)
if(APPLE)
find_library(COCOA_LIBRARY Cocoa REQUIRED)
find_library(IOKIT_LIBRARY IOKit REQUIRED)
find_library(CORE_VIDEO_LIBRARY CoreVideo REQUIRED)
target_link_libraries(shadertoy_sdl3 PRIVATE ${COCOA_LIBRARY} ${IOKIT_LIBRARY} ${CORE_VIDEO_LIBRARY})
target_compile_options(shadertoy_sdl3 PRIVATE -arch arm64)
else()
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
target_link_libraries(shadertoy_sdl3 PRIVATE OpenGL::GL) if(OPENGL_FOUND)
message(STATUS "OpenGL encontrado: ${OPENGL_LIBRARIES}")
target_link_libraries(${PROJECT_NAME} PRIVATE ${OPENGL_LIBRARIES})
else()
message(FATAL_ERROR "OpenGL no encontrado")
endif() endif()
endif() endif()
# Ensure RUNTIME output directory for single-config builds (Release/Debug) # Especificar la ubicación del ejecutable en la raíz del proyecto
set_target_properties(shadertoy_sdl3 PROPERTIES set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/bin/Release"
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/bin/Debug"
)
if(WIN32)
set(RELEASE_DIR "${CMAKE_BINARY_DIR}/release_package")
set(WINDOWS_ZIP "${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${CMAKE_SYSTEM_PROCESSOR}-win.zip")
set(DLL_SOURCE_DIR "${CMAKE_SOURCE_DIR}/release") # coloca tus SDL3.dll aquí
# PowerShell script que realizará las copias y el ZIP de forma robusta.
set(PS_SCRIPT "${CMAKE_BINARY_DIR}/package_windows.ps1")
file(WRITE "${PS_SCRIPT}"
"param(\n"
" [string]\$srcExe,\n"
" [string]\$releaseDir,\n"
" [string]\$readme,\n"
" [string]\$license,\n"
" [string]\$shadersDir,\n"
" [string]\$dllDir,\n"
" [string]\$outZip\n"
")\n"
"\n"
"# Remove old\n"
"if (Test-Path \$releaseDir) { Remove-Item -Recurse -Force \$releaseDir }\n"
"New-Item -ItemType Directory -Path \$releaseDir | Out-Null\n"
"\n"
"# Copy exe\n"
"Write-Host 'Copying executable...'\n"
"Copy-Item -Path \$srcExe -Destination (Join-Path \$releaseDir (Split-Path \$srcExe -Leaf)) -Force\n"
"\n"
"# Copy README / LICENSE if exist\n"
"if (Test-Path \$readme) { Copy-Item -Path \$readme -Destination (Join-Path \$releaseDir 'README.md') -Force }\n"
"if (Test-Path \$license) { Copy-Item -Path \$license -Destination (Join-Path \$releaseDir 'LICENSE') -Force }\n"
"\n"
"# Copy shaders if present\n"
"if (Test-Path \$shadersDir) { Copy-Item -Path (Join-Path \$shadersDir '*') -Destination (Join-Path \$releaseDir 'shaders') -Recurse -Force }\n"
"\n"
"# Copy DLLs if present\n"
"if (Test-Path \$dllDir) { Copy-Item -Path (Join-Path \$dllDir '*') -Destination (Join-Path \$releaseDir 'release') -Recurse -Force }\n"
"\n"
"# Create ZIP\n"
"Write-Host 'Creating zip: ' \$outZip\n"
"if (Test-Path \$outZip) { Remove-Item -Force \$outZip }\n"
"Compress-Archive -Path (Join-Path \$releaseDir '*') -DestinationPath \$outZip\n"
"Write-Host 'Package created: ' \$outZip\n"
)
add_custom_target(package_windows
COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --config Release
COMMAND powershell.exe -NoProfile -ExecutionPolicy Bypass -File "${PS_SCRIPT}"
-srcExe "$<TARGET_FILE:shadertoy_sdl3>"
-releaseDir "${RELEASE_DIR}"
-readme "${CMAKE_SOURCE_DIR}/README.md"
-license "${CMAKE_SOURCE_DIR}/LICENSE"
-shadersDir "${CMAKE_SOURCE_DIR}/shaders"
-dllDir "${DLL_SOURCE_DIR}"
-outZip "${WINDOWS_ZIP}"
COMMENT "Building Release and creating Windows package"
BYPRODUCTS "${WINDOWS_ZIP}"
)
endif()
# Helpful messages
message(STATUS "CMake generator: ${CMAKE_GENERATOR}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Binary output (runtime): ${CMAKE_BINARY_DIR}/bin")
# Developer target: build Release and then package on Windows
add_custom_target(build_release
COMMAND ${CMAKE_COMMAND} --build "${CMAKE_BINARY_DIR}" --config Release
COMMENT "Building Release configuration"
)
if(WIN32)
add_dependencies(package_windows build_release)
endif()

223
Makefile Normal file
View File

@@ -0,0 +1,223 @@
# Directorios
DIR_ROOT := $(dir $(abspath $(MAKEFILE_LIST)))
DIR_SOURCES := $(addsuffix /, $(DIR_ROOT)src)
DIR_BIN := $(addsuffix /, $(DIR_ROOT))
DIR_BUILD := $(addsuffix /, $(DIR_ROOT)build)
# Variables
TARGET_NAME := shadertoy
TARGET_FILE := $(DIR_BIN)$(TARGET_NAME)
APP_NAME := Shadertoy
RELEASE_FOLDER := shadertoy_release
# Versión automática basada en la fecha actual (específica por SO)
ifeq ($(OS),Windows_NT)
VERSION := $(shell powershell -Command "Get-Date -Format 'yyyy-MM-dd'")
else
VERSION := $(shell date +%Y-%m-%d)
endif
# Variables específicas para Windows (usando APP_NAME)
ifeq ($(OS),Windows_NT)
WIN_TARGET_FILE := $(DIR_BIN)$(APP_NAME)
WIN_RELEASE_FILE := $(RELEASE_FOLDER)/$(APP_NAME)
else
WIN_TARGET_FILE := $(TARGET_FILE)
WIN_RELEASE_FILE := $(RELEASE_FOLDER)/$(TARGET_NAME)
endif
# Nombres para los ficheros de lanzamiento
WINDOWS_RELEASE := $(TARGET_NAME)-$(VERSION)-win32-x64.zip
MACOS_APPLE_SILICON_RELEASE := $(TARGET_NAME)-$(VERSION)-macos-apple-silicon.dmg
LINUX_RELEASE := $(TARGET_NAME)-$(VERSION)-linux.tar.gz
# Lista completa de archivos fuente
APP_SOURCES := \
src/main.cpp \
third_party/glad/src/glad.c
# Includes
INCLUDES := -Isrc -Ithird_party/glad/include
# Variables según el sistema operativo
ifeq ($(OS),Windows_NT)
FixPath = $(subst /,\\,$1)
CXXFLAGS := -std=c++17 -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -static-libgcc -Wl,-Bstatic -lpthread -Wl,-Bdynamic -Wl,-subsystem,windows -DWINDOWS_BUILD
CXXFLAGS_DEBUG := -std=c++17 -Wall -g -D_DEBUG -DWINDOWS_BUILD
LDFLAGS := -lmingw32 -lws2_32 -lSDL3 -lopengl32
RM := del /Q
MKDIR := mkdir
else
FixPath = $1
CXXFLAGS := -std=c++17 -Wall -Os -ffunction-sections -fdata-sections
CXXFLAGS_DEBUG := -std=c++17 -Wall -g -D_DEBUG
LDFLAGS := -lSDL3
RMFILE := rm -f
RMDIR := rm -rdf
MKDIR := mkdir -p
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
CXXFLAGS += -DLINUX_BUILD
LDFLAGS += -lGL
endif
ifeq ($(UNAME_S),Darwin)
CXXFLAGS += -Wno-deprecated -DMACOS_BUILD
CXXFLAGS_DEBUG += -Wno-deprecated -DMACOS_BUILD
LDFLAGS += -framework OpenGL
# Configurar arquitectura (por defecto arm64, como en CMake)
CXXFLAGS += -arch arm64
CXXFLAGS_DEBUG += -arch arm64
endif
endif
# Reglas para compilación
windows:
@echo off
@echo Compilando para Windows con nombre: "$(APP_NAME).exe"
$(CXX) $(APP_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(WIN_TARGET_FILE).exe"
strip -s -R .comment -R .gnu.version "$(WIN_TARGET_FILE).exe" --strip-unneeded
windows_debug:
@echo off
@echo Compilando version debug para Windows: "$(APP_NAME)_debug.exe"
$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(WIN_TARGET_FILE)_debug.exe"
windows_release:
@echo off
@echo Creando release para Windows - Version: $(VERSION)
# Crea carpeta temporal 'RELEASE_FOLDER'
powershell if (Test-Path "$(RELEASE_FOLDER)") {Remove-Item "$(RELEASE_FOLDER)" -Recurse -Force}
powershell if (-not (Test-Path "$(RELEASE_FOLDER)")) {New-Item "$(RELEASE_FOLDER)" -ItemType Directory}
# Copia la carpeta 'shaders'
powershell Copy-Item -Path "shaders" -Destination "$(RELEASE_FOLDER)" -recurse -Force
# Copia los ficheros que están en la raíz del proyecto
powershell Copy-Item "LICENSE" -Destination "$(RELEASE_FOLDER)"
powershell Copy-Item "README.md" -Destination "$(RELEASE_FOLDER)"
powershell Copy-Item "release\*.dll" -Destination "$(RELEASE_FOLDER)"
# Compila
$(CXX) $(APP_SOURCES) $(INCLUDES) -DRELEASE_BUILD $(CXXFLAGS) $(LDFLAGS) -o "$(WIN_RELEASE_FILE).exe"
strip -s -R .comment -R .gnu.version "$(WIN_RELEASE_FILE).exe" --strip-unneeded
# Crea el fichero .zip
powershell if (Test-Path "$(WINDOWS_RELEASE)") {Remove-Item "$(WINDOWS_RELEASE)"}
powershell Compress-Archive -Path "$(RELEASE_FOLDER)"/* -DestinationPath "$(WINDOWS_RELEASE)"
@echo Release creado: $(WINDOWS_RELEASE)
# Elimina la carpeta temporal 'RELEASE_FOLDER'
powershell if (Test-Path "$(RELEASE_FOLDER)") {Remove-Item "$(RELEASE_FOLDER)" -Recurse -Force}
macos:
@echo "Compilando para macOS: $(TARGET_NAME)"
$(CXX) $(APP_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)"
macos_debug:
@echo "Compilando version debug para macOS: $(TARGET_NAME)_debug"
$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"
macos_release:
@echo "Creando release para macOS - Version: $(VERSION)"
# Verificar e instalar create-dmg si es necesario
@which create-dmg > /dev/null || (echo "Instalando create-dmg..." && brew install create-dmg)
# Elimina datos de compilaciones anteriores
$(RMDIR) "$(RELEASE_FOLDER)"
$(RMFILE) tmp.dmg
$(RMFILE) "$(MACOS_APPLE_SILICON_RELEASE)"
# Crea la carpeta temporal para hacer el trabajo y las carpetas obligatorias para crear una app de macos
$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS"
$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
# Copia carpetas y ficheros
cp -R shaders "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
cp LICENSE "$(RELEASE_FOLDER)"
cp README.md "$(RELEASE_FOLDER)"
# Compila la versión para procesadores Apple Silicon
$(CXX) $(APP_SOURCES) $(INCLUDES) -DMACOS_BUNDLE -DRELEASE_BUILD $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS/$(TARGET_NAME)" -target arm64-apple-macos11
# Firma la aplicación
codesign --deep --force --sign - --timestamp=none "$(RELEASE_FOLDER)/$(APP_NAME).app"
# Empaqueta el .dmg de la versión Apple Silicon con create-dmg
@echo "Creando DMG Apple Silicon con iconos de 96x96..."
create-dmg \
--volname "$(APP_NAME)" \
--window-pos 200 120 \
--window-size 720 300 \
--icon-size 96 \
--text-size 12 \
--icon "$(APP_NAME).app" 278 102 \
--icon "LICENSE" 441 102 \
--icon "README.md" 604 102 \
--app-drop-link 115 102 \
--hide-extension "$(APP_NAME).app" \
"$(MACOS_APPLE_SILICON_RELEASE)" \
"$(RELEASE_FOLDER)" || true
@echo "Release Apple Silicon creado: $(MACOS_APPLE_SILICON_RELEASE)"
# Elimina las carpetas temporales
$(RMDIR) "$(RELEASE_FOLDER)"
linux:
@echo "Compilando para Linux: $(TARGET_NAME)"
$(CXX) $(APP_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)"
strip -s -R .comment -R .gnu.version "$(TARGET_FILE)" --strip-unneeded
linux_debug:
@echo "Compilando version debug para Linux: $(TARGET_NAME)_debug"
$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"
linux_release:
@echo "Creando release para Linux - Version: $(VERSION)"
# Elimina carpetas previas
$(RMDIR) "$(RELEASE_FOLDER)"
# Crea la carpeta temporal para realizar el lanzamiento
$(MKDIR) "$(RELEASE_FOLDER)"
# Copia ficheros
cp -R shaders "$(RELEASE_FOLDER)"
cp LICENSE "$(RELEASE_FOLDER)"
cp README.md "$(RELEASE_FOLDER)"
# Compila
$(CXX) $(APP_SOURCES) $(INCLUDES) -DRELEASE_BUILD $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FOLDER)/$(TARGET_NAME)"
strip -s -R .comment -R .gnu.version "$(RELEASE_FOLDER)/$(TARGET_NAME)" --strip-unneeded
# Empaqueta ficheros
$(RMFILE) "$(LINUX_RELEASE)"
tar -czvf "$(LINUX_RELEASE)" -C "$(RELEASE_FOLDER)" .
@echo "Release creado: $(LINUX_RELEASE)"
# Elimina la carpeta temporal
$(RMDIR) "$(RELEASE_FOLDER)"
# Regla para mostrar la versión actual
show_version:
@echo "Version actual: $(VERSION)"
# Regla de ayuda
help:
@echo "Makefile para Shadertoy"
@echo "Comandos disponibles:"
@echo " windows - Compilar para Windows"
@echo " windows_debug - Compilar debug para Windows"
@echo " windows_release - Crear release completo para Windows"
@echo " linux - Compilar para Linux"
@echo " linux_debug - Compilar debug para Linux"
@echo " linux_release - Crear release completo para Linux"
@echo " macos - Compilar para macOS"
@echo " macos_debug - Compilar debug para macOS"
@echo " macos_release - Crear release completo para macOS"
@echo " show_version - Mostrar version actual ($(VERSION))"
@echo " help - Mostrar esta ayuda"
.PHONY: windows windows_debug windows_release macos macos_debug macos_release linux linux_debug linux_release show_version help
FORCE:

12
src/defines.hpp Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
// Nombre de la aplicación
constexpr const char* APP_NAME = "Shadertoy";
// Tamaño de ventana por defecto
constexpr int WINDOW_WIDTH = 800;
constexpr int WINDOW_HEIGHT = 800;
// Rutas
constexpr const char* SHADERS_FOLDER = "shaders";
constexpr const char* DEFAULT_SHADER = "shaders/test.frag.glsl";

View File

@@ -4,11 +4,14 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <algorithm>
#include <type_traits> #include <type_traits>
#include <filesystem> #include <filesystem>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <glad/glad.h> #include <glad/glad.h>
#include "defines.hpp"
// Simple logger compatible con el estilo que usas // Simple logger compatible con el estilo que usas
struct Logger { struct Logger {
static void info(const std::string& s) { std::cout << "[INFO] " << s << '\n'; } static void info(const std::string& s) { std::cout << "[INFO] " << s << '\n'; }
@@ -32,8 +35,12 @@ struct DisplayMonitor {
static DisplayMonitor display_monitor_; static DisplayMonitor display_monitor_;
static SDL_Window* window_ = nullptr; static SDL_Window* window_ = nullptr;
// Constante por defecto del fragment shader // Sistema de shaders
static constexpr const char* DEFAULT_FRAG = "shaders/test.frag.glsl"; static std::vector<std::filesystem::path> shader_list_;
static size_t current_shader_index_ = 0;
static std::filesystem::path shaders_directory_;
static GLuint current_program_ = 0;
static Uint32 shader_start_ticks_ = 0;
// Vertex shader embebido // Vertex shader embebido
static const char* vertexShaderSrc = R"glsl( static const char* vertexShaderSrc = R"glsl(
@@ -56,6 +63,38 @@ static bool loadFileToString(const std::filesystem::path& path, std::string& out
return true; return true;
} }
static std::vector<std::filesystem::path> scanShaderDirectory(const std::filesystem::path& directory) {
std::vector<std::filesystem::path> shaders;
if (!std::filesystem::exists(directory) || !std::filesystem::is_directory(directory)) {
Logger::error("Shader directory does not exist: " + directory.string());
return shaders;
}
for (const auto& entry : std::filesystem::directory_iterator(directory)) {
if (entry.is_regular_file()) {
auto ext = entry.path().extension().string();
if (ext == ".glsl") {
shaders.push_back(entry.path());
}
}
}
// Ordenar alfabéticamente
std::sort(shaders.begin(), shaders.end());
Logger::info("Found " + std::to_string(shaders.size()) + " shader(s) in " + directory.string());
return shaders;
}
static void updateWindowTitle() {
if (!window_ || shader_list_.empty()) return;
std::string filename = shader_list_[current_shader_index_].filename().string();
std::string title = std::string(APP_NAME) + " (" + filename + ")";
SDL_SetWindowTitle(window_, title.c_str());
}
static GLuint compileShader(GLenum type, const char* src) { static GLuint compileShader(GLenum type, const char* src) {
GLuint s = glCreateShader(type); GLuint s = glCreateShader(type);
glShaderSource(s, 1, &src, nullptr); glShaderSource(s, 1, &src, nullptr);
@@ -93,6 +132,44 @@ static GLuint linkProgram(GLuint vs, GLuint fs) {
return p; return p;
} }
static GLuint loadAndCompileShader(size_t index) {
if (index >= shader_list_.size()) {
Logger::error("Invalid shader index: " + std::to_string(index));
return 0;
}
const auto& shaderPath = shader_list_[index];
Logger::info("Loading shader: " + shaderPath.string());
std::string fragSrc;
if (!loadFileToString(shaderPath, fragSrc)) {
Logger::error("Failed to load shader file: " + shaderPath.string());
return 0;
}
GLuint vs = compileShader(GL_VERTEX_SHADER, vertexShaderSrc);
GLuint fs = compileShader(GL_FRAGMENT_SHADER, fragSrc.c_str());
if (!vs || !fs) {
if (vs) glDeleteShader(vs);
if (fs) glDeleteShader(fs);
Logger::error("Shader compilation failed for: " + shaderPath.string());
return 0;
}
GLuint program = linkProgram(vs, fs);
glDeleteShader(vs);
glDeleteShader(fs);
if (!program) {
Logger::error("Program linking failed for: " + shaderPath.string());
return 0;
}
Logger::info("Shader loaded successfully: " + shaderPath.filename().string());
return program;
}
// --- Funciones basadas en tu código --- // --- Funciones basadas en tu código ---
void getDisplayInfo() { void getDisplayInfo() {
int num_displays = 0; int num_displays = 0;
@@ -120,28 +197,22 @@ void getDisplayInfo() {
} }
void setFullscreenMode() { void setFullscreenMode() {
// SDL3: la API acepta un bool para fullscreen (true = fullscreen desktop / exclusive depending on param)
// En tu repos tienes Options::video.fullscreen (bool), así lo usamos:
if (!window_) return; if (!window_) return;
// Si se pide fullscreen = true, preferimos fullscreen exclusivo si se puede ajustar tamaño
if (Options_video.fullscreen) { if (Options_video.fullscreen) {
// Si conocemos la resolución nativa, forzamos tamaño de ventana y pedimos fullscreen exclusivo // Intentar fullscreen
if (display_monitor_.width > 0 && display_monitor_.height > 0) { if (!SDL_SetWindowFullscreen(window_, true)) {
SDL_SetWindowSize(window_, display_monitor_.width, display_monitor_.height); // Fallback: volver a modo ventana si falla
// SDL3 acepta SDL_SetWindowFullscreen(window, true) para fullscreen; algunos drivers pueden ofrecer exclusive/fullscreen desktop. Logger::error(std::string("Failed to set fullscreen: ") + SDL_GetError());
if (SDL_SetWindowFullscreen(window_, true) != 0) { Logger::info("Fallback to windowed mode 800x800");
// fallback: intentar fullscreen desktop (true funciona como desktop fullscreen en muchas implementaciones)
Logger::info("SDL_SetWindowFullscreen(exclusive) failed, fallback to fullscreen desktop");
SDL_SetWindowFullscreen(window_, true); // ya es el mismo en SDL3; dejamos registro
}
} else {
// desconocemos resolución, pedimos fullscreen desktop
SDL_SetWindowFullscreen(window_, true);
}
} else {
// volver a ventana
SDL_SetWindowFullscreen(window_, false); SDL_SetWindowFullscreen(window_, false);
SDL_SetWindowSize(window_, WINDOW_WIDTH, WINDOW_HEIGHT);
Options_video.fullscreen = false;
}
} else {
// Volver a modo ventana 800x800
SDL_SetWindowFullscreen(window_, false);
SDL_SetWindowSize(window_, WINDOW_WIDTH, WINDOW_HEIGHT);
} }
} }
@@ -150,25 +221,53 @@ void toggleFullscreen() {
setFullscreenMode(); setFullscreenMode();
} }
// Manejo de teclas de debug (adaptado a tu estilo) void switchShader(int direction) {
if (shader_list_.empty()) return;
// Calcular nuevo índice con wrap-around cíclico
size_t new_index = current_shader_index_;
if (direction > 0) {
new_index = (current_shader_index_ + 1) % shader_list_.size();
} else if (direction < 0) {
new_index = (current_shader_index_ == 0) ? shader_list_.size() - 1 : current_shader_index_ - 1;
}
// Intentar cargar el nuevo shader
GLuint new_program = loadAndCompileShader(new_index);
if (new_program == 0) {
Logger::error("Failed to switch shader, keeping current one");
return;
}
// Éxito: eliminar programa anterior y actualizar
if (current_program_ != 0) {
glDeleteProgram(current_program_);
}
current_program_ = new_program;
current_shader_index_ = new_index;
shader_start_ticks_ = SDL_GetTicks();
updateWindowTitle();
}
// Manejo de teclas
void handleDebugEvents(const SDL_Event& event) { void handleDebugEvents(const SDL_Event& event) {
// evitar repetición de teclas: event.key.repeat disponible en SDL3 // evitar repetición de teclas: event.key.repeat disponible en SDL3
if (event.type == SDL_EVENT_KEY_DOWN && static_cast<int>(event.key.repeat) == 0) { if (event.type == SDL_EVENT_KEY_DOWN && static_cast<int>(event.key.repeat) == 0) {
switch (event.key.key) { switch (event.key.key) {
case SDLK_1: { case SDLK_F3: {
Logger::info("Key 1 pressed (action placeholder)");
break;
}
case SDLK_2: {
static bool deploy_balloons_ = true;
deploy_balloons_ = !deploy_balloons_;
Logger::info(std::string("Toggle balloons: ") + (deploy_balloons_ ? "on" : "off"));
break;
}
case SDLK_F11: {
toggleFullscreen(); toggleFullscreen();
break; break;
} }
case SDLK_LEFT: {
switchShader(-1);
break;
}
case SDLK_RIGHT: {
switchShader(+1);
break;
}
default: default:
break; break;
} }
@@ -184,7 +283,7 @@ int main(int argc, char** argv) {
if (a == "-F" || a == "--fullscreen") { fullscreenFlag = true; continue; } if (a == "-F" || a == "--fullscreen") { fullscreenFlag = true; continue; }
if (shaderPath.empty()) shaderPath = a; if (shaderPath.empty()) shaderPath = a;
} }
if (shaderPath.empty()) shaderPath = DEFAULT_FRAG; if (shaderPath.empty()) shaderPath = DEFAULT_SHADER;
Options_video.fullscreen = fullscreenFlag; Options_video.fullscreen = fullscreenFlag;
// Inicializar SDL3 // Inicializar SDL3
@@ -204,8 +303,7 @@ int main(int argc, char** argv) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
// Crear ventana // Crear ventana
int winW = 800, winH = 800; window_ = SDL_CreateWindow(APP_NAME, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
window_ = SDL_CreateWindow("Shadertoy SDL3 + OpenGL", winW, winH, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!window_) { Logger::error(std::string("SDL_CreateWindow error: ") + SDL_GetError()); SDL_Quit(); return -1; } if (!window_) { Logger::error(std::string("SDL_CreateWindow error: ") + SDL_GetError()); SDL_Quit(); return -1; }
// Aplicar fullscreen si el flag estaba activado // Aplicar fullscreen si el flag estaba activado
@@ -228,41 +326,69 @@ int main(int argc, char** argv) {
return -1; return -1;
} }
// Localizar y cargar el fragment shader (intenta rutas comunes) // Determinar carpeta de shaders
std::vector<std::filesystem::path> candidates; std::filesystem::path shaderFile(shaderPath);
candidates.emplace_back(shaderPath); if (shaderFile.has_parent_path()) {
candidates.emplace_back(std::filesystem::path("..") / shaderPath); shaders_directory_ = shaderFile.parent_path();
candidates.emplace_back(std::filesystem::path(".") / shaderPath); } else {
if (argc > 0 && argv[0]) { shaders_directory_ = SHADERS_FOLDER;
std::filesystem::path exe = argv[0];
if (exe.has_parent_path()) {
candidates.emplace_back(exe.parent_path() / shaderPath);
candidates.emplace_back(exe.parent_path() / ".." / shaderPath);
}
} }
std::string fragSrc; // Escanear carpeta de shaders
std::filesystem::path found; shader_list_ = scanShaderDirectory(shaders_directory_);
for (auto &p : candidates) { if (shader_list_.empty()) {
if (loadFileToString(p, fragSrc)) { found = p; break; } Logger::error("No shaders found in directory: " + shaders_directory_.string());
}
if (found.empty()) {
Logger::error("Failed to load fragment shader file. Tried paths:");
for (auto &p : candidates) Logger::error(" " + p.string());
SDL_GL_DestroyContext(glContext); SDL_GL_DestroyContext(glContext);
SDL_DestroyWindow(window_); SDL_DestroyWindow(window_);
SDL_Quit(); SDL_Quit();
return -1; return -1;
} }
Logger::info("Loaded fragment shader from: " + found.string());
GLuint vs = compileShader(GL_VERTEX_SHADER, vertexShaderSrc); // Determinar shader inicial
GLuint fs = compileShader(GL_FRAGMENT_SHADER, fragSrc.c_str()); size_t initial_index = 0;
if (!vs || !fs) { SDL_GL_DestroyContext(glContext); SDL_DestroyWindow(window_); SDL_Quit(); return -1; } bool found_shader = false;
GLuint program = linkProgram(vs, fs);
glDeleteShader(vs); // Intentar encontrar el shader especificado
glDeleteShader(fs); std::filesystem::path target_shader = shaderFile.has_parent_path() ? shaderFile : (shaders_directory_ / shaderFile.filename());
if (!program) { SDL_GL_DestroyContext(glContext); SDL_DestroyWindow(window_); SDL_Quit(); return -1; } for (size_t i = 0; i < shader_list_.size(); ++i) {
if (shader_list_[i] == target_shader) {
initial_index = i;
found_shader = true;
break;
}
}
// Si no se encuentra, intentar con DEFAULT_SHADER
if (!found_shader) {
std::filesystem::path default_path(DEFAULT_SHADER);
for (size_t i = 0; i < shader_list_.size(); ++i) {
if (shader_list_[i] == default_path) {
initial_index = i;
found_shader = true;
break;
}
}
}
// Si aún no se encuentra, usar el primer shader de la lista
if (!found_shader) {
Logger::info("Specified shader not found, using first shader in directory");
initial_index = 0;
}
// Cargar shader inicial
current_shader_index_ = initial_index;
current_program_ = loadAndCompileShader(current_shader_index_);
if (current_program_ == 0) {
Logger::error("Failed to load initial shader");
SDL_GL_DestroyContext(glContext);
SDL_DestroyWindow(window_);
SDL_Quit();
return -1;
}
shader_start_ticks_ = SDL_GetTicks();
updateWindowTitle();
// Quad setup // Quad setup
float quadVertices[] = { float quadVertices[] = {
@@ -283,11 +409,7 @@ int main(int argc, char** argv) {
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
GLint locRes = glGetUniformLocation(program, "iResolution");
GLint locTime = glGetUniformLocation(program, "iTime");
bool running = true; bool running = true;
Uint32 startTicks = SDL_GetTicks();
while (running) { while (running) {
SDL_Event e; SDL_Event e;
@@ -310,10 +432,15 @@ int main(int argc, char** argv) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program); glUseProgram(current_program_);
// Obtener uniform locations (se recalculan porque el shader puede cambiar)
GLint locRes = glGetUniformLocation(current_program_, "iResolution");
GLint locTime = glGetUniformLocation(current_program_, "iTime");
if (locRes >= 0) glUniform2f(locRes, float(w), float(h)); if (locRes >= 0) glUniform2f(locRes, float(w), float(h));
if (locTime >= 0) { if (locTime >= 0) {
float t = (SDL_GetTicks() - startTicks) / 1000.0f; float t = (SDL_GetTicks() - shader_start_ticks_) / 1000.0f;
glUniform1f(locTime, t); glUniform1f(locTime, t);
} }
@@ -328,7 +455,9 @@ int main(int argc, char** argv) {
// Cleanup // Cleanup
glDeleteBuffers(1, &vbo); glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao);
glDeleteProgram(program); if (current_program_ != 0) {
glDeleteProgram(current_program_);
}
SDL_GL_DestroyContext(glContext); SDL_GL_DestroyContext(glContext);
SDL_DestroyWindow(window_); SDL_DestroyWindow(window_);