25 Commits

Author SHA1 Message Date
44509023dc build: compilar pack_resources con C++20 en tools/Makefile
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 11:04:44 +01:00
2d2e338c7a build: suprimir falso positivo -Wstringop-overflow en stb_image.h
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 11:03:43 +01:00
e5fdbd54ff build: compilar pack_resources con C++20 para soporte de std::ranges
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:57:29 +01:00
c9bcce6f9b style: aplicar fixes de clang-tidy (todo excepto uppercase-literal-suffix)
Corregidos ~2570 issues automáticamente con clang-tidy --fix-errors
más ajustes manuales posteriores:

- modernize: designated-initializers, trailing-return-type, use-auto,
  avoid-c-arrays (→ std::array<>), use-ranges, use-emplace,
  deprecated-headers, use-equals-default, pass-by-value,
  return-braced-init-list, use-default-member-init
- readability: math-missing-parentheses, implicit-bool-conversion,
  braces-around-statements, isolate-declaration, use-std-min-max,
  identifier-naming, else-after-return, redundant-casting,
  convert-member-functions-to-static, make-member-function-const,
  static-accessed-through-instance
- performance: avoid-endl, unnecessary-value-param, type-promotion,
  inefficient-vector-operation
- dead code: XOR_KEY (orphan tras eliminar encryptData/decryptData),
  dead stores en engine.cpp y png_shape.cpp
- NOLINT justificado en 10 funciones con alta complejidad cognitiva
  (initialize, render, main, processEvents, update×3, performDemoAction,
  randomizeOnDemoStart, renderDebugHUD, AppLogo::update)

Compilación: gcc -Wall sin warnings. clang-tidy: 0 issues.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:52:07 +01:00
4801f287df refactor: eliminar código muerto en Makefile y CMakeLists.txt
- Makefile: eliminar variable DIR_BUILD (nunca usada) y el bloque
  ifdef ENABLE_MACOS_X86_64 completo (~33 líneas, soporte Intel macOS
  que nunca se activaba al no definirse la variable en ningún sitio)
- CMakeLists.txt: eliminar variable PLATFORM (asignada pero nunca
  referenciada con ${PLATFORM}) y los dos bloques if(NOT SDL3_FOUND)
  redundantes (REQUIRED ya garantiza el error)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 09:35:26 +01:00
7af77fb3dd refactor: eliminar código muerto (constantes legacy y métodos sin usar)
- defines.hpp: eliminar WINDOW_ZOOM_MIN, WINDOW_ZOOM_MAX (legacy),
  ROTOBALL_TRANSITION_TIME y LOGO_MODE_MIN_BALLS (reemplazada por
  LOGO_MIN_SCENARIO_IDX)
- resource_pack: eliminar encryptData/decryptData (XOR preparado pero
  nunca integrado en el pipeline de carga/guardado)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 09:26:17 +01:00
96dc964d6a build: usar SPIR-V pre-compilados en Makefile si glslc no está disponible
Igual que en CMakeLists.txt: si glslc no está en PATH, INCLUDES apunta a
shaders/precompiled/ en lugar de build/generated_shaders/.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 01:41:04 +01:00
2846987450 build: hacer Vulkan SDK opcional usando SPIR-V pre-compilados
Si glslc no está disponible, CMake usa los headers en shaders/precompiled/
en lugar de fallar. Los SPIR-V son portátiles entre Windows y Linux, por
lo que el build funciona sin instalar Vulkan SDK.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 01:38:09 +01:00
e13905567d perf: benchmark CPU-only sin ventana visible durante medición
- Crear ventana con SDL_WINDOW_HIDDEN para que no aparezca hasta que
  el benchmark termine
- runPerformanceBenchmark() elimina todas las llamadas a render() y
  SDL_HideWindow/ShowWindow; mide solo update() (física pura)
- SDL_ShowWindow() se llama en initialize() tras el benchmark y warmup
- Imprimir resultados por consola con formato [Benchmark CPU]

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 01:25:33 +01:00
9ae851d5b6 feat: limitar modo BOIDS a escenarios con ≤1.000 bolas
Pulsar B en escenarios 6-8 (o custom >1K) no activa boids y muestra
notificación. Cambiar de escenario estando en BOIDS queda bloqueado
si el destino supera BOIDS_MAX_BALLS (= SCENE_BALLS_5).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 01:13:17 +01:00
e1f6fd0f39 refactor: constantes SCENE_BALLS_N y fix HUD overlay en SHAPE mode
- defines.hpp: añadir SCENE_BALLS_1..8 (topado en 50K), SCENARIO_COUNT,
  reconstruir BALL_COUNT_SCENARIOS con esas constantes
- theme_manager: añadir max_ball_count_ y setMaxBallCount() para capturar
  colores hasta el máximo real (custom incluido), eliminando literal 50000
- engine.cpp: llamar setMaxBallCount() tras inicializar ThemeManager
- gpu_sprite_batch: addFullscreenOverlay() escribe vértices directamente
  sin pasar por el guard de pushQuad(), igual que addBackground(); esto
  corrige el HUD/overlay invisible en SHAPE mode con escenario 8 (50K bolas)
- Textos UI actualizados: tecla 8, help overlay y --skip-benchmark → 50.000

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 00:55:57 +01:00
093b982e01 fix: corregir off-by-one en sprite_capacity para overlay en escenario 8
El +1 por background era incorrecto: addBackground() escribe directamente
en los vectores sin pasar por el guard de pushQuad(), así que no consume
slots del límite. El +1 que garantiza el slot del overlay ya está dentro
de init() con (max_sprites_+1). Quitarlo evita que el overlay se rechace
al llenar exactamente el escenario de 100K bolas.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 00:18:51 +01:00
74d954df1e fix: corregir límite de sprites en SHAPE mode con muchas bolas
GpuSpriteBatch::init() ahora acepta capacidad dinámica para soportar
--custom-balls N con N > 200000. El buffer se dimensiona a (N+1) quads,
reservando siempre un slot para el overlay. addFullscreenOverlay() calcula
overlay_index_count_ desde el delta real de indices_ en lugar de fijarlo
a 6 incondicionalmente. Engine calcula la capacidad correcta al init.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 00:14:28 +01:00
46b24bf075 fix: corregir escalado de texto en resoluciones altas y F4 fullscreen
- updatePhysicalWindowSize() acepta logical_height opcional para
  actualizar logical_window_height_ al entrar/salir de F4 real fullscreen
- Engine pasa current_screen_height_ a UIManager en cada cambio de
  tamaño físico, propagando la resolución lógica correcta
- calculateFontSize() subdivide el rango >=900px en tres tramos más
  conservadores (/40, /48, /60) para evitar texto excesivamente grande
  en resoluciones como 2000x1200

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 23:56:59 +01:00
33cb995872 refactor: unificar log de consola y centralizar fuente de UI
- Formato uniforme [Tipo] nombre (pack/disco) en texture, textrenderer, png_shape
- Eliminar logs verbosos de logo_scaler y app_logo (resolución, escalado, etc.)
- Centralizar fuente de UI en APP_FONT (defines.hpp) con las 8 opciones comentadas
- Actualizar carpeta data/fonts con nuevas fuentes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 23:28:32 +01:00
c40eb69fc1 fix: sincronizar texto de ayuda con constantes DEFAULT_SCREEN_*/ZOOM de defines.hpp
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 22:37:31 +01:00
1d2e9c5035 feat: F7/F8 redimensionan campo lógico, F1/F2 muestran notificación de zoom
- F7/F8: nuevo setFieldScale() cambia resolución lógica en pasos del 10%
  (mín 50%, máx limitado por pantalla), reinicia escena como F4
- F1/F2: muestran notificación "Zoom X%" al cambiar escala de ventana
- Ventana física = lógico × zoom en todo momento; resizeWindowCentered()
  unifica el cálculo de posición leyendo el tamaño real con SDL_GetWindowSize
- PostFXUniforms::time renombrado a screen_height; scanlines usan la altura
  lógica actual en lugar del 720 hardcodeado — F1/F2 escalan las scanlines
  visualmente, F7/F8 las mantienen a 1 franja por píxel lógico
- Eliminados logs de debug de calculateMaxWindowScale y setWindowScale

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 22:35:40 +01:00
f71f7cd5ed fix: corregir guard de padding inferior en HelpOverlay
El guard usaba `current_y + line_height >= box_height_ - padding`,
lo que cortaba la última línea de col0 (Num/) por un solo píxel.
Cambiado a `current_y + glyph_height > box_height_ - padding` para
usar el alto visual real del glifo en lugar del line_height completo.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 20:59:31 +01:00
dcea4ebbab fix: corregir padding asimétrico en HelpOverlay con getGlyphHeight()
Añade TextRenderer::getGlyphHeight() (ascent - descent, sin line_gap)
y lo usa en calculateTextDimensions() para descontar el gap sobrante
de la última línea, aproximando padding superior e inferior simétricos.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 19:29:35 +01:00
b9b5f0b29f feat: rediseño HUD ayuda — 3 columnas, colores diferenciados, font -1pt
- Reorganizar key_bindings_ en 3 columnas (SIMULACIÓN+FIGURAS3D / VISUAL+PANTALLA / MODOS+DEBUG)
- Añadir F6 (escalado entero) y corregir F5 (Toggle PostFX), X (ciclar presets)
- Teclas usan category_color, descripciones usan content_color
- Separadores vacíos avanzan media línea (fix secciones pegadas)
- Font size del overlay reducido en 1pt respecto al resto de la UI
- calculateTextDimensions y rebuildCachedTexture actualizados para 3 columnas

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 19:06:48 +01:00
200672756c refactor: separar F5 (toggle) y X (ciclo modos) en PostFX
- X ya no incluye OFF en el ciclo; si PostFX está desactivado, no hace nada
- cycleShader() cicla solo entre los 4 modos usando postfx_effect_mode_
- Eliminar postfx_cycle_idx_ (redundante, causaba desincronización)
- postfx_effect_mode_ por defecto = 3 (completo) para que F5 active completo sin --postfx

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 18:36:45 +01:00
f3b029c5b6 refactor: normalizar notificaciones a castellano, title case sin dos puntos
- Todo en castellano (Vinyeta→viñeta, Cromàtica→cromática, Complet→completo, Desactivat→desactivado, Boids→boids)
- Primera letra mayúscula, resto minúscula (MODO SANDBOX→Modo sandbox, etc.)
- Sin dos puntos separador (PostFX: X→PostFX X, Escalado: X→Escalado X, Sprite: X→Textura X)
- Separadores de miles en castellano (1,000→1.000 pelotas)
- Nombres de figura en minúscula via tolower (SPHERE→sphere → "Modo sphere")
- Ajuste valores PostFX por defecto (vignette 1.5→0.8, chroma 1.0→0.2)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 18:21:28 +01:00
e46c3eb4ba fix: ajustes de rutas y guards en vscode, rc y gpu_pipeline
- c_cpp_properties.json: añadir build/generated_shaders a includePath
- vibe3.rc: corregir ruta del icono a release/icons/icon.ico
- gpu_pipeline.cpp: envolver shaders MSL con guard #ifdef __APPLE__

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 18:00:30 +01:00
ea05e1eb2e fix: evitar error 'ruta no encontrada' en Windows con find
En Windows, `find` es un comando de cmd distinto al de Unix,
causando el error "El sistema no puede encontrar la ruta especificada"
al evaluar DATA_FILES. Se condiciona el uso de find solo en sistemas
no-Windows.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 17:58:04 +01:00
c052b45a60 refactor: eliminar sistema de shaders externos (ShaderManager + GpuShaderPreset)
Elimina el sistema multi-pass de shaders runtime en favor del PostFX nativo.
Queda solo el ciclo de 5 modos nativos: OFF → Vinyeta → Scanlines → Cromàtica → Complet.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 17:08:08 +01:00
112 changed files with 3917 additions and 4633 deletions

View File

@@ -16,6 +16,7 @@ Checks: >
-performance-inefficient-string-concatenation,
-bugprone-integer-division,
-bugprone-easily-swappable-parameters,
-readability-uppercase-literal-suffix,
WarningsAsErrors: '*'
# Solo incluir archivos de tu código fuente

View File

@@ -5,6 +5,7 @@
"includePath": [
"${workspaceFolder}/source",
"${workspaceFolder}/source/external",
"${workspaceFolder}/build/generated_shaders",
"${env:HOMEBREW_PREFIX}/include",
"/opt/homebrew/include"
],
@@ -25,6 +26,7 @@
"includePath": [
"${workspaceFolder}/source",
"${workspaceFolder}/source/external",
"${workspaceFolder}/build/generated_shaders",
"/usr/include",
"/usr/local/include"
],
@@ -41,6 +43,7 @@
"includePath": [
"${workspaceFolder}/source",
"${workspaceFolder}/source/external",
"${workspaceFolder}/build/generated_shaders",
"C:/mingw/include"
],
"defines": [

View File

@@ -11,26 +11,16 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Os -ffunction-sections -fdata-sec
# Buscar SDL3 automáticamente
find_package(SDL3 REQUIRED)
# Si no se encuentra SDL3, generar un error
if (NOT SDL3_FOUND)
message(FATAL_ERROR "SDL3 no encontrado. Por favor, verifica su instalación.")
endif()
# Buscar SDL3_ttf
find_package(SDL3_ttf REQUIRED)
# Si no se encuentra SDL3_ttf, generar un error
if (NOT SDL3_ttf_FOUND)
message(FATAL_ERROR "SDL3_ttf no encontrado. Por favor, verifica su instalación.")
endif()
# ---- Shader compilation (non-Apple only: Vulkan/SPIRV) ----
if(NOT APPLE)
find_program(GLSLC glslc HINTS "$ENV{VULKAN_SDK}/bin" "$ENV{VULKAN_SDK}/Bin")
if(NOT GLSLC)
message(FATAL_ERROR "glslc not found. Install the Vulkan SDK and ensure glslc is on PATH.")
endif()
message(STATUS "glslc not found — using precompiled SPIR-V headers from shaders/precompiled/")
set(SHADER_GEN_DIR "${CMAKE_SOURCE_DIR}/shaders/precompiled")
else()
set(SHADER_SRC_DIR "${CMAKE_SOURCE_DIR}/shaders")
set(SHADER_GEN_DIR "${CMAKE_BINARY_DIR}/generated_shaders")
file(MAKE_DIRECTORY "${SHADER_GEN_DIR}")
@@ -64,60 +54,6 @@ if(NOT APPLE)
endforeach()
add_custom_target(shaders ALL DEPENDS ${SPIRV_HEADERS})
# External runtime shaders: auto-discover and compile data/shaders/**/*.vert/*.frag
# Output: <source>.spv alongside each source file (loaded at runtime by GpuShaderPreset)
file(GLOB_RECURSE DATA_SHADERS
"${CMAKE_SOURCE_DIR}/data/shaders/**/*.vert"
"${CMAKE_SOURCE_DIR}/data/shaders/**/*.frag")
set(DATA_SHADER_SPVS)
foreach(SHADER_FILE ${DATA_SHADERS})
get_filename_component(SHADER_EXT "${SHADER_FILE}" EXT)
if(SHADER_EXT STREQUAL ".vert")
set(STAGE_FLAG "-fshader-stage=vertex")
else()
set(STAGE_FLAG "-fshader-stage=fragment")
endif()
set(SPV_FILE "${SHADER_FILE}.spv")
add_custom_command(
OUTPUT "${SPV_FILE}"
COMMAND "${GLSLC}" ${STAGE_FLAG} -o "${SPV_FILE}" "${SHADER_FILE}"
DEPENDS "${SHADER_FILE}"
COMMENT "Compiling ${SHADER_FILE}"
)
list(APPEND DATA_SHADER_SPVS "${SPV_FILE}")
endforeach()
if(DATA_SHADER_SPVS)
add_custom_target(data_shaders ALL DEPENDS ${DATA_SHADER_SPVS})
endif()
endif()
# macOS: transpile .spv → .msl for GpuShaderPreset
if(APPLE)
find_program(SPIRV_CROSS spirv-cross)
if(SPIRV_CROSS)
file(GLOB_RECURSE DATA_SHADER_SPVS
"${CMAKE_SOURCE_DIR}/data/shaders/*.vert.spv"
"${CMAKE_SOURCE_DIR}/data/shaders/*.frag.spv"
)
set(DATA_SHADER_MSLS)
foreach(SPV_FILE ${DATA_SHADER_SPVS})
set(MSL_FILE "${SPV_FILE}.msl")
add_custom_command(
OUTPUT "${MSL_FILE}"
COMMAND "${SPIRV_CROSS}" --msl --output "${MSL_FILE}" "${SPV_FILE}"
DEPENDS "${SPV_FILE}"
COMMENT "Transpiling ${SPV_FILE} to MSL"
)
list(APPEND DATA_SHADER_MSLS "${MSL_FILE}")
endforeach()
if(DATA_SHADER_MSLS)
add_custom_target(data_shader_msls ALL DEPENDS ${DATA_SHADER_MSLS})
endif()
else()
message(STATUS "spirv-cross not found — GpuShaderPreset will not work on macOS (run: brew install spirv-cross)")
endif()
endif()
@@ -125,6 +61,9 @@ endif()
file(GLOB SOURCE_FILES source/*.cpp source/external/*.cpp source/boids_mgr/*.cpp source/gpu/*.cpp source/input/*.cpp source/scene/*.cpp source/shapes/*.cpp source/shapes_mgr/*.cpp source/state/*.cpp source/themes/*.cpp source/text/*.cpp source/ui/*.cpp)
list(REMOVE_ITEM SOURCE_FILES "${CMAKE_SOURCE_DIR}/source/main_old.cpp")
# Suprimir falso positivo de GCC en stb_image.h (externo)
set_source_files_properties(source/external/texture.cpp PROPERTIES COMPILE_FLAGS "-Wno-stringop-overflow")
# Comprobar si se encontraron archivos fuente
if(NOT SOURCE_FILES)
message(FATAL_ERROR "No se encontraron archivos fuente en el directorio 'source/'. Verifica la ruta.")
@@ -132,13 +71,10 @@ endif()
# Detectar la plataforma y configuraciones específicas
if(WIN32)
set(PLATFORM windows)
set(LINK_LIBS SDL3::SDL3 SDL3_ttf::SDL3_ttf mingw32 ws2_32)
elseif(UNIX AND NOT APPLE)
set(PLATFORM linux)
set(LINK_LIBS SDL3::SDL3 SDL3_ttf::SDL3_ttf)
elseif(APPLE)
set(PLATFORM macos)
set(LINK_LIBS SDL3::SDL3 SDL3_ttf::SDL3_ttf)
endif()
@@ -158,15 +94,10 @@ set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAK
target_link_libraries(${PROJECT_NAME} ${LINK_LIBS})
if(NOT APPLE)
if(GLSLC)
add_dependencies(${PROJECT_NAME} shaders)
target_include_directories(${PROJECT_NAME} PRIVATE "${SHADER_GEN_DIR}")
if(TARGET data_shaders)
add_dependencies(${PROJECT_NAME} data_shaders)
endif()
endif()
if(APPLE AND TARGET data_shader_msls)
add_dependencies(${PROJECT_NAME} data_shader_msls)
target_include_directories(${PROJECT_NAME} PRIVATE "${SHADER_GEN_DIR}")
endif()
# Tool: pack_resources

View File

@@ -2,16 +2,15 @@
DIR_ROOT := $(dir $(abspath $(MAKEFILE_LIST)))
DIR_SOURCES := $(addsuffix /, $(DIR_ROOT)source)
DIR_BIN := $(addsuffix /, $(DIR_ROOT))
DIR_BUILD := $(addsuffix /, $(DIR_ROOT)build)
DIR_TOOLS := $(addsuffix /, $(DIR_ROOT)tools)
# Variables
TARGET_NAME := vibe3_physics
TARGET_FILE := $(DIR_BIN)$(TARGET_NAME)
APP_NAME := ViBe3 Physics
RELEASE_FOLDER := vibe3_release
RELEASE_FOLDER := dist/_tmp
RELEASE_FILE := $(RELEASE_FOLDER)/$(TARGET_NAME)
RESOURCE_FILE := release/windows/vibe3.res
RESOURCE_FILE := build/vibe3.res
DIST_DIR := dist
# Variables para herramienta de empaquetado
@@ -65,8 +64,18 @@ APP_SOURCES := $(wildcard source/*.cpp) \
# Excluir archivos antiguos si existen
APP_SOURCES := $(filter-out source/main_old.cpp, $(APP_SOURCES))
# Includes
INCLUDES := -Isource -Isource/external
# Includes: usar shaders pre-compilados si glslc no está disponible
ifeq ($(OS),Windows_NT)
GLSLC := $(shell where glslc 2>NUL)
else
GLSLC := $(shell command -v glslc 2>/dev/null)
endif
ifeq ($(GLSLC),)
SHADER_INCLUDE := -Ishaders/precompiled
else
SHADER_INCLUDE := -Ibuild/generated_shaders
endif
INCLUDES := -Isource -Isource/external $(SHADER_INCLUDE)
# Variables según el sistema operativo
CXXFLAGS_BASE := -std=c++20 -Wall
@@ -101,13 +110,17 @@ endif
# Reglas para herramienta de empaquetado y resources.pack
$(PACK_TOOL): $(PACK_SOURCES)
@echo "Compilando herramienta de empaquetado..."
$(PACK_CXX) -std=c++17 -Wall -Os $(PACK_INCLUDES) $(PACK_SOURCES) -o $(PACK_TOOL)
$(PACK_CXX) -std=c++20 -Wall -Os $(PACK_INCLUDES) $(PACK_SOURCES) -o $(PACK_TOOL)
@echo "✓ Herramienta de empaquetado lista: $(PACK_TOOL)"
pack_tool: $(PACK_TOOL)
# Detectar todos los archivos en data/ como dependencias (regenera si cualquiera cambia)
DATA_FILES := $(shell find data -type f 2>/dev/null)
ifeq ($(OS),Windows_NT)
DATA_FILES :=
else
DATA_FILES := $(shell find data -type f 2>/dev/null)
endif
resources.pack: $(PACK_TOOL) $(DATA_FILES)
@echo "Generando resources.pack desde directorio data/..."
@@ -123,8 +136,7 @@ force_resource_pack: $(PACK_TOOL)
# Reglas para compilación
windows:
@echo off
@echo Compilando para Windows con nombre: "$(APP_NAME).exe"
@echo Compilando para Windows con nombre: $(APP_NAME).exe
windres release/windows/vibe3.rc -O coff -o $(RESOURCE_FILE)
$(CXX) $(APP_SOURCES) $(RESOURCE_FILE) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(WIN_TARGET_FILE).exe"
strip -s -R .comment -R .gnu.version "$(WIN_TARGET_FILE).exe" --strip-unneeded
@@ -134,6 +146,7 @@ windows_release: force_resource_pack
# Crea carpeta temporal 'RELEASE_FOLDER'
@if exist "$(RELEASE_FOLDER)" rmdir /S /Q "$(RELEASE_FOLDER)"
@if not exist "$(DIST_DIR)" mkdir "$(DIST_DIR)"
@mkdir "$(RELEASE_FOLDER)"
# Copia el archivo 'resources.pack'
@@ -145,7 +158,6 @@ windows_release: force_resource_pack
@copy /Y release\windows\dll\*.dll "$(RELEASE_FOLDER)\" >nul 2>&1 || echo DLLs copied successfully
# Compila
@if not exist "$(DIST_DIR)" mkdir "$(DIST_DIR)"
@windres release/windows/vibe3.rc -O coff -o $(RESOURCE_FILE)
@$(CXX) $(APP_SOURCES) $(RESOURCE_FILE) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(WIN_RELEASE_FILE).exe"
@strip -s -R .comment -R .gnu.version "$(WIN_RELEASE_FILE).exe" --strip-unneeded
@@ -198,40 +210,6 @@ macos_release: force_resource_pack
cp LICENSE "$(RELEASE_FOLDER)"
cp README.md "$(RELEASE_FOLDER)"
# NOTA: create-dmg crea automáticamente el enlace a /Applications con --app-drop-link
# No es necesario crearlo manualmente aquí
# Compila la versión para procesadores Intel
ifdef ENABLE_MACOS_X86_64
$(CXX) $(APP_SOURCES) $(INCLUDES) -DMACOS_BUNDLE $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS/$(TARGET_NAME)" -rpath @executable_path/../Frameworks/ -target x86_64-apple-macos12
# Firma la aplicación
codesign --deep --force --sign - --timestamp=none "$(RELEASE_FOLDER)/$(APP_NAME).app"
# Empaqueta el .dmg de la versión Intel con create-dmg
@echo "Creando DMG Intel 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_INTEL_RELEASE)" \
"$(RELEASE_FOLDER)"
@if [ -f "$(MACOS_INTEL_RELEASE)" ]; then \
echo "✓ Release Intel creado exitosamente: $(MACOS_INTEL_RELEASE)"; \
else \
echo "✗ Error: No se pudo crear el DMG Intel"; \
exit 1; \
fi
@rm -f rw.*.dmg 2>/dev/null || true
endif
# Compila la versión para procesadores Apple Silicon
$(CXX) $(APP_SOURCES) $(INCLUDES) -DMACOS_BUNDLE -DSDL_DISABLE_IMMINTRIN_H $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS/$(TARGET_NAME)" -rpath @executable_path/../Frameworks/ -target arm64-apple-macos12

View File

@@ -538,7 +538,7 @@ vibe3_physics/
### Dependencias de shaders
El proyecto compila shaders GLSL a SPIR-V en todas las plataformas usando `glslc` (incluido en el Vulkan SDK). En macOS, adicionalmente transpila los `.spv` a MSL (Metal) con `spirv-cross`.
El proyecto compila shaders GLSL a SPIR-V en todas las plataformas usando `glslc` (incluido en el Vulkan SDK). En macOS, adicionalmente transpila los `.spv` a MSL (Metal) con `spirv-cross` (shaders internos: sprite, ball, postfx).
| Plataforma | Backend GPU | Herramienta de shaders | Resultado |
|------------|-------------|------------------------|-----------|
@@ -624,7 +624,7 @@ cmake ..
cmake --build .
```
En macOS, CMake detecta automáticamente `spirv-cross` y genera los `.spv.msl` necesarios. Si no está instalado, los shaders de preset externos no funcionarán (el resto de la app sí).
En macOS, CMake detecta automáticamente `spirv-cross` y genera los `.spv.msl` necesarios para los shaders internos (sprite, ball, postfx).
### Make directo (Linux/macOS)

BIN
data/fonts/Exo2-Regular.ttf Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,28 +0,0 @@
# Based on dannyld's rainbow settings
shaders = 2
shader0 = "../crt/shaders/mame_hlsl/shaders/mame_ntsc_encode.slang"
filter_linear0 = "true"
scale_type0 = "source"
scale0 = "1.000000"
shader1 = "../crt/shaders/mame_hlsl/shaders/mame_ntsc_decode.slang"
filter_linear1 = "true"
scale_type1 = "source"
scale_1 = "1.000000"
# ntsc parameters
ntscsignal = "1.000000"
avalue = "0.000000"
bvalue = "0.000000"
scantime = "47.900070"
# optional blur
shadowalpha = "0.100000"
notch_width = "3.450001"
ifreqresponse = "1.750000"
qfreqresponse = "1.450000"
# uncomment for jailbars in blue
#pvalue = "1.100000"

View File

@@ -1,69 +0,0 @@
#version 450
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz,ImJezze
// Adapted from mame_ntsc_encode.slang for SDL3 GPU / Vulkan SPIRV
layout(location=0) in vec2 v_uv;
layout(location=0) out vec4 FragColor;
layout(set=2, binding=0) uniform sampler2D Source;
layout(set=3, binding=0) uniform NTSCParams {
float source_width;
float source_height;
float a_value;
float b_value;
float cc_value;
float scan_time;
float notch_width;
float y_freq;
float i_freq;
float q_freq;
float _pad0;
float _pad1;
} u;
const float PI = 3.1415927;
const float PI2 = PI * 2.0;
void main() {
vec2 source_dims = vec2(u.source_width, u.source_height);
// p_value=1: one texel step per sub-sample (no horizontal stretch)
vec2 PValueSourceTexel = vec2(1.0, 0.0) / source_dims;
vec2 C0 = v_uv + PValueSourceTexel * vec2(0.00, 0.0);
vec2 C1 = v_uv + PValueSourceTexel * vec2(0.25, 0.0);
vec2 C2 = v_uv + PValueSourceTexel * vec2(0.50, 0.0);
vec2 C3 = v_uv + PValueSourceTexel * vec2(0.75, 0.0);
vec4 Cx = vec4(C0.x, C1.x, C2.x, C3.x);
vec4 Cy = vec4(C0.y, C1.y, C2.y, C3.y);
vec4 Texel0 = texture(Source, C0);
vec4 Texel1 = texture(Source, C1);
vec4 Texel2 = texture(Source, C2);
vec4 Texel3 = texture(Source, C3);
vec4 HPosition = Cx;
vec4 VPosition = Cy;
const vec4 YDot = vec4(0.299, 0.587, 0.114, 0.0);
const vec4 IDot = vec4(0.595716, -0.274453, -0.321263, 0.0);
const vec4 QDot = vec4(0.211456, -0.522591, 0.311135, 0.0);
vec4 Y = vec4(dot(Texel0, YDot), dot(Texel1, YDot), dot(Texel2, YDot), dot(Texel3, YDot));
vec4 I = vec4(dot(Texel0, IDot), dot(Texel1, IDot), dot(Texel2, IDot), dot(Texel3, IDot));
vec4 Q = vec4(dot(Texel0, QDot), dot(Texel1, QDot), dot(Texel2, QDot), dot(Texel3, QDot));
float W = PI2 * u.cc_value * u.scan_time;
float WoPI = W / PI;
float HOffset = u.a_value / WoPI;
float VScale = u.b_value * source_dims.y / WoPI;
vec4 T = HPosition + vec4(HOffset) + VPosition * vec4(VScale);
vec4 TW = T * W;
FragColor = Y + I * cos(TW) + Q * sin(TW);
}

View File

@@ -1,61 +0,0 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct NTSCParams
{
float source_width;
float source_height;
float a_value;
float b_value;
float cc_value;
float scan_time;
float notch_width;
float y_freq;
float i_freq;
float q_freq;
float _pad0;
float _pad1;
};
struct main0_out
{
float4 FragColor [[color(0)]];
};
struct main0_in
{
float2 v_uv [[user(locn0)]];
};
fragment main0_out main0(main0_in in [[stage_in]], constant NTSCParams& u [[buffer(0)]], texture2d<float> Source [[texture(0)]], sampler SourceSmplr [[sampler(0)]])
{
main0_out out = {};
float2 source_dims = float2(u.source_width, u.source_height);
float2 PValueSourceTexel = float2(1.0, 0.0) / source_dims;
float2 C0 = in.v_uv + (PValueSourceTexel * float2(0.0));
float2 C1 = in.v_uv + (PValueSourceTexel * float2(0.25, 0.0));
float2 C2 = in.v_uv + (PValueSourceTexel * float2(0.5, 0.0));
float2 C3 = in.v_uv + (PValueSourceTexel * float2(0.75, 0.0));
float4 Cx = float4(C0.x, C1.x, C2.x, C3.x);
float4 Cy = float4(C0.y, C1.y, C2.y, C3.y);
float4 Texel0 = Source.sample(SourceSmplr, C0);
float4 Texel1 = Source.sample(SourceSmplr, C1);
float4 Texel2 = Source.sample(SourceSmplr, C2);
float4 Texel3 = Source.sample(SourceSmplr, C3);
float4 HPosition = Cx;
float4 VPosition = Cy;
float4 Y = float4(dot(Texel0, float4(0.2989999949932098388671875, 0.58700001239776611328125, 0.114000000059604644775390625, 0.0)), dot(Texel1, float4(0.2989999949932098388671875, 0.58700001239776611328125, 0.114000000059604644775390625, 0.0)), dot(Texel2, float4(0.2989999949932098388671875, 0.58700001239776611328125, 0.114000000059604644775390625, 0.0)), dot(Texel3, float4(0.2989999949932098388671875, 0.58700001239776611328125, 0.114000000059604644775390625, 0.0)));
float4 I = float4(dot(Texel0, float4(0.595715999603271484375, -0.2744530141353607177734375, -0.3212629854679107666015625, 0.0)), dot(Texel1, float4(0.595715999603271484375, -0.2744530141353607177734375, -0.3212629854679107666015625, 0.0)), dot(Texel2, float4(0.595715999603271484375, -0.2744530141353607177734375, -0.3212629854679107666015625, 0.0)), dot(Texel3, float4(0.595715999603271484375, -0.2744530141353607177734375, -0.3212629854679107666015625, 0.0)));
float4 Q = float4(dot(Texel0, float4(0.211456000804901123046875, -0.52259099483489990234375, 0.311134994029998779296875, 0.0)), dot(Texel1, float4(0.211456000804901123046875, -0.52259099483489990234375, 0.311134994029998779296875, 0.0)), dot(Texel2, float4(0.211456000804901123046875, -0.52259099483489990234375, 0.311134994029998779296875, 0.0)), dot(Texel3, float4(0.211456000804901123046875, -0.52259099483489990234375, 0.311134994029998779296875, 0.0)));
float W = (6.283185482025146484375 * u.cc_value) * u.scan_time;
float WoPI = W / 3.1415927410125732421875;
float HOffset = u.a_value / WoPI;
float VScale = (u.b_value * source_dims.y) / WoPI;
float4 T = (HPosition + float4(HOffset)) + (VPosition * float4(VScale));
float4 TW = T * W;
out.FragColor = (Y + (I * cos(TW))) + (Q * sin(TW));
return out;
}

View File

@@ -1,8 +0,0 @@
#version 450
layout(location=0) out vec2 v_uv;
void main() {
vec2 positions[3] = vec2[3](vec2(-1.0,-1.0), vec2(3.0,-1.0), vec2(-1.0,3.0));
vec2 uvs[3] = vec2[3](vec2(0.0, 1.0), vec2(2.0, 1.0), vec2(0.0,-1.0));
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
v_uv = uvs[gl_VertexIndex];
}

View File

@@ -1,63 +0,0 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wmissing-braces"
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
template<typename T, size_t Num>
struct spvUnsafeArray
{
T elements[Num ? Num : 1];
thread T& operator [] (size_t pos) thread
{
return elements[pos];
}
constexpr const thread T& operator [] (size_t pos) const thread
{
return elements[pos];
}
device T& operator [] (size_t pos) device
{
return elements[pos];
}
constexpr const device T& operator [] (size_t pos) const device
{
return elements[pos];
}
constexpr const constant T& operator [] (size_t pos) const constant
{
return elements[pos];
}
threadgroup T& operator [] (size_t pos) threadgroup
{
return elements[pos];
}
constexpr const threadgroup T& operator [] (size_t pos) const threadgroup
{
return elements[pos];
}
};
constant spvUnsafeArray<float2, 3> _18 = spvUnsafeArray<float2, 3>({ float2(-1.0), float2(3.0, -1.0), float2(-1.0, 3.0) });
constant spvUnsafeArray<float2, 3> _26 = spvUnsafeArray<float2, 3>({ float2(0.0, 1.0), float2(2.0, 1.0), float2(0.0, -1.0) });
struct main0_out
{
float2 v_uv [[user(locn0)]];
float4 gl_Position [[position]];
};
vertex main0_out main0(uint gl_VertexIndex [[vertex_id]])
{
main0_out out = {};
out.gl_Position = float4(_18[int(gl_VertexIndex)], 0.0, 1.0);
out.v_uv = _26[int(gl_VertexIndex)];
return out;
}

View File

@@ -1,148 +0,0 @@
#version 450
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz,ImJezze
// Adapted from mame_ntsc_decode.slang for SDL3 GPU / Vulkan SPIRV
layout(location=0) in vec2 v_uv;
layout(location=0) out vec4 FragColor;
layout(set=2, binding=0) uniform sampler2D Source;
layout(set=3, binding=0) uniform NTSCParams {
float source_width;
float source_height;
float a_value;
float b_value;
float cc_value;
float scan_time;
float notch_width;
float y_freq;
float i_freq;
float q_freq;
float _pad0;
float _pad1;
} u;
const float PI = 3.1415927;
const float PI2 = PI * 2.0;
const vec3 RDot = vec3(1.0, 0.956, 0.621);
const vec3 GDot = vec3(1.0, -0.272, -0.647);
const vec3 BDot = vec3(1.0, -1.106, 1.703);
const vec4 NotchOffset = vec4(0.0, 1.0, 2.0, 3.0);
const int SampleCount = 64;
const int HalfSampleCount = 32;
void main() {
vec2 source_dims = vec2(u.source_width, u.source_height);
vec4 BaseTexel = texture(Source, v_uv);
float CCValue = u.cc_value;
float ScanTime = u.scan_time;
float NotchHalfWidth = u.notch_width / 2.0;
float YFreqResponse = u.y_freq;
float IFreqResponse = u.i_freq;
float QFreqResponse = u.q_freq;
float AValue = u.a_value;
float BValue = u.b_value;
float TimePerSample = ScanTime / (source_dims.x * 4.0);
float Fc_y1 = (CCValue - NotchHalfWidth) * TimePerSample;
float Fc_y2 = (CCValue + NotchHalfWidth) * TimePerSample;
float Fc_y3 = YFreqResponse * TimePerSample;
float Fc_i = IFreqResponse * TimePerSample;
float Fc_q = QFreqResponse * TimePerSample;
float Fc_i_2 = Fc_i * 2.0;
float Fc_q_2 = Fc_q * 2.0;
float Fc_y1_2 = Fc_y1 * 2.0;
float Fc_y2_2 = Fc_y2 * 2.0;
float Fc_y3_2 = Fc_y3 * 2.0;
float Fc_i_pi2 = Fc_i * PI2;
float Fc_q_pi2 = Fc_q * PI2;
float Fc_y1_pi2 = Fc_y1 * PI2;
float Fc_y2_pi2 = Fc_y2 * PI2;
float Fc_y3_pi2 = Fc_y3 * PI2;
float PI2Length = PI2 / float(SampleCount);
float W = PI2 * CCValue * ScanTime;
float WoPI = W / PI;
float HOffset = BValue / WoPI;
float VScale = AValue * source_dims.y / WoPI;
vec4 YAccum = vec4(0.0);
vec4 IAccum = vec4(0.0);
vec4 QAccum = vec4(0.0);
vec4 Cy = vec4(v_uv.y);
vec4 VPosition = Cy;
for (float i = 0.0; i < float(SampleCount); i += 4.0) {
float n = i - float(HalfSampleCount);
vec4 n4 = n + NotchOffset;
vec4 Cx = vec4(v_uv.x) + (n4 * 0.25) / source_dims.x;
vec4 HPosition = Cx;
vec4 C = texture(Source, vec2(Cx.r, Cy.r));
vec4 T = HPosition + vec4(HOffset) + VPosition * vec4(VScale);
vec4 WT = W * T;
vec4 SincKernel = 0.54 + 0.46 * cos(PI2Length * n4);
vec4 SincYIn1 = Fc_y1_pi2 * n4;
vec4 SincYIn2 = Fc_y2_pi2 * n4;
vec4 SincYIn3 = Fc_y3_pi2 * n4;
vec4 SincIIn = Fc_i_pi2 * n4;
vec4 SincQIn = Fc_q_pi2 * n4;
vec4 SincY1, SincY2, SincY3;
SincY1.x = (SincYIn1.x != 0.0) ? sin(SincYIn1.x) / SincYIn1.x : 1.0;
SincY1.y = (SincYIn1.y != 0.0) ? sin(SincYIn1.y) / SincYIn1.y : 1.0;
SincY1.z = (SincYIn1.z != 0.0) ? sin(SincYIn1.z) / SincYIn1.z : 1.0;
SincY1.w = (SincYIn1.w != 0.0) ? sin(SincYIn1.w) / SincYIn1.w : 1.0;
SincY2.x = (SincYIn2.x != 0.0) ? sin(SincYIn2.x) / SincYIn2.x : 1.0;
SincY2.y = (SincYIn2.y != 0.0) ? sin(SincYIn2.y) / SincYIn2.y : 1.0;
SincY2.z = (SincYIn2.z != 0.0) ? sin(SincYIn2.z) / SincYIn2.z : 1.0;
SincY2.w = (SincYIn2.w != 0.0) ? sin(SincYIn2.w) / SincYIn2.w : 1.0;
SincY3.x = (SincYIn3.x != 0.0) ? sin(SincYIn3.x) / SincYIn3.x : 1.0;
SincY3.y = (SincYIn3.y != 0.0) ? sin(SincYIn3.y) / SincYIn3.y : 1.0;
SincY3.z = (SincYIn3.z != 0.0) ? sin(SincYIn3.z) / SincYIn3.z : 1.0;
SincY3.w = (SincYIn3.w != 0.0) ? sin(SincYIn3.w) / SincYIn3.w : 1.0;
vec4 IdealY = Fc_y1_2 * SincY1 - Fc_y2_2 * SincY2 + Fc_y3_2 * SincY3;
vec4 IdealI, IdealQ;
IdealI.x = Fc_i_2 * ((SincIIn.x != 0.0) ? sin(SincIIn.x) / SincIIn.x : 1.0);
IdealI.y = Fc_i_2 * ((SincIIn.y != 0.0) ? sin(SincIIn.y) / SincIIn.y : 1.0);
IdealI.z = Fc_i_2 * ((SincIIn.z != 0.0) ? sin(SincIIn.z) / SincIIn.z : 1.0);
IdealI.w = Fc_i_2 * ((SincIIn.w != 0.0) ? sin(SincIIn.w) / SincIIn.w : 1.0);
IdealQ.x = Fc_q_2 * ((SincQIn.x != 0.0) ? sin(SincQIn.x) / SincQIn.x : 1.0);
IdealQ.y = Fc_q_2 * ((SincQIn.y != 0.0) ? sin(SincQIn.y) / SincQIn.y : 1.0);
IdealQ.z = Fc_q_2 * ((SincQIn.z != 0.0) ? sin(SincQIn.z) / SincQIn.z : 1.0);
IdealQ.w = Fc_q_2 * ((SincQIn.w != 0.0) ? sin(SincQIn.w) / SincQIn.w : 1.0);
vec4 FilterY = SincKernel * IdealY;
vec4 FilterI = SincKernel * IdealI;
vec4 FilterQ = SincKernel * IdealQ;
YAccum += C * FilterY;
IAccum += C * cos(WT) * FilterI;
QAccum += C * sin(WT) * FilterQ;
}
vec3 YIQ = vec3(
(YAccum.r + YAccum.g + YAccum.b + YAccum.a),
(IAccum.r + IAccum.g + IAccum.b + IAccum.a) * 2.0,
(QAccum.r + QAccum.g + QAccum.b + QAccum.a) * 2.0);
vec3 RGB = vec3(
dot(YIQ, RDot),
dot(YIQ, GDot),
dot(YIQ, BDot));
FragColor = vec4(RGB, BaseTexel.a);
}

View File

@@ -1,304 +0,0 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct NTSCParams
{
float source_width;
float source_height;
float a_value;
float b_value;
float cc_value;
float scan_time;
float notch_width;
float y_freq;
float i_freq;
float q_freq;
float _pad0;
float _pad1;
};
struct main0_out
{
float4 FragColor [[color(0)]];
};
struct main0_in
{
float2 v_uv [[user(locn0)]];
};
fragment main0_out main0(main0_in in [[stage_in]], constant NTSCParams& u [[buffer(0)]], texture2d<float> Source [[texture(0)]], sampler SourceSmplr [[sampler(0)]])
{
main0_out out = {};
float2 source_dims = float2(u.source_width, u.source_height);
float4 BaseTexel = Source.sample(SourceSmplr, in.v_uv);
float CCValue = u.cc_value;
float ScanTime = u.scan_time;
float NotchHalfWidth = u.notch_width / 2.0;
float YFreqResponse = u.y_freq;
float IFreqResponse = u.i_freq;
float QFreqResponse = u.q_freq;
float AValue = u.a_value;
float BValue = u.b_value;
float TimePerSample = ScanTime / (source_dims.x * 4.0);
float Fc_y1 = (CCValue - NotchHalfWidth) * TimePerSample;
float Fc_y2 = (CCValue + NotchHalfWidth) * TimePerSample;
float Fc_y3 = YFreqResponse * TimePerSample;
float Fc_i = IFreqResponse * TimePerSample;
float Fc_q = QFreqResponse * TimePerSample;
float Fc_i_2 = Fc_i * 2.0;
float Fc_q_2 = Fc_q * 2.0;
float Fc_y1_2 = Fc_y1 * 2.0;
float Fc_y2_2 = Fc_y2 * 2.0;
float Fc_y3_2 = Fc_y3 * 2.0;
float Fc_i_pi2 = Fc_i * 6.283185482025146484375;
float Fc_q_pi2 = Fc_q * 6.283185482025146484375;
float Fc_y1_pi2 = Fc_y1 * 6.283185482025146484375;
float Fc_y2_pi2 = Fc_y2 * 6.283185482025146484375;
float Fc_y3_pi2 = Fc_y3 * 6.283185482025146484375;
float PI2Length = 0.098174773156642913818359375;
float W = (6.283185482025146484375 * CCValue) * ScanTime;
float WoPI = W / 3.1415927410125732421875;
float HOffset = BValue / WoPI;
float VScale = (AValue * source_dims.y) / WoPI;
float4 YAccum = float4(0.0);
float4 IAccum = float4(0.0);
float4 QAccum = float4(0.0);
float4 Cy = float4(in.v_uv.y);
float4 VPosition = Cy;
float4 SincY1;
float _259;
float _274;
float _290;
float _306;
float4 SincY2;
float _322;
float _337;
float _352;
float _367;
float4 SincY3;
float _383;
float _398;
float _413;
float _428;
float4 IdealI;
float _457;
float _474;
float _491;
float _508;
float4 IdealQ;
float _526;
float _543;
float _560;
float _577;
for (float i = 0.0; i < 64.0; i += 4.0)
{
float n = i - 32.0;
float4 n4 = float4(n) + float4(0.0, 1.0, 2.0, 3.0);
float4 Cx = float4(in.v_uv.x) + ((n4 * 0.25) / float4(source_dims.x));
float4 HPosition = Cx;
float4 C = Source.sample(SourceSmplr, float2(Cx.x, Cy.x));
float4 T = (HPosition + float4(HOffset)) + (VPosition * float4(VScale));
float4 WT = T * W;
float4 SincKernel = float4(0.540000021457672119140625) + (cos(n4 * PI2Length) * 0.4600000083446502685546875);
float4 SincYIn1 = n4 * Fc_y1_pi2;
float4 SincYIn2 = n4 * Fc_y2_pi2;
float4 SincYIn3 = n4 * Fc_y3_pi2;
float4 SincIIn = n4 * Fc_i_pi2;
float4 SincQIn = n4 * Fc_q_pi2;
if (SincYIn1.x != 0.0)
{
_259 = sin(SincYIn1.x) / SincYIn1.x;
}
else
{
_259 = 1.0;
}
SincY1.x = _259;
if (SincYIn1.y != 0.0)
{
_274 = sin(SincYIn1.y) / SincYIn1.y;
}
else
{
_274 = 1.0;
}
SincY1.y = _274;
if (SincYIn1.z != 0.0)
{
_290 = sin(SincYIn1.z) / SincYIn1.z;
}
else
{
_290 = 1.0;
}
SincY1.z = _290;
if (SincYIn1.w != 0.0)
{
_306 = sin(SincYIn1.w) / SincYIn1.w;
}
else
{
_306 = 1.0;
}
SincY1.w = _306;
if (SincYIn2.x != 0.0)
{
_322 = sin(SincYIn2.x) / SincYIn2.x;
}
else
{
_322 = 1.0;
}
SincY2.x = _322;
if (SincYIn2.y != 0.0)
{
_337 = sin(SincYIn2.y) / SincYIn2.y;
}
else
{
_337 = 1.0;
}
SincY2.y = _337;
if (SincYIn2.z != 0.0)
{
_352 = sin(SincYIn2.z) / SincYIn2.z;
}
else
{
_352 = 1.0;
}
SincY2.z = _352;
if (SincYIn2.w != 0.0)
{
_367 = sin(SincYIn2.w) / SincYIn2.w;
}
else
{
_367 = 1.0;
}
SincY2.w = _367;
if (SincYIn3.x != 0.0)
{
_383 = sin(SincYIn3.x) / SincYIn3.x;
}
else
{
_383 = 1.0;
}
SincY3.x = _383;
if (SincYIn3.y != 0.0)
{
_398 = sin(SincYIn3.y) / SincYIn3.y;
}
else
{
_398 = 1.0;
}
SincY3.y = _398;
if (SincYIn3.z != 0.0)
{
_413 = sin(SincYIn3.z) / SincYIn3.z;
}
else
{
_413 = 1.0;
}
SincY3.z = _413;
if (SincYIn3.w != 0.0)
{
_428 = sin(SincYIn3.w) / SincYIn3.w;
}
else
{
_428 = 1.0;
}
SincY3.w = _428;
float4 IdealY = ((SincY1 * Fc_y1_2) - (SincY2 * Fc_y2_2)) + (SincY3 * Fc_y3_2);
if (SincIIn.x != 0.0)
{
_457 = sin(SincIIn.x) / SincIIn.x;
}
else
{
_457 = 1.0;
}
IdealI.x = Fc_i_2 * _457;
if (SincIIn.y != 0.0)
{
_474 = sin(SincIIn.y) / SincIIn.y;
}
else
{
_474 = 1.0;
}
IdealI.y = Fc_i_2 * _474;
if (SincIIn.z != 0.0)
{
_491 = sin(SincIIn.z) / SincIIn.z;
}
else
{
_491 = 1.0;
}
IdealI.z = Fc_i_2 * _491;
if (SincIIn.w != 0.0)
{
_508 = sin(SincIIn.w) / SincIIn.w;
}
else
{
_508 = 1.0;
}
IdealI.w = Fc_i_2 * _508;
if (SincQIn.x != 0.0)
{
_526 = sin(SincQIn.x) / SincQIn.x;
}
else
{
_526 = 1.0;
}
IdealQ.x = Fc_q_2 * _526;
if (SincQIn.y != 0.0)
{
_543 = sin(SincQIn.y) / SincQIn.y;
}
else
{
_543 = 1.0;
}
IdealQ.y = Fc_q_2 * _543;
if (SincQIn.z != 0.0)
{
_560 = sin(SincQIn.z) / SincQIn.z;
}
else
{
_560 = 1.0;
}
IdealQ.z = Fc_q_2 * _560;
if (SincQIn.w != 0.0)
{
_577 = sin(SincQIn.w) / SincQIn.w;
}
else
{
_577 = 1.0;
}
IdealQ.w = Fc_q_2 * _577;
float4 FilterY = SincKernel * IdealY;
float4 FilterI = SincKernel * IdealI;
float4 FilterQ = SincKernel * IdealQ;
YAccum += (C * FilterY);
IAccum += ((C * cos(WT)) * FilterI);
QAccum += ((C * sin(WT)) * FilterQ);
}
float3 YIQ = float3(((YAccum.x + YAccum.y) + YAccum.z) + YAccum.w, (((IAccum.x + IAccum.y) + IAccum.z) + IAccum.w) * 2.0, (((QAccum.x + QAccum.y) + QAccum.z) + QAccum.w) * 2.0);
float3 RGB = float3(dot(YIQ, float3(1.0, 0.95599997043609619140625, 0.620999991893768310546875)), dot(YIQ, float3(1.0, -0.272000014781951904296875, -0.647000014781951904296875)), dot(YIQ, float3(1.0, -1.10599994659423828125, 1.70299994945526123046875)));
out.FragColor = float4(RGB, BaseTexel.w);
return out;
}

View File

@@ -1,8 +0,0 @@
#version 450
layout(location=0) out vec2 v_uv;
void main() {
vec2 positions[3] = vec2[3](vec2(-1.0,-1.0), vec2(3.0,-1.0), vec2(-1.0,3.0));
vec2 uvs[3] = vec2[3](vec2(0.0, 1.0), vec2(2.0, 1.0), vec2(0.0,-1.0));
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
v_uv = uvs[gl_VertexIndex];
}

View File

@@ -1,63 +0,0 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wmissing-braces"
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
template<typename T, size_t Num>
struct spvUnsafeArray
{
T elements[Num ? Num : 1];
thread T& operator [] (size_t pos) thread
{
return elements[pos];
}
constexpr const thread T& operator [] (size_t pos) const thread
{
return elements[pos];
}
device T& operator [] (size_t pos) device
{
return elements[pos];
}
constexpr const device T& operator [] (size_t pos) const device
{
return elements[pos];
}
constexpr const constant T& operator [] (size_t pos) const constant
{
return elements[pos];
}
threadgroup T& operator [] (size_t pos) threadgroup
{
return elements[pos];
}
constexpr const threadgroup T& operator [] (size_t pos) const threadgroup
{
return elements[pos];
}
};
constant spvUnsafeArray<float2, 3> _18 = spvUnsafeArray<float2, 3>({ float2(-1.0), float2(3.0, -1.0), float2(-1.0, 3.0) });
constant spvUnsafeArray<float2, 3> _26 = spvUnsafeArray<float2, 3>({ float2(0.0, 1.0), float2(2.0, 1.0), float2(0.0, -1.0) });
struct main0_out
{
float2 v_uv [[user(locn0)]];
float4 gl_Position [[position]];
};
vertex main0_out main0(uint gl_VertexIndex [[vertex_id]])
{
main0_out out = {};
out.gl_Position = float4(_18[int(gl_VertexIndex)], 0.0, 1.0);
out.v_uv = _26[int(gl_VertexIndex)];
return out;
}

View File

@@ -1,18 +0,0 @@
name = ntsc-md-rainbows
passes = 2
pass0_vert = pass0_encode.vert
pass0_frag = pass0_encode.frag
pass1_vert = pass1_decode.vert
pass1_frag = pass1_decode.frag
; NTSC parameters (mapped to NTSCParams uniform buffer)
scantime = 47.9
avalue = 0.0
bvalue = 0.0
ccvalue = 3.5795455
notch_width = 3.45
yfreqresponse = 1.75
ifreqresponse = 1.75
qfreqresponse = 1.45

View File

@@ -1,2 +1,2 @@
// coffee.rc
IDI_ICON1 ICON "icon.ico"
IDI_ICON1 ICON "release/icons/icon.ico"

View File

@@ -6,7 +6,7 @@ layout(set=3, binding=0) uniform PostFXUniforms {
float vignette_strength;
float chroma_strength;
float scanline_strength;
float time;
float screen_height;
} u;
void main() {
float ca = u.chroma_strength * 0.005;
@@ -15,7 +15,7 @@ void main() {
color.g = texture(scene, v_uv).g;
color.b = texture(scene, v_uv - vec2( ca, 0.0)).b;
color.a = texture(scene, v_uv).a;
float scan = 0.85 + 0.15 * sin(v_uv.y * 3.14159265 * 720.0);
float scan = 0.85 + 0.15 * sin(v_uv.y * 3.14159265 * u.screen_height);
color.rgb *= mix(1.0, scan, u.scanline_strength);
vec2 d = v_uv - vec2(0.5, 0.5);
float vignette = 1.0 - dot(d, d) * u.vignette_strength;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,30 +1,31 @@
#include "ball.hpp"
#include <stdlib.h> // for rand
#include <algorithm>
#include <cmath> // for fabs
#include <cstdlib> // for rand
#include <utility>
#include "defines.hpp" // for Color, SCREEN_HEIGHT, GRAVITY_FORCE
class Texture;
// Función auxiliar para generar pérdida aleatoria en rebotes
float generateBounceVariation() {
auto generateBounceVariation() -> float {
// Genera un valor entre 0 y BOUNCE_RANDOM_LOSS_PERCENT (solo pérdida adicional)
float loss = (rand() % 1000) / 1000.0f * BOUNCE_RANDOM_LOSS_PERCENT;
return 1.0f - loss; // Retorna multiplicador (ej: 0.90 - 1.00 para 10% max pérdida)
}
// Función auxiliar para generar pérdida lateral aleatoria
float generateLateralLoss() {
auto generateLateralLoss() -> float {
// Genera un valor entre 0 y LATERAL_LOSS_PERCENT
float loss = (rand() % 1000) / 1000.0f * LATERAL_LOSS_PERCENT;
return 1.0f - loss; // Retorna multiplicador (ej: 0.98 - 1.0 para 0-2% pérdida)
}
// Constructor
Ball::Ball(float x, float y, float vx, float vy, Color color, std::shared_ptr<Texture> texture, int screen_width, int screen_height, int ball_size, GravityDirection gravity_dir, float mass_factor)
Ball::Ball(float x, float y, float vx, float vy, Color color, const std::shared_ptr<Texture>& texture, int screen_width, int screen_height, int ball_size, GravityDirection gravity_dir, float mass_factor)
: sprite_(std::make_unique<Sprite>(texture)),
pos_({x, y, static_cast<float>(ball_size), static_cast<float>(ball_size)}) {
pos_({.x = x, .y = y, .w = static_cast<float>(ball_size), .h = static_cast<float>(ball_size)}) {
// Convertir velocidades de píxeles/frame a píxeles/segundo (multiplicar por 60)
vx_ = vx * 60.0f;
vy_ = vy * 60.0f;
@@ -54,11 +55,11 @@ Ball::Ball(float x, float y, float vx, float vy, Color color, std::shared_ptr<Te
}
// Actualiza la lógica de la clase
void Ball::update(float deltaTime) {
void Ball::update(float delta_time) { // NOLINT(readability-function-cognitive-complexity)
// Aplica la gravedad según la dirección (píxeles/segundo²)
if (!on_surface_) {
// Aplicar gravedad multiplicada por factor de masa individual
float effective_gravity = gravity_force_ * gravity_mass_factor_ * deltaTime;
float effective_gravity = gravity_force_ * gravity_mass_factor_ * delta_time;
switch (gravity_direction_) {
case GravityDirection::DOWN:
vy_ += effective_gravity;
@@ -77,26 +78,26 @@ void Ball::update(float deltaTime) {
// Actualiza la posición en función de la velocidad (píxeles/segundo)
if (!on_surface_) {
pos_.x += vx_ * deltaTime;
pos_.y += vy_ * deltaTime;
pos_.x += vx_ * delta_time;
pos_.y += vy_ * delta_time;
} else {
// Si está en superficie, mantener posición según dirección de gravedad
switch (gravity_direction_) {
case GravityDirection::DOWN:
pos_.y = screen_height_ - pos_.h;
pos_.x += vx_ * deltaTime; // Seguir moviéndose en X
pos_.x += vx_ * delta_time; // Seguir moviéndose en X
break;
case GravityDirection::UP:
pos_.y = 0;
pos_.x += vx_ * deltaTime; // Seguir moviéndose en X
pos_.x += vx_ * delta_time; // Seguir moviéndose en X
break;
case GravityDirection::LEFT:
pos_.x = 0;
pos_.y += vy_ * deltaTime; // Seguir moviéndose en Y
pos_.y += vy_ * delta_time; // Seguir moviéndose en Y
break;
case GravityDirection::RIGHT:
pos_.x = screen_width_ - pos_.w;
pos_.y += vy_ * deltaTime; // Seguir moviéndose en Y
pos_.y += vy_ * delta_time; // Seguir moviéndose en Y
break;
}
}
@@ -176,7 +177,7 @@ void Ball::update(float deltaTime) {
// Aplica rozamiento al estar en superficie
if (on_surface_) {
// Convertir rozamiento de frame-based a time-based
float friction_factor = pow(0.97f, 60.0f * deltaTime);
float friction_factor = std::pow(0.97f, 60.0f * delta_time);
switch (gravity_direction_) {
case GravityDirection::DOWN:
@@ -246,7 +247,7 @@ void Ball::setGravityDirection(GravityDirection direction) {
// Aplica un pequeño empuje lateral aleatorio
void Ball::applyRandomLateralPush() {
// Generar velocidad lateral aleatoria (nunca 0)
float lateral_speed = GRAVITY_CHANGE_LATERAL_MIN + (rand() % 1000) / 1000.0f * (GRAVITY_CHANGE_LATERAL_MAX - GRAVITY_CHANGE_LATERAL_MIN);
float lateral_speed = GRAVITY_CHANGE_LATERAL_MIN + ((rand() % 1000) / 1000.0f * (GRAVITY_CHANGE_LATERAL_MAX - GRAVITY_CHANGE_LATERAL_MIN));
// Signo aleatorio (+ o -)
int sign = ((rand() % 2) * 2) - 1;
@@ -304,18 +305,18 @@ void Ball::enableShapeAttraction(bool enable) {
}
// Obtener distancia actual al punto objetivo (para calcular convergencia)
float Ball::getDistanceToTarget() const {
auto Ball::getDistanceToTarget() const -> float {
// Siempre calcular distancia (útil para convergencia en LOGO mode)
float dx = target_x_ - pos_.x;
float dy = target_y_ - pos_.y;
return sqrtf(dx * dx + dy * dy);
return sqrtf((dx * dx) + (dy * dy));
}
// Aplicar fuerza de resorte hacia punto objetivo en figuras 3D
void Ball::applyShapeForce(float target_x, float target_y, float sphere_radius, float deltaTime,
float spring_k_base, float damping_base_base, float damping_near_base,
float near_threshold_base, float max_force_base) {
if (!shape_attraction_active_) return;
void Ball::applyShapeForce(float target_x, float target_y, float sphere_radius, float delta_time, float spring_k_base, float damping_base_base, float damping_near_base, float near_threshold_base, float max_force_base) {
if (!shape_attraction_active_) {
return;
}
// Calcular factor de escala basado en el radio (radio base = 80px)
// Si radius=80 → scale=1.0, si radius=160 → scale=2.0, si radius=360 → scale=4.5
@@ -334,7 +335,7 @@ void Ball::applyShapeForce(float target_x, float target_y, float sphere_radius,
float diff_y = target_y - pos_.y;
// Calcular distancia al punto objetivo
float distance = sqrtf(diff_x * diff_x + diff_y * diff_y);
float distance = sqrtf((diff_x * diff_x) + (diff_y * diff_y));
// Fuerza de resorte (Ley de Hooke: F = -k * x)
float spring_force_x = spring_k * diff_x;
@@ -354,7 +355,7 @@ void Ball::applyShapeForce(float target_x, float target_y, float sphere_radius,
float total_force_y = spring_force_y - damping_force_y;
// Limitar magnitud de fuerza (evitar explosiones numéricas)
float force_magnitude = sqrtf(total_force_x * total_force_x + total_force_y * total_force_y);
float force_magnitude = sqrtf((total_force_x * total_force_x) + (total_force_y * total_force_y));
if (force_magnitude > max_force) {
float scale_limit = max_force / force_magnitude;
total_force_x *= scale_limit;
@@ -363,18 +364,22 @@ void Ball::applyShapeForce(float target_x, float target_y, float sphere_radius,
// Aplicar aceleración (F = ma, asumiendo m = 1 para simplificar)
// a = F/m, pero m=1, así que a = F
vx_ += total_force_x * deltaTime;
vy_ += total_force_y * deltaTime;
vx_ += total_force_x * delta_time;
vy_ += total_force_y * delta_time;
// Actualizar posición con física normal (velocidad integrada)
pos_.x += vx_ * deltaTime;
pos_.y += vy_ * deltaTime;
pos_.x += vx_ * delta_time;
pos_.y += vy_ * delta_time;
// Mantener pelotas dentro de los límites de pantalla
if (pos_.x < 0) pos_.x = 0;
if (pos_.x + pos_.w > screen_width_) pos_.x = screen_width_ - pos_.w;
if (pos_.y < 0) pos_.y = 0;
if (pos_.y + pos_.h > screen_height_) pos_.y = screen_height_ - pos_.h;
pos_.x = std::max<float>(pos_.x, 0);
if (pos_.x + pos_.w > screen_width_) {
pos_.x = screen_width_ - pos_.w;
}
pos_.y = std::max<float>(pos_.y, 0);
if (pos_.y + pos_.h > screen_height_) {
pos_.y = screen_height_ - pos_.h;
}
// Actualizar sprite para renderizado
sprite_->setPos({pos_.x, pos_.y});
@@ -393,5 +398,5 @@ void Ball::updateSize(int new_size) {
void Ball::setTexture(std::shared_ptr<Texture> texture) {
// Actualizar textura del sprite
sprite_->setTexture(texture);
sprite_->setTexture(std::move(texture));
}

View File

@@ -31,13 +31,13 @@ class Ball {
public:
// Constructor
Ball(float x, float y, float vx, float vy, Color color, std::shared_ptr<Texture> texture, int screen_width, int screen_height, int ball_size, GravityDirection gravity_dir = GravityDirection::DOWN, float mass_factor = 1.0f);
Ball(float x, float y, float vx, float vy, Color color, const std::shared_ptr<Texture>& texture, int screen_width, int screen_height, int ball_size, GravityDirection gravity_dir = GravityDirection::DOWN, float mass_factor = 1.0f);
// Destructor
~Ball() = default;
// Actualiza la lógica de la clase
void update(float deltaTime);
void update(float delta_time);
// Pinta la clase
void render();
@@ -72,11 +72,20 @@ class Ball {
bool isOnSurface() const { return on_surface_; }
// Getters/Setters para velocidad (usado por BoidManager)
void getVelocity(float& vx, float& vy) const { vx = vx_; vy = vy_; }
void setVelocity(float vx, float vy) { vx_ = vx; vy_ = vy; }
void getVelocity(float& vx, float& vy) const {
vx = vx_;
vy = vy_;
}
void setVelocity(float vx, float vy) {
vx_ = vx;
vy_ = vy;
}
// Setter para posición simple (usado por BoidManager)
void setPosition(float x, float y) { pos_.x = x; pos_.y = y; }
void setPosition(float x, float y) {
pos_.x = x;
pos_.y = y;
}
// Getters/Setters para batch rendering
SDL_FRect getPosition() const { return pos_; }
@@ -99,10 +108,5 @@ class Ball {
// Sistema de atracción física hacia figuras 3D
void enableShapeAttraction(bool enable);
float getDistanceToTarget() const; // Distancia actual al punto objetivo
void applyShapeForce(float target_x, float target_y, float sphere_radius, float deltaTime,
float spring_k = SHAPE_SPRING_K,
float damping_base = SHAPE_DAMPING_BASE,
float damping_near = SHAPE_DAMPING_NEAR,
float near_threshold = SHAPE_NEAR_THRESHOLD,
float max_force = SHAPE_MAX_FORCE);
void applyShapeForce(float target_x, float target_y, float sphere_radius, float delta_time, float spring_k_base = SHAPE_SPRING_K, float damping_base_base = SHAPE_DAMPING_BASE, float damping_near_base = SHAPE_DAMPING_NEAR, float near_threshold_base = SHAPE_NEAR_THRESHOLD, float max_force_base = SHAPE_MAX_FORCE);
};

View File

@@ -10,32 +10,31 @@
#include "ui/ui_manager.hpp" // for UIManager
BoidManager::BoidManager()
: engine_(nullptr)
, scene_mgr_(nullptr)
, ui_mgr_(nullptr)
, state_mgr_(nullptr)
, screen_width_(0)
, screen_height_(0)
, boids_active_(false)
, spatial_grid_(800, 600, BOID_GRID_CELL_SIZE) // Tamaño por defecto, se actualiza en initialize()
, separation_radius_(BOID_SEPARATION_RADIUS)
, alignment_radius_(BOID_ALIGNMENT_RADIUS)
, cohesion_radius_(BOID_COHESION_RADIUS)
, separation_weight_(BOID_SEPARATION_WEIGHT)
, alignment_weight_(BOID_ALIGNMENT_WEIGHT)
, cohesion_weight_(BOID_COHESION_WEIGHT)
, max_speed_(BOID_MAX_SPEED)
, min_speed_(BOID_MIN_SPEED)
, max_force_(BOID_MAX_FORCE)
, boundary_margin_(BOID_BOUNDARY_MARGIN)
, boundary_weight_(BOID_BOUNDARY_WEIGHT) {
: engine_(nullptr),
scene_mgr_(nullptr),
ui_mgr_(nullptr),
state_mgr_(nullptr),
screen_width_(0),
screen_height_(0),
boids_active_(false),
spatial_grid_(800, 600, BOID_GRID_CELL_SIZE) // Tamaño por defecto, se actualiza en initialize()
,
separation_radius_(BOID_SEPARATION_RADIUS),
alignment_radius_(BOID_ALIGNMENT_RADIUS),
cohesion_radius_(BOID_COHESION_RADIUS),
separation_weight_(BOID_SEPARATION_WEIGHT),
alignment_weight_(BOID_ALIGNMENT_WEIGHT),
cohesion_weight_(BOID_COHESION_WEIGHT),
max_speed_(BOID_MAX_SPEED),
min_speed_(BOID_MIN_SPEED),
max_force_(BOID_MAX_FORCE),
boundary_margin_(BOID_BOUNDARY_MARGIN),
boundary_weight_(BOID_BOUNDARY_WEIGHT) {
}
BoidManager::~BoidManager() {
}
BoidManager::~BoidManager() = default;
void BoidManager::initialize(Engine* engine, SceneManager* scene_mgr, UIManager* ui_mgr,
StateManager* state_mgr, int screen_width, int screen_height) {
void BoidManager::initialize(Engine* engine, SceneManager* scene_mgr, UIManager* ui_mgr, StateManager* state_mgr, int screen_width, int screen_height) {
engine_ = engine;
scene_mgr_ = scene_mgr;
ui_mgr_ = ui_mgr;
@@ -65,7 +64,8 @@ void BoidManager::activateBoids() {
auto& balls = scene_mgr_->getBallsMutable();
for (auto& ball : balls) {
// Dar velocidad inicial aleatoria si está quieto
float vx, vy;
float vx;
float vy;
ball->getVelocity(vx, vy);
if (vx == 0.0f && vy == 0.0f) {
// Velocidad aleatoria entre -60 y +60 px/s (time-based)
@@ -76,13 +76,15 @@ void BoidManager::activateBoids() {
}
// Mostrar notificación (solo si NO estamos en modo demo o logo)
if (state_mgr_ && ui_mgr_ && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
ui_mgr_->showNotification("Modo Boids");
if ((state_mgr_ != nullptr) && (ui_mgr_ != nullptr) && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
ui_mgr_->showNotification("Modo boids");
}
}
void BoidManager::deactivateBoids(bool force_gravity_on) {
if (!boids_active_) return;
if (!boids_active_) {
return;
}
boids_active_ = false;
@@ -92,8 +94,8 @@ void BoidManager::deactivateBoids(bool force_gravity_on) {
}
// Mostrar notificación (solo si NO estamos en modo demo o logo)
if (state_mgr_ && ui_mgr_ && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
ui_mgr_->showNotification("Modo Física");
if ((state_mgr_ != nullptr) && (ui_mgr_ != nullptr) && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
ui_mgr_->showNotification("Modo física");
}
}
@@ -106,7 +108,9 @@ void BoidManager::toggleBoidsMode(bool force_gravity_on) {
}
void BoidManager::update(float delta_time) {
if (!boids_active_) return;
if (!boids_active_) {
return;
}
auto& balls = scene_mgr_->getBallsMutable();
@@ -114,8 +118,8 @@ void BoidManager::update(float delta_time) {
spatial_grid_.clear();
for (auto& ball : balls) {
SDL_FRect pos = ball->getPosition();
float center_x = pos.x + pos.w / 2.0f;
float center_y = pos.y + pos.h / 2.0f;
float center_x = pos.x + (pos.w / 2.0f);
float center_y = pos.y + (pos.h / 2.0f);
spatial_grid_.insert(ball.get(), center_x, center_y);
}
@@ -131,7 +135,8 @@ void BoidManager::update(float delta_time) {
// Actualizar posiciones con velocidades resultantes (time-based)
for (auto& ball : balls) {
float vx, vy;
float vx;
float vy;
ball->getVelocity(vx, vy);
SDL_FRect pos = ball->getPosition();
@@ -153,22 +158,24 @@ void BoidManager::applySeparation(Ball* boid, float delta_time) {
int count = 0;
SDL_FRect pos = boid->getPosition();
float center_x = pos.x + pos.w / 2.0f;
float center_y = pos.y + pos.h / 2.0f;
float center_x = pos.x + (pos.w / 2.0f);
float center_y = pos.y + (pos.h / 2.0f);
// FASE 2: Usar spatial grid para buscar solo vecinos cercanos (O(1) en lugar de O(n))
auto neighbors = spatial_grid_.queryRadius(center_x, center_y, separation_radius_);
for (Ball* other : neighbors) {
if (other == boid) continue; // Ignorar a sí mismo
if (other == boid) {
continue; // Ignorar a sí mismo
}
SDL_FRect other_pos = other->getPosition();
float other_x = other_pos.x + other_pos.w / 2.0f;
float other_y = other_pos.y + other_pos.h / 2.0f;
float other_x = other_pos.x + (other_pos.w / 2.0f);
float other_y = other_pos.y + (other_pos.h / 2.0f);
float dx = center_x - other_x;
float dy = center_y - other_y;
float distance = std::sqrt(dx * dx + dy * dy);
float distance = std::sqrt((dx * dx) + (dy * dy));
if (distance > 0.0f && distance < separation_radius_) {
// FASE 1.3: Separación más fuerte cuando más cerca (inversamente proporcional a distancia)
@@ -186,7 +193,8 @@ void BoidManager::applySeparation(Ball* boid, float delta_time) {
steer_y /= count;
// Aplicar fuerza de separación
float vx, vy;
float vx;
float vy;
boid->getVelocity(vx, vy);
vx += steer_x * separation_weight_ * delta_time;
vy += steer_y * separation_weight_ * delta_time;
@@ -201,25 +209,28 @@ void BoidManager::applyAlignment(Ball* boid, float delta_time) {
int count = 0;
SDL_FRect pos = boid->getPosition();
float center_x = pos.x + pos.w / 2.0f;
float center_y = pos.y + pos.h / 2.0f;
float center_x = pos.x + (pos.w / 2.0f);
float center_y = pos.y + (pos.h / 2.0f);
// FASE 2: Usar spatial grid para buscar solo vecinos cercanos (O(1) en lugar de O(n))
auto neighbors = spatial_grid_.queryRadius(center_x, center_y, alignment_radius_);
for (Ball* other : neighbors) {
if (other == boid) continue;
if (other == boid) {
continue;
}
SDL_FRect other_pos = other->getPosition();
float other_x = other_pos.x + other_pos.w / 2.0f;
float other_y = other_pos.y + other_pos.h / 2.0f;
float other_x = other_pos.x + (other_pos.w / 2.0f);
float other_y = other_pos.y + (other_pos.h / 2.0f);
float dx = center_x - other_x;
float dy = center_y - other_y;
float distance = std::sqrt(dx * dx + dy * dy);
float distance = std::sqrt((dx * dx) + (dy * dy));
if (distance < alignment_radius_) {
float other_vx, other_vy;
float other_vx;
float other_vy;
other->getVelocity(other_vx, other_vy);
avg_vx += other_vx;
avg_vy += other_vy;
@@ -233,13 +244,14 @@ void BoidManager::applyAlignment(Ball* boid, float delta_time) {
avg_vy /= count;
// Steering hacia la velocidad promedio
float vx, vy;
float vx;
float vy;
boid->getVelocity(vx, vy);
float steer_x = (avg_vx - vx) * alignment_weight_ * delta_time;
float steer_y = (avg_vy - vy) * alignment_weight_ * delta_time;
// Limitar fuerza máxima de steering
float steer_mag = std::sqrt(steer_x * steer_x + steer_y * steer_y);
float steer_mag = std::sqrt((steer_x * steer_x) + (steer_y * steer_y));
if (steer_mag > max_force_) {
steer_x = (steer_x / steer_mag) * max_force_;
steer_y = (steer_y / steer_mag) * max_force_;
@@ -258,22 +270,24 @@ void BoidManager::applyCohesion(Ball* boid, float delta_time) {
int count = 0;
SDL_FRect pos = boid->getPosition();
float center_x = pos.x + pos.w / 2.0f;
float center_y = pos.y + pos.h / 2.0f;
float center_x = pos.x + (pos.w / 2.0f);
float center_y = pos.y + (pos.h / 2.0f);
// FASE 2: Usar spatial grid para buscar solo vecinos cercanos (O(1) en lugar de O(n))
auto neighbors = spatial_grid_.queryRadius(center_x, center_y, cohesion_radius_);
for (Ball* other : neighbors) {
if (other == boid) continue;
if (other == boid) {
continue;
}
SDL_FRect other_pos = other->getPosition();
float other_x = other_pos.x + other_pos.w / 2.0f;
float other_y = other_pos.y + other_pos.h / 2.0f;
float other_x = other_pos.x + (other_pos.w / 2.0f);
float other_y = other_pos.y + (other_pos.h / 2.0f);
float dx = center_x - other_x;
float dy = center_y - other_y;
float distance = std::sqrt(dx * dx + dy * dy);
float distance = std::sqrt((dx * dx) + (dy * dy));
if (distance < cohesion_radius_) {
center_of_mass_x += other_x;
@@ -290,7 +304,7 @@ void BoidManager::applyCohesion(Ball* boid, float delta_time) {
// FASE 1.4: Normalizar dirección hacia el centro (CRÍTICO - antes no estaba normalizado!)
float dx_to_center = center_of_mass_x - center_x;
float dy_to_center = center_of_mass_y - center_y;
float distance_to_center = std::sqrt(dx_to_center * dx_to_center + dy_to_center * dy_to_center);
float distance_to_center = std::sqrt((dx_to_center * dx_to_center) + (dy_to_center * dy_to_center));
// Solo aplicar si hay distancia al centro (evitar división por cero)
if (distance_to_center > 0.1f) {
@@ -299,13 +313,14 @@ void BoidManager::applyCohesion(Ball* boid, float delta_time) {
float steer_y = (dy_to_center / distance_to_center) * cohesion_weight_ * delta_time;
// Limitar fuerza máxima de steering
float steer_mag = std::sqrt(steer_x * steer_x + steer_y * steer_y);
float steer_mag = std::sqrt((steer_x * steer_x) + (steer_y * steer_y));
if (steer_mag > max_force_) {
steer_x = (steer_x / steer_mag) * max_force_;
steer_y = (steer_y / steer_mag) * max_force_;
}
float vx, vy;
float vx;
float vy;
boid->getVelocity(vx, vy);
vx += steer_x;
vy += steer_y;
@@ -314,12 +329,12 @@ void BoidManager::applyCohesion(Ball* boid, float delta_time) {
}
}
void BoidManager::applyBoundaries(Ball* boid) {
void BoidManager::applyBoundaries(Ball* boid) const {
// NUEVA IMPLEMENTACIÓN: Bordes como obstáculos (repulsión en lugar de wrapping)
// Cuando un boid se acerca a un borde, se aplica una fuerza alejándolo
SDL_FRect pos = boid->getPosition();
float center_x = pos.x + pos.w / 2.0f;
float center_y = pos.y + pos.h / 2.0f;
float center_x = pos.x + (pos.w / 2.0f);
float center_y = pos.y + (pos.h / 2.0f);
float steer_x = 0.0f;
float steer_y = 0.0f;
@@ -363,11 +378,12 @@ void BoidManager::applyBoundaries(Ball* boid) {
// Aplicar fuerza de repulsión si hay alguna
if (steer_x != 0.0f || steer_y != 0.0f) {
float vx, vy;
float vx;
float vy;
boid->getVelocity(vx, vy);
// Normalizar fuerza de repulsión (para que todas las direcciones tengan la misma intensidad)
float steer_mag = std::sqrt(steer_x * steer_x + steer_y * steer_y);
float steer_mag = std::sqrt((steer_x * steer_x) + (steer_y * steer_y));
if (steer_mag > 0.0f) {
steer_x /= steer_mag;
steer_y /= steer_mag;
@@ -381,12 +397,13 @@ void BoidManager::applyBoundaries(Ball* boid) {
}
}
void BoidManager::limitSpeed(Ball* boid) {
void BoidManager::limitSpeed(Ball* boid) const {
// Limitar velocidad máxima del boid
float vx, vy;
float vx;
float vy;
boid->getVelocity(vx, vy);
float speed = std::sqrt(vx * vx + vy * vy);
float speed = std::sqrt((vx * vx) + (vy * vy));
// Limitar velocidad máxima
if (speed > max_speed_) {

View File

@@ -46,8 +46,7 @@ class BoidManager {
* @param screen_width Ancho de pantalla actual
* @param screen_height Alto de pantalla actual
*/
void initialize(Engine* engine, SceneManager* scene_mgr, UIManager* ui_mgr,
StateManager* state_mgr, int screen_width, int screen_height);
void initialize(Engine* engine, SceneManager* scene_mgr, UIManager* ui_mgr, StateManager* state_mgr, int screen_width, int screen_height);
/**
* @brief Actualiza el tamaño de pantalla (llamado en resize/fullscreen)
@@ -121,6 +120,6 @@ class BoidManager {
void applySeparation(Ball* boid, float delta_time);
void applyAlignment(Ball* boid, float delta_time);
void applyCohesion(Ball* boid, float delta_time);
void applyBoundaries(Ball* boid); // Repulsión de bordes (ya no wrapping)
void limitSpeed(Ball* boid); // Limitar velocidad máxima
void applyBoundaries(Ball* boid) const; // Repulsión de bordes (ya no wrapping)
void limitSpeed(Ball* boid) const; // Limitar velocidad máxima
};

View File

@@ -6,9 +6,9 @@
#include "ball.hpp" // for Ball
SpatialGrid::SpatialGrid(int world_width, int world_height, float cell_size)
: world_width_(world_width)
, world_height_(world_height)
, cell_size_(cell_size) {
: world_width_(world_width),
world_height_(world_height),
cell_size_(cell_size) {
// Calcular número de celdas en cada dimensión
grid_cols_ = static_cast<int>(std::ceil(world_width / cell_size));
grid_rows_ = static_cast<int>(std::ceil(world_height / cell_size));
@@ -21,7 +21,8 @@ void SpatialGrid::clear() {
void SpatialGrid::insert(Ball* ball, float x, float y) {
// Obtener coordenadas de celda
int cell_x, cell_y;
int cell_x;
int cell_y;
getCellCoords(x, y, cell_x, cell_y);
// Generar hash key y añadir a la celda
@@ -29,11 +30,14 @@ void SpatialGrid::insert(Ball* ball, float x, float y) {
cells_[key].push_back(ball);
}
std::vector<Ball*> SpatialGrid::queryRadius(float x, float y, float radius) {
auto SpatialGrid::queryRadius(float x, float y, float radius) -> std::vector<Ball*> {
std::vector<Ball*> results;
// Calcular rango de celdas a revisar (AABB del círculo de búsqueda)
int min_cell_x, min_cell_y, max_cell_x, max_cell_y;
int min_cell_x;
int min_cell_y;
int max_cell_x;
int max_cell_y;
getCellCoords(x - radius, y - radius, min_cell_x, min_cell_y);
getCellCoords(x + radius, y + radius, max_cell_x, max_cell_y);
@@ -82,8 +86,8 @@ void SpatialGrid::getCellCoords(float x, float y, int& cell_x, int& cell_y) cons
cell_y = static_cast<int>(std::floor(y / cell_size_));
}
int SpatialGrid::getCellKey(int cell_x, int cell_y) const {
auto SpatialGrid::getCellKey(int cell_x, int cell_y) const -> int {
// Hash espacial 2D → 1D usando codificación por filas
// Formula: key = y * ancho + x (similar a array 2D aplanado)
return cell_y * grid_cols_ + cell_x;
return (cell_y * grid_cols_) + cell_x;
}

View File

@@ -30,7 +30,7 @@ class Ball; // Forward declaration
// ============================================================================
class SpatialGrid {
public:
public:
// Constructor: especificar dimensiones del mundo y tamaño de celda
SpatialGrid(int world_width, int world_height, float cell_size);
@@ -47,7 +47,7 @@ public:
// Actualizar dimensiones del mundo (útil para cambios de resolución F4)
void updateWorldSize(int world_width, int world_height);
private:
private:
// Convertir coordenadas (x, y) a índice de celda (cell_x, cell_y)
void getCellCoords(float x, float y, int& cell_x, int& cell_y) const;

View File

@@ -5,22 +5,26 @@
#include <vector> // for std::vector in DynamicThemeKeyframe/DynamicTheme
// Configuración de ventana y pantalla
constexpr char WINDOW_CAPTION[] = "ViBe3 Physics (JailDesigner 2025)";
constexpr char WINDOW_CAPTION[] = "© 2025 ViBe3 Physics JailDesigner";
// Resolución por defecto (usada si no se especifica en CLI)
constexpr int DEFAULT_SCREEN_WIDTH = 1280; // Ancho lógico por defecto (si no hay -w)
constexpr int DEFAULT_SCREEN_HEIGHT = 720; // Alto lógico por defecto (si no hay -h)
constexpr int DEFAULT_WINDOW_ZOOM = 1; // Zoom inicial de ventana (1x = sin zoom)
// Configuración de zoom dinámico de ventana
constexpr int WINDOW_ZOOM_MIN = 1; // Zoom mínimo (320x240)
constexpr int WINDOW_ZOOM_MAX = 10; // Zoom máximo teórico (3200x2400)
constexpr int WINDOW_DESKTOP_MARGIN = 10; // Margen mínimo con bordes del escritorio
constexpr int WINDOW_DECORATION_HEIGHT = 30; // Altura estimada de decoraciones del SO
// Configuración de escala de ventana por pasos (F1/F2)
constexpr float WINDOW_SCALE_STEP = 0.1f; // Incremento/decremento por pulsación (10%)
constexpr float WINDOW_SCALE_MIN = 0.5f; // Escala mínima (50% de la resolución base)
// Configuración de física
constexpr float GRAVITY_FORCE = 0.2f; // Fuerza de gravedad (píxeles/frame²)
// Fuente de la interfaz
#define APP_FONT "data/fonts/Exo2-Regular.ttf"
// Configuración de interfaz
constexpr float THEME_TRANSITION_DURATION = 0.5f; // Duración de transiciones LERP entre temas (segundos)
@@ -32,7 +36,7 @@ constexpr Uint64 NOTIFICATION_FADE_TIME = 200; // Duración animación salida
constexpr float NOTIFICATION_BG_ALPHA = 0.7f; // Opacidad fondo semitransparente (0.0-1.0)
constexpr int NOTIFICATION_PADDING = 10; // Padding interno del fondo (píxeles físicos)
constexpr int NOTIFICATION_TOP_MARGIN = 20; // Margen superior desde borde pantalla (píxeles físicos)
constexpr char KIOSK_NOTIFICATION_TEXT[] = "MODO KIOSKO";
constexpr char KIOSK_NOTIFICATION_TEXT[] = "Modo kiosko";
// Configuración de pérdida aleatoria en rebotes
constexpr float BASE_BOUNCE_COEFFICIENT = 0.75f; // Coeficiente base IGUAL para todas las pelotas
@@ -51,12 +55,27 @@ constexpr float GRAVITY_CHANGE_LATERAL_MAX = 0.08f; // Velocidad lateral máxim
constexpr float BALL_SPAWN_MARGIN = 0.15f; // Margen lateral para spawn (0.25 = 25% a cada lado)
// Escenarios de número de pelotas (teclas 1-8)
// Fase 1 (instanced rendering): límit pràctic ~100K a 60fps (physics bound)
constexpr int BALL_COUNT_SCENARIOS[8] = {10, 50, 100, 500, 1000, 5000, 10000, 100000};
constexpr int SCENE_BALLS_1 = 10;
constexpr int SCENE_BALLS_2 = 50;
constexpr int SCENE_BALLS_3 = 100;
constexpr int SCENE_BALLS_4 = 500;
constexpr int SCENE_BALLS_5 = 1000;
constexpr int SCENE_BALLS_6 = 5000;
constexpr int SCENE_BALLS_7 = 10000;
constexpr int SCENE_BALLS_8 = 50000; // Máximo escenario estándar (tecla 8)
// Límites de escenario para modos automáticos (índices en BALL_COUNT_SCENARIOS)
// BALL_COUNT_SCENARIOS = {10, 50, 100, 500, 1000, 5000, 10000, 50000}
// 0 1 2 3 4 5 6 7
constexpr int SCENARIO_COUNT = 8;
constexpr int BALL_COUNT_SCENARIOS[SCENARIO_COUNT] = {
SCENE_BALLS_1,
SCENE_BALLS_2,
SCENE_BALLS_3,
SCENE_BALLS_4,
SCENE_BALLS_5,
SCENE_BALLS_6,
SCENE_BALLS_7,
SCENE_BALLS_8};
constexpr int BOIDS_MAX_BALLS = SCENE_BALLS_5; // 1 000 bolas máximo en modo BOIDS
constexpr int DEMO_AUTO_MIN_SCENARIO = 2; // mínimo 100 bolas
constexpr int DEMO_AUTO_MAX_SCENARIO = 7; // máximo sin restricción hardware (ajustado por benchmark)
constexpr int LOGO_MIN_SCENARIO_IDX = 4; // mínimo 1000 bolas (sustituye LOGO_MODE_MIN_BALLS)
@@ -165,7 +184,6 @@ enum class ScalingMode {
constexpr float ROTOBALL_RADIUS_FACTOR = 0.333f; // Radio como proporción de altura de pantalla (80/240 ≈ 0.333)
constexpr float ROTOBALL_ROTATION_SPEED_Y = 1.5f; // Velocidad rotación eje Y (rad/s)
constexpr float ROTOBALL_ROTATION_SPEED_X = 0.8f; // Velocidad rotación eje X (rad/s)
constexpr float ROTOBALL_TRANSITION_TIME = 1.5f; // Tiempo de transición (segundos)
constexpr int ROTOBALL_MIN_BRIGHTNESS = 50; // Brillo mínimo (fondo, 0-255)
constexpr int ROTOBALL_MAX_BRIGHTNESS = 255; // Brillo máximo (frente, 0-255)
@@ -274,7 +292,6 @@ constexpr int DEMO_LITE_WEIGHT_IMPULSE = 10; // Aplicar impulso (10%)
// TOTAL: 100
// Configuración de Modo LOGO (easter egg - "marca de agua")
constexpr int LOGO_MODE_MIN_BALLS = 500; // Mínimo de pelotas para activar modo logo
constexpr float LOGO_MODE_SHAPE_SCALE = 1.2f; // Escala de figura en modo logo (120%)
constexpr float LOGO_ACTION_INTERVAL_MIN = 3.0f; // Tiempo mínimo entre alternancia SHAPE/PHYSICS (escalado con resolución)
constexpr float LOGO_ACTION_INTERVAL_MAX = 5.0f; // Tiempo máximo entre alternancia SHAPE/PHYSICS (escalado con resolución)

File diff suppressed because it is too large Load Diff

View File

@@ -11,7 +11,6 @@
#include <string> // for string
#include <vector> // for vector
#include "ui/app_logo.hpp" // for AppLogo
#include "ball.hpp" // for Ball
#include "boids_mgr/boid_manager.hpp" // for BoidManager
#include "defines.hpp" // for GravityDirection, ColorTheme, ShapeType
@@ -19,15 +18,14 @@
#include "gpu/gpu_ball_buffer.hpp" // for GpuBallBuffer, BallGPUData
#include "gpu/gpu_context.hpp" // for GpuContext
#include "gpu/gpu_pipeline.hpp" // for GpuPipeline
#include "gpu/gpu_shader_preset.hpp" // for NTSCParams, GpuShaderPreset
#include "gpu/gpu_sprite_batch.hpp" // for GpuSpriteBatch
#include "gpu/gpu_texture.hpp" // for GpuTexture
#include "gpu/shader_manager.hpp" // for ShaderManager
#include "input/input_handler.hpp" // for InputHandler
#include "scene/scene_manager.hpp" // for SceneManager
#include "shapes_mgr/shape_manager.hpp" // for ShapeManager
#include "state/state_manager.hpp" // for StateManager
#include "theme_manager.hpp" // for ThemeManager
#include "ui/app_logo.hpp" // for AppLogo
#include "ui/ui_manager.hpp" // for UIManager
class Engine {
@@ -78,16 +76,19 @@ class Engine {
void toggleRealFullscreen();
void toggleIntegerScaling();
// Campo de juego (tamaño lógico + físico)
void fieldSizeUp();
void fieldSizeDown();
void setFieldScale(float new_scale);
// PostFX presets
void handlePostFXCycle();
void handlePostFXToggle();
void setInitialPostFX(int mode);
void setPostFXParamOverrides(float vignette, float chroma);
// External shader presets (loaded from data/shaders/)
// Cicle PostFX nadiu (OFF → Vinyeta → Scanlines → Cromàtica → Complet)
void cycleShader();
void setInitialShader(const std::string& name);
std::string getActiveShaderName() const;
// Modo kiosko
void setKioskMode(bool enabled) { kiosk_mode_ = enabled; }
@@ -137,7 +138,6 @@ class Engine {
float getPostFXVignette() const { return postfx_uniforms_.vignette_strength; }
float getPostFXChroma() const { return postfx_uniforms_.chroma_strength; }
float getPostFXScanline() const { return postfx_uniforms_.scanline_strength; }
bool isExternalShaderActive() const { return active_shader_ != nullptr; }
private:
// === Componentes del sistema (Composición) ===
@@ -187,19 +187,16 @@ class Engine {
// PostFX uniforms (passed to GPU each frame)
PostFXUniforms postfx_uniforms_ = {0.0f, 0.0f, 0.0f, 0.0f};
int postfx_effect_mode_ = 0;
int postfx_effect_mode_ = 3;
bool postfx_enabled_ = false;
float postfx_override_vignette_ = -1.f; // -1 = sin override
float postfx_override_chroma_ = -1.f;
// External shader system
std::unique_ptr<ShaderManager> shader_manager_;
GpuShaderPreset* active_shader_ = nullptr; // null = native PostFX
int active_shader_idx_ = -1; // index into shader_manager_->names()
std::string initial_shader_name_; // set before initialize()
// Sistema de escala de ventana
float current_window_scale_ = 1.0f;
// Sistema de zoom dinámico
int current_window_zoom_ = DEFAULT_WINDOW_ZOOM;
// Escala del campo de juego lógico (F7/F8)
float current_field_scale_ = 1.0f;
// V-Sync y fullscreen
bool vsync_enabled_ = true;
@@ -253,9 +250,9 @@ class Engine {
// Sistema de cambio de sprites dinámico
void switchTextureInternal(bool show_notification);
// Sistema de zoom dinámico
int calculateMaxWindowZoom() const;
void setWindowZoom(int new_zoom);
// Sistema de escala de ventana
float calculateMaxWindowScale() const;
void setWindowScale(float new_scale);
void zoomIn();
void zoomOut();
void updatePhysicalWindowSize();
@@ -272,10 +269,12 @@ class Engine {
// PostFX helper
void applyPostFXPreset(int mode);
// Boids: comprueba si un escenario tiene ≤ BOIDS_MAX_BALLS bolas
bool isScenarioAllowedForBoids(int scenario_id) const;
// GPU helpers
bool loadGpuSpriteTexture(size_t index); // Upload one sprite texture to GPU
void recreateOffscreenTexture(); // Recreate when resolution changes
void renderUIToSurface(); // Render text/UI to ui_surface_
void uploadUISurface(SDL_GPUCommandBuffer* cmd_buf); // Upload ui_surface_ → ui_tex_
};

View File

@@ -50,11 +50,7 @@ bool Texture::loadFromFile(const std::string &file_path) {
delete[] resourceData; // Liberar buffer temporal
if (data != nullptr) {
if (ResourceManager::isPackLoaded()) {
std::cout << "Imagen cargada desde pack: " << filename.c_str() << std::endl;
} else {
std::cout << "Imagen cargada desde disco: " << filename.c_str() << std::endl;
}
std::cout << "[Textura] " << filename << " (" << (ResourceManager::isPackLoaded() ? "pack" : "disco") << ")\n";
}
}

View File

@@ -1,10 +1,11 @@
#include "gpu_ball_buffer.hpp"
#include <SDL3/SDL_log.h>
#include <algorithm> // std::min
#include <cstring> // memcpy
bool GpuBallBuffer::init(SDL_GPUDevice* device) {
auto GpuBallBuffer::init(SDL_GPUDevice* device) -> bool {
Uint32 buf_size = static_cast<Uint32>(MAX_BALLS) * sizeof(BallGPUData);
// GPU vertex buffer (instance-rate data read by the ball instanced shader)
@@ -12,7 +13,7 @@ bool GpuBallBuffer::init(SDL_GPUDevice* device) {
buf_info.usage = SDL_GPU_BUFFERUSAGE_VERTEX;
buf_info.size = buf_size;
gpu_buf_ = SDL_CreateGPUBuffer(device, &buf_info);
if (!gpu_buf_) {
if (gpu_buf_ == nullptr) {
SDL_Log("GpuBallBuffer: GPU buffer creation failed: %s", SDL_GetError());
return false;
}
@@ -22,32 +23,43 @@ bool GpuBallBuffer::init(SDL_GPUDevice* device) {
tb_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
tb_info.size = buf_size;
transfer_buf_ = SDL_CreateGPUTransferBuffer(device, &tb_info);
if (!transfer_buf_) {
if (transfer_buf_ == nullptr) {
SDL_Log("GpuBallBuffer: transfer buffer creation failed: %s", SDL_GetError());
return false;
}
SDL_Log("GpuBallBuffer: initialized (capacity %d balls, %.1f MB VRAM)",
MAX_BALLS, buf_size / (1024.0f * 1024.0f));
MAX_BALLS,
buf_size / (1024.0f * 1024.0f));
return true;
}
void GpuBallBuffer::destroy(SDL_GPUDevice* device) {
if (!device) return;
if (transfer_buf_) { SDL_ReleaseGPUTransferBuffer(device, transfer_buf_); transfer_buf_ = nullptr; }
if (gpu_buf_) { SDL_ReleaseGPUBuffer(device, gpu_buf_); gpu_buf_ = nullptr; }
if (device == nullptr) {
return;
}
if (transfer_buf_ != nullptr) {
SDL_ReleaseGPUTransferBuffer(device, transfer_buf_);
transfer_buf_ = nullptr;
}
if (gpu_buf_ != nullptr) {
SDL_ReleaseGPUBuffer(device, gpu_buf_);
gpu_buf_ = nullptr;
}
count_ = 0;
}
bool GpuBallBuffer::upload(SDL_GPUDevice* device, SDL_GPUCommandBuffer* cmd,
const BallGPUData* data, int count) {
if (!data || count <= 0) { count_ = 0; return false; }
auto GpuBallBuffer::upload(SDL_GPUDevice* device, SDL_GPUCommandBuffer* cmd, const BallGPUData* data, int count) -> bool {
if ((data == nullptr) || count <= 0) {
count_ = 0;
return false;
}
count = std::min(count, MAX_BALLS);
Uint32 upload_size = static_cast<Uint32>(count) * sizeof(BallGPUData);
void* ptr = SDL_MapGPUTransferBuffer(device, transfer_buf_, true /* cycle */);
if (!ptr) {
if (ptr == nullptr) {
SDL_Log("GpuBallBuffer: transfer buffer map failed: %s", SDL_GetError());
return false;
}
@@ -55,8 +67,8 @@ bool GpuBallBuffer::upload(SDL_GPUDevice* device, SDL_GPUCommandBuffer* cmd,
SDL_UnmapGPUTransferBuffer(device, transfer_buf_);
SDL_GPUCopyPass* copy = SDL_BeginGPUCopyPass(cmd);
SDL_GPUTransferBufferLocation src = { transfer_buf_, 0 };
SDL_GPUBufferRegion dst = { gpu_buf_, 0, upload_size };
SDL_GPUTransferBufferLocation src = {transfer_buf_, 0};
SDL_GPUBufferRegion dst = {gpu_buf_, 0, upload_size};
SDL_UploadToGPUBuffer(copy, &src, &dst, true /* cycle */);
SDL_EndGPUCopyPass(copy);

View File

@@ -1,6 +1,7 @@
#pragma once
#include <SDL3/SDL_gpu.h>
#include <cstdint>
// ---------------------------------------------------------------------------
@@ -26,7 +27,7 @@ static_assert(sizeof(BallGPUData) == 32, "BallGPUData must be 32 bytes");
// // Then in render pass: bind buffer, SDL_DrawGPUPrimitives(pass, 6, count, 0, 0)
// ============================================================================
class GpuBallBuffer {
public:
public:
static constexpr int MAX_BALLS = 500000;
bool init(SDL_GPUDevice* device);
@@ -34,13 +35,12 @@ public:
// Upload ball array to GPU via an internal copy pass.
// count is clamped to MAX_BALLS. Returns false on error or empty input.
bool upload(SDL_GPUDevice* device, SDL_GPUCommandBuffer* cmd,
const BallGPUData* data, int count);
bool upload(SDL_GPUDevice* device, SDL_GPUCommandBuffer* cmd, const BallGPUData* data, int count);
SDL_GPUBuffer* buffer() const { return gpu_buf_; }
int count() const { return count_; }
private:
private:
SDL_GPUBuffer* gpu_buf_ = nullptr;
SDL_GPUTransferBuffer* transfer_buf_ = nullptr;
int count_ = 0;

View File

@@ -1,9 +1,10 @@
#include "gpu_context.hpp"
#include <SDL3/SDL_log.h>
#include <iostream>
bool GpuContext::init(SDL_Window* window) {
auto GpuContext::init(SDL_Window* window) -> bool {
window_ = window;
// Create GPU device: Metal on Apple, Vulkan elsewhere
@@ -13,15 +14,15 @@ bool GpuContext::init(SDL_Window* window) {
SDL_GPUShaderFormat preferred = SDL_GPU_SHADERFORMAT_SPIRV;
#endif
device_ = SDL_CreateGPUDevice(preferred, false, nullptr);
if (!device_) {
std::cerr << "GpuContext: SDL_CreateGPUDevice failed: " << SDL_GetError() << std::endl;
if (device_ == nullptr) {
std::cerr << "GpuContext: SDL_CreateGPUDevice failed: " << SDL_GetError() << '\n';
return false;
}
std::cout << "GpuContext: driver = " << SDL_GetGPUDeviceDriver(device_) << std::endl;
std::cout << "GpuContext: driver = " << SDL_GetGPUDeviceDriver(device_) << '\n';
// Claim the window so the GPU device owns its swapchain
if (!SDL_ClaimWindowForGPUDevice(device_, window_)) {
std::cerr << "GpuContext: SDL_ClaimWindowForGPUDevice failed: " << SDL_GetError() << std::endl;
std::cerr << "GpuContext: SDL_ClaimWindowForGPUDevice failed: " << SDL_GetError() << '\n';
SDL_DestroyGPUDevice(device_);
device_ = nullptr;
return false;
@@ -29,17 +30,15 @@ bool GpuContext::init(SDL_Window* window) {
// Query swapchain format (Metal: typically B8G8R8A8_UNORM or R8G8B8A8_UNORM)
swapchain_format_ = SDL_GetGPUSwapchainTextureFormat(device_, window_);
std::cout << "GpuContext: swapchain format = " << static_cast<int>(swapchain_format_) << std::endl;
std::cout << "GpuContext: swapchain format = " << static_cast<int>(swapchain_format_) << '\n';
// Default: VSync ON
SDL_SetGPUSwapchainParameters(device_, window_,
SDL_GPU_SWAPCHAINCOMPOSITION_SDR,
SDL_GPU_PRESENTMODE_VSYNC);
SDL_SetGPUSwapchainParameters(device_, window_, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, SDL_GPU_PRESENTMODE_VSYNC);
return true;
}
void GpuContext::destroy() {
if (device_) {
if (device_ != nullptr) {
SDL_WaitForGPUIdle(device_);
SDL_ReleaseWindowFromGPUDevice(device_, window_);
SDL_DestroyGPUDevice(device_);
@@ -48,16 +47,17 @@ void GpuContext::destroy() {
window_ = nullptr;
}
SDL_GPUCommandBuffer* GpuContext::acquireCommandBuffer() {
auto GpuContext::acquireCommandBuffer() -> SDL_GPUCommandBuffer* {
SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(device_);
if (!cmd) {
if (cmd == nullptr) {
SDL_Log("GpuContext: SDL_AcquireGPUCommandBuffer failed: %s", SDL_GetError());
}
return cmd;
}
SDL_GPUTexture* GpuContext::acquireSwapchainTexture(SDL_GPUCommandBuffer* cmd_buf,
Uint32* out_w, Uint32* out_h) {
auto GpuContext::acquireSwapchainTexture(SDL_GPUCommandBuffer* cmd_buf,
Uint32* out_w,
Uint32* out_h) -> SDL_GPUTexture* {
SDL_GPUTexture* tex = nullptr;
if (!SDL_AcquireGPUSwapchainTexture(cmd_buf, window_, &tex, out_w, out_h)) {
SDL_Log("GpuContext: SDL_AcquireGPUSwapchainTexture failed: %s", SDL_GetError());
@@ -71,10 +71,8 @@ void GpuContext::submit(SDL_GPUCommandBuffer* cmd_buf) {
SDL_SubmitGPUCommandBuffer(cmd_buf);
}
bool GpuContext::setVSync(bool enabled) {
auto GpuContext::setVSync(bool enabled) -> bool {
SDL_GPUPresentMode mode = enabled ? SDL_GPU_PRESENTMODE_VSYNC
: SDL_GPU_PRESENTMODE_IMMEDIATE;
return SDL_SetGPUSwapchainParameters(device_, window_,
SDL_GPU_SWAPCHAINCOMPOSITION_SDR,
mode);
return SDL_SetGPUSwapchainParameters(device_, window_, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, mode);
}

View File

@@ -8,7 +8,7 @@
// Replaces SDL_Renderer as the main rendering backend.
// ============================================================================
class GpuContext {
public:
public:
bool init(SDL_Window* window);
void destroy();
@@ -20,13 +20,14 @@ public:
SDL_GPUCommandBuffer* acquireCommandBuffer();
// Returns nullptr if window is minimized (swapchain not available).
SDL_GPUTexture* acquireSwapchainTexture(SDL_GPUCommandBuffer* cmd_buf,
Uint32* out_w, Uint32* out_h);
void submit(SDL_GPUCommandBuffer* cmd_buf);
Uint32* out_w,
Uint32* out_h);
static void submit(SDL_GPUCommandBuffer* cmd_buf);
// VSync control (call after init)
bool setVSync(bool enabled);
private:
private:
SDL_GPUDevice* device_ = nullptr;
SDL_Window* window_ = nullptr;
SDL_GPUTextureFormat swapchain_format_ = SDL_GPU_TEXTUREFORMAT_INVALID;

View File

@@ -1,20 +1,24 @@
#include "gpu_pipeline.hpp"
#include "gpu_sprite_batch.hpp" // for GpuVertex layout
#include "gpu_ball_buffer.hpp" // for BallGPUData layout
#include <SDL3/SDL_log.h>
#include <array> // for std::array
#include <cstddef> // offsetof
#include <cstring> // strlen
#include "gpu_ball_buffer.hpp" // for BallGPUData layout
#include "gpu_sprite_batch.hpp" // for GpuVertex layout
#ifndef __APPLE__
// Generated at build time by CMake + glslc (see cmake/spv_to_header.cmake)
#include "sprite_vert_spv.h"
#include "sprite_frag_spv.h"
#include "postfx_vert_spv.h"
#include "postfx_frag_spv.h"
#include "ball_vert_spv.h"
#include "postfx_frag_spv.h"
#include "postfx_vert_spv.h"
#include "sprite_frag_spv.h"
#include "sprite_vert_spv.h"
#endif
#ifdef __APPLE__
// ============================================================================
// MSL Shaders (Metal Shading Language, macOS)
// ============================================================================
@@ -116,7 +120,7 @@ struct PostFXUniforms {
float vignette_strength;
float chroma_strength;
float scanline_strength;
float time;
float screen_height;
};
fragment float4 postfx_fs(PostVOut in [[stage_in]],
@@ -132,7 +136,7 @@ fragment float4 postfx_fs(PostVOut in [[stage_in]],
color.a = scene.sample(samp, in.uv ).a;
// Scanlines: horizontal sine-wave at ~360 lines (one dark band per 2 px at 720p)
float scan = 0.85 + 0.15 * sin(in.uv.y * 3.14159265 * 720.0);
float scan = 0.85 + 0.15 * sin(in.uv.y * 3.14159265 * u.screen_height);
color.rgb *= mix(1.0, scan, u.scanline_strength);
// Vignette: radial edge darkening
@@ -197,14 +201,15 @@ vertex BallVOut ball_instanced_vs(BallInstance inst [[stage_in]],
return out;
}
)";
#endif // __APPLE__
// ============================================================================
// GpuPipeline implementation
// ============================================================================
bool GpuPipeline::init(SDL_GPUDevice* device,
auto GpuPipeline::init(SDL_GPUDevice* device,
SDL_GPUTextureFormat target_format,
SDL_GPUTextureFormat offscreen_format) {
SDL_GPUTextureFormat offscreen_format) -> bool {
SDL_GPUShaderFormat supported = SDL_GetGPUShaderFormats(device);
#ifdef __APPLE__
if (!(supported & SDL_GPU_SHADERFORMAT_MSL)) {
@@ -212,7 +217,7 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
return false;
}
#else
if (!(supported & SDL_GPU_SHADERFORMAT_SPIRV)) {
if ((supported & SDL_GPU_SHADERFORMAT_SPIRV) == 0u) {
SDL_Log("GpuPipeline: SPIRV not supported (format mask=%u)", supported);
return false;
}
@@ -222,20 +227,20 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
// Sprite pipeline
// ----------------------------------------------------------------
#ifdef __APPLE__
SDL_GPUShader* sprite_vert = createShader(device, kSpriteVertMSL, "sprite_vs",
SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* sprite_frag = createShader(device, kSpriteFragMSL, "sprite_fs",
SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 0);
SDL_GPUShader* sprite_vert = createShader(device, kSpriteVertMSL, "sprite_vs", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* sprite_frag = createShader(device, kSpriteFragMSL, "sprite_fs", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 0);
#else
SDL_GPUShader* sprite_vert = createShaderSPIRV(device, ksprite_vert_spv, ksprite_vert_spv_size,
"main", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* sprite_frag = createShaderSPIRV(device, ksprite_frag_spv, ksprite_frag_spv_size,
"main", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 0);
SDL_GPUShader* sprite_vert = createShaderSPIRV(device, ksprite_vert_spv, ksprite_vert_spv_size, "main", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* sprite_frag = createShaderSPIRV(device, ksprite_frag_spv, ksprite_frag_spv_size, "main", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 0);
#endif
if (!sprite_vert || !sprite_frag) {
if ((sprite_vert == nullptr) || (sprite_frag == nullptr)) {
SDL_Log("GpuPipeline: failed to create sprite shaders");
if (sprite_vert) SDL_ReleaseGPUShader(device, sprite_vert);
if (sprite_frag) SDL_ReleaseGPUShader(device, sprite_frag);
if (sprite_vert != nullptr) {
SDL_ReleaseGPUShader(device, sprite_vert);
}
if (sprite_frag != nullptr) {
SDL_ReleaseGPUShader(device, sprite_frag);
}
return false;
}
@@ -246,7 +251,7 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
vb_desc.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX;
vb_desc.instance_step_rate = 0;
SDL_GPUVertexAttribute attrs[3] = {};
std::array<SDL_GPUVertexAttribute, 3> attrs = {};
attrs[0].location = 0;
attrs[0].buffer_slot = 0;
attrs[0].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
@@ -265,7 +270,7 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
SDL_GPUVertexInputState vertex_input = {};
vertex_input.vertex_buffer_descriptions = &vb_desc;
vertex_input.num_vertex_buffers = 1;
vertex_input.vertex_attributes = attrs;
vertex_input.vertex_attributes = attrs.data();
vertex_input.num_vertex_attributes = 3;
// Alpha blend state (SRC_ALPHA, ONE_MINUS_SRC_ALPHA)
@@ -296,7 +301,7 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
SDL_ReleaseGPUShader(device, sprite_vert);
SDL_ReleaseGPUShader(device, sprite_frag);
if (!sprite_pipeline_) {
if (sprite_pipeline_ == nullptr) {
SDL_Log("GpuPipeline: sprite pipeline creation failed: %s", SDL_GetError());
return false;
}
@@ -308,20 +313,20 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
// Targets: offscreen (same as sprite pipeline)
// ----------------------------------------------------------------
#ifdef __APPLE__
SDL_GPUShader* ball_vert = createShader(device, kBallInstancedVertMSL, "ball_instanced_vs",
SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* ball_frag = createShader(device, kSpriteFragMSL, "sprite_fs",
SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 0);
SDL_GPUShader* ball_vert = createShader(device, kBallInstancedVertMSL, "ball_instanced_vs", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* ball_frag = createShader(device, kSpriteFragMSL, "sprite_fs", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 0);
#else
SDL_GPUShader* ball_vert = createShaderSPIRV(device, kball_vert_spv, kball_vert_spv_size,
"main", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* ball_frag = createShaderSPIRV(device, ksprite_frag_spv, ksprite_frag_spv_size,
"main", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 0);
SDL_GPUShader* ball_vert = createShaderSPIRV(device, kball_vert_spv, kball_vert_spv_size, "main", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* ball_frag = createShaderSPIRV(device, ksprite_frag_spv, ksprite_frag_spv_size, "main", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 0);
#endif
if (!ball_vert || !ball_frag) {
if ((ball_vert == nullptr) || (ball_frag == nullptr)) {
SDL_Log("GpuPipeline: failed to create ball instanced shaders");
if (ball_vert) SDL_ReleaseGPUShader(device, ball_vert);
if (ball_frag) SDL_ReleaseGPUShader(device, ball_frag);
if (ball_vert != nullptr) {
SDL_ReleaseGPUShader(device, ball_vert);
}
if (ball_frag != nullptr) {
SDL_ReleaseGPUShader(device, ball_frag);
}
return false;
}
@@ -332,7 +337,7 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
ball_vb_desc.input_rate = SDL_GPU_VERTEXINPUTRATE_INSTANCE;
ball_vb_desc.instance_step_rate = 1;
SDL_GPUVertexAttribute ball_attrs[3] = {};
std::array<SDL_GPUVertexAttribute, 3> ball_attrs = {};
// attr 0: center (float2) at offset 0
ball_attrs[0].location = 0;
ball_attrs[0].buffer_slot = 0;
@@ -352,7 +357,7 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
SDL_GPUVertexInputState ball_vertex_input = {};
ball_vertex_input.vertex_buffer_descriptions = &ball_vb_desc;
ball_vertex_input.num_vertex_buffers = 1;
ball_vertex_input.vertex_attributes = ball_attrs;
ball_vertex_input.vertex_attributes = ball_attrs.data();
ball_vertex_input.num_vertex_attributes = 3;
SDL_GPUGraphicsPipelineCreateInfo ball_pipe_info = {};
@@ -368,7 +373,7 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
SDL_ReleaseGPUShader(device, ball_vert);
SDL_ReleaseGPUShader(device, ball_frag);
if (!ball_pipeline_) {
if (ball_pipeline_ == nullptr) {
SDL_Log("GpuPipeline: ball instanced pipeline creation failed: %s", SDL_GetError());
return false;
}
@@ -387,20 +392,20 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
// PostFX pipeline
// ----------------------------------------------------------------
#ifdef __APPLE__
SDL_GPUShader* postfx_vert = createShader(device, kPostFXVertMSL, "postfx_vs",
SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* postfx_frag = createShader(device, kPostFXFragMSL, "postfx_fs",
SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1);
SDL_GPUShader* postfx_vert = createShader(device, kPostFXVertMSL, "postfx_vs", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* postfx_frag = createShader(device, kPostFXFragMSL, "postfx_fs", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1);
#else
SDL_GPUShader* postfx_vert = createShaderSPIRV(device, kpostfx_vert_spv, kpostfx_vert_spv_size,
"main", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* postfx_frag = createShaderSPIRV(device, kpostfx_frag_spv, kpostfx_frag_spv_size,
"main", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1);
SDL_GPUShader* postfx_vert = createShaderSPIRV(device, kpostfx_vert_spv, kpostfx_vert_spv_size, "main", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0);
SDL_GPUShader* postfx_frag = createShaderSPIRV(device, kpostfx_frag_spv, kpostfx_frag_spv_size, "main", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1);
#endif
if (!postfx_vert || !postfx_frag) {
if ((postfx_vert == nullptr) || (postfx_frag == nullptr)) {
SDL_Log("GpuPipeline: failed to create postfx shaders");
if (postfx_vert) SDL_ReleaseGPUShader(device, postfx_vert);
if (postfx_frag) SDL_ReleaseGPUShader(device, postfx_frag);
if (postfx_vert != nullptr) {
SDL_ReleaseGPUShader(device, postfx_vert);
}
if (postfx_frag != nullptr) {
SDL_ReleaseGPUShader(device, postfx_frag);
}
return false;
}
@@ -428,7 +433,7 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
SDL_ReleaseGPUShader(device, postfx_vert);
SDL_ReleaseGPUShader(device, postfx_frag);
if (!postfx_pipeline_) {
if (postfx_pipeline_ == nullptr) {
SDL_Log("GpuPipeline: postfx pipeline creation failed: %s", SDL_GetError());
return false;
}
@@ -438,19 +443,28 @@ bool GpuPipeline::init(SDL_GPUDevice* device,
}
void GpuPipeline::destroy(SDL_GPUDevice* device) {
if (sprite_pipeline_) { SDL_ReleaseGPUGraphicsPipeline(device, sprite_pipeline_); sprite_pipeline_ = nullptr; }
if (ball_pipeline_) { SDL_ReleaseGPUGraphicsPipeline(device, ball_pipeline_); ball_pipeline_ = nullptr; }
if (postfx_pipeline_) { SDL_ReleaseGPUGraphicsPipeline(device, postfx_pipeline_); postfx_pipeline_ = nullptr; }
if (sprite_pipeline_ != nullptr) {
SDL_ReleaseGPUGraphicsPipeline(device, sprite_pipeline_);
sprite_pipeline_ = nullptr;
}
if (ball_pipeline_ != nullptr) {
SDL_ReleaseGPUGraphicsPipeline(device, ball_pipeline_);
ball_pipeline_ = nullptr;
}
if (postfx_pipeline_ != nullptr) {
SDL_ReleaseGPUGraphicsPipeline(device, postfx_pipeline_);
postfx_pipeline_ = nullptr;
}
}
SDL_GPUShader* GpuPipeline::createShaderSPIRV(SDL_GPUDevice* device,
auto GpuPipeline::createShaderSPIRV(SDL_GPUDevice* device,
const uint8_t* spv_code,
size_t spv_size,
const char* entrypoint,
SDL_GPUShaderStage stage,
Uint32 num_samplers,
Uint32 num_uniform_buffers,
Uint32 num_storage_buffers) {
Uint32 num_storage_buffers) -> SDL_GPUShader* {
SDL_GPUShaderCreateInfo info = {};
info.code = spv_code;
info.code_size = spv_size;
@@ -462,18 +476,19 @@ SDL_GPUShader* GpuPipeline::createShaderSPIRV(SDL_GPUDevice* device,
info.num_storage_buffers = num_storage_buffers;
info.num_uniform_buffers = num_uniform_buffers;
SDL_GPUShader* shader = SDL_CreateGPUShader(device, &info);
if (!shader)
if (shader == nullptr) {
SDL_Log("GpuPipeline: SPIRV shader '%s' failed: %s", entrypoint, SDL_GetError());
}
return shader;
}
SDL_GPUShader* GpuPipeline::createShader(SDL_GPUDevice* device,
auto GpuPipeline::createShader(SDL_GPUDevice* device,
const char* msl_source,
const char* entrypoint,
SDL_GPUShaderStage stage,
Uint32 num_samplers,
Uint32 num_uniform_buffers,
Uint32 num_storage_buffers) {
Uint32 num_storage_buffers) -> SDL_GPUShader* {
SDL_GPUShaderCreateInfo info = {};
info.code = reinterpret_cast<const Uint8*>(msl_source);
info.code_size = static_cast<size_t>(strlen(msl_source) + 1);
@@ -486,7 +501,7 @@ SDL_GPUShader* GpuPipeline::createShader(SDL_GPUDevice* device,
info.num_uniform_buffers = num_uniform_buffers;
SDL_GPUShader* shader = SDL_CreateGPUShader(device, &info);
if (!shader) {
if (shader == nullptr) {
SDL_Log("GpuPipeline: shader '%s' failed: %s", entrypoint, SDL_GetError());
}
return shader;

View File

@@ -8,10 +8,10 @@
// MSL binding: constant PostFXUniforms& u [[buffer(0)]]
// ============================================================================
struct PostFXUniforms {
float vignette_strength; // 0 = none, 1.5 = default subtle
float chroma_strength; // 0 = off, 1 = full chromatic aberration
float vignette_strength; // 0 = none, 0.8 = default subtle
float chroma_strength; // 0 = off, 0.2 = default chromatic aberration
float scanline_strength; // 0 = off, 1 = full scanlines
float time; // accumulated seconds (for future animations)
float screen_height; // logical render target height (px), for resolution-independent scanlines
};
// ============================================================================
@@ -27,7 +27,7 @@ struct PostFXUniforms {
// Accepts PostFXUniforms via fragment uniform buffer slot 0.
// ============================================================================
class GpuPipeline {
public:
public:
// target_format: pass SDL_GetGPUSwapchainTextureFormat() result.
// offscreen_format: format of the offscreen render target.
bool init(SDL_GPUDevice* device,
@@ -39,8 +39,8 @@ public:
SDL_GPUGraphicsPipeline* ballPipeline() const { return ball_pipeline_; }
SDL_GPUGraphicsPipeline* postfxPipeline() const { return postfx_pipeline_; }
private:
SDL_GPUShader* createShader(SDL_GPUDevice* device,
private:
static SDL_GPUShader* createShader(SDL_GPUDevice* device,
const char* msl_source,
const char* entrypoint,
SDL_GPUShaderStage stage,
@@ -48,7 +48,7 @@ private:
Uint32 num_uniform_buffers,
Uint32 num_storage_buffers = 0);
SDL_GPUShader* createShaderSPIRV(SDL_GPUDevice* device,
static SDL_GPUShader* createShaderSPIRV(SDL_GPUDevice* device,
const uint8_t* spv_code,
size_t spv_size,
const char* entrypoint,

View File

@@ -1,274 +0,0 @@
#include "gpu_shader_preset.hpp"
#include "gpu_texture.hpp"
#include <SDL3/SDL_log.h>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <memory>
// ============================================================================
// Helpers
// ============================================================================
static std::vector<uint8_t> readFile(const std::string& path) {
std::ifstream f(path, std::ios::binary | std::ios::ate);
if (!f) return {};
std::streamsize sz = f.tellg();
f.seekg(0, std::ios::beg);
std::vector<uint8_t> buf(static_cast<size_t>(sz));
if (!f.read(reinterpret_cast<char*>(buf.data()), sz)) return {};
return buf;
}
static std::string trim(const std::string& s) {
size_t a = s.find_first_not_of(" \t\r\n");
if (a == std::string::npos) return {};
size_t b = s.find_last_not_of(" \t\r\n");
return s.substr(a, b - a + 1);
}
// ============================================================================
// GpuShaderPreset
// ============================================================================
bool GpuShaderPreset::parseIni(const std::string& ini_path) {
std::ifstream f(ini_path);
if (!f) {
SDL_Log("GpuShaderPreset: cannot open %s", ini_path.c_str());
return false;
}
int num_passes = 0;
std::string line;
while (std::getline(f, line)) {
// Strip comments
auto comment = line.find(';');
if (comment != std::string::npos) line = line.substr(0, comment);
line = trim(line);
if (line.empty()) continue;
auto eq = line.find('=');
if (eq == std::string::npos) continue;
std::string key = trim(line.substr(0, eq));
std::string value = trim(line.substr(eq + 1));
if (key.empty() || value.empty()) continue;
if (key == "name") {
name_ = value;
} else if (key == "passes") {
num_passes = std::stoi(value);
} else {
// Try to parse as float parameter
try {
params_[key] = std::stof(value);
} catch (...) {
// Non-float values stored separately (pass0_vert etc.)
}
}
}
if (num_passes <= 0) {
SDL_Log("GpuShaderPreset: no passes defined in %s", ini_path.c_str());
return false;
}
// Second pass: read per-pass file names
f.clear();
f.seekg(0, std::ios::beg);
descs_.resize(num_passes);
while (std::getline(f, line)) {
auto comment = line.find(';');
if (comment != std::string::npos) line = line.substr(0, comment);
line = trim(line);
if (line.empty()) continue;
auto eq = line.find('=');
if (eq == std::string::npos) continue;
std::string key = trim(line.substr(0, eq));
std::string value = trim(line.substr(eq + 1));
for (int i = 0; i < num_passes; ++i) {
std::string vi = "pass" + std::to_string(i) + "_vert";
std::string fi = "pass" + std::to_string(i) + "_frag";
if (key == vi) descs_[i].vert_name = value;
if (key == fi) descs_[i].frag_name = value;
}
}
// Validate
for (int i = 0; i < num_passes; ++i) {
if (descs_[i].vert_name.empty() || descs_[i].frag_name.empty()) {
SDL_Log("GpuShaderPreset: pass %d missing vert or frag in %s", i, ini_path.c_str());
return false;
}
}
return true;
}
SDL_GPUGraphicsPipeline* GpuShaderPreset::buildPassPipeline(SDL_GPUDevice* device,
const std::string& vert_spv_path,
const std::string& frag_spv_path,
SDL_GPUTextureFormat target_fmt) {
#ifdef __APPLE__
constexpr SDL_GPUShaderFormat kFmt = SDL_GPU_SHADERFORMAT_MSL;
constexpr const char* kEntry = "main0";
#else
constexpr SDL_GPUShaderFormat kFmt = SDL_GPU_SHADERFORMAT_SPIRV;
constexpr const char* kEntry = "main";
#endif
auto vert_spv = readFile(vert_spv_path);
auto frag_spv = readFile(frag_spv_path);
if (vert_spv.empty()) {
SDL_Log("GpuShaderPreset: cannot read %s", vert_spv_path.c_str());
return nullptr;
}
if (frag_spv.empty()) {
SDL_Log("GpuShaderPreset: cannot read %s", frag_spv_path.c_str());
return nullptr;
}
#ifdef __APPLE__
vert_spv.push_back(0);
frag_spv.push_back(0);
#endif
SDL_GPUShaderCreateInfo vert_info = {};
vert_info.code = vert_spv.data();
vert_info.code_size = vert_spv.size();
vert_info.entrypoint = kEntry;
vert_info.format = kFmt;
vert_info.stage = SDL_GPU_SHADERSTAGE_VERTEX;
vert_info.num_samplers = 0;
vert_info.num_uniform_buffers = 0;
SDL_GPUShaderCreateInfo frag_info = {};
frag_info.code = frag_spv.data();
frag_info.code_size = frag_spv.size();
frag_info.entrypoint = kEntry;
frag_info.format = kFmt;
frag_info.stage = SDL_GPU_SHADERSTAGE_FRAGMENT;
frag_info.num_samplers = 1;
frag_info.num_uniform_buffers = 1;
SDL_GPUShader* vert_shader = SDL_CreateGPUShader(device, &vert_info);
SDL_GPUShader* frag_shader = SDL_CreateGPUShader(device, &frag_info);
if (!vert_shader || !frag_shader) {
SDL_Log("GpuShaderPreset: shader creation failed for %s / %s: %s",
vert_spv_path.c_str(), frag_spv_path.c_str(), SDL_GetError());
if (vert_shader) SDL_ReleaseGPUShader(device, vert_shader);
if (frag_shader) SDL_ReleaseGPUShader(device, frag_shader);
return nullptr;
}
// Full-screen triangle: no vertex input, no blend
SDL_GPUColorTargetBlendState no_blend = {};
no_blend.enable_blend = false;
no_blend.enable_color_write_mask = false;
SDL_GPUColorTargetDescription ct_desc = {};
ct_desc.format = target_fmt;
ct_desc.blend_state = no_blend;
SDL_GPUVertexInputState no_input = {};
SDL_GPUGraphicsPipelineCreateInfo pipe_info = {};
pipe_info.vertex_shader = vert_shader;
pipe_info.fragment_shader = frag_shader;
pipe_info.vertex_input_state = no_input;
pipe_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
pipe_info.target_info.num_color_targets = 1;
pipe_info.target_info.color_target_descriptions = &ct_desc;
SDL_GPUGraphicsPipeline* pipeline = SDL_CreateGPUGraphicsPipeline(device, &pipe_info);
SDL_ReleaseGPUShader(device, vert_shader);
SDL_ReleaseGPUShader(device, frag_shader);
if (!pipeline)
SDL_Log("GpuShaderPreset: pipeline creation failed: %s", SDL_GetError());
return pipeline;
}
bool GpuShaderPreset::load(SDL_GPUDevice* device,
const std::string& dir,
SDL_GPUTextureFormat swapchain_fmt,
int w, int h) {
dir_ = dir;
swapchain_fmt_ = swapchain_fmt;
// Parse ini
if (!parseIni(dir + "/preset.ini"))
return false;
int n = static_cast<int>(descs_.size());
passes_.resize(n);
// Intermediate render target format (signed float to handle NTSC signal range)
SDL_GPUTextureFormat inter_fmt = SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT;
for (int i = 0; i < n; ++i) {
bool is_last = (i == n - 1);
SDL_GPUTextureFormat target_fmt = is_last ? swapchain_fmt : inter_fmt;
#ifdef __APPLE__
const char* ext = ".spv.msl";
#else
const char* ext = ".spv";
#endif
std::string vert_spv = dir + "/" + descs_[i].vert_name + ext;
std::string frag_spv = dir + "/" + descs_[i].frag_name + ext;
passes_[i].pipeline = buildPassPipeline(device, vert_spv, frag_spv, target_fmt);
if (!passes_[i].pipeline) {
SDL_Log("GpuShaderPreset: failed to build pipeline for pass %d", i);
return false;
}
if (!is_last) {
// Create intermediate render target
auto tex = std::make_unique<GpuTexture>();
if (!tex->createRenderTarget(device, w, h, inter_fmt)) {
SDL_Log("GpuShaderPreset: failed to create intermediate target for pass %d", i);
return false;
}
passes_[i].target = tex.get();
targets_.push_back(std::move(tex));
}
// Last pass: target = null (caller binds swapchain)
}
SDL_Log("GpuShaderPreset: loaded '%s' (%d passes)", name_.c_str(), n);
return true;
}
void GpuShaderPreset::recreateTargets(SDL_GPUDevice* device, int w, int h) {
SDL_GPUTextureFormat inter_fmt = SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT;
for (auto& tex : targets_) {
tex->destroy(device);
tex->createRenderTarget(device, w, h, inter_fmt);
}
}
void GpuShaderPreset::destroy(SDL_GPUDevice* device) {
for (auto& pass : passes_) {
if (pass.pipeline) {
SDL_ReleaseGPUGraphicsPipeline(device, pass.pipeline);
pass.pipeline = nullptr;
}
}
for (auto& tex : targets_) {
if (tex) tex->destroy(device);
}
targets_.clear();
passes_.clear();
descs_.clear();
params_.clear();
}
float GpuShaderPreset::param(const std::string& key, float default_val) const {
auto it = params_.find(key);
return (it != params_.end()) ? it->second : default_val;
}

View File

@@ -1,94 +0,0 @@
#pragma once
#include <SDL3/SDL_gpu.h>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "gpu_texture.hpp"
// ============================================================================
// NTSCParams — uniform buffer for NTSC shader passes (set=3, binding=0)
// Matches the layout in pass0_encode.frag and pass1_decode.frag.
// Pushed via SDL_PushGPUFragmentUniformData(cmd, 0, &ntsc, sizeof(NTSCParams)).
// ============================================================================
struct NTSCParams {
float source_width;
float source_height;
float a_value;
float b_value;
float cc_value;
float scan_time;
float notch_width;
float y_freq;
float i_freq;
float q_freq;
float _pad[2];
};
static_assert(sizeof(NTSCParams) == 48, "NTSCParams must be 48 bytes");
// ============================================================================
// ShaderPass — one render pass in a multi-pass shader preset
// ============================================================================
struct ShaderPass {
SDL_GPUGraphicsPipeline* pipeline = nullptr;
GpuTexture* target = nullptr; // null = swapchain (last pass)
};
// ============================================================================
// GpuShaderPreset — loads and owns a multi-pass shader preset from disk.
//
// Directory layout:
// <dir>/preset.ini — descriptor
// <dir>/pass0_xxx.vert — GLSL 4.50 vertex shader source
// <dir>/pass0_xxx.frag — GLSL 4.50 fragment shader source
// <dir>/pass0_xxx.vert.spv — compiled SPIRV (by CMake/glslc at build time)
// <dir>/pass0_xxx.frag.spv — compiled SPIRV
// ...
// ============================================================================
class GpuShaderPreset {
public:
// Load preset from directory. swapchain_fmt is the target format for the
// last pass; intermediate passes use R16G16B16A16_FLOAT.
bool load(SDL_GPUDevice* device,
const std::string& dir,
SDL_GPUTextureFormat swapchain_fmt,
int w, int h);
void destroy(SDL_GPUDevice* device);
// Recreate intermediate render targets on resolution change.
void recreateTargets(SDL_GPUDevice* device, int w, int h);
int passCount() const { return static_cast<int>(passes_.size()); }
ShaderPass& pass(int i) { return passes_[i]; }
const std::string& name() const { return name_; }
// Read a float parameter parsed from preset.ini (returns default_val if absent).
float param(const std::string& key, float default_val) const;
private:
std::vector<ShaderPass> passes_;
std::vector<std::unique_ptr<GpuTexture>> targets_; // intermediate render targets
std::string name_;
std::string dir_;
std::unordered_map<std::string, float> params_;
SDL_GPUTextureFormat swapchain_fmt_ = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
// Entries read from preset.ini for each pass
struct PassDesc {
std::string vert_name; // e.g. "pass0_encode.vert"
std::string frag_name; // e.g. "pass0_encode.frag"
};
std::vector<PassDesc> descs_;
bool parseIni(const std::string& ini_path);
// Build a full-screen-triangle pipeline from two on-disk SPV files.
SDL_GPUGraphicsPipeline* buildPassPipeline(SDL_GPUDevice* device,
const std::string& vert_spv_path,
const std::string& frag_spv_path,
SDL_GPUTextureFormat target_fmt);
};

View File

@@ -1,16 +1,19 @@
#include "gpu_sprite_batch.hpp"
#include <SDL3/SDL_log.h>
#include <cstring> // memcpy
// ---------------------------------------------------------------------------
// Public interface
// ---------------------------------------------------------------------------
bool GpuSpriteBatch::init(SDL_GPUDevice* device) {
// Pre-allocate GPU buffers large enough for MAX_SPRITES quads.
Uint32 max_verts = static_cast<Uint32>(MAX_SPRITES) * 4;
Uint32 max_indices = static_cast<Uint32>(MAX_SPRITES) * 6;
auto GpuSpriteBatch::init(SDL_GPUDevice* device, int max_sprites) -> bool {
max_sprites_ = max_sprites;
// Pre-allocate GPU buffers large enough for (max_sprites_ + 2) quads.
// The +2 reserves one slot for the background quad and one for the fullscreen overlay.
Uint32 max_verts = static_cast<Uint32>(max_sprites_ + 2) * 4;
Uint32 max_indices = static_cast<Uint32>(max_sprites_ + 2) * 6;
Uint32 vb_size = max_verts * sizeof(GpuVertex);
Uint32 ib_size = max_indices * sizeof(uint32_t);
@@ -20,7 +23,7 @@ bool GpuSpriteBatch::init(SDL_GPUDevice* device) {
vb_info.usage = SDL_GPU_BUFFERUSAGE_VERTEX;
vb_info.size = vb_size;
vertex_buf_ = SDL_CreateGPUBuffer(device, &vb_info);
if (!vertex_buf_) {
if (vertex_buf_ == nullptr) {
SDL_Log("GpuSpriteBatch: vertex buffer creation failed: %s", SDL_GetError());
return false;
}
@@ -30,7 +33,7 @@ bool GpuSpriteBatch::init(SDL_GPUDevice* device) {
ib_info.usage = SDL_GPU_BUFFERUSAGE_INDEX;
ib_info.size = ib_size;
index_buf_ = SDL_CreateGPUBuffer(device, &ib_info);
if (!index_buf_) {
if (index_buf_ == nullptr) {
SDL_Log("GpuSpriteBatch: index buffer creation failed: %s", SDL_GetError());
return false;
}
@@ -41,29 +44,43 @@ bool GpuSpriteBatch::init(SDL_GPUDevice* device) {
tb_info.size = vb_size;
vertex_transfer_ = SDL_CreateGPUTransferBuffer(device, &tb_info);
if (!vertex_transfer_) {
if (vertex_transfer_ == nullptr) {
SDL_Log("GpuSpriteBatch: vertex transfer buffer failed: %s", SDL_GetError());
return false;
}
tb_info.size = ib_size;
index_transfer_ = SDL_CreateGPUTransferBuffer(device, &tb_info);
if (!index_transfer_) {
if (index_transfer_ == nullptr) {
SDL_Log("GpuSpriteBatch: index transfer buffer failed: %s", SDL_GetError());
return false;
}
vertices_.reserve(MAX_SPRITES * 4);
indices_.reserve(MAX_SPRITES * 6);
vertices_.reserve(static_cast<size_t>(max_sprites_ + 2) * 4);
indices_.reserve(static_cast<size_t>(max_sprites_ + 2) * 6);
return true;
}
void GpuSpriteBatch::destroy(SDL_GPUDevice* device) {
if (!device) return;
if (vertex_transfer_) { SDL_ReleaseGPUTransferBuffer(device, vertex_transfer_); vertex_transfer_ = nullptr; }
if (index_transfer_) { SDL_ReleaseGPUTransferBuffer(device, index_transfer_); index_transfer_ = nullptr; }
if (vertex_buf_) { SDL_ReleaseGPUBuffer(device, vertex_buf_); vertex_buf_ = nullptr; }
if (index_buf_) { SDL_ReleaseGPUBuffer(device, index_buf_); index_buf_ = nullptr; }
if (device == nullptr) {
return;
}
if (vertex_transfer_ != nullptr) {
SDL_ReleaseGPUTransferBuffer(device, vertex_transfer_);
vertex_transfer_ = nullptr;
}
if (index_transfer_ != nullptr) {
SDL_ReleaseGPUTransferBuffer(device, index_transfer_);
index_transfer_ = nullptr;
}
if (vertex_buf_ != nullptr) {
SDL_ReleaseGPUBuffer(device, vertex_buf_);
vertex_buf_ = nullptr;
}
if (index_buf_ != nullptr) {
SDL_ReleaseGPUBuffer(device, index_buf_);
index_buf_ = nullptr;
}
}
void GpuSpriteBatch::beginFrame() {
@@ -76,38 +93,38 @@ void GpuSpriteBatch::beginFrame() {
overlay_index_count_ = 0;
}
void GpuSpriteBatch::addBackground(float screen_w, float screen_h,
float top_r, float top_g, float top_b,
float bot_r, float bot_g, float bot_b) {
void GpuSpriteBatch::addBackground(float screen_w, float screen_h, float top_r, float top_g, float top_b, float bot_r, float bot_g, float bot_b) {
// Background is the full screen quad, corners:
// TL(-1, 1) TR(1, 1) → top color
// BL(-1,-1) BR(1,-1) → bottom color
// We push it as 4 separate vertices (different colors per row).
uint32_t vi = static_cast<uint32_t>(vertices_.size());
auto vi = static_cast<uint32_t>(vertices_.size());
// Top-left
vertices_.push_back({ -1.0f, 1.0f, 0.0f, 0.0f, top_r, top_g, top_b, 1.0f });
vertices_.push_back({-1.0f, 1.0f, 0.0f, 0.0f, top_r, top_g, top_b, 1.0f});
// Top-right
vertices_.push_back({ 1.0f, 1.0f, 1.0f, 0.0f, top_r, top_g, top_b, 1.0f });
vertices_.push_back({1.0f, 1.0f, 1.0f, 0.0f, top_r, top_g, top_b, 1.0f});
// Bottom-right
vertices_.push_back({ 1.0f, -1.0f, 1.0f, 1.0f, bot_r, bot_g, bot_b, 1.0f });
vertices_.push_back({1.0f, -1.0f, 1.0f, 1.0f, bot_r, bot_g, bot_b, 1.0f});
// Bottom-left
vertices_.push_back({ -1.0f, -1.0f, 0.0f, 1.0f, bot_r, bot_g, bot_b, 1.0f });
vertices_.push_back({-1.0f, -1.0f, 0.0f, 1.0f, bot_r, bot_g, bot_b, 1.0f});
// Two triangles: TL-TR-BR, BR-BL-TL
indices_.push_back(vi + 0); indices_.push_back(vi + 1); indices_.push_back(vi + 2);
indices_.push_back(vi + 2); indices_.push_back(vi + 3); indices_.push_back(vi + 0);
indices_.push_back(vi + 0);
indices_.push_back(vi + 1);
indices_.push_back(vi + 2);
indices_.push_back(vi + 2);
indices_.push_back(vi + 3);
indices_.push_back(vi + 0);
bg_index_count_ = 6;
sprite_index_offset_ = 6;
(void)screen_w; (void)screen_h; // unused — bg always covers full NDC
(void)screen_w;
(void)screen_h; // unused — bg always covers full NDC
}
void GpuSpriteBatch::addSprite(float x, float y, float w, float h,
float r, float g, float b, float a,
float scale,
float screen_w, float screen_h) {
void GpuSpriteBatch::addSprite(float x, float y, float w, float h, float r, float g, float b, float a, float scale, float screen_w, float screen_h) {
// Apply scale around the sprite centre
float scaled_w = w * scale;
float scaled_h = h * scale;
@@ -119,7 +136,10 @@ void GpuSpriteBatch::addSprite(float x, float y, float w, float h,
float px1 = px0 + scaled_w;
float py1 = py0 + scaled_h;
float ndx0, ndy0, ndx1, ndy1;
float ndx0;
float ndy0;
float ndx1;
float ndy1;
toNDC(px0, py0, screen_w, screen_h, ndx0, ndy0);
toNDC(px1, py1, screen_w, screen_h, ndx1, ndy1);
@@ -128,37 +148,57 @@ void GpuSpriteBatch::addSprite(float x, float y, float w, float h,
}
void GpuSpriteBatch::addFullscreenOverlay() {
// El overlay es un slot reservado fuera del espacio de max_sprites_, igual que el background.
// Escribe directamente sin pasar por el guard de pushQuad().
overlay_index_offset_ = static_cast<int>(indices_.size());
pushQuad(-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f);
auto vi = static_cast<uint32_t>(vertices_.size());
vertices_.push_back({-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f});
vertices_.push_back({1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f});
vertices_.push_back({1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f});
vertices_.push_back({-1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f});
indices_.push_back(vi + 0);
indices_.push_back(vi + 1);
indices_.push_back(vi + 2);
indices_.push_back(vi + 2);
indices_.push_back(vi + 3);
indices_.push_back(vi + 0);
overlay_index_count_ = 6;
}
bool GpuSpriteBatch::uploadBatch(SDL_GPUDevice* device, SDL_GPUCommandBuffer* cmd_buf) {
if (vertices_.empty()) return false;
auto GpuSpriteBatch::uploadBatch(SDL_GPUDevice* device, SDL_GPUCommandBuffer* cmd_buf) -> bool {
if (vertices_.empty()) {
return false;
}
Uint32 vb_size = static_cast<Uint32>(vertices_.size() * sizeof(GpuVertex));
Uint32 ib_size = static_cast<Uint32>(indices_.size() * sizeof(uint32_t));
auto vb_size = static_cast<Uint32>(vertices_.size() * sizeof(GpuVertex));
auto ib_size = static_cast<Uint32>(indices_.size() * sizeof(uint32_t));
// Map → write → unmap transfer buffers
void* vp = SDL_MapGPUTransferBuffer(device, vertex_transfer_, true /* cycle */);
if (!vp) { SDL_Log("GpuSpriteBatch: vertex map failed"); return false; }
if (vp == nullptr) {
SDL_Log("GpuSpriteBatch: vertex map failed");
return false;
}
memcpy(vp, vertices_.data(), vb_size);
SDL_UnmapGPUTransferBuffer(device, vertex_transfer_);
void* ip = SDL_MapGPUTransferBuffer(device, index_transfer_, true /* cycle */);
if (!ip) { SDL_Log("GpuSpriteBatch: index map failed"); return false; }
if (ip == nullptr) {
SDL_Log("GpuSpriteBatch: index map failed");
return false;
}
memcpy(ip, indices_.data(), ib_size);
SDL_UnmapGPUTransferBuffer(device, index_transfer_);
// Upload via copy pass
SDL_GPUCopyPass* copy = SDL_BeginGPUCopyPass(cmd_buf);
SDL_GPUTransferBufferLocation v_src = { vertex_transfer_, 0 };
SDL_GPUBufferRegion v_dst = { vertex_buf_, 0, vb_size };
SDL_GPUTransferBufferLocation v_src = {vertex_transfer_, 0};
SDL_GPUBufferRegion v_dst = {vertex_buf_, 0, vb_size};
SDL_UploadToGPUBuffer(copy, &v_src, &v_dst, true /* cycle */);
SDL_GPUTransferBufferLocation i_src = { index_transfer_, 0 };
SDL_GPUBufferRegion i_dst = { index_buf_, 0, ib_size };
SDL_GPUTransferBufferLocation i_src = {index_transfer_, 0};
SDL_GPUBufferRegion i_dst = {index_buf_, 0, ib_size};
SDL_UploadToGPUBuffer(copy, &i_src, &i_dst, true /* cycle */);
SDL_EndGPUCopyPass(copy);
@@ -169,25 +209,28 @@ bool GpuSpriteBatch::uploadBatch(SDL_GPUDevice* device, SDL_GPUCommandBuffer* cm
// Private helpers
// ---------------------------------------------------------------------------
void GpuSpriteBatch::toNDC(float px, float py,
float screen_w, float screen_h,
float& ndx, float& ndy) const {
void GpuSpriteBatch::toNDC(float px, float py, float screen_w, float screen_h, float& ndx, float& ndy) {
ndx = (px / screen_w) * 2.0f - 1.0f;
ndy = 1.0f - (py / screen_h) * 2.0f;
}
void GpuSpriteBatch::pushQuad(float ndx0, float ndy0, float ndx1, float ndy1,
float u0, float v0, float u1, float v1,
float r, float g, float b, float a) {
if (vertices_.size() + 4 > static_cast<size_t>(MAX_SPRITES) * 4) return;
uint32_t vi = static_cast<uint32_t>(vertices_.size());
void GpuSpriteBatch::pushQuad(float ndx0, float ndy0, float ndx1, float ndy1, float u0, float v0, float u1, float v1, float r, float g, float b, float a) {
// +1 reserva el slot del background que ya entró sin pasar por este guard.
if (vertices_.size() + 4 > static_cast<size_t>(max_sprites_ + 1) * 4) {
return;
}
auto vi = static_cast<uint32_t>(vertices_.size());
// TL, TR, BR, BL
vertices_.push_back({ ndx0, ndy0, u0, v0, r, g, b, a });
vertices_.push_back({ ndx1, ndy0, u1, v0, r, g, b, a });
vertices_.push_back({ ndx1, ndy1, u1, v1, r, g, b, a });
vertices_.push_back({ ndx0, ndy1, u0, v1, r, g, b, a });
vertices_.push_back({ndx0, ndy0, u0, v0, r, g, b, a});
vertices_.push_back({ndx1, ndy0, u1, v0, r, g, b, a});
vertices_.push_back({ndx1, ndy1, u1, v1, r, g, b, a});
vertices_.push_back({ndx0, ndy1, u0, v1, r, g, b, a});
indices_.push_back(vi + 0); indices_.push_back(vi + 1); indices_.push_back(vi + 2);
indices_.push_back(vi + 2); indices_.push_back(vi + 3); indices_.push_back(vi + 0);
indices_.push_back(vi + 0);
indices_.push_back(vi + 1);
indices_.push_back(vi + 2);
indices_.push_back(vi + 2);
indices_.push_back(vi + 3);
indices_.push_back(vi + 0);
}

View File

@@ -1,8 +1,9 @@
#pragma once
#include <SDL3/SDL_gpu.h>
#include <vector>
#include <cstdint>
#include <vector>
// ---------------------------------------------------------------------------
// GpuVertex — 8-float vertex layout sent to the GPU.
@@ -25,27 +26,22 @@ struct GpuVertex {
// // Then in render pass: bind buffers, draw bg with white tex, draw sprites.
// ============================================================================
class GpuSpriteBatch {
public:
// Maximum sprites (background + UI overlay each count as one sprite)
static constexpr int MAX_SPRITES = 200000;
public:
// Default maximum sprites (background + UI overlay each count as one sprite)
static constexpr int DEFAULT_MAX_SPRITES = 200000;
bool init(SDL_GPUDevice* device);
bool init(SDL_GPUDevice* device, int max_sprites = DEFAULT_MAX_SPRITES);
void destroy(SDL_GPUDevice* device);
void beginFrame();
// Add the full-screen background gradient quad.
// top_* and bot_* are RGB in [0,1].
void addBackground(float screen_w, float screen_h,
float top_r, float top_g, float top_b,
float bot_r, float bot_g, float bot_b);
void addBackground(float screen_w, float screen_h, float top_r, float top_g, float top_b, float bot_r, float bot_g, float bot_b);
// Add a sprite quad (pixel coordinates).
// scale: uniform scale around the quad centre.
void addSprite(float x, float y, float w, float h,
float r, float g, float b, float a,
float scale,
float screen_w, float screen_h);
void addSprite(float x, float y, float w, float h, float r, float g, float b, float a, float scale, float screen_w, float screen_h);
// Add a full-screen overlay quad (e.g. UI surface, NDC 1..1).
void addFullscreenOverlay();
@@ -63,12 +59,9 @@ public:
int spriteIndexCount() const { return sprite_index_count_; }
bool isEmpty() const { return vertices_.empty(); }
private:
void toNDC(float px, float py, float screen_w, float screen_h,
float& ndx, float& ndy) const;
void pushQuad(float ndx0, float ndy0, float ndx1, float ndy1,
float u0, float v0, float u1, float v1,
float r, float g, float b, float a);
private:
static void toNDC(float px, float py, float screen_w, float screen_h, float& ndx, float& ndy);
void pushQuad(float ndx0, float ndy0, float ndx1, float ndy1, float u0, float v0, float u1, float v1, float r, float g, float b, float a);
std::vector<GpuVertex> vertices_;
std::vector<uint32_t> indices_;
@@ -83,4 +76,6 @@ private:
int sprite_index_count_ = 0;
int overlay_index_offset_ = 0;
int overlay_index_count_ = 0;
int max_sprites_ = DEFAULT_MAX_SPRITES;
};

View File

@@ -2,6 +2,8 @@
#include <SDL3/SDL_log.h>
#include <SDL3/SDL_pixels.h>
#include <array> // for std::array
#include <cstring> // memcpy
#include <string>
@@ -13,7 +15,7 @@
// Public interface
// ---------------------------------------------------------------------------
bool GpuTexture::fromFile(SDL_GPUDevice* device, const std::string& file_path) {
auto GpuTexture::fromFile(SDL_GPUDevice* device, const std::string& file_path) -> bool {
unsigned char* resource_data = nullptr;
size_t resource_size = 0;
@@ -22,15 +24,22 @@ bool GpuTexture::fromFile(SDL_GPUDevice* device, const std::string& file_path) {
return false;
}
int w = 0, h = 0, orig = 0;
int w = 0;
int h = 0;
int orig = 0;
unsigned char* pixels = stbi_load_from_memory(
resource_data, static_cast<int>(resource_size),
&w, &h, &orig, STBI_rgb_alpha);
resource_data,
static_cast<int>(resource_size),
&w,
&h,
&orig,
STBI_rgb_alpha);
delete[] resource_data;
if (!pixels) {
if (pixels == nullptr) {
SDL_Log("GpuTexture: stbi decode failed for '%s': %s",
file_path.c_str(), stbi_failure_reason());
file_path.c_str(),
stbi_failure_reason());
return false;
}
@@ -44,15 +53,17 @@ bool GpuTexture::fromFile(SDL_GPUDevice* device, const std::string& file_path) {
return ok;
}
bool GpuTexture::fromSurface(SDL_GPUDevice* device, SDL_Surface* surface, bool nearest) {
if (!surface) return false;
auto GpuTexture::fromSurface(SDL_GPUDevice* device, SDL_Surface* surface, bool nearest) -> bool {
if (surface == nullptr) {
return false;
}
// Ensure RGBA32 format
SDL_Surface* rgba = surface;
bool need_free = false;
if (surface->format != SDL_PIXELFORMAT_RGBA32) {
rgba = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
if (!rgba) {
if (rgba == nullptr) {
SDL_Log("GpuTexture: SDL_ConvertSurface failed: %s", SDL_GetError());
return false;
}
@@ -60,23 +71,24 @@ bool GpuTexture::fromSurface(SDL_GPUDevice* device, SDL_Surface* surface, bool n
}
destroy(device);
bool ok = uploadPixels(device, rgba->pixels, rgba->w, rgba->h,
SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM);
if (ok) ok = createSampler(device, nearest);
bool ok = uploadPixels(device, rgba->pixels, rgba->w, rgba->h, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM);
if (ok) {
ok = createSampler(device, nearest);
}
if (need_free) SDL_DestroySurface(rgba);
if (need_free) {
SDL_DestroySurface(rgba);
}
return ok;
}
bool GpuTexture::createRenderTarget(SDL_GPUDevice* device, int w, int h,
SDL_GPUTextureFormat format) {
auto GpuTexture::createRenderTarget(SDL_GPUDevice* device, int w, int h, SDL_GPUTextureFormat format) -> bool {
destroy(device);
SDL_GPUTextureCreateInfo info = {};
info.type = SDL_GPU_TEXTURETYPE_2D;
info.format = format;
info.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET
| SDL_GPU_TEXTUREUSAGE_SAMPLER;
info.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER;
info.width = static_cast<Uint32>(w);
info.height = static_cast<Uint32>(h);
info.layer_count_or_depth = 1;
@@ -84,7 +96,7 @@ bool GpuTexture::createRenderTarget(SDL_GPUDevice* device, int w, int h,
info.sample_count = SDL_GPU_SAMPLECOUNT_1;
texture_ = SDL_CreateGPUTexture(device, &info);
if (!texture_) {
if (texture_ == nullptr) {
SDL_Log("GpuTexture: createRenderTarget failed: %s", SDL_GetError());
return false;
}
@@ -95,20 +107,29 @@ bool GpuTexture::createRenderTarget(SDL_GPUDevice* device, int w, int h,
return createSampler(device, false);
}
bool GpuTexture::createWhite(SDL_GPUDevice* device) {
auto GpuTexture::createWhite(SDL_GPUDevice* device) -> bool {
destroy(device);
// 1×1 white RGBA pixel
const Uint8 white[4] = {255, 255, 255, 255};
bool ok = uploadPixels(device, white, 1, 1,
SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM);
if (ok) ok = createSampler(device, true);
constexpr std::array<Uint8, 4> WHITE = {255, 255, 255, 255};
bool ok = uploadPixels(device, WHITE.data(), 1, 1, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM);
if (ok) {
ok = createSampler(device, true);
}
return ok;
}
void GpuTexture::destroy(SDL_GPUDevice* device) {
if (!device) return;
if (sampler_) { SDL_ReleaseGPUSampler(device, sampler_); sampler_ = nullptr; }
if (texture_) { SDL_ReleaseGPUTexture(device, texture_); texture_ = nullptr; }
if (device == nullptr) {
return;
}
if (sampler_ != nullptr) {
SDL_ReleaseGPUSampler(device, sampler_);
sampler_ = nullptr;
}
if (texture_ != nullptr) {
SDL_ReleaseGPUTexture(device, texture_);
texture_ = nullptr;
}
width_ = height_ = 0;
}
@@ -116,8 +137,7 @@ void GpuTexture::destroy(SDL_GPUDevice* device) {
// Private helpers
// ---------------------------------------------------------------------------
bool GpuTexture::uploadPixels(SDL_GPUDevice* device, const void* pixels,
int w, int h, SDL_GPUTextureFormat format) {
auto GpuTexture::uploadPixels(SDL_GPUDevice* device, const void* pixels, int w, int h, SDL_GPUTextureFormat format) -> bool {
// Create GPU texture
SDL_GPUTextureCreateInfo tex_info = {};
tex_info.type = SDL_GPU_TEXTURETYPE_2D;
@@ -130,20 +150,20 @@ bool GpuTexture::uploadPixels(SDL_GPUDevice* device, const void* pixels,
tex_info.sample_count = SDL_GPU_SAMPLECOUNT_1;
texture_ = SDL_CreateGPUTexture(device, &tex_info);
if (!texture_) {
if (texture_ == nullptr) {
SDL_Log("GpuTexture: SDL_CreateGPUTexture failed: %s", SDL_GetError());
return false;
}
// Create transfer buffer and upload pixels
Uint32 data_size = static_cast<Uint32>(w * h * 4); // RGBA = 4 bytes/pixel
auto data_size = static_cast<Uint32>(w * h * 4); // RGBA = 4 bytes/pixel
SDL_GPUTransferBufferCreateInfo tb_info = {};
tb_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
tb_info.size = data_size;
SDL_GPUTransferBuffer* transfer = SDL_CreateGPUTransferBuffer(device, &tb_info);
if (!transfer) {
if (transfer == nullptr) {
SDL_Log("GpuTexture: transfer buffer creation failed: %s", SDL_GetError());
SDL_ReleaseGPUTexture(device, texture_);
texture_ = nullptr;
@@ -151,7 +171,7 @@ bool GpuTexture::uploadPixels(SDL_GPUDevice* device, const void* pixels,
}
void* mapped = SDL_MapGPUTransferBuffer(device, transfer, false);
if (!mapped) {
if (mapped == nullptr) {
SDL_Log("GpuTexture: map failed: %s", SDL_GetError());
SDL_ReleaseGPUTransferBuffer(device, transfer);
SDL_ReleaseGPUTexture(device, texture_);
@@ -190,7 +210,7 @@ bool GpuTexture::uploadPixels(SDL_GPUDevice* device, const void* pixels,
return true;
}
bool GpuTexture::createSampler(SDL_GPUDevice* device, bool nearest) {
auto GpuTexture::createSampler(SDL_GPUDevice* device, bool nearest) -> bool {
SDL_GPUSamplerCreateInfo info = {};
info.min_filter = nearest ? SDL_GPU_FILTER_NEAREST : SDL_GPU_FILTER_LINEAR;
info.mag_filter = nearest ? SDL_GPU_FILTER_NEAREST : SDL_GPU_FILTER_LINEAR;
@@ -200,7 +220,7 @@ bool GpuTexture::createSampler(SDL_GPUDevice* device, bool nearest) {
info.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
sampler_ = SDL_CreateGPUSampler(device, &info);
if (!sampler_) {
if (sampler_ == nullptr) {
SDL_Log("GpuTexture: SDL_CreateGPUSampler failed: %s", SDL_GetError());
return false;
}

View File

@@ -2,6 +2,7 @@
#include <SDL3/SDL_gpu.h>
#include <SDL3/SDL_surface.h>
#include <string>
// ============================================================================
@@ -9,7 +10,7 @@
// Handles sprite textures, render targets, and the 1×1 white utility texture.
// ============================================================================
class GpuTexture {
public:
public:
GpuTexture() = default;
~GpuTexture() = default;
@@ -21,8 +22,7 @@ public:
bool fromSurface(SDL_GPUDevice* device, SDL_Surface* surface, bool nearest = true);
// Create an offscreen render target (COLOR_TARGET | SAMPLER usage).
bool createRenderTarget(SDL_GPUDevice* device, int w, int h,
SDL_GPUTextureFormat format);
bool createRenderTarget(SDL_GPUDevice* device, int w, int h, SDL_GPUTextureFormat format);
// Create a 1×1 opaque white texture (used for untextured geometry).
bool createWhite(SDL_GPUDevice* device);
@@ -36,9 +36,8 @@ public:
int height() const { return height_; }
bool isValid() const { return texture_ != nullptr; }
private:
bool uploadPixels(SDL_GPUDevice* device, const void* pixels,
int w, int h, SDL_GPUTextureFormat format);
private:
bool uploadPixels(SDL_GPUDevice* device, const void* pixels, int w, int h, SDL_GPUTextureFormat format);
bool createSampler(SDL_GPUDevice* device, bool nearest);
SDL_GPUTexture* texture_ = nullptr;

View File

@@ -1,68 +0,0 @@
#include "shader_manager.hpp"
#include <SDL3/SDL_log.h>
#include <filesystem>
#include <algorithm>
namespace fs = std::filesystem;
void ShaderManager::scan(const std::string& root_dir) {
root_dir_ = root_dir;
names_.clear();
dirs_.clear();
std::error_code ec;
for (const auto& entry : fs::directory_iterator(root_dir, ec)) {
if (!entry.is_directory()) continue;
fs::path ini = entry.path() / "preset.ini";
if (!fs::exists(ini)) continue;
std::string preset_name = entry.path().filename().string();
names_.push_back(preset_name);
dirs_[preset_name] = entry.path().string();
}
if (ec) {
SDL_Log("ShaderManager: scan error on %s: %s", root_dir.c_str(), ec.message().c_str());
}
std::sort(names_.begin(), names_.end());
SDL_Log("ShaderManager: found %d preset(s) in %s", (int)names_.size(), root_dir.c_str());
}
GpuShaderPreset* ShaderManager::load(SDL_GPUDevice* device,
const std::string& name,
SDL_GPUTextureFormat swapchain_fmt,
int w, int h) {
auto it = loaded_.find(name);
if (it != loaded_.end()) return it->second.get();
auto dir_it = dirs_.find(name);
if (dir_it == dirs_.end()) {
SDL_Log("ShaderManager: preset '%s' not found", name.c_str());
return nullptr;
}
auto preset = std::make_unique<GpuShaderPreset>();
if (!preset->load(device, dir_it->second, swapchain_fmt, w, h)) {
SDL_Log("ShaderManager: failed to load preset '%s'", name.c_str());
return nullptr;
}
GpuShaderPreset* raw = preset.get();
loaded_[name] = std::move(preset);
return raw;
}
void ShaderManager::onResize(SDL_GPUDevice* device, SDL_GPUTextureFormat /*swapchain_fmt*/, int w, int h) {
for (auto& [name, preset] : loaded_) {
preset->recreateTargets(device, w, h);
}
}
void ShaderManager::destroyAll(SDL_GPUDevice* device) {
for (auto& [name, preset] : loaded_) {
preset->destroy(device);
}
loaded_.clear();
}

View File

@@ -1,41 +0,0 @@
#pragma once
#include <SDL3/SDL_gpu.h>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "gpu_shader_preset.hpp"
// ============================================================================
// ShaderManager — discovers and manages runtime shader presets under
// a root directory (e.g., data/shaders/).
//
// Each subdirectory with a preset.ini is treated as a shader preset.
// ============================================================================
class ShaderManager {
public:
// Scan root_dir for preset subdirectories (each must contain preset.ini).
void scan(const std::string& root_dir);
// Available preset names (e.g. {"ntsc-md-rainbows"}).
const std::vector<std::string>& names() const { return names_; }
// Load and return a preset (cached). Returns null on failure.
GpuShaderPreset* load(SDL_GPUDevice* device,
const std::string& name,
SDL_GPUTextureFormat swapchain_fmt,
int w, int h);
// Recreate intermediate render targets on resolution change.
void onResize(SDL_GPUDevice* device, SDL_GPUTextureFormat swapchain_fmt, int w, int h);
void destroyAll(SDL_GPUDevice* device);
private:
std::string root_dir_;
std::vector<std::string> names_;
std::map<std::string, std::string> dirs_;
std::map<std::string, std::unique_ptr<GpuShaderPreset>> loaded_;
};

View File

@@ -1,13 +1,14 @@
#include "input_handler.hpp"
#include <SDL3/SDL_keycode.h> // for SDL_Keycode
#include <string> // for std::string, std::to_string
#include "defines.hpp" // for KIOSK_NOTIFICATION_TEXT
#include "engine.hpp" // for Engine
#include "external/mouse.hpp" // for Mouse namespace
bool InputHandler::processEvents(Engine& engine) {
auto InputHandler::processEvents(Engine& engine) -> bool { // NOLINT(readability-function-cognitive-complexity)
SDL_Event event;
while (SDL_PollEvent(&event)) {
// Procesar eventos de ratón (auto-ocultar cursor)
@@ -19,7 +20,7 @@ bool InputHandler::processEvents(Engine& engine) {
}
// Procesar eventos de teclado
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0) {
if (event.type == SDL_EVENT_KEY_DOWN && static_cast<int>(event.key.repeat) == 0) {
switch (event.key.key) {
case SDLK_ESCAPE:
if (engine.isKioskMode()) {
@@ -38,19 +39,19 @@ bool InputHandler::processEvents(Engine& engine) {
// Controles de dirección de gravedad con teclas de cursor
case SDLK_UP:
engine.handleGravityDirectionChange(GravityDirection::UP, "Gravedad Arriba");
engine.handleGravityDirectionChange(GravityDirection::UP, "Gravedad arriba");
break;
case SDLK_DOWN:
engine.handleGravityDirectionChange(GravityDirection::DOWN, "Gravedad Abajo");
engine.handleGravityDirectionChange(GravityDirection::DOWN, "Gravedad abajo");
break;
case SDLK_LEFT:
engine.handleGravityDirectionChange(GravityDirection::LEFT, "Gravedad Izquierda");
engine.handleGravityDirectionChange(GravityDirection::LEFT, "Gravedad izquierda");
break;
case SDLK_RIGHT:
engine.handleGravityDirectionChange(GravityDirection::RIGHT, "Gravedad Derecha");
engine.handleGravityDirectionChange(GravityDirection::RIGHT, "Gravedad derecha");
break;
case SDLK_V:
@@ -109,19 +110,17 @@ bool InputHandler::processEvents(Engine& engine) {
break;
// Ciclar temas de color (movido de B a C)
case SDLK_C:
{
case SDLK_C: {
// Detectar si Shift está presionado
SDL_Keymod modstate = SDL_GetModState();
if (modstate & SDL_KMOD_SHIFT) {
if ((modstate & SDL_KMOD_SHIFT) != 0u) {
// Shift+C: Ciclar hacia atrás (tema anterior)
engine.cycleTheme(false);
} else {
// C solo: Ciclar hacia adelante (tema siguiente)
engine.cycleTheme(true);
}
}
break;
} break;
// Temas de colores con teclado numérico (con transición suave)
case SDLK_KP_1:
@@ -193,65 +192,77 @@ bool InputHandler::processEvents(Engine& engine) {
// Cambio de número de pelotas (escenarios 1-8)
case SDLK_1:
engine.changeScenario(0, "10 Pelotas");
engine.changeScenario(0, "10 pelotas");
break;
case SDLK_2:
engine.changeScenario(1, "50 Pelotas");
engine.changeScenario(1, "50 pelotas");
break;
case SDLK_3:
engine.changeScenario(2, "100 Pelotas");
engine.changeScenario(2, "100 pelotas");
break;
case SDLK_4:
engine.changeScenario(3, "500 Pelotas");
engine.changeScenario(3, "500 pelotas");
break;
case SDLK_5:
engine.changeScenario(4, "1,000 Pelotas");
engine.changeScenario(4, "1.000 pelotas");
break;
case SDLK_6:
engine.changeScenario(5, "5,000 Pelotas");
engine.changeScenario(5, "5.000 pelotas");
break;
case SDLK_7:
engine.changeScenario(6, "10,000 Pelotas");
engine.changeScenario(6, "10.000 pelotas");
break;
case SDLK_8:
engine.changeScenario(7, "50,000 Pelotas");
engine.changeScenario(7, "50.000 pelotas");
break;
case SDLK_9:
if (engine.isCustomScenarioEnabled()) {
std::string custom_notif = std::to_string(engine.getCustomScenarioBalls()) + " Pelotas";
std::string custom_notif = std::to_string(engine.getCustomScenarioBalls()) + " pelotas";
engine.changeScenario(CUSTOM_SCENARIO_IDX, custom_notif.c_str());
}
break;
// Controles de zoom dinámico (solo si no estamos en fullscreen)
case SDLK_F1:
if (engine.isKioskMode()) engine.showNotificationForAction(KIOSK_NOTIFICATION_TEXT);
else engine.handleZoomOut();
if (engine.isKioskMode()) {
engine.showNotificationForAction(KIOSK_NOTIFICATION_TEXT);
} else {
engine.handleZoomOut();
}
break;
case SDLK_F2:
if (engine.isKioskMode()) engine.showNotificationForAction(KIOSK_NOTIFICATION_TEXT);
else engine.handleZoomIn();
if (engine.isKioskMode()) {
engine.showNotificationForAction(KIOSK_NOTIFICATION_TEXT);
} else {
engine.handleZoomIn();
}
break;
// Control de pantalla completa
case SDLK_F3:
if (engine.isKioskMode()) engine.showNotificationForAction(KIOSK_NOTIFICATION_TEXT);
else engine.toggleFullscreen();
if (engine.isKioskMode()) {
engine.showNotificationForAction(KIOSK_NOTIFICATION_TEXT);
} else {
engine.toggleFullscreen();
}
break;
// Modo real fullscreen (cambia resolución interna)
case SDLK_F4:
if (engine.isKioskMode()) engine.showNotificationForAction(KIOSK_NOTIFICATION_TEXT);
else engine.toggleRealFullscreen();
if (engine.isKioskMode()) {
engine.showNotificationForAction(KIOSK_NOTIFICATION_TEXT);
} else {
engine.toggleRealFullscreen();
}
break;
// Toggle PostFX activo/inactivo
@@ -264,10 +275,27 @@ bool InputHandler::processEvents(Engine& engine) {
engine.toggleIntegerScaling();
break;
// Redimensionar campo de juego (tamaño lógico + físico)
case SDLK_F7:
if (engine.isKioskMode()) {
engine.showNotificationForAction(KIOSK_NOTIFICATION_TEXT);
} else {
engine.fieldSizeDown();
}
break;
case SDLK_F8:
if (engine.isKioskMode()) {
engine.showNotificationForAction(KIOSK_NOTIFICATION_TEXT);
} else {
engine.fieldSizeUp();
}
break;
// Toggle Modo DEMO COMPLETO (auto-play) o Pausar tema dinámico (Shift+D)
case SDLK_D:
// Shift+D = Pausar tema dinámico
if (event.key.mod & SDL_KMOD_SHIFT) {
if ((event.key.mod & SDL_KMOD_SHIFT) != 0u) {
engine.pauseDynamicTheme();
} else {
// D sin Shift = Toggle DEMO ↔ SANDBOX

View File

@@ -24,7 +24,7 @@ class InputHandler {
* @param engine Referencia al engine para ejecutar acciones
* @return true si se debe salir de la aplicación (ESC o cerrar ventana), false en caso contrario
*/
bool processEvents(Engine& engine);
static bool processEvents(Engine& engine);
private:
// Sin estado interno por ahora - el InputHandler es stateless

View File

@@ -1,8 +1,9 @@
#include <iostream>
#include <cstring>
#include <iostream>
#include <string>
#include "engine.hpp"
#include "defines.hpp"
#include "engine.hpp"
#include "resource_manager.hpp"
// getExecutableDirectory() ya está definido en defines.h como inline
@@ -11,9 +12,9 @@ void printHelp() {
std::cout << "ViBe3 Physics - Simulador de físicas avanzadas\n";
std::cout << "\nUso: vibe3_physics [opciones]\n\n";
std::cout << "Opciones:\n";
std::cout << " -w, --width <px> Ancho de resolución (default: 320)\n";
std::cout << " -h, --height <px> Alto de resolución (default: 240)\n";
std::cout << " -z, --zoom <n> Zoom de ventana (default: 3)\n";
std::cout << " -w, --width <px> Ancho de resolución (default: " << DEFAULT_SCREEN_WIDTH << ")\n";
std::cout << " -h, --height <px> Alto de resolución (default: " << DEFAULT_SCREEN_HEIGHT << ")\n";
std::cout << " -z, --zoom <n> Zoom de ventana (default: " << DEFAULT_WINDOW_ZOOM << ")\n";
std::cout << " -f, --fullscreen Modo pantalla completa (F3 - letterbox)\n";
std::cout << " -F, --real-fullscreen Modo pantalla completa real (F4 - nativo)\n";
std::cout << " -k, --kiosk Modo kiosko (F4 fijo, sin ESC, sin zoom)\n";
@@ -24,10 +25,9 @@ void printHelp() {
std::cout << " --postfx [efecto] Arrancar con PostFX activo (default: complet): vinyeta, scanlines, cromatica, complet\n";
std::cout << " --vignette <float> Sobreescribir vignette_strength (activa PostFX si no hay --postfx)\n";
std::cout << " --chroma <float> Sobreescribir chroma_strength (activa PostFX si no hay --postfx)\n";
std::cout << " --shader <nombre> Arrancar con shader externo (ej: ntsc-md-rainbows)\n";
std::cout << " --help Mostrar esta ayuda\n\n";
std::cout << "Ejemplos:\n";
std::cout << " vibe3_physics # 320x240 zoom 3 (ventana 960x720)\n";
std::cout << " vibe3_physics # " << DEFAULT_SCREEN_WIDTH << "x" << DEFAULT_SCREEN_HEIGHT << " zoom " << DEFAULT_WINDOW_ZOOM << " (default)\n";
std::cout << " vibe3_physics -w 1920 -h 1080 # 1920x1080 zoom 1 (auto)\n";
std::cout << " vibe3_physics -w 640 -h 480 -z 2 # 640x480 zoom 2 (ventana 1280x960)\n";
std::cout << " vibe3_physics -f # Fullscreen letterbox (F3)\n";
@@ -39,7 +39,7 @@ void printHelp() {
std::cout << "Nota: Si resolución > pantalla, se usa default. Zoom se ajusta automáticamente.\n";
}
int main(int argc, char* argv[]) {
auto main(int argc, char* argv[]) -> int { // NOLINT(readability-function-cognitive-complexity)
int width = 0;
int height = 0;
int zoom = 0;
@@ -52,7 +52,6 @@ int main(int argc, char* argv[]) {
int initial_postfx = -1;
float override_vignette = -1.f;
float override_chroma = -1.f;
std::string initial_shader;
AppMode initial_mode = AppMode::SANDBOX; // Modo inicial (default: SANDBOX)
// Parsear argumentos
@@ -60,7 +59,8 @@ int main(int argc, char* argv[]) {
if (strcmp(argv[i], "--help") == 0) {
printHelp();
return 0;
} else if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "--width") == 0) {
}
if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "--width") == 0) {
if (i + 1 < argc) {
width = atoi(argv[++i]);
if (width < 320) {
@@ -177,13 +177,6 @@ int main(int argc, char* argv[]) {
std::cerr << "Error: --max-balls requiere un valor\n";
return -1;
}
} else if (strcmp(argv[i], "--shader") == 0) {
if (i + 1 < argc) {
initial_shader = argv[++i];
} else {
std::cerr << "Error: --shader requiere un nombre de preset\n";
return -1;
}
} else {
std::cerr << "Error: Opción desconocida '" << argv[i] << "'\n";
printHelp();
@@ -198,28 +191,29 @@ int main(int argc, char* argv[]) {
Engine engine;
if (custom_balls > 0)
if (custom_balls > 0) {
engine.setCustomScenario(custom_balls); // pre-init: asigna campos antes del benchmark
}
if (max_balls_override > 0)
if (max_balls_override > 0) {
engine.setMaxBallsOverride(max_balls_override);
else if (skip_benchmark)
} else if (skip_benchmark) {
engine.setSkipBenchmark();
}
if (initial_postfx >= 0)
if (initial_postfx >= 0) {
engine.setInitialPostFX(initial_postfx);
}
if (override_vignette >= 0.f || override_chroma >= 0.f) {
if (initial_postfx < 0)
if (initial_postfx < 0) {
engine.setInitialPostFX(0);
}
engine.setPostFXParamOverrides(override_vignette, override_chroma);
}
if (!initial_shader.empty())
engine.setInitialShader(initial_shader);
if (!engine.initialize(width, height, zoom, fullscreen, initial_mode)) {
std::cout << "¡Error al inicializar el engine!" << std::endl;
std::cout << "¡Error al inicializar el engine!" << '\n';
return -1;
}

View File

@@ -1,15 +1,16 @@
#include "resource_manager.hpp"
#include "resource_pack.hpp"
#include <iostream>
#include <fstream>
#include <cstring>
#include <fstream>
#include <iostream>
#include "resource_pack.hpp"
// Inicializar estáticos
ResourcePack* ResourceManager::resourcePack_ = nullptr;
std::map<std::string, std::vector<unsigned char>> ResourceManager::cache_;
bool ResourceManager::init(const std::string& packFilePath) {
auto ResourceManager::init(const std::string& pack_file_path) -> bool {
// Si ya estaba inicializado, liberar primero
if (resourcePack_ != nullptr) {
delete resourcePack_;
@@ -18,15 +19,15 @@ bool ResourceManager::init(const std::string& packFilePath) {
// Intentar cargar el pack
resourcePack_ = new ResourcePack();
if (!resourcePack_->loadPack(packFilePath)) {
if (!resourcePack_->loadPack(pack_file_path)) {
// Si falla, borrar instancia (usará fallback a disco)
delete resourcePack_;
resourcePack_ = nullptr;
std::cout << "resources.pack no encontrado - usando carpeta data/" << std::endl;
std::cout << "resources.pack no encontrado - usando carpeta data/" << '\n';
return false;
}
std::cout << "resources.pack cargado (" << resourcePack_->getResourceCount() << " recursos)" << std::endl;
std::cout << "resources.pack cargado (" << resourcePack_->getResourceCount() << " recursos)" << '\n';
return true;
}
@@ -38,12 +39,12 @@ void ResourceManager::shutdown() {
}
}
bool ResourceManager::loadResource(const std::string& resourcePath, unsigned char*& data, size_t& size) {
auto ResourceManager::loadResource(const std::string& resource_path, unsigned char*& data, size_t& size) -> bool {
data = nullptr;
size = 0;
// 1. Consultar caché en RAM (sin I/O)
auto it = cache_.find(resourcePath);
auto it = cache_.find(resource_path);
if (it != cache_.end()) {
size = it->second.size();
data = new unsigned char[size];
@@ -53,20 +54,20 @@ bool ResourceManager::loadResource(const std::string& resourcePath, unsigned cha
// 2. Intentar cargar desde pack (si está disponible)
if (resourcePack_ != nullptr) {
ResourcePack::ResourceData packData = resourcePack_->loadResource(resourcePath);
if (packData.data != nullptr) {
cache_[resourcePath] = std::vector<unsigned char>(packData.data, packData.data + packData.size);
data = packData.data;
size = packData.size;
ResourcePack::ResourceData pack_data = resourcePack_->loadResource(resource_path);
if (pack_data.data != nullptr) {
cache_[resource_path] = std::vector<unsigned char>(pack_data.data, pack_data.data + pack_data.size);
data = pack_data.data;
size = pack_data.size;
return true;
}
}
// 3. Fallback: cargar desde disco
std::ifstream file(resourcePath, std::ios::binary | std::ios::ate);
std::ifstream file(resource_path, std::ios::binary | std::ios::ate);
if (!file) {
std::string dataPath = "data/" + resourcePath;
file.open(dataPath, std::ios::binary | std::ios::ate);
std::string data_path = "data/" + resource_path;
file.open(data_path, std::ios::binary | std::ios::ate);
if (!file) { return false; }
}
size = static_cast<size_t>(file.tellg());
@@ -76,22 +77,22 @@ bool ResourceManager::loadResource(const std::string& resourcePath, unsigned cha
file.close();
// Guardar en caché
cache_[resourcePath] = std::vector<unsigned char>(data, data + size);
cache_[resource_path] = std::vector<unsigned char>(data, data + size);
return true;
}
bool ResourceManager::isPackLoaded() {
auto ResourceManager::isPackLoaded() -> bool {
return resourcePack_ != nullptr;
}
std::vector<std::string> ResourceManager::getResourceList() {
auto ResourceManager::getResourceList() -> std::vector<std::string> {
if (resourcePack_ != nullptr) {
return resourcePack_->getResourceList();
}
return std::vector<std::string>(); // Vacío si no hay pack
return {}; // Vacío si no hay pack
}
size_t ResourceManager::getResourceCount() {
auto ResourceManager::getResourceCount() -> size_t {
if (resourcePack_ != nullptr) {
return resourcePack_->getResourceCount();
}

View File

@@ -25,7 +25,7 @@ class ResourcePack;
* }
*/
class ResourceManager {
public:
public:
/**
* Inicializa el sistema de recursos empaquetados
* Debe llamarse una única vez al inicio del programa
@@ -33,7 +33,7 @@ public:
* @param packFilePath Ruta al archivo .pack (ej: "resources.pack")
* @return true si el pack se cargó correctamente, false si no existe (fallback a disco)
*/
static bool init(const std::string& packFilePath);
static bool init(const std::string& pack_file_path);
/**
* Libera el sistema de recursos
@@ -49,7 +49,7 @@ public:
* @param size [out] Tamaño del buffer en bytes
* @return true si se cargó correctamente, false si falla
*/
static bool loadResource(const std::string& resourcePath, unsigned char*& data, size_t& size);
static bool loadResource(const std::string& resource_path, unsigned char*& data, size_t& size);
/**
* Verifica si el pack está cargado
@@ -69,7 +69,7 @@ public:
*/
static size_t getResourceCount();
private:
private:
// Constructor privado (singleton)
ResourceManager() = default;
~ResourceManager() = default;

View File

@@ -7,10 +7,8 @@
namespace fs = std::filesystem;
// Clave XOR para ofuscación simple (puede cambiarse)
constexpr uint8_t XOR_KEY = 0x5A;
ResourcePack::ResourcePack() : isLoaded_(false) {}
ResourcePack::ResourcePack()
: isLoaded_(false) {}
ResourcePack::~ResourcePack() {
clear();
@@ -20,54 +18,54 @@ ResourcePack::~ResourcePack() {
// EMPAQUETADO (herramienta pack_resources)
// ============================================================================
bool ResourcePack::addDirectory(const std::string& dirPath, const std::string& prefix) {
if (!fs::exists(dirPath) || !fs::is_directory(dirPath)) {
std::cerr << "Error: Directorio no existe: " << dirPath << std::endl;
auto ResourcePack::addDirectory(const std::string& dir_path, const std::string& prefix) -> bool {
if (!fs::exists(dir_path) || !fs::is_directory(dir_path)) {
std::cerr << "Error: Directorio no existe: " << dir_path << '\n';
return false;
}
for (const auto& entry : fs::recursive_directory_iterator(dirPath)) {
for (const auto& entry : fs::recursive_directory_iterator(dir_path)) {
if (entry.is_regular_file()) {
// Construir ruta relativa desde data/ (ej: "data/ball.png" → "ball.png")
std::string relativePath = fs::relative(entry.path(), dirPath).string();
std::string fullPath = prefix.empty() ? relativePath : prefix + "/" + relativePath;
fullPath = normalizePath(fullPath);
std::string relative_path = fs::relative(entry.path(), dir_path).string();
std::string full_path = prefix.empty() ? relative_path : prefix + "/" + relative_path;
full_path = normalizePath(full_path);
// Leer archivo completo
std::ifstream file(entry.path(), std::ios::binary);
if (!file) {
std::cerr << "Error: No se pudo abrir: " << entry.path() << std::endl;
std::cerr << "Error: No se pudo abrir: " << entry.path() << '\n';
continue;
}
file.seekg(0, std::ios::end);
size_t fileSize = file.tellg();
size_t file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<unsigned char> buffer(fileSize);
file.read(reinterpret_cast<char*>(buffer.data()), fileSize);
std::vector<unsigned char> buffer(file_size);
file.read(reinterpret_cast<char*>(buffer.data()), file_size);
file.close();
// Crear entrada de recurso
ResourceEntry resource;
resource.path = fullPath;
resource.path = full_path;
resource.offset = 0; // Se calculará al guardar
resource.size = static_cast<uint32_t>(fileSize);
resource.checksum = calculateChecksum(buffer.data(), fileSize);
resource.size = static_cast<uint32_t>(file_size);
resource.checksum = calculateChecksum(buffer.data(), file_size);
resources_[fullPath] = resource;
resources_[full_path] = resource;
std::cout << " Añadido: " << fullPath << " (" << fileSize << " bytes)" << std::endl;
std::cout << " Añadido: " << full_path << " (" << file_size << " bytes)" << '\n';
}
}
return !resources_.empty();
}
bool ResourcePack::savePack(const std::string& packFilePath) {
std::ofstream packFile(packFilePath, std::ios::binary);
if (!packFile) {
std::cerr << "Error: No se pudo crear pack: " << packFilePath << std::endl;
auto ResourcePack::savePack(const std::string& pack_file_path) -> bool {
std::ofstream pack_file(pack_file_path, std::ios::binary);
if (!pack_file) {
std::cerr << "Error: No se pudo crear pack: " << pack_file_path << '\n';
return false;
}
@@ -76,39 +74,39 @@ bool ResourcePack::savePack(const std::string& packFilePath) {
std::memcpy(header.magic, "VBE3", 4);
header.version = 1;
header.fileCount = static_cast<uint32_t>(resources_.size());
packFile.write(reinterpret_cast<const char*>(&header), sizeof(PackHeader));
pack_file.write(reinterpret_cast<const char*>(&header), sizeof(PackHeader));
// 2. Calcular offsets (después del header + índice)
uint32_t currentOffset = sizeof(PackHeader);
uint32_t current_offset = sizeof(PackHeader);
// Calcular tamaño del índice (cada entrada: uint32_t pathLen + path + 3*uint32_t)
for (const auto& [path, entry] : resources_) {
currentOffset += sizeof(uint32_t); // pathLen
currentOffset += static_cast<uint32_t>(path.size()); // path
currentOffset += sizeof(uint32_t) * 3; // offset, size, checksum
current_offset += sizeof(uint32_t); // pathLen
current_offset += static_cast<uint32_t>(path.size()); // path
current_offset += sizeof(uint32_t) * 3; // offset, size, checksum
}
// 3. Escribir índice
for (auto& [path, entry] : resources_) {
entry.offset = currentOffset;
entry.offset = current_offset;
uint32_t pathLen = static_cast<uint32_t>(path.size());
packFile.write(reinterpret_cast<const char*>(&pathLen), sizeof(uint32_t));
packFile.write(path.c_str(), pathLen);
packFile.write(reinterpret_cast<const char*>(&entry.offset), sizeof(uint32_t));
packFile.write(reinterpret_cast<const char*>(&entry.size), sizeof(uint32_t));
packFile.write(reinterpret_cast<const char*>(&entry.checksum), sizeof(uint32_t));
auto path_len = static_cast<uint32_t>(path.size());
pack_file.write(reinterpret_cast<const char*>(&path_len), sizeof(uint32_t));
pack_file.write(path.c_str(), path_len);
pack_file.write(reinterpret_cast<const char*>(&entry.offset), sizeof(uint32_t));
pack_file.write(reinterpret_cast<const char*>(&entry.size), sizeof(uint32_t));
pack_file.write(reinterpret_cast<const char*>(&entry.checksum), sizeof(uint32_t));
currentOffset += entry.size;
current_offset += entry.size;
}
// 4. Escribir datos de archivos (sin encriptar en pack, se encripta al cargar)
for (const auto& [path, entry] : resources_) {
// Encontrar archivo original en disco
fs::path originalPath = fs::current_path() / "data" / path;
std::ifstream file(originalPath, std::ios::binary);
fs::path original_path = fs::current_path() / "data" / path;
std::ifstream file(original_path, std::ios::binary);
if (!file) {
std::cerr << "Error: No se pudo re-leer: " << originalPath << std::endl;
std::cerr << "Error: No se pudo re-leer: " << original_path << '\n';
continue;
}
@@ -116,10 +114,10 @@ bool ResourcePack::savePack(const std::string& packFilePath) {
file.read(reinterpret_cast<char*>(buffer.data()), entry.size);
file.close();
packFile.write(reinterpret_cast<const char*>(buffer.data()), entry.size);
pack_file.write(reinterpret_cast<const char*>(buffer.data()), entry.size);
}
packFile.close();
pack_file.close();
return true;
}
@@ -127,10 +125,10 @@ bool ResourcePack::savePack(const std::string& packFilePath) {
// DESEMPAQUETADO (juego)
// ============================================================================
bool ResourcePack::loadPack(const std::string& packFilePath) {
auto ResourcePack::loadPack(const std::string& pack_file_path) -> bool {
clear();
packFile_.open(packFilePath, std::ios::binary);
packFile_.open(pack_file_path, std::ios::binary);
if (!packFile_) {
return false;
}
@@ -140,13 +138,13 @@ bool ResourcePack::loadPack(const std::string& packFilePath) {
packFile_.read(reinterpret_cast<char*>(&header), sizeof(PackHeader));
if (std::memcmp(header.magic, "VBE3", 4) != 0) {
std::cerr << "Error: Pack inválido (magic incorrecto)" << std::endl;
std::cerr << "Error: Pack inválido (magic incorrecto)" << '\n';
packFile_.close();
return false;
}
if (header.version != 1) {
std::cerr << "Error: Versión de pack no soportada: " << header.version << std::endl;
std::cerr << "Error: Versión de pack no soportada: " << header.version << '\n';
packFile_.close();
return false;
}
@@ -155,12 +153,12 @@ bool ResourcePack::loadPack(const std::string& packFilePath) {
for (uint32_t i = 0; i < header.fileCount; i++) {
ResourceEntry entry;
uint32_t pathLen;
packFile_.read(reinterpret_cast<char*>(&pathLen), sizeof(uint32_t));
uint32_t path_len;
packFile_.read(reinterpret_cast<char*>(&path_len), sizeof(uint32_t));
std::vector<char> pathBuffer(pathLen + 1, '\0');
packFile_.read(pathBuffer.data(), pathLen);
entry.path = std::string(pathBuffer.data());
std::vector<char> path_buffer(path_len + 1, '\0');
packFile_.read(path_buffer.data(), path_len);
entry.path = std::string(path_buffer.data());
packFile_.read(reinterpret_cast<char*>(&entry.offset), sizeof(uint32_t));
packFile_.read(reinterpret_cast<char*>(&entry.size), sizeof(uint32_t));
@@ -173,15 +171,15 @@ bool ResourcePack::loadPack(const std::string& packFilePath) {
return true;
}
ResourcePack::ResourceData ResourcePack::loadResource(const std::string& resourcePath) {
ResourceData result = {nullptr, 0};
auto ResourcePack::loadResource(const std::string& resource_path) -> ResourcePack::ResourceData {
ResourceData result = {.data = nullptr, .size = 0};
if (!isLoaded_) {
return result;
}
std::string normalizedPath = normalizePath(resourcePath);
auto it = resources_.find(normalizedPath);
std::string normalized_path = normalizePath(resource_path);
auto it = resources_.find(normalized_path);
if (it == resources_.end()) {
return result;
}
@@ -197,7 +195,7 @@ ResourcePack::ResourceData ResourcePack::loadResource(const std::string& resourc
// Verificar checksum
uint32_t checksum = calculateChecksum(result.data, entry.size);
if (checksum != entry.checksum) {
std::cerr << "Warning: Checksum incorrecto para: " << resourcePath << std::endl;
std::cerr << "Warning: Checksum incorrecto para: " << resource_path << '\n';
}
return result;
@@ -207,15 +205,16 @@ ResourcePack::ResourceData ResourcePack::loadResource(const std::string& resourc
// UTILIDADES
// ============================================================================
std::vector<std::string> ResourcePack::getResourceList() const {
auto ResourcePack::getResourceList() const -> std::vector<std::string> {
std::vector<std::string> list;
list.reserve(resources_.size());
for (const auto& [path, entry] : resources_) {
list.push_back(path);
}
return list;
}
size_t ResourcePack::getResourceCount() const {
auto ResourcePack::getResourceCount() const -> size_t {
return resources_.size();
}
@@ -231,18 +230,7 @@ void ResourcePack::clear() {
// FUNCIONES AUXILIARES
// ============================================================================
void ResourcePack::encryptData(unsigned char* data, size_t size) {
for (size_t i = 0; i < size; i++) {
data[i] ^= XOR_KEY;
}
}
void ResourcePack::decryptData(unsigned char* data, size_t size) {
// XOR es simétrico
encryptData(data, size);
}
uint32_t ResourcePack::calculateChecksum(const unsigned char* data, size_t size) {
auto ResourcePack::calculateChecksum(const unsigned char* data, size_t size) -> uint32_t {
uint32_t checksum = 0;
for (size_t i = 0; i < size; i++) {
checksum ^= static_cast<uint32_t>(data[i]);
@@ -251,11 +239,11 @@ uint32_t ResourcePack::calculateChecksum(const unsigned char* data, size_t size)
return checksum;
}
std::string ResourcePack::normalizePath(const std::string& path) {
auto ResourcePack::normalizePath(const std::string& path) -> std::string {
std::string normalized = path;
// Reemplazar \ por /
std::replace(normalized.begin(), normalized.end(), '\\', '/');
std::ranges::replace(normalized, '\\', '/');
// Buscar "data/" en cualquier parte del path y extraer lo que viene después
size_t data_pos = normalized.find("data/");

View File

@@ -13,30 +13,30 @@
* único y ofuscado. Proporciona fallback automático a carpeta data/ si no existe pack.
*/
class ResourcePack {
public:
public:
ResourcePack();
~ResourcePack();
// Empaquetado (usado por herramienta pack_resources)
bool addDirectory(const std::string& dirPath, const std::string& prefix = "");
bool savePack(const std::string& packFilePath);
bool addDirectory(const std::string& dir_path, const std::string& prefix = "");
bool savePack(const std::string& pack_file_path);
// Desempaquetado (usado por el juego)
bool loadPack(const std::string& packFilePath);
bool loadPack(const std::string& pack_file_path);
// Carga de recursos individuales
struct ResourceData {
unsigned char* data;
size_t size;
};
ResourceData loadResource(const std::string& resourcePath);
ResourceData loadResource(const std::string& resource_path);
// Utilidades
std::vector<std::string> getResourceList() const;
size_t getResourceCount() const;
void clear();
private:
private:
// Header del pack (12 bytes)
struct PackHeader {
char magic[4]; // "VBE3"
@@ -58,8 +58,6 @@ private:
bool isLoaded_;
// Funciones auxiliares
void encryptData(unsigned char* data, size_t size);
void decryptData(unsigned char* data, size_t size);
uint32_t calculateChecksum(const unsigned char* data, size_t size);
std::string normalizePath(const std::string& path);
static uint32_t calculateChecksum(const unsigned char* data, size_t size);
static std::string normalizePath(const std::string& path);
};

View File

@@ -1,24 +1,25 @@
#include "scene_manager.hpp"
#include <cstdlib> // for rand
#include <utility>
#include "defines.hpp" // for BALL_COUNT_SCENARIOS, GRAVITY_MASS_MIN, etc
#include "external/texture.hpp" // for Texture
#include "theme_manager.hpp" // for ThemeManager
SceneManager::SceneManager(int screen_width, int screen_height)
: current_gravity_(GravityDirection::DOWN)
, scenario_(0)
, screen_width_(screen_width)
, screen_height_(screen_height)
, current_ball_size_(10)
, texture_(nullptr)
, theme_manager_(nullptr) {
: current_gravity_(GravityDirection::DOWN),
scenario_(0),
screen_width_(screen_width),
screen_height_(screen_height),
current_ball_size_(10),
texture_(nullptr),
theme_manager_(nullptr) {
}
void SceneManager::initialize(int scenario, std::shared_ptr<Texture> texture, ThemeManager* theme_manager) {
scenario_ = scenario;
texture_ = texture;
texture_ = std::move(texture);
theme_manager_ = theme_manager;
current_ball_size_ = texture_->getWidth();
@@ -48,28 +49,31 @@ void SceneManager::changeScenario(int scenario_id, SimulationMode mode) {
? custom_ball_count_
: BALL_COUNT_SCENARIOS[scenario_id];
for (int i = 0; i < ball_count; ++i) {
float X, Y, VX, VY;
float x;
float y;
float vx;
float vy;
// Inicialización según SimulationMode (RULES.md líneas 23-26)
switch (mode) {
case SimulationMode::PHYSICS: {
// PHYSICS: Parte superior, 75% distribución central en X
const int SIGN = ((rand() % 2) * 2) - 1;
const int margin = static_cast<int>(screen_width_ * BALL_SPAWN_MARGIN);
const int spawn_zone_width = screen_width_ - (2 * margin);
X = (rand() % spawn_zone_width) + margin;
Y = 0.0f; // Parte superior
VX = (((rand() % 20) + 10) * 0.1f) * SIGN;
VY = ((rand() % 60) - 30) * 0.1f;
const int MARGIN = static_cast<int>(screen_width_ * BALL_SPAWN_MARGIN);
const int SPAWN_ZONE_WIDTH = screen_width_ - (2 * MARGIN);
x = (rand() % SPAWN_ZONE_WIDTH) + MARGIN;
y = 0.0f; // Parte superior
vx = (((rand() % 20) + 10) * 0.1f) * SIGN;
vy = ((rand() % 60) - 30) * 0.1f;
break;
}
case SimulationMode::SHAPE: {
// SHAPE: Centro de pantalla, sin velocidad inicial
X = screen_width_ / 2.0f;
Y = screen_height_ / 2.0f; // Centro vertical
VX = 0.0f;
VY = 0.0f;
x = screen_width_ / 2.0f;
y = screen_height_ / 2.0f; // Centro vertical
vx = 0.0f;
vy = 0.0f;
break;
}
@@ -77,48 +81,57 @@ void SceneManager::changeScenario(int scenario_id, SimulationMode mode) {
// BOIDS: Posiciones aleatorias, velocidades aleatorias
const int SIGN_X = ((rand() % 2) * 2) - 1;
const int SIGN_Y = ((rand() % 2) * 2) - 1;
X = static_cast<float>(rand() % screen_width_);
Y = static_cast<float>(rand() % screen_height_); // Posición Y aleatoria
VX = (((rand() % 40) + 10) * 0.1f) * SIGN_X; // 1.0 - 5.0 px/frame
VY = (((rand() % 40) + 10) * 0.1f) * SIGN_Y;
x = static_cast<float>(rand() % screen_width_);
y = static_cast<float>(rand() % screen_height_); // Posición Y aleatoria
vx = (((rand() % 40) + 10) * 0.1f) * SIGN_X; // 1.0 - 5.0 px/frame
vy = (((rand() % 40) + 10) * 0.1f) * SIGN_Y;
break;
}
default:
// Fallback a PHYSICS por seguridad
const int SIGN = ((rand() % 2) * 2) - 1;
const int margin = static_cast<int>(screen_width_ * BALL_SPAWN_MARGIN);
const int spawn_zone_width = screen_width_ - (2 * margin);
X = (rand() % spawn_zone_width) + margin;
Y = 0.0f; // Parte superior
VX = (((rand() % 20) + 10) * 0.1f) * SIGN;
VY = ((rand() % 60) - 30) * 0.1f;
const int MARGIN = static_cast<int>(screen_width_ * BALL_SPAWN_MARGIN);
const int SPAWN_ZONE_WIDTH = screen_width_ - (2 * MARGIN);
x = (rand() % SPAWN_ZONE_WIDTH) + MARGIN;
y = 0.0f; // Parte superior
vx = (((rand() % 20) + 10) * 0.1f) * SIGN;
vy = ((rand() % 60) - 30) * 0.1f;
break;
}
// Seleccionar color de la paleta del tema actual (delegado a ThemeManager)
int random_index = rand();
Color COLOR = theme_manager_->getInitialBallColor(random_index);
Color color = theme_manager_->getInitialBallColor(random_index);
// Generar factor de masa aleatorio (0.7 = ligera, 1.3 = pesada)
float mass_factor = GRAVITY_MASS_MIN + (rand() % 1000) / 1000.0f * (GRAVITY_MASS_MAX - GRAVITY_MASS_MIN);
float mass_factor = GRAVITY_MASS_MIN + ((rand() % 1000) / 1000.0f * (GRAVITY_MASS_MAX - GRAVITY_MASS_MIN));
balls_.emplace_back(std::make_unique<Ball>(
X, Y, VX, VY, COLOR, texture_,
screen_width_, screen_height_, current_ball_size_,
current_gravity_, mass_factor
));
x,
y,
vx,
vy,
color,
texture_,
screen_width_,
screen_height_,
current_ball_size_,
current_gravity_,
mass_factor));
}
}
void SceneManager::updateBallTexture(std::shared_ptr<Texture> new_texture, int new_ball_size) {
if (balls_.empty()) return;
if (balls_.empty()) {
return;
}
// Guardar tamaño antiguo
int old_size = current_ball_size_;
// Actualizar textura y tamaño
texture_ = new_texture;
texture_ = std::move(new_texture);
current_ball_size_ = new_ball_size;
// Actualizar texturas de todas las pelotas
@@ -136,7 +149,8 @@ void SceneManager::pushBallsAwayFromGravity() {
const float LATERAL = (((rand() % 20) + 10) * 0.1f) * SIGNO;
const float MAIN = ((rand() % 40) * 0.1f) + 5;
float vx = 0, vy = 0;
float vx = 0;
float vy = 0;
switch (current_gravity_) {
case GravityDirection::DOWN: // Impulsar ARRIBA
vx = LATERAL;

View File

@@ -1,7 +1,10 @@
#include "atom_shape.hpp"
#include "defines.hpp"
#include <algorithm>
#include <cmath>
#include "defines.hpp"
void AtomShape::generatePoints(int num_points, float screen_width, float screen_height) {
num_points_ = num_points;
nucleus_radius_ = screen_height * ATOM_NUCLEUS_RADIUS_FACTOR;
@@ -26,13 +29,13 @@ void AtomShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Calcular cuántos puntos para núcleo vs órbitas
int nucleus_points = (num_points_ < 10) ? 1 : (num_points_ / 10); // 10% para núcleo
if (nucleus_points < 1) nucleus_points = 1;
nucleus_points = std::max(nucleus_points, 1);
// Si estamos en el núcleo
if (index < nucleus_points) {
// Distribuir puntos en esfera pequeña (núcleo)
float t = static_cast<float>(index) / static_cast<float>(nucleus_points);
float phi = acosf(1.0f - 2.0f * t);
float phi = acosf(1.0f - (2.0f * t));
float theta = PI * 2.0f * t * 3.61803398875f; // Golden ratio
float x_nuc = nucleus_radius_ * cosf(theta) * sinf(phi);
@@ -51,10 +54,12 @@ void AtomShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Puntos restantes: distribuir en órbitas
int orbit_points = num_points_ - nucleus_points;
int points_per_orbit = orbit_points / num_orbits;
if (points_per_orbit < 1) points_per_orbit = 1;
points_per_orbit = std::max(points_per_orbit, 1);
int orbit_index = (index - nucleus_points) / points_per_orbit;
if (orbit_index >= num_orbits) orbit_index = num_orbits - 1;
if (orbit_index >= num_orbits) {
orbit_index = num_orbits - 1;
}
int point_in_orbit = (index - nucleus_points) % points_per_orbit;
@@ -73,21 +78,21 @@ void AtomShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Inclinar el plano orbital (rotación en eje X local)
float cos_tilt = cosf(orbit_tilt);
float sin_tilt = sinf(orbit_tilt);
float y_tilted = y_local * cos_tilt - z_local * sin_tilt;
float z_tilted = y_local * sin_tilt + z_local * cos_tilt;
float y_tilted = (y_local * cos_tilt) - (z_local * sin_tilt);
float z_tilted = (y_local * sin_tilt) + (z_local * cos_tilt);
// Aplicar rotación global del átomo (eje Y)
float cos_y = cosf(angle_y_);
float sin_y = sinf(angle_y_);
float x_rot = x_local * cos_y - z_tilted * sin_y;
float z_rot = x_local * sin_y + z_tilted * cos_y;
float x_rot = (x_local * cos_y) - (z_tilted * sin_y);
float z_rot = (x_local * sin_y) + (z_tilted * cos_y);
x = x_rot;
y = y_tilted;
z = z_rot;
}
float AtomShape::getScaleFactor(float screen_height) const {
auto AtomShape::getScaleFactor(float screen_height) const -> float {
// Factor de escala para física: proporcional al radio de órbita
// Radio órbita base = 72px (0.30 * 240px en resolución 320x240)
const float BASE_RADIUS = 72.0f;

View File

@@ -6,14 +6,14 @@
// Comportamiento: Núcleo estático + electrones orbitando en planos inclinados
// Efecto: Modelo atómico clásico Bohr
class AtomShape : public Shape {
private:
private:
float angle_y_ = 0.0f; // Ángulo de rotación global en eje Y (rad)
float orbit_phase_ = 0.0f; // Fase de rotación de electrones (rad)
float nucleus_radius_ = 0.0f; // Radio del núcleo central (píxeles)
float orbit_radius_ = 0.0f; // Radio de las órbitas (píxeles)
int num_points_ = 0; // Cantidad total de puntos
public:
public:
void generatePoints(int num_points, float screen_width, float screen_height) override;
void update(float delta_time, float screen_width, float screen_height) override;
void getPoint3D(int index, float& x, float& y, float& z) const override;

View File

@@ -1,7 +1,10 @@
#include "cube_shape.hpp"
#include "defines.hpp"
#include <algorithm>
#include <cmath>
#include "defines.hpp"
void CubeShape::generatePoints(int num_points, float screen_width, float screen_height) {
num_points_ = num_points;
size_ = screen_height * CUBE_SIZE_FACTOR;
@@ -52,23 +55,23 @@ void CubeShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Aplicar rotación en eje Z
float cos_z = cosf(angle_z_);
float sin_z = sinf(angle_z_);
float x_rot_z = x_base * cos_z - y_base * sin_z;
float y_rot_z = x_base * sin_z + y_base * cos_z;
float x_rot_z = (x_base * cos_z) - (y_base * sin_z);
float y_rot_z = (x_base * sin_z) + (y_base * cos_z);
float z_rot_z = z_base;
// Aplicar rotación en eje Y
float cos_y = cosf(angle_y_);
float sin_y = sinf(angle_y_);
float x_rot_y = x_rot_z * cos_y + z_rot_z * sin_y;
float x_rot_y = (x_rot_z * cos_y) + (z_rot_z * sin_y);
float y_rot_y = y_rot_z;
float z_rot_y = -x_rot_z * sin_y + z_rot_z * cos_y;
float z_rot_y = (-x_rot_z * sin_y) + (z_rot_z * cos_y);
// Aplicar rotación en eje X
float cos_x = cosf(angle_x_);
float sin_x = sinf(angle_x_);
float x_final = x_rot_y;
float y_final = y_rot_y * cos_x - z_rot_y * sin_x;
float z_final = y_rot_y * sin_x + z_rot_y * cos_x;
float y_final = (y_rot_y * cos_x) - (z_rot_y * sin_x);
float z_final = (y_rot_y * sin_x) + (z_rot_y * cos_x);
// Retornar coordenadas finales rotadas
x = x_final;
@@ -76,7 +79,7 @@ void CubeShape::getPoint3D(int index, float& x, float& y, float& z) const {
z = z_final;
}
float CubeShape::getScaleFactor(float screen_height) const {
auto CubeShape::getScaleFactor(float screen_height) const -> float {
// Factor de escala para física: proporcional al tamaño del cubo
// Tamaño base = 60px (resolución 320x240, factor 0.25)
const float BASE_SIZE = 60.0f;
@@ -105,12 +108,24 @@ void CubeShape::generateVerticesAndCenters() {
// 2. Añadir 6 centros de caras
// Caras: X=±size (Y,Z varían), Y=±size (X,Z varían), Z=±size (X,Y varían)
base_x_.push_back(size_); base_y_.push_back(0); base_z_.push_back(0); // +X
base_x_.push_back(-size_); base_y_.push_back(0); base_z_.push_back(0); // -X
base_x_.push_back(0); base_y_.push_back(size_); base_z_.push_back(0); // +Y
base_x_.push_back(0); base_y_.push_back(-size_);base_z_.push_back(0); // -Y
base_x_.push_back(0); base_y_.push_back(0); base_z_.push_back(size_); // +Z
base_x_.push_back(0); base_y_.push_back(0); base_z_.push_back(-size_); // -Z
base_x_.push_back(size_);
base_y_.push_back(0);
base_z_.push_back(0); // +X
base_x_.push_back(-size_);
base_y_.push_back(0);
base_z_.push_back(0); // -X
base_x_.push_back(0);
base_y_.push_back(size_);
base_z_.push_back(0); // +Y
base_x_.push_back(0);
base_y_.push_back(-size_);
base_z_.push_back(0); // -Y
base_x_.push_back(0);
base_y_.push_back(0);
base_z_.push_back(size_); // +Z
base_x_.push_back(0);
base_y_.push_back(0);
base_z_.push_back(-size_); // -Z
// 3. Añadir 12 centros de aristas
// Aristas paralelas a X (4), Y (4), Z (4)
@@ -143,16 +158,16 @@ void CubeShape::generateVerticesAndCenters() {
void CubeShape::generateVolumetricGrid() {
// Calcular dimensión del grid cúbico: N³ ≈ num_points
int grid_dim = static_cast<int>(ceilf(cbrtf(static_cast<float>(num_points_))));
if (grid_dim < 3) grid_dim = 3; // Mínimo grid 3x3x3
grid_dim = std::max(grid_dim, 3); // Mínimo grid 3x3x3
float step = (2.0f * size_) / (grid_dim - 1); // Espacio entre puntos
for (int ix = 0; ix < grid_dim; ix++) {
for (int iy = 0; iy < grid_dim; iy++) {
for (int iz = 0; iz < grid_dim; iz++) {
float x = -size_ + ix * step;
float y = -size_ + iy * step;
float z = -size_ + iz * step;
float x = -size_ + (ix * step);
float y = -size_ + (iy * step);
float z = -size_ + (iz * step);
base_x_.push_back(x);
base_y_.push_back(y);

View File

@@ -1,8 +1,9 @@
#pragma once
#include "shape.hpp"
#include <vector>
#include "shape.hpp"
// Figura: Cubo 3D rotante
// Distribución:
// - 1-8 pelotas: Solo vértices (8 puntos)
@@ -10,7 +11,7 @@
// - 27+ pelotas: Grid volumétrico 3D uniforme
// Comportamiento: Rotación simultánea en ejes X, Y, Z (efecto Rubik)
class CubeShape : public Shape {
private:
private:
float angle_x_ = 0.0f; // Ángulo de rotación en eje X (rad)
float angle_y_ = 0.0f; // Ángulo de rotación en eje Y (rad)
float angle_z_ = 0.0f; // Ángulo de rotación en eje Z (rad)
@@ -22,14 +23,14 @@ private:
std::vector<float> base_y_;
std::vector<float> base_z_;
public:
public:
void generatePoints(int num_points, float screen_width, float screen_height) override;
void update(float delta_time, float screen_width, float screen_height) override;
void getPoint3D(int index, float& x, float& y, float& z) const override;
const char* getName() const override { return "CUBE"; }
float getScaleFactor(float screen_height) const override;
private:
private:
// Métodos auxiliares para distribución de puntos
void generateVertices(); // 8 vértices
void generateVerticesAndCenters(); // 26 puntos (vértices + caras + aristas)

View File

@@ -1,8 +1,11 @@
#include "cylinder_shape.hpp"
#include "defines.hpp"
#include <algorithm>
#include <cmath>
#include <cstdlib> // Para rand()
#include "defines.hpp"
void CylinderShape::generatePoints(int num_points, float screen_width, float screen_height) {
num_points_ = num_points;
radius_ = screen_height * CYLINDER_RADIUS_FACTOR;
@@ -37,7 +40,7 @@ void CylinderShape::update(float delta_time, float screen_width, float screen_he
float t = tumble_progress;
float ease = t < 0.5f
? 2.0f * t * t
: 1.0f - (-2.0f * t + 2.0f) * (-2.0f * t + 2.0f) / 2.0f;
: 1.0f - ((-2.0f * t + 2.0f) * (-2.0f * t + 2.0f) / 2.0f);
angle_x_ = ease * tumble_target_;
}
} else {
@@ -58,10 +61,10 @@ void CylinderShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Calcular número de anillos (altura) y puntos por anillo (circunferencia)
int num_rings = static_cast<int>(sqrtf(static_cast<float>(num_points_) * 0.5f));
if (num_rings < 2) num_rings = 2;
num_rings = std::max(num_rings, 2);
int points_per_ring = num_points_ / num_rings;
if (points_per_ring < 3) points_per_ring = 3;
points_per_ring = std::max(points_per_ring, 3);
// Obtener parámetros u (ángulo) y v (altura) del índice
int ring = index / points_per_ring;
@@ -80,8 +83,10 @@ void CylinderShape::getPoint3D(int index, float& x, float& y, float& z) const {
float u = (static_cast<float>(point_in_ring) / static_cast<float>(points_per_ring)) * 2.0f * PI;
// Parámetro v (altura normalizada): [-1, 1]
float v = (static_cast<float>(ring) / static_cast<float>(num_rings - 1)) * 2.0f - 1.0f;
if (num_rings == 1) v = 0.0f;
float v = ((static_cast<float>(ring) / static_cast<float>(num_rings - 1)) * 2.0f) - 1.0f;
if (num_rings == 1) {
v = 0.0f;
}
// Ecuaciones paramétricas del cilindro
// x = radius * cos(u)
@@ -94,14 +99,14 @@ void CylinderShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Aplicar rotación en eje Y (principal, siempre activa)
float cos_y = cosf(angle_y_);
float sin_y = sinf(angle_y_);
float x_rot_y = x_base * cos_y - z_base * sin_y;
float z_rot_y = x_base * sin_y + z_base * cos_y;
float x_rot_y = (x_base * cos_y) - (z_base * sin_y);
float z_rot_y = (x_base * sin_y) + (z_base * cos_y);
// Aplicar rotación en eje X (tumbling ocasional)
float cos_x = cosf(angle_x_);
float sin_x = sinf(angle_x_);
float y_rot = y_base * cos_x - z_rot_y * sin_x;
float z_rot = y_base * sin_x + z_rot_y * cos_x;
float y_rot = (y_base * cos_x) - (z_rot_y * sin_x);
float z_rot = (y_base * sin_x) + (z_rot_y * cos_x);
// Retornar coordenadas finales con ambas rotaciones
x = x_rot_y;
@@ -109,7 +114,7 @@ void CylinderShape::getPoint3D(int index, float& x, float& y, float& z) const {
z = z_rot;
}
float CylinderShape::getScaleFactor(float screen_height) const {
auto CylinderShape::getScaleFactor(float screen_height) const -> float {
// Factor de escala para física: proporcional a la dimensión mayor (altura)
// Altura base = 120px (0.5 * 240px en resolución 320x240)
const float BASE_HEIGHT = 120.0f;

View File

@@ -6,7 +6,7 @@
// Comportamiento: Superficie cilíndrica con rotación en eje Y + tumbling ocasional en X/Z
// Ecuaciones: x = r*cos(u), y = v, z = r*sin(u)
class CylinderShape : public Shape {
private:
private:
float angle_y_ = 0.0f; // Ángulo de rotación en eje Y (rad)
float angle_x_ = 0.0f; // Ángulo de rotación en eje X (tumbling ocasional)
float radius_ = 0.0f; // Radio del cilindro (píxeles)
@@ -19,7 +19,7 @@ private:
bool is_tumbling_ = false; // ¿Estamos en modo tumble?
float tumble_target_ = 0.0f; // Ángulo objetivo del tumble
public:
public:
void generatePoints(int num_points, float screen_width, float screen_height) override;
void update(float delta_time, float screen_width, float screen_height) override;
void getPoint3D(int index, float& x, float& y, float& z) const override;

View File

@@ -1,7 +1,9 @@
#include "helix_shape.hpp"
#include "defines.hpp"
#include <cmath>
#include "defines.hpp"
void HelixShape::generatePoints(int num_points, float screen_width, float screen_height) {
num_points_ = num_points;
radius_ = screen_height * HELIX_RADIUS_FACTOR;
@@ -41,8 +43,8 @@ void HelixShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Aplicar rotación en eje Y (horizontal)
float cos_y = cosf(angle_y_);
float sin_y = sinf(angle_y_);
float x_rot = x_base * cos_y - z_base * sin_y;
float z_rot = x_base * sin_y + z_base * cos_y;
float x_rot = (x_base * cos_y) - (z_base * sin_y);
float z_rot = (x_base * sin_y) + (z_base * cos_y);
// Retornar coordenadas finales
x = x_rot;
@@ -50,7 +52,7 @@ void HelixShape::getPoint3D(int index, float& x, float& y, float& z) const {
z = z_rot;
}
float HelixShape::getScaleFactor(float screen_height) const {
auto HelixShape::getScaleFactor(float screen_height) const -> float {
// Factor de escala para física: proporcional a la dimensión mayor (altura total)
// Altura base = 180px para 3 vueltas con pitch=0.25 en 240px de altura (180 = 240 * 0.25 * 3)
const float BASE_HEIGHT = 180.0f;

View File

@@ -6,7 +6,7 @@
// Comportamiento: Rotación en eje Y + animación de fase vertical
// Ecuaciones: x = r*cos(t), y = pitch*t + phase, z = r*sin(t)
class HelixShape : public Shape {
private:
private:
float angle_y_ = 0.0f; // Ángulo de rotación en eje Y (rad)
float phase_offset_ = 0.0f; // Offset de fase para animación vertical (rad)
float radius_ = 0.0f; // Radio de la espiral (píxeles)
@@ -14,7 +14,7 @@ private:
float total_height_ = 0.0f; // Altura total de la espiral (píxeles)
int num_points_ = 0; // Cantidad de puntos generados
public:
public:
void generatePoints(int num_points, float screen_width, float screen_height) override;
void update(float delta_time, float screen_width, float screen_height) override;
void getPoint3D(int index, float& x, float& y, float& z) const override;

View File

@@ -1,8 +1,12 @@
#include "icosahedron_shape.hpp"
#include "defines.hpp"
#include <algorithm>
#include <array>
#include <cmath>
#include <vector>
#include "defines.hpp"
void IcosahedronShape::generatePoints(int num_points, float screen_width, float screen_height) {
num_points_ = num_points;
radius_ = screen_height * ICOSAHEDRON_RADIUS_FACTOR;
@@ -21,37 +25,36 @@ void IcosahedronShape::update(float delta_time, float screen_width, float screen
void IcosahedronShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Proporción áurea (golden ratio)
const float phi = (1.0f + sqrtf(5.0f)) / 2.0f;
const float PHI = (1.0f + sqrtf(5.0f)) / 2.0f;
// 12 vértices del icosaedro regular normalizado
// Basados en 3 rectángulos áureos ortogonales
static const float vertices[12][3] = {
const std::array<std::array<float, 3>, 12> VERTICES = {{
// Rectángulo XY
{-1.0f, phi, 0.0f},
{ 1.0f, phi, 0.0f},
{-1.0f, -phi, 0.0f},
{ 1.0f, -phi, 0.0f},
{-1.0f, PHI, 0.0f},
{1.0f, PHI, 0.0f},
{-1.0f, -PHI, 0.0f},
{1.0f, -PHI, 0.0f},
// Rectángulo YZ
{ 0.0f, -1.0f, phi},
{ 0.0f, 1.0f, phi},
{ 0.0f, -1.0f, -phi},
{ 0.0f, 1.0f, -phi},
{0.0f, -1.0f, PHI},
{0.0f, 1.0f, PHI},
{0.0f, -1.0f, -PHI},
{0.0f, 1.0f, -PHI},
// Rectángulo ZX
{ phi, 0.0f, -1.0f},
{ phi, 0.0f, 1.0f},
{-phi, 0.0f, -1.0f},
{-phi, 0.0f, 1.0f}
};
{PHI, 0.0f, -1.0f},
{PHI, 0.0f, 1.0f},
{-PHI, 0.0f, -1.0f},
{-PHI, 0.0f, 1.0f}}};
// Normalizar para esfera circunscrita
const float normalization = sqrtf(1.0f + phi * phi);
const float NORMALIZATION = sqrtf(1.0f + (PHI * PHI));
// Si tenemos 12 o menos puntos, usar solo vértices
if (num_points_ <= 12) {
int vertex_index = index % 12;
float x_base = vertices[vertex_index][0] / normalization * radius_;
float y_base = vertices[vertex_index][1] / normalization * radius_;
float z_base = vertices[vertex_index][2] / normalization * radius_;
float x_base = VERTICES[vertex_index][0] / NORMALIZATION * radius_;
float y_base = VERTICES[vertex_index][1] / NORMALIZATION * radius_;
float z_base = VERTICES[vertex_index][2] / NORMALIZATION * radius_;
// Aplicar rotaciones
applyRotations(x_base, y_base, z_base, x, y, z);
@@ -62,9 +65,9 @@ void IcosahedronShape::getPoint3D(int index, float& x, float& y, float& z) const
// Distribuir puntos entre vértices (primero) y caras (después)
if (index < 12) {
// Primeros 12 puntos: vértices del icosaedro
float x_base = vertices[index][0] / normalization * radius_;
float y_base = vertices[index][1] / normalization * radius_;
float z_base = vertices[index][2] / normalization * radius_;
float x_base = VERTICES[index][0] / NORMALIZATION * radius_;
float y_base = VERTICES[index][1] / NORMALIZATION * radius_;
float z_base = VERTICES[index][2] / NORMALIZATION * radius_;
applyRotations(x_base, y_base, z_base, x, y, z);
return;
}
@@ -73,38 +76,55 @@ void IcosahedronShape::getPoint3D(int index, float& x, float& y, float& z) const
// El icosaedro tiene 20 caras triangulares
int remaining_points = index - 12;
int points_per_face = (num_points_ - 12) / 20;
if (points_per_face < 1) points_per_face = 1;
points_per_face = std::max(points_per_face, 1);
int face_index = remaining_points / points_per_face;
if (face_index >= 20) face_index = 19;
if (face_index >= 20) {
face_index = 19;
}
int point_in_face = remaining_points % points_per_face;
// Definir algunas caras del icosaedro (usando índices de vértices)
// Solo necesitamos generar puntos, no renderizar caras completas
static const int faces[20][3] = {
{0, 11, 5}, {0, 5, 1}, {0, 1, 7}, {0, 7, 10}, {0, 10, 11},
{1, 5, 9}, {5, 11, 4}, {11, 10, 2}, {10, 7, 6}, {7, 1, 8},
{3, 9, 4}, {3, 4, 2}, {3, 2, 6}, {3, 6, 8}, {3, 8, 9},
{4, 9, 5}, {2, 4, 11}, {6, 2, 10}, {8, 6, 7}, {9, 8, 1}
};
static constexpr std::array<std::array<int, 3>, 20> FACES = {{
{0, 11, 5},
{0, 5, 1},
{0, 1, 7},
{0, 7, 10},
{0, 10, 11},
{1, 5, 9},
{5, 11, 4},
{11, 10, 2},
{10, 7, 6},
{7, 1, 8},
{3, 9, 4},
{3, 4, 2},
{3, 2, 6},
{3, 6, 8},
{3, 8, 9},
{4, 9, 5},
{2, 4, 11},
{6, 2, 10},
{8, 6, 7},
{9, 8, 1}}};
// Obtener vértices de la cara
int v0 = faces[face_index][0];
int v1 = faces[face_index][1];
int v2 = faces[face_index][2];
int v0 = FACES[face_index][0];
int v1 = FACES[face_index][1];
int v2 = FACES[face_index][2];
// Interpolar dentro del triángulo usando coordenadas baricéntricas simples
float t = static_cast<float>(point_in_face) / static_cast<float>(points_per_face + 1);
float u = sqrtf(t);
float v = t - u;
float x_interp = vertices[v0][0] * (1.0f - u - v) + vertices[v1][0] * u + vertices[v2][0] * v;
float y_interp = vertices[v0][1] * (1.0f - u - v) + vertices[v1][1] * u + vertices[v2][1] * v;
float z_interp = vertices[v0][2] * (1.0f - u - v) + vertices[v1][2] * u + vertices[v2][2] * v;
float x_interp = (VERTICES[v0][0] * (1.0f - u - v)) + (VERTICES[v1][0] * u) + (VERTICES[v2][0] * v);
float y_interp = (VERTICES[v0][1] * (1.0f - u - v)) + (VERTICES[v1][1] * u) + (VERTICES[v2][1] * v);
float z_interp = (VERTICES[v0][2] * (1.0f - u - v)) + (VERTICES[v1][2] * u) + (VERTICES[v2][2] * v);
// Proyectar a la esfera
float len = sqrtf(x_interp * x_interp + y_interp * y_interp + z_interp * z_interp);
float len = sqrtf((x_interp * x_interp) + (y_interp * y_interp) + (z_interp * z_interp));
if (len > 0.0001f) {
x_interp /= len;
y_interp /= len;
@@ -122,27 +142,27 @@ void IcosahedronShape::applyRotations(float x_in, float y_in, float z_in, float&
// Aplicar rotación en eje X
float cos_x = cosf(angle_x_);
float sin_x = sinf(angle_x_);
float y_rot_x = y_in * cos_x - z_in * sin_x;
float z_rot_x = y_in * sin_x + z_in * cos_x;
float y_rot_x = (y_in * cos_x) - (z_in * sin_x);
float z_rot_x = (y_in * sin_x) + (z_in * cos_x);
// Aplicar rotación en eje Y
float cos_y = cosf(angle_y_);
float sin_y = sinf(angle_y_);
float x_rot_y = x_in * cos_y - z_rot_x * sin_y;
float z_rot_y = x_in * sin_y + z_rot_x * cos_y;
float x_rot_y = (x_in * cos_y) - (z_rot_x * sin_y);
float z_rot_y = (x_in * sin_y) + (z_rot_x * cos_y);
// Aplicar rotación en eje Z
float cos_z = cosf(angle_z_);
float sin_z = sinf(angle_z_);
float x_final = x_rot_y * cos_z - y_rot_x * sin_z;
float y_final = x_rot_y * sin_z + y_rot_x * cos_z;
float x_final = (x_rot_y * cos_z) - (y_rot_x * sin_z);
float y_final = (x_rot_y * sin_z) + (y_rot_x * cos_z);
x_out = x_final;
y_out = y_final;
z_out = z_rot_y;
}
float IcosahedronShape::getScaleFactor(float screen_height) const {
auto IcosahedronShape::getScaleFactor(float screen_height) const -> float {
// Factor de escala para física: proporcional al radio
// Radio base = 72px (0.30 * 240px en resolución 320x240)
const float BASE_RADIUS = 72.0f;

View File

@@ -6,7 +6,7 @@
// Comportamiento: 12 vértices distribuidos uniformemente con rotación triple
// Geometría: Basado en proporción áurea (golden ratio)
class IcosahedronShape : public Shape {
private:
private:
float angle_x_ = 0.0f; // Ángulo de rotación en eje X (rad)
float angle_y_ = 0.0f; // Ángulo de rotación en eje Y (rad)
float angle_z_ = 0.0f; // Ángulo de rotación en eje Z (rad)
@@ -16,7 +16,7 @@ private:
// Helper para aplicar rotaciones triple XYZ
void applyRotations(float x_in, float y_in, float z_in, float& x_out, float& y_out, float& z_out) const;
public:
public:
void generatePoints(int num_points, float screen_width, float screen_height) override;
void update(float delta_time, float screen_width, float screen_height) override;
void getPoint3D(int index, float& x, float& y, float& z) const override;

View File

@@ -1,7 +1,9 @@
#include "lissajous_shape.hpp"
#include "defines.hpp"
#include <cmath>
#include "defines.hpp"
void LissajousShape::generatePoints(int num_points, float screen_width, float screen_height) {
num_points_ = num_points;
amplitude_ = screen_height * LISSAJOUS_SIZE_FACTOR;
@@ -33,21 +35,21 @@ void LissajousShape::getPoint3D(int index, float& x, float& y, float& z) const {
// x(t) = A * sin(freq_x * t + phase_x)
// y(t) = A * sin(freq_y * t)
// z(t) = A * sin(freq_z * t + phase_z)
float x_local = amplitude_ * sinf(freq_x_ * t + phase_x_);
float x_local = amplitude_ * sinf((freq_x_ * t) + phase_x_);
float y_local = amplitude_ * sinf(freq_y_ * t);
float z_local = amplitude_ * sinf(freq_z_ * t + phase_z_);
float z_local = amplitude_ * sinf((freq_z_ * t) + phase_z_);
// Aplicar rotación global en eje X
float cos_x = cosf(rotation_x_);
float sin_x = sinf(rotation_x_);
float y_rot = y_local * cos_x - z_local * sin_x;
float z_rot = y_local * sin_x + z_local * cos_x;
float y_rot = (y_local * cos_x) - (z_local * sin_x);
float z_rot = (y_local * sin_x) + (z_local * cos_x);
// Aplicar rotación global en eje Y
float cos_y = cosf(rotation_y_);
float sin_y = sinf(rotation_y_);
float x_final = x_local * cos_y - z_rot * sin_y;
float z_final = x_local * sin_y + z_rot * cos_y;
float x_final = (x_local * cos_y) - (z_rot * sin_y);
float z_final = (x_local * sin_y) + (z_rot * cos_y);
// Retornar coordenadas rotadas
x = x_final;
@@ -55,7 +57,7 @@ void LissajousShape::getPoint3D(int index, float& x, float& y, float& z) const {
z = z_final;
}
float LissajousShape::getScaleFactor(float screen_height) const {
auto LissajousShape::getScaleFactor(float screen_height) const -> float {
// Factor de escala para física: proporcional a la amplitud de la curva
// Amplitud base = 84px (0.35 * 240px en resolución 320x240)
const float BASE_SIZE = 84.0f;

View File

@@ -6,7 +6,7 @@
// Comportamiento: Curva paramétrica 3D con rotación global y animación de fase
// Ecuaciones: x(t) = A*sin(freq_x*t + phase_x), y(t) = A*sin(freq_y*t), z(t) = A*sin(freq_z*t + phase_z)
class LissajousShape : public Shape {
private:
private:
float freq_x_ = 0.0f; // Frecuencia en eje X
float freq_y_ = 0.0f; // Frecuencia en eje Y
float freq_z_ = 0.0f; // Frecuencia en eje Z
@@ -17,7 +17,7 @@ private:
float amplitude_ = 0.0f; // Amplitud de la curva (píxeles)
int num_points_ = 0; // Cantidad total de puntos
public:
public:
void generatePoints(int num_points, float screen_width, float screen_height) override;
void update(float delta_time, float screen_width, float screen_height) override;
void getPoint3D(int index, float& x, float& y, float& z) const override;

View File

@@ -1,16 +1,18 @@
#include "png_shape.hpp"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include "defines.hpp"
#include "external/stb_image.h"
#include "resource_manager.hpp"
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
PNGShape::PNGShape(const char* png_path) {
// Cargar PNG desde path
if (!loadPNG(png_path)) {
std::cerr << "[PNGShape] Usando fallback 10x10" << std::endl;
std::cerr << "[PNGShape] Usando fallback 10x10" << '\n';
// Fallback: generar un cuadrado simple si falla la carga
image_width_ = 10;
image_height_ = 10;
@@ -21,20 +23,25 @@ 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* resource_key) {
std::cout << "[PNGShape] Cargando recurso: " << resource_key << std::endl;
auto PNGShape::loadPNG(const char* resource_key) -> bool {
{
std::string fn = std::string(resource_key);
fn = fn.substr(fn.find_last_of("\\/") + 1);
std::cout << "[PNGShape] " << fn << " (" << (ResourceManager::isPackLoaded() ? "pack" : "disco") << ")\n";
}
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;
std::cerr << "[PNGShape] ERROR: recurso no encontrado: " << resource_key << '\n';
return false;
}
int width, height, channels;
unsigned char* pixels = stbi_load_from_memory(file_data, static_cast<int>(file_size),
&width, &height, &channels, 1);
int width;
int height;
int channels;
unsigned char* pixels = stbi_load_from_memory(file_data, static_cast<int>(file_size), &width, &height, &channels, 1);
delete[] file_data;
if (!pixels) {
std::cerr << "[PNGShape] ERROR al decodificar PNG: " << stbi_failure_reason() << std::endl;
if (pixels == nullptr) {
std::cerr << "[PNGShape] ERROR al decodificar PNG: " << stbi_failure_reason() << '\n';
return false;
}
image_width_ = width;
@@ -53,9 +60,11 @@ void PNGShape::detectEdges() {
// Detectar píxeles del contorno (píxeles blancos con al menos un vecino negro)
for (int y = 0; y < image_height_; y++) {
for (int x = 0; x < image_width_; x++) {
int idx = y * image_width_ + x;
int idx = (y * image_width_) + x;
if (!pixel_data_[idx]) continue; // Solo píxeles blancos
if (!pixel_data_[idx]) {
continue; // Solo píxeles blancos
}
// Verificar vecinos (arriba, abajo, izq, der)
bool is_edge = false;
@@ -86,7 +95,7 @@ void PNGShape::floodFill() {
for (int y = 0; y < image_height_; y++) {
for (int x = 0; x < image_width_; x++) {
int idx = y * image_width_ + x;
int idx = (y * image_width_) + x;
if (pixel_data_[idx]) {
filled_points_.push_back({static_cast<float>(x), static_cast<float>(y)});
}
@@ -119,7 +128,7 @@ void PNGShape::generatePoints(int num_points, float screen_width, float screen_h
// Conjunto de puntos ACTIVO (será modificado por filtros)
std::vector<Point2D> active_points_data;
std::string mode_name = "";
std::string mode_name;
// === SISTEMA DE DISTRIBUCIÓN ADAPTATIVA ===
// Estrategia: Optimizar según número de pelotas disponibles
@@ -192,8 +201,6 @@ void PNGShape::generatePoints(int num_points, float screen_width, float screen_h
std::vector<Point2D> vertices = extractCornerVertices(source_for_vertices);
if (!vertices.empty() && vertices.size() < active_points_data.size()) {
active_points_data = vertices;
num_2d_points = active_points_data.size();
total_3d_points = num_2d_points * num_layers_;
mode_name = "VÉRTICES";
}
}
@@ -212,7 +219,7 @@ void PNGShape::generatePoints(int num_points, float screen_width, float screen_h
// Extraer filas alternas de puntos (FUNCIÓN PURA: no modifica parámetros)
// Recibe vector original y devuelve nuevo vector filtrado
std::vector<PNGShape::Point2D> PNGShape::extractAlternateRows(const std::vector<Point2D>& source, int row_skip) {
auto PNGShape::extractAlternateRows(const std::vector<Point2D>& source, int row_skip) -> std::vector<PNGShape::Point2D> {
std::vector<Point2D> result;
if (row_skip <= 1 || source.empty()) {
@@ -239,7 +246,7 @@ std::vector<PNGShape::Point2D> PNGShape::extractAlternateRows(const std::vector<
}
// Extraer vértices y esquinas (FUNCIÓN PURA: devuelve nuevo vector)
std::vector<PNGShape::Point2D> PNGShape::extractCornerVertices(const std::vector<Point2D>& source) {
auto PNGShape::extractCornerVertices(const std::vector<Point2D>& source) -> std::vector<PNGShape::Point2D> {
std::vector<Point2D> result;
if (source.empty()) {
@@ -382,14 +389,14 @@ void PNGShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Aplicar rotación en eje Y (horizontal)
float cos_y = cosf(angle_y_);
float sin_y = sinf(angle_y_);
float x_rot_y = x_base * cos_y - z_base * sin_y;
float z_rot_y = x_base * sin_y + z_base * cos_y;
float x_rot_y = (x_base * cos_y) - (z_base * sin_y);
float z_rot_y = (x_base * sin_y) + (z_base * cos_y);
// Aplicar rotación en eje X (vertical)
float cos_x = cosf(angle_x_);
float sin_x = sinf(angle_x_);
float y_rot = y_base * cos_x - z_rot_y * sin_x;
float z_rot = y_base * sin_x + z_rot_y * cos_x;
float y_rot = (y_base * cos_x) - (z_rot_y * sin_x);
float z_rot = (y_base * sin_x) + (z_rot_y * cos_x);
// Retornar coordenadas finales
x = x_rot_y;
@@ -404,7 +411,7 @@ void PNGShape::getPoint3D(int index, float& x, float& y, float& z) const {
}
}
float PNGShape::getScaleFactor(float screen_height) const {
auto PNGShape::getScaleFactor(float screen_height) const -> float {
// Escala dinámica según resolución
return PNG_SIZE_FACTOR;
}
@@ -428,7 +435,7 @@ void PNGShape::setConvergence(float convergence) {
}
// Obtener progreso del flip actual (0.0 = inicio del flip, 1.0 = fin del flip)
float PNGShape::getFlipProgress() const {
auto PNGShape::getFlipProgress() const -> float {
if (!is_flipping_) {
return 0.0f; // No está flipping, progreso = 0
}

View File

@@ -1,15 +1,16 @@
#pragma once
#include "shape.hpp"
#include "defines.hpp" // Para PNG_IDLE_TIME_MIN/MAX constantes
#include <vector>
#include <cstdlib> // Para rand()
#include <vector>
#include "defines.hpp" // Para PNG_IDLE_TIME_MIN/MAX constantes
#include "shape.hpp"
// Figura: Shape generada desde PNG 1-bit (blanco sobre negro)
// Enfoque A: Extrusión 2D (implementado)
// Enfoque B: Voxelización 3D (preparado para futuro)
class PNGShape : public Shape {
private:
private:
// Datos de la imagen cargada
int image_width_ = 0;
int image_height_ = 0;
@@ -59,16 +60,16 @@ private:
int num_points_ = 0; // Total de puntos generados (para indexación)
// Métodos internos
bool loadPNG(const char* path); // Cargar PNG con stb_image
bool loadPNG(const char* resource_key); // Cargar PNG con stb_image
void detectEdges(); // Detectar contorno (Enfoque A)
void floodFill(); // Rellenar interior (Enfoque B - futuro)
void generateExtrudedPoints(); // Generar puntos con extrusión 2D
// Métodos de distribución adaptativa (funciones puras, no modifican parámetros)
std::vector<Point2D> extractAlternateRows(const std::vector<Point2D>& source, int row_skip); // Extraer filas alternas
std::vector<Point2D> extractCornerVertices(const std::vector<Point2D>& source); // Extraer vértices/esquinas
static std::vector<Point2D> extractAlternateRows(const std::vector<Point2D>& source, int row_skip); // Extraer filas alternas
static std::vector<Point2D> extractCornerVertices(const std::vector<Point2D>& source); // Extraer vértices/esquinas
public:
public:
// Constructor: recibe path relativo al PNG
PNGShape(const char* png_path = "data/shapes/jailgames.png");
@@ -88,7 +89,10 @@ public:
int getFlipCount() const { return flip_count_; }
// Resetear contador de flips (llamar al entrar a LOGO MODE)
void resetFlipCount() { flip_count_ = 0; was_flipping_last_frame_ = false; }
void resetFlipCount() {
flip_count_ = 0;
was_flipping_last_frame_ = false;
}
// Control de modo LOGO (flip intervals más largos)
void setLogoMode(bool enable) {

View File

@@ -2,7 +2,7 @@
// Interfaz abstracta para todas las figuras 3D
class Shape {
public:
public:
virtual ~Shape() = default;
// Generar distribución inicial de puntos en la figura

View File

@@ -1,7 +1,9 @@
#include "sphere_shape.hpp"
#include "defines.hpp"
#include <cmath>
#include "defines.hpp"
void SphereShape::generatePoints(int num_points, float screen_width, float screen_height) {
num_points_ = num_points;
radius_ = screen_height * ROTOBALL_RADIUS_FACTOR;
@@ -19,12 +21,12 @@ void SphereShape::update(float delta_time, float screen_width, float screen_heig
void SphereShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Algoritmo Fibonacci Sphere para distribución uniforme
const float golden_ratio = (1.0f + sqrtf(5.0f)) / 2.0f;
const float angle_increment = PI * 2.0f * golden_ratio;
const float GOLDEN_RATIO = (1.0f + sqrtf(5.0f)) / 2.0f;
const float ANGLE_INCREMENT = PI * 2.0f * GOLDEN_RATIO;
float t = static_cast<float>(index) / static_cast<float>(num_points_);
float phi = acosf(1.0f - 2.0f * t); // Latitud
float theta = angle_increment * static_cast<float>(index); // Longitud
float phi = acosf(1.0f - (2.0f * t)); // Latitud
float theta = ANGLE_INCREMENT * static_cast<float>(index); // Longitud
// Convertir coordenadas esféricas a cartesianas
float x_base = cosf(theta) * sinf(phi) * radius_;
@@ -34,14 +36,14 @@ void SphereShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Aplicar rotación en eje Y
float cos_y = cosf(angle_y_);
float sin_y = sinf(angle_y_);
float x_rot = x_base * cos_y - z_base * sin_y;
float z_rot = x_base * sin_y + z_base * cos_y;
float x_rot = (x_base * cos_y) - (z_base * sin_y);
float z_rot = (x_base * sin_y) + (z_base * cos_y);
// Aplicar rotación en eje X
float cos_x = cosf(angle_x_);
float sin_x = sinf(angle_x_);
float y_rot = y_base * cos_x - z_rot * sin_x;
float z_final = y_base * sin_x + z_rot * cos_x;
float y_rot = (y_base * cos_x) - (z_rot * sin_x);
float z_final = (y_base * sin_x) + (z_rot * cos_x);
// Retornar coordenadas finales rotadas
x = x_rot;
@@ -49,7 +51,7 @@ void SphereShape::getPoint3D(int index, float& x, float& y, float& z) const {
z = z_final;
}
float SphereShape::getScaleFactor(float screen_height) const {
auto SphereShape::getScaleFactor(float screen_height) const -> float {
// Factor de escala para física: proporcional al radio
// Radio base = 80px (resolución 320x240)
const float BASE_RADIUS = 80.0f;

View File

@@ -6,13 +6,13 @@
// Comportamiento: Rotación dual en ejes X e Y
// Uso anterior: RotoBall
class SphereShape : public Shape {
private:
private:
float angle_x_ = 0.0f; // Ángulo de rotación en eje X (rad)
float angle_y_ = 0.0f; // Ángulo de rotación en eje Y (rad)
float radius_ = 0.0f; // Radio de la esfera (píxeles)
int num_points_ = 0; // Cantidad de puntos generados
public:
public:
void generatePoints(int num_points, float screen_width, float screen_height) override;
void update(float delta_time, float screen_width, float screen_height) override;
void getPoint3D(int index, float& x, float& y, float& z) const override;

View File

@@ -1,7 +1,10 @@
#include "torus_shape.hpp"
#include "defines.hpp"
#include <algorithm>
#include <cmath>
#include "defines.hpp"
void TorusShape::generatePoints(int num_points, float screen_width, float screen_height) {
num_points_ = num_points;
major_radius_ = screen_height * TORUS_MAJOR_RADIUS_FACTOR;
@@ -26,10 +29,10 @@ void TorusShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Calcular número aproximado de anillos y puntos por anillo
int num_rings = static_cast<int>(sqrtf(static_cast<float>(num_points_) * 0.5f));
if (num_rings < 2) num_rings = 2;
num_rings = std::max(num_rings, 2);
int points_per_ring = num_points_ / num_rings;
if (points_per_ring < 3) points_per_ring = 3;
points_per_ring = std::max(points_per_ring, 3);
// Obtener parámetros u y v del índice
int ring = index / points_per_ring;
@@ -57,7 +60,7 @@ void TorusShape::getPoint3D(int index, float& x, float& y, float& z) const {
float cos_u = cosf(u);
float sin_u = sinf(u);
float radius_at_v = major_radius_ + minor_radius_ * cos_v;
float radius_at_v = major_radius_ + (minor_radius_ * cos_v);
float x_base = radius_at_v * cos_u;
float y_base = radius_at_v * sin_u;
@@ -66,20 +69,20 @@ void TorusShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Aplicar rotación en eje X
float cos_x = cosf(angle_x_);
float sin_x = sinf(angle_x_);
float y_rot_x = y_base * cos_x - z_base * sin_x;
float z_rot_x = y_base * sin_x + z_base * cos_x;
float y_rot_x = (y_base * cos_x) - (z_base * sin_x);
float z_rot_x = (y_base * sin_x) + (z_base * cos_x);
// Aplicar rotación en eje Y
float cos_y = cosf(angle_y_);
float sin_y = sinf(angle_y_);
float x_rot_y = x_base * cos_y - z_rot_x * sin_y;
float z_rot_y = x_base * sin_y + z_rot_x * cos_y;
float x_rot_y = (x_base * cos_y) - (z_rot_x * sin_y);
float z_rot_y = (x_base * sin_y) + (z_rot_x * cos_y);
// Aplicar rotación en eje Z
float cos_z = cosf(angle_z_);
float sin_z = sinf(angle_z_);
float x_final = x_rot_y * cos_z - y_rot_x * sin_z;
float y_final = x_rot_y * sin_z + y_rot_x * cos_z;
float x_final = (x_rot_y * cos_z) - (y_rot_x * sin_z);
float y_final = (x_rot_y * sin_z) + (y_rot_x * cos_z);
// Retornar coordenadas finales rotadas
x = x_final;
@@ -87,7 +90,7 @@ void TorusShape::getPoint3D(int index, float& x, float& y, float& z) const {
z = z_rot_y;
}
float TorusShape::getScaleFactor(float screen_height) const {
auto TorusShape::getScaleFactor(float screen_height) const -> float {
// Factor de escala para física: proporcional al radio mayor
// Radio mayor base = 60px (0.25 * 240px en resolución 320x240)
const float BASE_RADIUS = 60.0f;

View File

@@ -6,7 +6,7 @@
// Comportamiento: Superficie toroidal con rotación triple (X, Y, Z)
// Ecuaciones: x = (R + r*cos(v))*cos(u), y = (R + r*cos(v))*sin(u), z = r*sin(v)
class TorusShape : public Shape {
private:
private:
float angle_x_ = 0.0f; // Ángulo de rotación en eje X (rad)
float angle_y_ = 0.0f; // Ángulo de rotación en eje Y (rad)
float angle_z_ = 0.0f; // Ángulo de rotación en eje Z (rad)
@@ -14,7 +14,7 @@ private:
float minor_radius_ = 0.0f; // Radio menor r (grosor del tubo)
int num_points_ = 0; // Cantidad de puntos generados
public:
public:
void generatePoints(int num_points, float screen_width, float screen_height) override;
void update(float delta_time, float screen_width, float screen_height) override;
void getPoint3D(int index, float& x, float& y, float& z) const override;

View File

@@ -1,6 +1,7 @@
#include "shape_manager.hpp"
#include <algorithm> // for std::min, std::max
#include <algorithm> // for std::min, std::max, std::transform
#include <cctype> // for ::tolower
#include <cstdlib> // for rand
#include <string> // for std::string
@@ -22,26 +23,24 @@
#include "shapes/torus_shape.hpp"
ShapeManager::ShapeManager()
: engine_(nullptr)
, scene_mgr_(nullptr)
, ui_mgr_(nullptr)
, state_mgr_(nullptr)
, current_mode_(SimulationMode::PHYSICS)
, current_shape_type_(ShapeType::SPHERE)
, last_shape_type_(ShapeType::SPHERE)
, active_shape_(nullptr)
, shape_scale_factor_(1.0f)
, depth_zoom_enabled_(true)
, screen_width_(0)
, screen_height_(0)
, shape_convergence_(0.0f) {
: engine_(nullptr),
scene_mgr_(nullptr),
ui_mgr_(nullptr),
state_mgr_(nullptr),
current_mode_(SimulationMode::PHYSICS),
current_shape_type_(ShapeType::SPHERE),
last_shape_type_(ShapeType::SPHERE),
active_shape_(nullptr),
shape_scale_factor_(1.0f),
depth_zoom_enabled_(true),
screen_width_(0),
screen_height_(0),
shape_convergence_(0.0f) {
}
ShapeManager::~ShapeManager() {
}
ShapeManager::~ShapeManager() = default;
void ShapeManager::initialize(Engine* engine, SceneManager* scene_mgr, UIManager* ui_mgr,
StateManager* state_mgr, int screen_width, int screen_height) {
void ShapeManager::initialize(Engine* engine, SceneManager* scene_mgr, UIManager* ui_mgr, StateManager* state_mgr, int screen_width, int screen_height) {
engine_ = engine;
scene_mgr_ = scene_mgr;
ui_mgr_ = ui_mgr;
@@ -65,17 +64,17 @@ void ShapeManager::toggleShapeMode(bool force_gravity_on_exit) {
activateShapeInternal(last_shape_type_);
// Si estamos en modo LOGO y la figura es PNG_SHAPE, restaurar configuración LOGO
if (state_mgr_ && state_mgr_->getCurrentMode() == AppMode::LOGO && last_shape_type_ == ShapeType::PNG_SHAPE) {
if ((state_mgr_ != nullptr) && state_mgr_->getCurrentMode() == AppMode::LOGO && last_shape_type_ == ShapeType::PNG_SHAPE) {
if (active_shape_) {
PNGShape* png_shape = dynamic_cast<PNGShape*>(active_shape_.get());
if (png_shape) {
auto* png_shape = dynamic_cast<PNGShape*>(active_shape_.get());
if (png_shape != nullptr) {
png_shape->setLogoMode(true);
}
}
}
// Si estamos en LOGO MODE, resetear convergencia al entrar
if (state_mgr_ && state_mgr_->getCurrentMode() == AppMode::LOGO) {
if ((state_mgr_ != nullptr) && state_mgr_->getCurrentMode() == AppMode::LOGO) {
shape_convergence_ = 0.0f;
}
} else {
@@ -92,8 +91,8 @@ void ShapeManager::toggleShapeMode(bool force_gravity_on_exit) {
}
// Mostrar notificación (solo si NO estamos en modo demo o logo)
if (state_mgr_ && ui_mgr_ && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
ui_mgr_->showNotification("Modo Física");
if ((state_mgr_ != nullptr) && (ui_mgr_ != nullptr) && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
ui_mgr_->showNotification("Modo física");
}
}
}
@@ -112,8 +111,8 @@ void ShapeManager::handleShapeScaleChange(bool increase) {
clampShapeScale();
// Mostrar notificación si está en modo SANDBOX
if (ui_mgr_ && state_mgr_ && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
std::string notification = "Escala " + std::to_string(static_cast<int>(shape_scale_factor_ * 100.0f + 0.5f)) + "%";
if ((ui_mgr_ != nullptr) && (state_mgr_ != nullptr) && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
std::string notification = "Escala " + std::to_string(static_cast<int>((shape_scale_factor_ * 100.0f) + 0.5f)) + "%";
ui_mgr_->showNotification(notification);
}
}
@@ -124,7 +123,7 @@ void ShapeManager::resetShapeScale() {
shape_scale_factor_ = SHAPE_SCALE_DEFAULT;
// Mostrar notificación si está en modo SANDBOX
if (ui_mgr_ && state_mgr_ && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
if ((ui_mgr_ != nullptr) && (state_mgr_ != nullptr) && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
ui_mgr_->showNotification("Escala 100%");
}
}
@@ -135,14 +134,16 @@ void ShapeManager::toggleDepthZoom() {
depth_zoom_enabled_ = !depth_zoom_enabled_;
// Mostrar notificación si está en modo SANDBOX
if (ui_mgr_ && state_mgr_ && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
ui_mgr_->showNotification(depth_zoom_enabled_ ? "Profundidad On" : "Profundidad Off");
if ((ui_mgr_ != nullptr) && (state_mgr_ != nullptr) && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
ui_mgr_->showNotification(depth_zoom_enabled_ ? "Profundidad on" : "Profundidad off");
}
}
}
void ShapeManager::update(float delta_time) {
if (!active_shape_ || current_mode_ != SimulationMode::SHAPE) return;
if (!active_shape_ || current_mode_ != SimulationMode::SHAPE) {
return;
}
// Actualizar animación de la figura
active_shape_->update(delta_time, static_cast<float>(screen_width_), static_cast<float>(screen_height_));
@@ -160,7 +161,9 @@ void ShapeManager::update(float delta_time) {
// Actualizar cada pelota con física de atracción
for (size_t i = 0; i < balls.size(); i++) {
// Obtener posición 3D rotada del punto i
float x_3d, y_3d, z_3d;
float x_3d;
float y_3d;
float z_3d;
active_shape_->getPoint3D(static_cast<int>(i), x_3d, y_3d, z_3d);
// Aplicar escala manual a las coordenadas 3D
@@ -178,9 +181,7 @@ void ShapeManager::update(float delta_time) {
// Aplicar fuerza de atracción física hacia el punto rotado
// Usar constantes SHAPE (mayor pegajosidad que ROTOBALL)
float shape_size = scale_factor * 80.0f; // 80px = radio base
balls[i]->applyShapeForce(target_x, target_y, shape_size, delta_time,
SHAPE_SPRING_K, SHAPE_DAMPING_BASE, SHAPE_DAMPING_NEAR,
SHAPE_NEAR_THRESHOLD, SHAPE_MAX_FORCE);
balls[i]->applyShapeForce(target_x, target_y, shape_size, delta_time, SHAPE_SPRING_K, SHAPE_DAMPING_BASE, SHAPE_DAMPING_NEAR, SHAPE_NEAR_THRESHOLD, SHAPE_MAX_FORCE);
// Calcular brillo según profundidad Z para renderizado
// Normalizar Z al rango de la figura (asumiendo simetría ±shape_size)
@@ -190,12 +191,12 @@ void ShapeManager::update(float delta_time) {
// Calcular escala según profundidad Z (perspectiva) - solo si está activado
// 0.0 (fondo) → 0.5x, 0.5 (medio) → 1.0x, 1.0 (frente) → 1.5x
float depth_scale = depth_zoom_enabled_ ? (0.5f + z_normalized * 1.0f) : 1.0f;
float depth_scale = depth_zoom_enabled_ ? (0.5f + (z_normalized * 1.0f)) : 1.0f;
balls[i]->setDepthScale(depth_scale);
}
// Calcular convergencia en LOGO MODE (% de pelotas cerca de su objetivo)
if (state_mgr_ && state_mgr_->getCurrentMode() == AppMode::LOGO && current_mode_ == SimulationMode::SHAPE) {
if ((state_mgr_ != nullptr) && state_mgr_->getCurrentMode() == AppMode::LOGO && current_mode_ == SimulationMode::SHAPE) {
int balls_near = 0;
float distance_threshold = LOGO_CONVERGENCE_DISTANCE; // 20px fijo (más permisivo)
@@ -214,7 +215,9 @@ void ShapeManager::update(float delta_time) {
}
void ShapeManager::generateShape() {
if (!active_shape_) return;
if (!active_shape_) {
return;
}
int num_points = static_cast<int>(scene_mgr_->getBallCount());
active_shape_->generatePoints(num_points, static_cast<float>(screen_width_), static_cast<float>(screen_height_));
@@ -276,8 +279,10 @@ void ShapeManager::activateShapeInternal(ShapeType type) {
scene_mgr_->enableShapeAttractionAll(true);
// Mostrar notificación con nombre de figura (solo si NO estamos en modo demo o logo)
if (active_shape_ && state_mgr_ && ui_mgr_ && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
std::string notification = std::string("Modo ") + active_shape_->getName();
if (active_shape_ && (state_mgr_ != nullptr) && (ui_mgr_ != nullptr) && state_mgr_->getCurrentMode() == AppMode::SANDBOX) {
std::string shape_name = active_shape_->getName();
std::ranges::transform(shape_name, shape_name.begin(), ::tolower);
std::string notification = std::string("Modo ") + shape_name;
ui_mgr_->showNotification(notification);
}
}

View File

@@ -46,8 +46,7 @@ class ShapeManager {
* @param screen_width Ancho lógico de pantalla
* @param screen_height Alto lógico de pantalla
*/
void initialize(Engine* engine, SceneManager* scene_mgr, UIManager* ui_mgr,
StateManager* state_mgr, int screen_width, int screen_height);
void initialize(Engine* engine, SceneManager* scene_mgr, UIManager* ui_mgr, StateManager* state_mgr, int screen_width, int screen_height);
/**
* @brief Toggle entre modo PHYSICS y SHAPE

View File

@@ -1,39 +1,37 @@
#include "state_manager.hpp"
#include <algorithm> // for std::min
#include <array> // for std::array
#include <cstdlib> // for rand
#include <vector> // for std::vector
#include "defines.hpp" // for constantes DEMO/LOGO
#include "engine.hpp" // for Engine (enter/exitShapeMode, texture)
#include "scene/scene_manager.hpp" // for SceneManager
#include "shapes_mgr/shape_manager.hpp" // for ShapeManager
#include "shapes/png_shape.hpp" // for PNGShape flip detection
#include "shapes_mgr/shape_manager.hpp" // for ShapeManager
#include "theme_manager.hpp" // for ThemeManager
StateManager::StateManager()
: engine_(nullptr)
, scene_mgr_(nullptr)
, theme_mgr_(nullptr)
, shape_mgr_(nullptr)
, current_app_mode_(AppMode::SANDBOX)
, previous_app_mode_(AppMode::SANDBOX)
, demo_timer_(0.0f)
, demo_next_action_time_(0.0f)
, logo_convergence_threshold_(0.90f)
, logo_min_time_(3.0f)
, logo_max_time_(5.0f)
, logo_waiting_for_flip_(false)
, logo_target_flip_number_(0)
, logo_target_flip_percentage_(0.0f)
, logo_current_flip_count_(0)
, logo_entered_manually_(false)
, logo_previous_theme_(0)
, logo_previous_texture_index_(0)
, logo_previous_shape_scale_(1.0f) {
}
StateManager::~StateManager() {
: engine_(nullptr),
scene_mgr_(nullptr),
theme_mgr_(nullptr),
shape_mgr_(nullptr),
current_app_mode_(AppMode::SANDBOX),
previous_app_mode_(AppMode::SANDBOX),
demo_timer_(0.0f),
demo_next_action_time_(0.0f),
logo_convergence_threshold_(0.90f),
logo_min_time_(3.0f),
logo_max_time_(5.0f),
logo_waiting_for_flip_(false),
logo_target_flip_number_(0),
logo_target_flip_percentage_(0.0f),
logo_current_flip_count_(0),
logo_entered_manually_(false),
logo_previous_theme_(0),
logo_previous_texture_index_(0),
logo_previous_shape_scale_(1.0f) {
}
void StateManager::initialize(Engine* engine, SceneManager* scene_mgr, ThemeManager* theme_mgr, ShapeManager* shape_mgr) {
@@ -53,8 +51,10 @@ void StateManager::setLogoPreviousState(int theme, size_t texture_index, float s
// ACTUALIZACIÓN DE ESTADOS
// ===========================================================================
void StateManager::update(float delta_time, float shape_convergence, Shape* active_shape) {
if (current_app_mode_ == AppMode::SANDBOX) return;
void StateManager::update(float delta_time, float shape_convergence, Shape* active_shape) { // NOLINT(readability-function-cognitive-complexity)
if (current_app_mode_ == AppMode::SANDBOX) {
return;
}
demo_timer_ += delta_time;
@@ -63,14 +63,12 @@ void StateManager::update(float delta_time, float shape_convergence, Shape* acti
if (current_app_mode_ == AppMode::LOGO) {
if (logo_waiting_for_flip_) {
// CAMINO B: Esperando a que ocurran flips
PNGShape* png_shape = dynamic_cast<PNGShape*>(active_shape);
auto* png_shape = dynamic_cast<PNGShape*>(active_shape);
if (png_shape) {
if (png_shape != nullptr) {
int current_flip_count = png_shape->getFlipCount();
if (current_flip_count > logo_current_flip_count_) {
logo_current_flip_count_ = current_flip_count;
}
logo_current_flip_count_ = std::max(current_flip_count, logo_current_flip_count_);
if (logo_current_flip_count_ + 1 >= logo_target_flip_number_) {
if (png_shape->isFlipping()) {
@@ -93,7 +91,9 @@ void StateManager::update(float delta_time, float shape_convergence, Shape* acti
should_trigger = demo_timer_ >= demo_next_action_time_;
}
if (!should_trigger) return;
if (!should_trigger) {
return;
}
if (current_app_mode_ == AppMode::LOGO) {
// LOGO MODE: Sistema de acciones variadas con gravedad dinámica
@@ -122,8 +122,8 @@ void StateManager::update(float delta_time, float shape_convergence, Shape* acti
logo_target_flip_percentage_ = LOGO_FLIP_TRIGGER_MIN + (rand() % 1000) / 1000.0f * (LOGO_FLIP_TRIGGER_MAX - LOGO_FLIP_TRIGGER_MIN);
logo_current_flip_count_ = 0;
PNGShape* png_shape = dynamic_cast<PNGShape*>(shape_mgr_->getActiveShape());
if (png_shape) {
auto* png_shape = dynamic_cast<PNGShape*>(shape_mgr_->getActiveShape());
if (png_shape != nullptr) {
png_shape->resetFlipCount();
}
// No hacer nada más — esperar a que ocurran los flips
@@ -158,7 +158,7 @@ void StateManager::update(float delta_time, float shape_convergence, Shape* acti
scene_mgr_->forceBallsGravityOff();
} else {
// 16%: Cambiar dirección de gravedad
GravityDirection new_direction = static_cast<GravityDirection>(rand() % 4);
auto new_direction = static_cast<GravityDirection>(rand() % 4);
scene_mgr_->changeGravityDirection(new_direction);
scene_mgr_->forceBallsGravityOn();
}
@@ -186,7 +186,9 @@ void StateManager::update(float delta_time, float shape_convergence, Shape* acti
}
void StateManager::setState(AppMode new_mode, int current_screen_width, int current_screen_height) {
if (current_app_mode_ == new_mode) return;
if (current_app_mode_ == new_mode) {
return;
}
if (current_app_mode_ == AppMode::LOGO && new_mode != AppMode::LOGO) {
previous_app_mode_ = new_mode;
@@ -201,7 +203,8 @@ void StateManager::setState(AppMode new_mode, int current_screen_width, int curr
demo_timer_ = 0.0f;
if (new_mode == AppMode::DEMO || new_mode == AppMode::DEMO_LITE || new_mode == AppMode::LOGO) {
float min_interval, max_interval;
float min_interval;
float max_interval;
if (new_mode == AppMode::LOGO) {
float resolution_scale = current_screen_height / 720.0f;
@@ -250,8 +253,10 @@ void StateManager::toggleLogoMode(int current_screen_width, int current_screen_h
// ACCIONES DE DEMO
// ===========================================================================
void StateManager::performDemoAction(bool is_lite) {
if (!engine_ || !scene_mgr_ || !theme_mgr_ || !shape_mgr_) return;
void StateManager::performDemoAction(bool is_lite) { // NOLINT(readability-function-cognitive-complexity)
if ((engine_ == nullptr) || (scene_mgr_ == nullptr) || (theme_mgr_ == nullptr) || (shape_mgr_ == nullptr)) {
return;
}
// ============================================
// SALTO AUTOMÁTICO A LOGO MODE (Easter Egg)
@@ -278,18 +283,18 @@ void StateManager::performDemoAction(bool is_lite) {
// ACCIONES NORMALES DE DEMO/DEMO_LITE
// ============================================
int TOTAL_WEIGHT;
int total_weight;
int random_value;
int accumulated_weight = 0;
if (is_lite) {
TOTAL_WEIGHT = DEMO_LITE_WEIGHT_GRAVITY_DIR + DEMO_LITE_WEIGHT_GRAVITY_TOGGLE + DEMO_LITE_WEIGHT_SHAPE + DEMO_LITE_WEIGHT_TOGGLE_PHYSICS + DEMO_LITE_WEIGHT_IMPULSE;
random_value = rand() % TOTAL_WEIGHT;
total_weight = DEMO_LITE_WEIGHT_GRAVITY_DIR + DEMO_LITE_WEIGHT_GRAVITY_TOGGLE + DEMO_LITE_WEIGHT_SHAPE + DEMO_LITE_WEIGHT_TOGGLE_PHYSICS + DEMO_LITE_WEIGHT_IMPULSE;
random_value = rand() % total_weight;
// Cambiar dirección gravedad (25%)
accumulated_weight += DEMO_LITE_WEIGHT_GRAVITY_DIR;
if (random_value < accumulated_weight) {
GravityDirection new_direction = static_cast<GravityDirection>(rand() % 4);
auto new_direction = static_cast<GravityDirection>(rand() % 4);
scene_mgr_->changeGravityDirection(new_direction);
return;
}
@@ -304,8 +309,8 @@ void StateManager::performDemoAction(bool is_lite) {
// Activar figura 3D (25%) - PNG_SHAPE excluido
accumulated_weight += DEMO_LITE_WEIGHT_SHAPE;
if (random_value < accumulated_weight) {
ShapeType shapes[] = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX, ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER, ShapeType::ICOSAHEDRON, ShapeType::ATOM};
engine_->enterShapeMode(shapes[rand() % 8]);
constexpr std::array<ShapeType, 8> SHAPES = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX, ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER, ShapeType::ICOSAHEDRON, ShapeType::ATOM};
engine_->enterShapeMode(SHAPES[rand() % 8]);
return;
}
@@ -324,13 +329,13 @@ void StateManager::performDemoAction(bool is_lite) {
}
} else {
TOTAL_WEIGHT = DEMO_WEIGHT_GRAVITY_DIR + DEMO_WEIGHT_GRAVITY_TOGGLE + DEMO_WEIGHT_SHAPE + DEMO_WEIGHT_TOGGLE_PHYSICS + DEMO_WEIGHT_REGENERATE_SHAPE + DEMO_WEIGHT_THEME + DEMO_WEIGHT_SCENARIO + DEMO_WEIGHT_IMPULSE + DEMO_WEIGHT_DEPTH_ZOOM + DEMO_WEIGHT_SHAPE_SCALE + DEMO_WEIGHT_SPRITE;
random_value = rand() % TOTAL_WEIGHT;
total_weight = DEMO_WEIGHT_GRAVITY_DIR + DEMO_WEIGHT_GRAVITY_TOGGLE + DEMO_WEIGHT_SHAPE + DEMO_WEIGHT_TOGGLE_PHYSICS + DEMO_WEIGHT_REGENERATE_SHAPE + DEMO_WEIGHT_THEME + DEMO_WEIGHT_SCENARIO + DEMO_WEIGHT_IMPULSE + DEMO_WEIGHT_DEPTH_ZOOM + DEMO_WEIGHT_SHAPE_SCALE + DEMO_WEIGHT_SPRITE;
random_value = rand() % total_weight;
// Cambiar dirección gravedad (10%)
accumulated_weight += DEMO_WEIGHT_GRAVITY_DIR;
if (random_value < accumulated_weight) {
GravityDirection new_direction = static_cast<GravityDirection>(rand() % 4);
auto new_direction = static_cast<GravityDirection>(rand() % 4);
scene_mgr_->changeGravityDirection(new_direction);
return;
}
@@ -345,8 +350,8 @@ void StateManager::performDemoAction(bool is_lite) {
// Activar figura 3D (20%) - PNG_SHAPE excluido
accumulated_weight += DEMO_WEIGHT_SHAPE;
if (random_value < accumulated_weight) {
ShapeType shapes[] = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX, ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER, ShapeType::ICOSAHEDRON, ShapeType::ATOM};
engine_->enterShapeMode(shapes[rand() % 8]);
constexpr std::array<ShapeType, 8> SHAPES = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX, ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER, ShapeType::ICOSAHEDRON, ShapeType::ATOM};
engine_->enterShapeMode(SHAPES[rand() % 8]);
return;
}
@@ -378,10 +383,12 @@ void StateManager::performDemoAction(bool is_lite) {
if (random_value < accumulated_weight) {
int auto_max = std::min(engine_->getMaxAutoScenario(), DEMO_AUTO_MAX_SCENARIO);
std::vector<int> candidates;
for (int i = DEMO_AUTO_MIN_SCENARIO; i <= auto_max; ++i)
for (int i = DEMO_AUTO_MIN_SCENARIO; i <= auto_max; ++i) {
candidates.push_back(i);
if (engine_->isCustomScenarioEnabled() && engine_->isCustomAutoAvailable())
}
if (engine_->isCustomScenarioEnabled() && engine_->isCustomAutoAvailable()) {
candidates.push_back(CUSTOM_SCENARIO_IDX);
}
int new_scenario = candidates[rand() % candidates.size()];
SimulationMode current_sim_mode = shape_mgr_->getCurrentMode();
scene_mgr_->changeScenario(new_scenario, current_sim_mode);
@@ -439,15 +446,15 @@ void StateManager::performDemoAction(bool is_lite) {
// RANDOMIZACIÓN AL INICIAR DEMO
// ===========================================================================
void StateManager::randomizeOnDemoStart(bool is_lite) {
if (!engine_ || !scene_mgr_ || !theme_mgr_ || !shape_mgr_) return;
void StateManager::randomizeOnDemoStart(bool is_lite) { // NOLINT(readability-function-cognitive-complexity)
if ((engine_ == nullptr) || (scene_mgr_ == nullptr) || (theme_mgr_ == nullptr) || (shape_mgr_ == nullptr)) {
return;
}
// Si venimos de LOGO con PNG_SHAPE, cambiar figura obligatoriamente
if (shape_mgr_->getCurrentShapeType() == ShapeType::PNG_SHAPE) {
ShapeType shapes[] = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX,
ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER,
ShapeType::ICOSAHEDRON, ShapeType::ATOM};
engine_->enterShapeMode(shapes[rand() % 8]);
constexpr std::array<ShapeType, 8> SHAPES = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX, ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER, ShapeType::ICOSAHEDRON, ShapeType::ATOM};
engine_->enterShapeMode(SHAPES[rand() % 8]);
}
if (is_lite) {
@@ -457,11 +464,11 @@ void StateManager::randomizeOnDemoStart(bool is_lite) {
engine_->exitShapeMode(false);
}
} else {
ShapeType shapes[] = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX, ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER, ShapeType::ICOSAHEDRON, ShapeType::ATOM};
engine_->enterShapeMode(shapes[rand() % 8]);
constexpr std::array<ShapeType, 8> SHAPES = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX, ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER, ShapeType::ICOSAHEDRON, ShapeType::ATOM};
engine_->enterShapeMode(SHAPES[rand() % 8]);
}
GravityDirection new_direction = static_cast<GravityDirection>(rand() % 4);
auto new_direction = static_cast<GravityDirection>(rand() % 4);
scene_mgr_->changeGravityDirection(new_direction);
if (rand() % 2 == 0) {
toggleGravityOnOff();
@@ -476,14 +483,14 @@ void StateManager::randomizeOnDemoStart(bool is_lite) {
engine_->exitShapeMode(false);
}
} else {
ShapeType shapes[] = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX, ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER, ShapeType::ICOSAHEDRON, ShapeType::ATOM};
ShapeType selected_shape = shapes[rand() % 8];
constexpr std::array<ShapeType, 8> SHAPES = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX, ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER, ShapeType::ICOSAHEDRON, ShapeType::ATOM};
ShapeType selected_shape = SHAPES[rand() % 8];
// Randomizar profundidad y escala ANTES de activar la figura
if (rand() % 2 == 0) {
shape_mgr_->setDepthZoomEnabled(!shape_mgr_->isDepthZoomEnabled());
}
shape_mgr_->setShapeScaleFactor(0.5f + (rand() % 1500) / 1000.0f);
shape_mgr_->setShapeScaleFactor(0.5f + ((rand() % 1500) / 1000.0f));
engine_->enterShapeMode(selected_shape);
}
@@ -491,10 +498,12 @@ void StateManager::randomizeOnDemoStart(bool is_lite) {
// 2. Escenario
int auto_max = std::min(engine_->getMaxAutoScenario(), DEMO_AUTO_MAX_SCENARIO);
std::vector<int> candidates;
for (int i = DEMO_AUTO_MIN_SCENARIO; i <= auto_max; ++i)
for (int i = DEMO_AUTO_MIN_SCENARIO; i <= auto_max; ++i) {
candidates.push_back(i);
if (engine_->isCustomScenarioEnabled() && engine_->isCustomAutoAvailable())
}
if (engine_->isCustomScenarioEnabled() && engine_->isCustomAutoAvailable()) {
candidates.push_back(CUSTOM_SCENARIO_IDX);
}
int new_scenario = candidates[rand() % candidates.size()];
SimulationMode current_sim_mode = shape_mgr_->getCurrentMode();
scene_mgr_->changeScenario(new_scenario, current_sim_mode);
@@ -513,7 +522,7 @@ void StateManager::randomizeOnDemoStart(bool is_lite) {
}
// 5. Gravedad
GravityDirection new_direction = static_cast<GravityDirection>(rand() % 4);
auto new_direction = static_cast<GravityDirection>(rand() % 4);
scene_mgr_->changeGravityDirection(new_direction);
if (rand() % 3 == 0) {
toggleGravityOnOff();
@@ -526,7 +535,9 @@ void StateManager::randomizeOnDemoStart(bool is_lite) {
// ===========================================================================
void StateManager::toggleGravityOnOff() {
if (!scene_mgr_) return;
if (scene_mgr_ == nullptr) {
return;
}
bool gravity_enabled = scene_mgr_->hasBalls() &&
(scene_mgr_->getFirstBall()->getGravityForce() > 0.0f);
@@ -543,7 +554,9 @@ void StateManager::toggleGravityOnOff() {
// ===========================================================================
void StateManager::enterLogoMode(bool from_demo, int current_screen_width, int current_screen_height, size_t ball_count) {
if (!engine_ || !scene_mgr_ || !theme_mgr_ || !shape_mgr_) return;
if ((engine_ == nullptr) || (scene_mgr_ == nullptr) || (theme_mgr_ == nullptr) || (shape_mgr_ == nullptr)) {
return;
}
logo_entered_manually_ = !from_demo;
@@ -585,8 +598,8 @@ void StateManager::enterLogoMode(bool from_demo, int current_screen_width, int c
}
// Cambiar a tema aleatorio entre: MONOCHROME, LAVENDER, CRIMSON, ESMERALDA
int logo_themes[] = {5, 6, 7, 8};
theme_mgr_->switchToTheme(logo_themes[rand() % 4]);
constexpr std::array<int, 4> LOGO_THEMES = {5, 6, 7, 8};
theme_mgr_->switchToTheme(LOGO_THEMES[rand() % 4]);
// Establecer escala a 120%
shape_mgr_->setShapeScaleFactor(LOGO_MODE_SHAPE_SCALE);
@@ -595,8 +608,8 @@ void StateManager::enterLogoMode(bool from_demo, int current_screen_width, int c
engine_->enterShapeMode(ShapeType::PNG_SHAPE);
// Configurar PNG_SHAPE en modo LOGO
PNGShape* png_shape = dynamic_cast<PNGShape*>(shape_mgr_->getActiveShape());
if (png_shape) {
auto* png_shape = dynamic_cast<PNGShape*>(shape_mgr_->getActiveShape());
if (png_shape != nullptr) {
png_shape->setLogoMode(true);
png_shape->resetFlipCount();
}
@@ -607,8 +620,12 @@ void StateManager::enterLogoMode(bool from_demo, int current_screen_width, int c
// ===========================================================================
void StateManager::exitLogoMode(bool return_to_demo) {
if (current_app_mode_ != AppMode::LOGO) return;
if (!engine_ || !scene_mgr_ || !theme_mgr_ || !shape_mgr_) return;
if (current_app_mode_ != AppMode::LOGO) {
return;
}
if ((engine_ == nullptr) || (scene_mgr_ == nullptr) || (theme_mgr_ == nullptr) || (shape_mgr_ == nullptr)) {
return;
}
logo_entered_manually_ = false;
@@ -624,17 +641,15 @@ void StateManager::exitLogoMode(bool return_to_demo) {
}
// Desactivar modo LOGO en PNG_SHAPE
PNGShape* png_shape = dynamic_cast<PNGShape*>(shape_mgr_->getActiveShape());
if (png_shape) {
auto* png_shape = dynamic_cast<PNGShape*>(shape_mgr_->getActiveShape());
if (png_shape != nullptr) {
png_shape->setLogoMode(false);
}
// Si la figura activa es PNG_SHAPE, cambiar a otra figura aleatoria
if (shape_mgr_->getCurrentShapeType() == ShapeType::PNG_SHAPE) {
ShapeType shapes[] = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX,
ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER,
ShapeType::ICOSAHEDRON, ShapeType::ATOM};
engine_->enterShapeMode(shapes[rand() % 8]);
constexpr std::array<ShapeType, 8> SHAPES = {ShapeType::SPHERE, ShapeType::LISSAJOUS, ShapeType::HELIX, ShapeType::TORUS, ShapeType::CUBE, ShapeType::CYLINDER, ShapeType::ICOSAHEDRON, ShapeType::ATOM};
engine_->enterShapeMode(SHAPES[rand() % 8]);
}
if (!return_to_demo) {

View File

@@ -1,6 +1,7 @@
#pragma once
#include <SDL3/SDL_stdinc.h> // for Uint64
#include <cstddef> // for size_t
#include "defines.hpp" // for AppMode, ShapeType, GravityDirection
@@ -37,7 +38,7 @@ class StateManager {
/**
* @brief Destructor
*/
~StateManager();
~StateManager() = default;
/**
* @brief Inicializa el StateManager con referencias a los subsistemas necesarios

View File

@@ -1,23 +1,32 @@
#include "textrenderer.hpp"
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <iostream>
#include "resource_manager.hpp"
TextRenderer::TextRenderer() : renderer_(nullptr), font_(nullptr), font_size_(0), use_antialiasing_(true), font_data_buffer_(nullptr) {
TextRenderer::TextRenderer()
: renderer_(nullptr),
font_(nullptr),
font_size_(0),
use_antialiasing_(true),
font_data_buffer_(nullptr) {
}
TextRenderer::~TextRenderer() {
cleanup();
}
bool TextRenderer::init(SDL_Renderer* renderer, const char* font_path, int font_size, bool use_antialiasing) {
auto TextRenderer::init(SDL_Renderer* renderer, const char* font_path, int font_size, bool use_antialiasing) -> bool {
renderer_ = renderer;
font_size_ = font_size;
use_antialiasing_ = use_antialiasing;
font_path_ = font_path; // Guardar ruta para reinitialize()
// Inicializar SDL_ttf si no está inicializado
if (!TTF_WasInit()) {
if (TTF_WasInit() == 0) {
if (!TTF_Init()) {
SDL_Log("Error al inicializar SDL_ttf: %s", SDL_GetError());
return false;
@@ -25,30 +34,33 @@ bool TextRenderer::init(SDL_Renderer* renderer, const char* font_path, int font_
}
// Intentar cargar la fuente desde ResourceManager (pack o disco)
unsigned char* fontData = nullptr;
size_t fontDataSize = 0;
unsigned char* font_data = nullptr;
size_t font_data_size = 0;
if (ResourceManager::loadResource(font_path, fontData, fontDataSize)) {
if (ResourceManager::loadResource(font_path, font_data, font_data_size)) {
// Crear SDL_IOStream desde memoria
SDL_IOStream* fontIO = SDL_IOFromConstMem(fontData, static_cast<size_t>(fontDataSize));
if (fontIO != nullptr) {
SDL_IOStream* font_io = SDL_IOFromConstMem(font_data, font_data_size);
if (font_io != nullptr) {
// Cargar fuente desde IOStream
font_ = TTF_OpenFontIO(fontIO, true, font_size); // true = cerrar stream automáticamente
font_ = TTF_OpenFontIO(font_io, true, font_size); // true = cerrar stream automáticamente
if (font_ == nullptr) {
SDL_Log("Error al cargar fuente desde memoria '%s': %s", font_path, SDL_GetError());
delete[] fontData; // Liberar solo si falla la carga
delete[] font_data; // Liberar solo si falla la carga
return false;
}
// CRÍTICO: NO eliminar fontData aquí - SDL_ttf necesita estos datos en memoria
// mientras la fuente esté abierta. Se liberará en cleanup()
font_data_buffer_ = fontData;
SDL_Log("Fuente cargada desde ResourceManager: %s (%lu bytes)", font_path, (unsigned long)fontDataSize);
return true;
} else {
delete[] fontData;
font_data_buffer_ = font_data;
{
std::string fn = std::string(font_path);
fn = fn.substr(fn.find_last_of("\\/") + 1);
std::cout << "[Fuente] " << fn << " (" << (ResourceManager::isPackLoaded() ? "pack" : "disco") << ")\n";
}
return true;
}
delete[] font_data;
}
// Fallback final: intentar cargar directamente desde disco (por si falla ResourceManager)
@@ -61,7 +73,7 @@ bool TextRenderer::init(SDL_Renderer* renderer, const char* font_path, int font_
return true;
}
bool TextRenderer::reinitialize(int new_font_size) {
auto TextRenderer::reinitialize(int new_font_size) -> bool {
// Verificar que tenemos todo lo necesario
if (renderer_ == nullptr || font_path_.empty()) {
SDL_Log("Error: TextRenderer no inicializado correctamente para reinitialize()");
@@ -84,36 +96,42 @@ bool TextRenderer::reinitialize(int new_font_size) {
}
// Intentar cargar la fuente desde ResourceManager con el nuevo tamaño
unsigned char* fontData = nullptr;
size_t fontDataSize = 0;
unsigned char* font_data = nullptr;
size_t font_data_size = 0;
if (ResourceManager::loadResource(font_path_, fontData, fontDataSize)) {
SDL_IOStream* fontIO = SDL_IOFromConstMem(fontData, static_cast<size_t>(fontDataSize));
if (fontIO != nullptr) {
font_ = TTF_OpenFontIO(fontIO, true, new_font_size);
if (ResourceManager::loadResource(font_path_, font_data, font_data_size)) {
SDL_IOStream* font_io = SDL_IOFromConstMem(font_data, font_data_size);
if (font_io != nullptr) {
font_ = TTF_OpenFontIO(font_io, true, new_font_size);
if (font_ == nullptr) {
SDL_Log("Error al recargar fuente '%s' con tamaño %d: %s",
font_path_.c_str(), new_font_size, SDL_GetError());
delete[] fontData; // Liberar solo si falla
font_path_.c_str(),
new_font_size,
SDL_GetError());
delete[] font_data; // Liberar solo si falla
return false;
}
// Mantener buffer en memoria (NO eliminar)
font_data_buffer_ = fontData;
font_data_buffer_ = font_data;
font_size_ = new_font_size;
SDL_Log("Fuente recargada desde ResourceManager: %s (tamaño %d)", font_path_.c_str(), new_font_size);
return true;
} else {
delete[] fontData;
{
std::string fn = font_path_.substr(font_path_.find_last_of("\\/") + 1);
std::cout << "[Fuente] " << fn << " (" << (ResourceManager::isPackLoaded() ? "pack" : "disco") << ")\n";
}
return true;
}
delete[] font_data;
}
// Fallback: cargar directamente desde disco
font_ = TTF_OpenFont(font_path_.c_str(), new_font_size);
if (font_ == nullptr) {
SDL_Log("Error al recargar fuente '%s' con tamaño %d: %s",
font_path_.c_str(), new_font_size, SDL_GetError());
font_path_.c_str(),
new_font_size,
SDL_GetError());
return false;
}
@@ -225,7 +243,8 @@ void TextRenderer::printPhysical(int logical_x, int logical_y, const char* text,
dest_rect.h = static_cast<float>(text_surface->h);
// Deshabilitar temporalmente presentación lógica para renderizar en píxeles físicos
int logical_w = 0, logical_h = 0;
int logical_w = 0;
int logical_h = 0;
SDL_RendererLogicalPresentation presentation_mode;
SDL_GetRenderLogicalPresentation(renderer_, &logical_w, &logical_h, &presentation_mode);
@@ -293,7 +312,8 @@ void TextRenderer::printAbsolute(int physical_x, int physical_y, const char* tex
dest_rect.h = static_cast<float>(text_surface->h);
// Deshabilitar temporalmente presentación lógica para renderizar en píxeles físicos
int logical_w = 0, logical_h = 0;
int logical_w = 0;
int logical_h = 0;
SDL_RendererLogicalPresentation presentation_mode;
SDL_GetRenderLogicalPresentation(renderer_, &logical_w, &logical_h, &presentation_mode);
@@ -324,7 +344,7 @@ void TextRenderer::printAbsoluteShadowed(int physical_x, int physical_y, const s
printAbsoluteShadowed(physical_x, physical_y, text.c_str());
}
int TextRenderer::getTextWidth(const char* text) {
auto TextRenderer::getTextWidth(const char* text) -> int {
if (!isInitialized() || text == nullptr) {
return 0;
}
@@ -337,7 +357,7 @@ int TextRenderer::getTextWidth(const char* text) {
return width;
}
int TextRenderer::getTextWidthPhysical(const char* text) {
auto TextRenderer::getTextWidthPhysical(const char* text) -> int {
// Retorna el ancho REAL en píxeles físicos (sin escalado lógico)
// Idéntico a getTextWidth() pero semánticamente diferente:
// - Este método se usa cuando se necesita el ancho REAL de la fuente
@@ -354,10 +374,18 @@ int TextRenderer::getTextWidthPhysical(const char* text) {
return width; // Ancho real de la textura generada por TTF
}
int TextRenderer::getTextHeight() {
auto TextRenderer::getTextHeight() -> int {
if (!isInitialized()) {
return 0;
}
return TTF_GetFontHeight(font_);
}
auto TextRenderer::getGlyphHeight() -> int {
if (!isInitialized()) {
return 0;
}
return TTF_GetFontAscent(font_) - TTF_GetFontDescent(font_);
}

View File

@@ -2,10 +2,11 @@
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <string>
class TextRenderer {
public:
public:
TextRenderer();
~TextRenderer();
@@ -42,13 +43,16 @@ public:
// Útil para notificaciones y elementos UI de tamaño fijo
int getTextWidthPhysical(const char* text);
// Obtiene la altura de la fuente
// Obtiene la altura de la fuente (incluye line_gap)
int getTextHeight();
// Obtiene la altura real del glifo (ascender + |descendente|, sin line_gap)
int getGlyphHeight();
// Verifica si está inicializado correctamente
bool isInitialized() const { return font_ != nullptr && renderer_ != nullptr; }
private:
private:
SDL_Renderer* renderer_;
TTF_Font* font_;
int font_size_;

File diff suppressed because it is too large Load Diff

View File

@@ -43,6 +43,7 @@ class ThemeManager {
// Inicialización
void initialize(); // Inicializa 15 temas unificados (9 estáticos + 6 dinámicos)
void setMaxBallCount(int n) { max_ball_count_ = n; } // Máximo real (escenario 8 o custom si mayor)
// Interfaz unificada (PHASE 2 + PHASE 3)
void switchToTheme(int theme_index); // Cambia a tema 0-14 con transición LERP suave (PHASE 3)
@@ -53,8 +54,7 @@ class ThemeManager {
// Queries de colores (usado en rendering)
Color getInterpolatedColor(size_t ball_index) const; // Obtiene color interpolado para pelota
void getBackgroundColors(float& top_r, float& top_g, float& top_b,
float& bottom_r, float& bottom_g, float& bottom_b) const; // Obtiene colores de fondo degradado
void getBackgroundColors(float& top_r, float& top_g, float& top_b, float& bottom_r, float& bottom_g, float& bottom_b) const; // Obtiene colores de fondo degradado
// Queries de estado (para debug display y lógica)
int getCurrentThemeIndex() const { return current_theme_index_; }
@@ -99,6 +99,9 @@ class ThemeManager {
// Snapshot del tema origen (capturado al iniciar transición)
std::unique_ptr<ThemeSnapshot> source_snapshot_; // nullptr si no hay transición
// Máximo de bolas posible en esta sesión (max(SCENE_BALLS_8, custom_balls))
int max_ball_count_ = SCENE_BALLS_8;
// ========================================
// MÉTODOS PRIVADOS
// ========================================

View File

@@ -1,19 +1,15 @@
#include "dynamic_theme.hpp"
#include <algorithm> // for std::min
DynamicTheme::DynamicTheme(const char* name_en, const char* name_es,
int text_r, int text_g, int text_b,
std::vector<DynamicThemeKeyframe> keyframes,
bool loop)
DynamicTheme::DynamicTheme(const char* name_en, const char* name_es, int text_r, int text_g, int text_b, std::vector<DynamicThemeKeyframe> keyframes, bool loop)
: name_en_(name_en),
name_es_(name_es),
text_r_(text_r), text_g_(text_g), text_b_(text_b),
text_r_(text_r),
text_g_(text_g),
text_b_(text_b),
keyframes_(std::move(keyframes)),
loop_(loop),
current_keyframe_index_(0),
target_keyframe_index_(1),
transition_progress_(0.0f),
paused_(false) {
loop_(loop) {
// Validación: mínimo 2 keyframes
if (keyframes_.size() < 2) {
// Fallback: duplicar primer keyframe si solo hay 1
@@ -29,7 +25,9 @@ DynamicTheme::DynamicTheme(const char* name_en, const char* name_es,
}
void DynamicTheme::update(float delta_time) {
if (paused_) return; // No actualizar si está pausado
if (paused_) {
return; // No actualizar si está pausado
}
// Obtener duración del keyframe objetivo
float duration = keyframes_[target_keyframe_index_].duration;
@@ -74,14 +72,14 @@ void DynamicTheme::advanceToNextKeyframe() {
transition_progress_ = 0.0f;
}
Color DynamicTheme::getBallColor(size_t ball_index, float progress) const {
auto DynamicTheme::getBallColor(size_t ball_index, float progress) const -> Color {
// Obtener keyframes actual y objetivo
const auto& current_kf = keyframes_[current_keyframe_index_];
const auto& target_kf = keyframes_[target_keyframe_index_];
// Si paletas vacías, retornar blanco
if (current_kf.ball_colors.empty() || target_kf.ball_colors.empty()) {
return {255, 255, 255};
return {.r = 255, .g = 255, .b = 255};
}
// Obtener colores de ambos keyframes (con wrap)
@@ -95,15 +93,18 @@ Color DynamicTheme::getBallColor(size_t ball_index, float progress) const {
// (progress parámetro será usado en PHASE 3 para LERP externo)
float t = transition_progress_;
return {
static_cast<int>(lerp(c1.r, c2.r, t)),
static_cast<int>(lerp(c1.g, c2.g, t)),
static_cast<int>(lerp(c1.b, c2.b, t))
};
.r = static_cast<int>(lerp(c1.r, c2.r, t)),
.g = static_cast<int>(lerp(c1.g, c2.g, t)),
.b = static_cast<int>(lerp(c1.b, c2.b, t))};
}
void DynamicTheme::getBackgroundColors(float progress,
float& tr, float& tg, float& tb,
float& br, float& bg, float& bb) const {
float& tr,
float& tg,
float& tb,
float& br,
float& bg,
float& bb) const {
// Obtener keyframes actual y objetivo
const auto& current_kf = keyframes_[current_keyframe_index_];
const auto& target_kf = keyframes_[target_keyframe_index_];

View File

@@ -1,8 +1,9 @@
#pragma once
#include "theme.hpp"
#include <string>
#include "theme.hpp"
// Forward declaration (estructura definida en defines.h)
struct DynamicThemeKeyframe;
@@ -30,10 +31,7 @@ class DynamicTheme : public Theme {
* @param keyframes: Vector de keyframes (mínimo 2)
* @param loop: ¿Volver al inicio al terminar? (siempre true en esta app)
*/
DynamicTheme(const char* name_en, const char* name_es,
int text_r, int text_g, int text_b,
std::vector<DynamicThemeKeyframe> keyframes,
bool loop = true);
DynamicTheme(const char* name_en, const char* name_es, int text_r, int text_g, int text_b, std::vector<DynamicThemeKeyframe> keyframes, bool loop = true);
~DynamicTheme() override = default;
@@ -56,8 +54,12 @@ class DynamicTheme : public Theme {
Color getBallColor(size_t ball_index, float progress) const override;
void getBackgroundColors(float progress,
float& tr, float& tg, float& tb,
float& br, float& bg, float& bb) const override;
float& tr,
float& tg,
float& tb,
float& br,
float& bg,
float& bb) const override;
// ========================================
// ANIMACIÓN (soporte completo)

View File

@@ -1,32 +1,39 @@
#include "static_theme.hpp"
StaticTheme::StaticTheme(const char* name_en, const char* name_es,
int text_r, int text_g, int text_b,
int notif_bg_r, int notif_bg_g, int notif_bg_b,
float bg_top_r, float bg_top_g, float bg_top_b,
float bg_bottom_r, float bg_bottom_g, float bg_bottom_b,
std::vector<Color> ball_colors)
StaticTheme::StaticTheme(const char* name_en, const char* name_es, int text_r, int text_g, int text_b, int notif_bg_r, int notif_bg_g, int notif_bg_b, float bg_top_r, float bg_top_g, float bg_top_b, float bg_bottom_r, float bg_bottom_g, float bg_bottom_b, std::vector<Color> ball_colors)
: name_en_(name_en),
name_es_(name_es),
text_r_(text_r), text_g_(text_g), text_b_(text_b),
notif_bg_r_(notif_bg_r), notif_bg_g_(notif_bg_g), notif_bg_b_(notif_bg_b),
bg_top_r_(bg_top_r), bg_top_g_(bg_top_g), bg_top_b_(bg_top_b),
bg_bottom_r_(bg_bottom_r), bg_bottom_g_(bg_bottom_g), bg_bottom_b_(bg_bottom_b),
text_r_(text_r),
text_g_(text_g),
text_b_(text_b),
notif_bg_r_(notif_bg_r),
notif_bg_g_(notif_bg_g),
notif_bg_b_(notif_bg_b),
bg_top_r_(bg_top_r),
bg_top_g_(bg_top_g),
bg_top_b_(bg_top_b),
bg_bottom_r_(bg_bottom_r),
bg_bottom_g_(bg_bottom_g),
bg_bottom_b_(bg_bottom_b),
ball_colors_(std::move(ball_colors)) {
}
Color StaticTheme::getBallColor(size_t ball_index, float progress) const {
auto StaticTheme::getBallColor(size_t ball_index, float progress) const -> Color {
// Tema estático: siempre retorna color de paleta según índice
// (progress se ignora aquí, pero será usado en PHASE 3 para LERP externo)
if (ball_colors_.empty()) {
return {255, 255, 255}; // Blanco por defecto si paleta vacía
return {.r = 255, .g = 255, .b = 255}; // Blanco por defecto si paleta vacía
}
return ball_colors_[ball_index % ball_colors_.size()];
}
void StaticTheme::getBackgroundColors(float progress,
float& tr, float& tg, float& tb,
float& br, float& bg, float& bb) const {
float& tr,
float& tg,
float& tb,
float& br,
float& bg,
float& bb) const {
// Tema estático: siempre retorna colores de fondo fijos
// (progress se ignora aquí, pero será usado en PHASE 3 para LERP externo)
tr = bg_top_r_;

View File

@@ -1,8 +1,9 @@
#pragma once
#include "theme.hpp"
#include <string>
#include "theme.hpp"
/**
* StaticTheme: Tema estático con 1 keyframe (sin animación)
*
@@ -28,12 +29,7 @@ class StaticTheme : public Theme {
* @param bg_bottom_r, bg_bottom_g, bg_bottom_b: Color inferior de fondo
* @param ball_colors: Paleta de colores para pelotas
*/
StaticTheme(const char* name_en, const char* name_es,
int text_r, int text_g, int text_b,
int notif_bg_r, int notif_bg_g, int notif_bg_b,
float bg_top_r, float bg_top_g, float bg_top_b,
float bg_bottom_r, float bg_bottom_g, float bg_bottom_b,
std::vector<Color> ball_colors);
StaticTheme(const char* name_en, const char* name_es, int text_r, int text_g, int text_b, int notif_bg_r, int notif_bg_g, int notif_bg_b, float bg_top_r, float bg_top_g, float bg_top_b, float bg_bottom_r, float bg_bottom_g, float bg_bottom_b, std::vector<Color> ball_colors);
~StaticTheme() override = default;
@@ -60,8 +56,12 @@ class StaticTheme : public Theme {
Color getBallColor(size_t ball_index, float progress) const override;
void getBackgroundColors(float progress,
float& tr, float& tg, float& tb,
float& br, float& bg, float& bb) const override;
float& tr,
float& tg,
float& tb,
float& br,
float& bg,
float& bb) const override;
// ========================================
// ANIMACIÓN (sin soporte - tema estático)

View File

@@ -1,6 +1,7 @@
#pragma once
#include <vector>
#include "defines.hpp" // for Color, ThemeKeyframe
/**
@@ -47,8 +48,12 @@ class Theme {
* @param br, bg, bb: Color inferior (out)
*/
virtual void getBackgroundColors(float progress,
float& tr, float& tg, float& tb,
float& br, float& bg, float& bb) const = 0;
float& tr,
float& tg,
float& tb,
float& br,
float& bg,
float& bb) const = 0;
// ========================================
// ANIMACIÓN (solo temas dinámicos)
@@ -58,7 +63,7 @@ class Theme {
* Actualiza progreso de animación interna (solo dinámicos)
* @param delta_time: Tiempo transcurrido desde último frame
*/
virtual void update(float delta_time) { }
virtual void update(float delta_time) {}
/**
* ¿Este tema necesita update() cada frame?
@@ -75,7 +80,7 @@ class Theme {
/**
* Reinicia progreso de animación a 0.0 (usado al activar tema)
*/
virtual void resetProgress() { }
virtual void resetProgress() {}
// ========================================
// PAUSA (solo temas dinámicos)
@@ -90,5 +95,5 @@ class Theme {
/**
* Toggle pausa de animación (solo dinámicos, tecla Shift+D)
*/
virtual void togglePause() { }
virtual void togglePause() {}
};

View File

@@ -2,6 +2,7 @@
#include <string>
#include <vector>
#include "defines.hpp" // for Color
/**

Some files were not shown because too many files have changed in this diff Show More