Compare commits

58 Commits

Author SHA1 Message Date
d6ffbda00d WIP: Metal shader backend para macOS
- Shaders MSL portados desde GLSL (vertex + fragment)
- Estructura básica de MetalShader class
- Device, command queue, pipeline y buffers creados
- CMakeLists.txt actualizado con Metal frameworks
- assets.txt incluye shaders .metal como opcionales

PENDIENTE:
- Implementar render() loop completo
- Obtener MTLTexture desde SDL_Texture
- Crear sampler state
- Testing en macOS real

Ver METAL_BACKEND_NOTES.md para detalles de implementación.
2025-10-02 21:05:28 +02:00
ff7aef827c migracio a OpenGL 3.3 Core Profile completada 2025-10-02 18:24:18 +02:00
6ff7ccf69a migrat a OpenGL 3.3 Core Profile 2025-10-02 18:15:39 +02:00
e347e04d33 fix: arreglat bug en jail_shader.cpp que no aplicava be el tamany de la textura amb filtros al canviar el tamany de la finestra si arrancaves el joc sense filtros activats 2025-10-02 17:24:40 +02:00
7946ea54a6 unificats els shaders glsl en un sol fitxer
corregida la inicialització de opengl i shaders
2025-10-02 17:11:38 +02:00
79033346c0 migrat fitxer de config a v2 2025-10-02 16:35:11 +02:00
62b73d6f41 bug fix: si desapareixia la maquina de cafe, ja no eixia mes 2025-10-02 12:04:17 +02:00
218ddabb5e bug fix: no eixien pacos 2025-10-02 08:28:15 +02:00
427f40632a scoreboard.cpp: animació de SCORE a ENTER_NAME 2025-10-01 21:31:42 +02:00
a29b4d4379 scoreboard.cpp: modificada la easing function de desplaçament vertical a easeInOutSine 2025-10-01 20:10:42 +02:00
d851cdd2fe scoreboard.cpp: afegit un setMode() com deu mana 2025-10-01 20:06:08 +02:00
3354d00814 Transició acabada, encara que hi ha un desfase de 1 pixel 2025-10-01 19:34:23 +02:00
7bd7ba84e0 scoreboard.cpp: treballant en transicio de ENTER_NAME a SHOW_NAME 2025-10-01 19:11:58 +02:00
6ad34eaf57 finalitzada la implementació del carrusel 2025-10-01 18:49:11 +02:00
b4f2251508 animacio al pixel del carrusel feta, falla el color que no transiciona 2025-10-01 18:36:14 +02:00
473a52f986 treballant en la animacio alpixel del carrusel 2025-10-01 18:05:00 +02:00
bcdd48d622 carrusel funcional i acabat 2025-10-01 17:49:29 +02:00
6985569573 el carrusel ara es mou amb esquerra i dreta en lloc de amb amunt i avall 2025-10-01 17:13:52 +02:00
5db43e674d Color: afegit metode LERP() 2025-10-01 14:11:32 +02:00
34baa3c97d treballan en el carrusel per a posar el nom 2025-10-01 14:00:56 +02:00
bddb790fe2 creat bullet_manager.cpp 2025-09-30 20:41:35 +02:00
6fae12ba02 fix: la ultima tarjeta de la intro no tenia temps de repos 2025-09-30 19:59:38 +02:00
4e083a8cdb item.cpp: afegida rotació 2025-09-30 14:16:25 +02:00
cbe4315701 moving_sprite.cpp: afegit umbral a stopRotate() 2025-09-30 14:11:58 +02:00
49d561b583 moving_sprite.cpp: afegida funcio per a escalar la velocitat de rotacio 2025-09-30 13:59:13 +02:00
6e56a6fd79 moving_sprite.cpp: afegits nous metodes per controlar la rotació 2025-09-30 13:47:48 +02:00
267d9647e0 moving_sprite.cpp: la variable rotate.speed ja no es gastava 2025-09-30 13:38:06 +02:00
13b3702d00 eliminat param.game.item_size 2025-09-30 13:23:50 +02:00
4500845dcd muguda la logica de demo de utils.cpp a demo.cpp 2025-09-30 12:58:32 +02:00
a4abc02f88 nou: modificat el valor de velocitat en la creació dels globos verds. i tornat a deixar com estava 2025-09-30 12:42:38 +02:00
a0fb6934b0 corregit: en el mode demo no calculava correctament el estat del fondo 2025-09-30 09:56:32 +02:00
19645445b2 corregit: els fills dels globos verds eixien taronja 2025-09-30 08:47:49 +02:00
efe8628a3c corregit: el log de CREATING PLAYER TEXTURES en resource.cpp 2025-09-29 14:22:44 +02:00
c98cb0d29f repensada la forma d'asignar fitxers de demo als jugadors
refets els fitxers de demo i afegit un tercer fitxer
2025-09-29 14:00:10 +02:00
c16fc1bae5 corregit: el mode demo ja funciona correctament 2025-09-29 12:47:13 +02:00
fa0af1179a corregit: no trobava version.h 2025-09-29 07:54:46 +02:00
d1e4a5eb07 eliminat tot el define NO_AUDIO del codi 2025-09-27 00:33:05 +02:00
e18d1b186a player.h: eliminat codi mort 2025-09-27 00:22:46 +02:00
d056a5e336 nou: afegida versió de git en la pantalla de carrega 2025-09-27 00:20:46 +02:00
b9e26aa755 corregit: flags estatics en credits.cpp i title.cpp 2025-09-26 23:48:08 +02:00
b2afef2226 corregit: flags estatics en hiscore_tale.cpp 2025-09-26 23:40:37 +02:00
c400aa96c0 corregit: flags estatics en game.cpp 2025-09-26 23:36:49 +02:00
8818954dcd afegit define rapidet per a renderer metal basic en macos 2025-09-26 22:45:14 +02:00
b92e5df98b nou: sonidos de bala diferent per a cada jugador 2025-09-26 20:48:22 +02:00
83871273ec nou: bales de colors diferents per a cada jugador 2025-09-26 20:37:00 +02:00
0459b39366 screen.cpp: getDisplayInfo()
resource.cpp: afegida info del display en la pantalla de carrega
2025-09-26 19:42:39 +02:00
5bb0ff19bc corregit: asset::checkFile() fallava desde fora del directori 2025-09-26 19:16:25 +02:00
a867b3cf4d integrat empaquetador de recursos en el makefile 2025-09-26 18:16:48 +02:00
8a6ce8e66d organitzat player.h 2025-09-26 17:37:29 +02:00
a40f04a739 nou: musiqueta i veu per al game over i timings ajustats 2025-09-26 17:20:35 +02:00
0c670fd344 novetat: canvi de color de les bales quan queda poc powerUp 2025-09-26 14:13:52 +02:00
35f4bf690c corregit: bug en Audio::fadeOutMusic quan la musica no es reproduia en bucle 2025-09-25 21:21:39 +02:00
abeaf47f96 corregit: timing de les celebracions del final 2025-09-25 19:48:58 +02:00
6498c35628 corregit audio i timing del game over 2025-09-25 19:36:40 +02:00
d1c6af02db corregit: la musica del joc soles sonava la primera volta 2025-09-25 19:19:46 +02:00
5edef17d84 nou: musica al completar el joc 2025-09-25 19:10:46 +02:00
e4532fcef2 nou: ruidet per a quan acabes de posar el nom 2025-09-25 18:18:39 +02:00
7a8d66c29d nou: quan arribes a la maxima puntuació, posa un lletreret 2025-09-25 17:52:49 +02:00
92 changed files with 4097 additions and 2348 deletions

3
.gitignore vendored
View File

@@ -17,4 +17,5 @@ coffee_crisis*
debug.txt
cppcheck-result*
desktop.ini
ccae_release/
ccae_release/
resources.pack

View File

@@ -14,6 +14,22 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
cmake_policy(SET CMP0072 NEW)
set(OpenGL_GL_PREFERENCE GLVND)
# --- GENERACIÓN DE VERSIÓN AUTOMÁTICA ---
find_package(Git QUIET)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --short=7 HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
else()
set(GIT_HASH "unknown")
endif()
# Configurar archivo de versión
configure_file(${CMAKE_SOURCE_DIR}/source/version.h.in ${CMAKE_BINARY_DIR}/version.h @ONLY)
# --- 1. LISTA EXPLÍCITA DE FUENTES ---
set(APP_SOURCES
@@ -48,6 +64,7 @@ set(APP_SOURCES
source/balloon_manager.cpp
source/balloon.cpp
source/bullet.cpp
source/bullet_manager.cpp
source/enter_name.cpp
source/explosions.cpp
source/game_logo.cpp
@@ -79,6 +96,7 @@ set(APP_SOURCES
# --- Otros ---
source/color.cpp
source/demo.cpp
source/define_buttons.cpp
source/difficulty.cpp
source/input_types.cpp
@@ -92,28 +110,37 @@ set(APP_SOURCES
# Fuentes de librerías de terceros
set(EXTERNAL_SOURCES
source/external/jail_shader.cpp
source/external/jail_audio.cpp
source/external/json.hpp
source/external/gif.cpp
source/external/gif.cpp
)
# Añadir jail_audio.cpp solo si el audio está habilitado
if(NOT DISABLE_AUDIO)
list(APPEND EXTERNAL_SOURCES source/external/jail_audio.cpp)
endif()
# Fuentes del sistema de renderizado
set(RENDERING_SOURCES
source/rendering/opengl/opengl_shader.cpp
)
# Añadir backend de Metal en macOS
if(APPLE)
list(APPEND RENDERING_SOURCES
source/rendering/metal/metal_shader.mm
)
message(STATUS "Metal backend habilitado para macOS")
endif()
# Configuración de SDL3
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}")
# --- 2. AÑADIR EJECUTABLE ---
add_executable(${PROJECT_NAME} ${APP_SOURCES} ${EXTERNAL_SOURCES})
add_executable(${PROJECT_NAME} ${APP_SOURCES} ${EXTERNAL_SOURCES} ${RENDERING_SOURCES})
# --- 3. DIRECTORIOS DE INCLUSIÓN ---
target_include_directories(${PROJECT_NAME} PUBLIC
"${CMAKE_SOURCE_DIR}/source"
"${CMAKE_SOURCE_DIR}/source/external"
"${CMAKE_SOURCE_DIR}/source/external"
"${CMAKE_SOURCE_DIR}/source/rendering"
"${CMAKE_BINARY_DIR}"
)
# Enlazar la librería SDL3
@@ -128,16 +155,9 @@ target_compile_options(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:-Os -ffunctio
# Definir _DEBUG en modo Debug
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:DEBUG>:_DEBUG>)
# Opción para habilitar/deshabilitar audio
option(DISABLE_AUDIO "Disable audio system" OFF)
# Descomentar la siguiente línea para activar el modo grabación de demos
# target_compile_definitions(${PROJECT_NAME} PRIVATE RECORDING)
# Definir NO_AUDIO si la opción está activada
if(DISABLE_AUDIO)
target_compile_definitions(${PROJECT_NAME} PRIVATE NO_AUDIO)
message(STATUS "Audio deshabilitado - NO_AUDIO definido")
else()
message(STATUS "Audio habilitado")
endif()
# Configuración específica para cada plataforma
if(WIN32)
@@ -147,6 +167,11 @@ elseif(APPLE)
target_compile_definitions(${PROJECT_NAME} PRIVATE MACOS_BUILD)
target_compile_options(${PROJECT_NAME} PRIVATE -Wno-deprecated)
set(CMAKE_OSX_ARCHITECTURES "arm64")
# Enlazar frameworks de Metal
target_link_libraries(${PROJECT_NAME} PRIVATE
"-framework Metal"
"-framework QuartzCore"
)
elseif(UNIX AND NOT APPLE)
target_compile_definitions(${PROJECT_NAME} PRIVATE LINUX_BUILD)
endif()

View File

@@ -3,6 +3,7 @@ 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 := coffee_crisis_arcade_edition
@@ -12,6 +13,17 @@ RELEASE_FOLDER := ccae_release
RELEASE_FILE := $(RELEASE_FOLDER)/$(TARGET_NAME)
RESOURCE_FILE := release/coffee.res
# Variables para herramienta de empaquetado
ifeq ($(OS),Windows_NT)
PACK_TOOL := $(DIR_TOOLS)pack_resources.exe
PACK_CXX := $(CXX)
else
PACK_TOOL := $(DIR_TOOLS)pack_resources
PACK_CXX := $(CXX)
endif
PACK_SOURCES := $(DIR_TOOLS)pack_resources.cpp $(DIR_SOURCES)resource_pack.cpp
PACK_INCLUDES := -I$(DIR_ROOT)
# Versión automática basada en la fecha actual (específica por SO)
ifeq ($(OS),Windows_NT)
VERSION := $(shell powershell -Command "Get-Date -Format 'yyyy-MM-dd'")
@@ -135,6 +147,19 @@ else
endif
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)
@echo "✓ Herramienta de empaquetado lista: $(PACK_TOOL)"
pack_tool: $(PACK_TOOL)
resources.pack: $(PACK_TOOL)
@echo "Generando resources.pack desde directorio data/..."
$(PACK_TOOL) data resources.pack
@echo "✓ resources.pack generado exitosamente"
# Reglas para compilación
windows:
@echo off
@@ -153,7 +178,7 @@ windows_debug:
@echo Compilando version debug para Windows: "$(APP_NAME)_debug.exe"
$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(WIN_TARGET_FILE)_debug.exe"
windows_release:
windows_release: resources.pack
@echo off
@echo Creando release para Windows - Version: $(VERSION)
@@ -191,7 +216,7 @@ macos_debug:
@echo "Compilando version debug para macOS: $(TARGET_NAME)_debug"
$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"
macos_release:
macos_release: resources.pack
@echo "Creando release para macOS - Version: $(VERSION)"
# Elimina datos de compilaciones anteriores
$(RMDIR) "$(RELEASE_FOLDER)"
@@ -258,7 +283,7 @@ linux_debug:
@echo "Compilando version debug para Linux: $(TARGET_NAME)_debug"
$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"
linux_release:
linux_release: resources.pack
@echo "Creando release para Linux - Version: $(VERSION)"
# Elimina carpetas previas
$(RMDIR) "$(RELEASE_FOLDER)"
@@ -284,7 +309,7 @@ linux_release:
# Elimina la carpeta temporal
$(RMDIR) "$(RELEASE_FOLDER)"
linux_release_desktop:
linux_release_desktop: resources.pack
@echo "Creando release con integracion desktop para Linux - Version: $(VERSION)"
# Elimina carpetas previas
$(RMDIR) "$(RELEASE_FOLDER)"
@@ -389,7 +414,7 @@ raspi_debug:
@echo "Compilando version debug para Raspberry Pi: $(TARGET_NAME)_debug"
$(CXX) $(APP_SOURCES) $(INCLUDES) -DVERBOSE -DDEBUG $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"
raspi_release:
raspi_release: resources.pack
@echo "Creando release para Raspberry Pi - Version: $(VERSION)"
# Elimina carpetas previas
$(RMDIR) "$(RELEASE_FOLDER)"
@@ -415,7 +440,7 @@ raspi_release:
# Elimina la carpeta temporal
$(RMDIR) "$(RELEASE_FOLDER)"
anbernic:
anbernic: resources.pack
@echo "Compilando para Anbernic: $(TARGET_NAME)"
# Elimina carpetas previas
$(RMDIR) "$(RELEASE_FOLDER)"_anbernic
@@ -457,7 +482,9 @@ help:
@echo " raspi_release - Crear release completo para Raspberry Pi"
@echo " anbernic - Compilar para Anbernic"
@echo " no_audio - Compilar sin sistema de audio"
@echo " pack_tool - Compilar herramienta de empaquetado"
@echo " resources.pack - Generar pack de recursos desde data/"
@echo " show_version - Mostrar version actual ($(VERSION))"
@echo " help - Mostrar esta ayuda"
.PHONY: windows windows_rec windows_debug windows_release macos macos_debug macos_release linux linux_debug linux_release linux_release_desktop raspi raspi_debug raspi_release anbernic no_audio show_version help
.PHONY: windows windows_rec windows_debug windows_release macos macos_debug macos_release linux linux_debug linux_release linux_release_desktop raspi raspi_debug raspi_release anbernic no_audio show_version help pack_tool

61
TODO.md Normal file
View File

@@ -0,0 +1,61 @@
# TODO
## Tareas pendientes
- [ ] Revisar todas las variables static de los métodos para ver si se resetean correctamente
## Mejoras arquitecturales (refactoring)
### Eliminar variables static locales y usar patrones profesionales:
**Opción 1: Máquina de Estados**
```cpp
class GameCompletedState {
bool start_celebrations_done = false;
bool end_celebrations_done = false;
float timer = 0.0f;
public:
void reset() {
start_celebrations_done = false;
end_celebrations_done = false;
timer = 0.0f;
}
void update(float deltaTime) {
timer += deltaTime;
// lógica aquí
}
};
```
**Opción 2: Sistema de Eventos/Callbacks**
```cpp
// Al entrar en COMPLETED state
eventSystem.scheduleEvent(6.0f, []{ startCelebrations(); });
eventSystem.scheduleEvent(14.0f, []{ endCelebrations(); });
```
**Opción 3: Flags como miembros privados**
```cpp
class Game {
private:
struct GameOverState {
bool game_over_triggered = false;
bool start_celebrations_triggered = false;
bool end_celebrations_triggered = false;
void reset() {
game_over_triggered = false;
start_celebrations_triggered = false;
end_celebrations_triggered = false;
}
} game_over_state_;
};
```
**Ventajas:**
- Más fáciles de testear
- Más fáciles de debugear
- Más fáciles de entender y mantener
- No tienen "estado oculto"

View File

@@ -4,7 +4,7 @@
# Variables: ${PREFIX}, ${SYSTEM_FOLDER}
# Archivos de configuración del sistema (absolutos y opcionales)
DATA|${SYSTEM_FOLDER}/config.txt|optional,absolute
DATA|${SYSTEM_FOLDER}/config_v2.txt|optional,absolute
DATA|${SYSTEM_FOLDER}/controllers.json|optional,absolute
DATA|${SYSTEM_FOLDER}/score.bin|optional,absolute
@@ -20,8 +20,10 @@ DATA|${PREFIX}/config/stages.txt
# Archivos con los datos de la demo
DEMODATA|${PREFIX}/data/demo/demo1.bin
DEMODATA|${PREFIX}/data/demo/demo2.bin
DEMODATA|${PREFIX}/data/demo/demo3.bin
# Música
MUSIC|${PREFIX}/data/music/congratulations.ogg
MUSIC|${PREFIX}/data/music/credits.ogg
MUSIC|${PREFIX}/data/music/intro.ogg
MUSIC|${PREFIX}/data/music/playing.ogg
@@ -36,7 +38,8 @@ SOUND|${PREFIX}/data/sound/balloon_pop0.wav
SOUND|${PREFIX}/data/sound/balloon_pop1.wav
SOUND|${PREFIX}/data/sound/balloon_pop2.wav
SOUND|${PREFIX}/data/sound/balloon_pop3.wav
SOUND|${PREFIX}/data/sound/bullet.wav
SOUND|${PREFIX}/data/sound/bullet1p.wav
SOUND|${PREFIX}/data/sound/bullet2p.wav
SOUND|${PREFIX}/data/sound/clock.wav
SOUND|${PREFIX}/data/sound/coffee_out.wav
SOUND|${PREFIX}/data/sound/continue_clock.wav
@@ -48,6 +51,7 @@ SOUND|${PREFIX}/data/sound/item_drop.wav
SOUND|${PREFIX}/data/sound/item_pickup.wav
SOUND|${PREFIX}/data/sound/jump.wav
SOUND|${PREFIX}/data/sound/logo.wav
SOUND|${PREFIX}/data/sound/name_input_accept.wav
SOUND|${PREFIX}/data/sound/notify.wav
SOUND|${PREFIX}/data/sound/player_collision.wav
SOUND|${PREFIX}/data/sound/power_ball_explosion.wav
@@ -62,6 +66,7 @@ SOUND|${PREFIX}/data/sound/title.wav
SOUND|${PREFIX}/data/sound/voice_aw_aw_aw.wav
SOUND|${PREFIX}/data/sound/voice_coffee.wav
SOUND|${PREFIX}/data/sound/voice_credit_thankyou.wav
SOUND|${PREFIX}/data/sound/voice_game_over.wav
SOUND|${PREFIX}/data/sound/voice_get_ready.wav
SOUND|${PREFIX}/data/sound/voice_no.wav
SOUND|${PREFIX}/data/sound/voice_power_up.wav
@@ -69,9 +74,13 @@ SOUND|${PREFIX}/data/sound/voice_recover.wav
SOUND|${PREFIX}/data/sound/voice_thankyou.wav
SOUND|${PREFIX}/data/sound/walk.wav
# Shaders
DATA|${PREFIX}/data/shaders/crtpi_240.glsl
DATA|${PREFIX}/data/shaders/crtpi_256.glsl
# Shaders OpenGL (Windows/Linux)
DATA|${PREFIX}/data/shaders/crtpi_vertex.glsl
DATA|${PREFIX}/data/shaders/crtpi_fragment.glsl
# Shaders Metal (macOS) - opcionales
DATA|${PREFIX}/data/shaders/crtpi_vertex.metal|optional
DATA|${PREFIX}/data/shaders/crtpi_fragment.metal|optional
# Texturas - Balloons
ANIMATION|${PREFIX}/data/gfx/balloon/balloon0.ani

View File

@@ -2,7 +2,6 @@
# Formato: PARAMETRO VALOR
# --- GAME ---
game.item_size 20 # Tamaño de los items del juego (en píxeles)
game.item_text_outline_color E0E0E0F0 # Color del outline del texto de los items (RGBA hex)
game.width 320 # Ancho de la resolución nativa del juego (en píxeles)
game.height 240 # Alto de la resolución nativa del juego (en píxeles)

View File

@@ -4,31 +4,31 @@
# Los pools no necesitan estar ordenados ni ser consecutivos
# Pool para la fase 1
POOL: 0 FORMATIONS: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
POOL: 0 FORMATIONS: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
# Pool para la fase 2
POOL: 1 FORMATIONS: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
POOL: 1 FORMATIONS: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
# Pool para la fase 3
POOL: 2 FORMATIONS: 0, 1, 2, 3, 4, 55, 56, 57, 58, 59
POOL: 2 FORMATIONS: 0, 1, 2, 3, 4, 55, 56, 57, 58, 59
# Pool para la fase 4
POOL: 3 FORMATIONS: 50, 51, 52, 53, 54, 5, 6, 7, 8, 9
POOL: 3 FORMATIONS: 50, 51, 52, 53, 54, 5, 6, 7, 8, 9
# Pool para la fase 5
POOL: 4 FORMATIONS: 60, 61, 62, 63, 64, 65, 66, 67, 68, 69
POOL: 4 FORMATIONS: 60, 61, 62, 63, 64, 65, 66, 67, 68, 69
# Pool para la fase 6
POOL: 5 FORMATIONS: 10, 61, 12, 63, 14, 65, 16, 67, 18, 69
POOL: 5 FORMATIONS: 10, 61, 12, 63, 14, 65, 16, 67, 18, 69
# Pool para la fase 7
POOL: 6 FORMATIONS: 60, 11, 62, 13, 64, 15, 66, 17, 68, 19
POOL: 6 FORMATIONS: 60, 11, 62, 13, 64, 15, 66, 17, 68, 19
# Pool para la fase 8
POOL: 7 FORMATIONS: 20, 21, 22, 23, 24, 65, 66, 67, 68, 69
POOL: 7 FORMATIONS: 20, 21, 22, 23, 24, 65, 66, 67, 68, 69
# Pool para la fase 9
POOL: 8 FORMATIONS: 70, 71, 72, 73, 74, 15, 16, 17, 18, 19
POOL: 8 FORMATIONS: 70, 71, 72, 73, 74, 15, 16, 17, 18, 19
# Pool para la fase 10
POOL: 9 FORMATIONS: 20, 21, 22, 23, 24, 70, 71, 72, 73, 74

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

BIN
data/demo/demo3.bin Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -2,43 +2,85 @@ frame_width=12
frame_height=12
[animation]
name=normal_up
speed=0.0833
loop=0
frames=0,1,2
name=yellow_up
speed=20
loop=-1
frames=0
[/animation]
[animation]
name=normal_left
speed=0.0833
loop=0
frames=3,4,5,5,4,3
name=yellow_left
speed=20
loop=-1
frames=1
[/animation]
[animation]
name=normal_right
speed=0.0833
loop=0
frames=6,7,8,8,7,6
name=yellow_right
speed=20
loop=-1
frames=2
[/animation]
[animation]
name=powered_up
speed=0.0833
loop=0
frames=9,10,11,11,10,9
name=green_up
speed=20
loop=-1
frames=3
[/animation]
[animation]
name=powered_left
speed=0.0833
loop=0
frames=12,13,14,14,13,12
name=green_left
speed=20
loop=-1
frames=4
[/animation]
[animation]
name=powered_right
speed=0.0833
loop=0
frames=15,16,17,17,26,15
name=green_right
speed=20
loop=-1
frames=5
[/animation]
[animation]
name=red_up
speed=20
loop=-1
frames=6
[/animation]
[animation]
name=red_left
speed=20
loop=-1
frames=7
[/animation]
[animation]
name=red_right
speed=20
loop=-1
frames=8
[/animation]
[animation]
name=purple_up
speed=20
loop=-1
frames=9
[/animation]
[animation]
name=purple_left
speed=20
loop=-1
frames=10
[/animation]
[animation]
name=purple_right
speed=20
loop=-1
frames=11
[/animation]

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@@ -109,7 +109,7 @@ frames=38,39,40,41
[animation]
name=celebration
speed=0.167
loop=-1
loop=0
frames=42,42,42,42,42,42,43,44,45,46,46,46,46,46,46,45,45,45,46,46,46,45,45,45,44,43,42,42,42
[/animation]

View File

@@ -27,6 +27,7 @@
"[GAME_TEXT] 7": "Endavant!",
"[GAME_TEXT] 8": "1.000.000 de punts!",
"[GAME_TEXT] THANK_YOU": "Gracies!",
"[GAME_TEXT] NEW_RECORD": "Nou record!",
"[HIGHSCORE_TABLE] CAPTION": "Millors puntuacions",

View File

@@ -26,6 +26,7 @@
"[GAME_TEXT] 7": "Get Ready!",
"[GAME_TEXT] 8": "1,000,000 points!",
"[GAME_TEXT] THANK_YOU": "Thank you!",
"[GAME_TEXT] NEW_RECORD": "New record!",
"[HIGHSCORE_TABLE] CAPTION": "Best scores",

View File

@@ -26,6 +26,7 @@
"[GAME_TEXT] 7": "Adelante!",
"[GAME_TEXT] 8": "1.000.000 de puntos!",
"[GAME_TEXT] THANK_YOU": "Gracias!",
"[GAME_TEXT] NEW_RECORD": "Nuevo record!",
"[HIGHSCORE_TABLE] CAPTION": "Mejores puntuaciones",

Binary file not shown.

View File

@@ -1,234 +0,0 @@
/*
crt-pi - A Raspberry Pi friendly CRT shader.
Copyright (C) 2015-2016 davej
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
Notes:
This shader is designed to work well on Raspberry Pi GPUs (i.e. 1080P @ 60Hz on a game with a 4:3 aspect ratio). It pushes the Pi's GPU hard and enabling some features will slow it down so that it is no longer able to match 1080P @ 60Hz. You will need to overclock your Pi to the fastest setting in raspi-config to get the best results from this shader: 'Pi2' for Pi2 and 'Turbo' for original Pi and Pi Zero. Note: Pi2s are slower at running the shader than other Pis, this seems to be down to Pi2s lower maximum memory speed. Pi2s don't quite manage 1080P @ 60Hz - they drop about 1 in 1000 frames. You probably won't notice this, but if you do, try enabling FAKE_GAMMA.
SCANLINES enables scanlines. You'll almost certainly want to use it with MULTISAMPLE to reduce moire effects. SCANLINE_WEIGHT defines how wide scanlines are (it is an inverse value so a higher number = thinner lines). SCANLINE_GAP_BRIGHTNESS defines how dark the gaps between the scan lines are. Darker gaps between scan lines make moire effects more likely.
GAMMA enables gamma correction using the values in INPUT_GAMMA and OUTPUT_GAMMA. FAKE_GAMMA causes it to ignore the values in INPUT_GAMMA and OUTPUT_GAMMA and approximate gamma correction in a way which is faster than true gamma whilst still looking better than having none. You must have GAMMA defined to enable FAKE_GAMMA.
CURVATURE distorts the screen by CURVATURE_X and CURVATURE_Y. Curvature slows things down a lot.
By default the shader uses linear blending horizontally. If you find this too blury, enable SHARPER.
BLOOM_FACTOR controls the increase in width for bright scanlines.
MASK_TYPE defines what, if any, shadow mask to use. MASK_BRIGHTNESS defines how much the mask type darkens the screen.
*/
#pragma parameter CURVATURE_X "Screen curvature - horizontal" 0.10 0.0 1.0 0.01
#pragma parameter CURVATURE_Y "Screen curvature - vertical" 0.15 0.0 1.0 0.01
#pragma parameter MASK_BRIGHTNESS "Mask brightness" 0.70 0.0 1.0 0.01
#pragma parameter SCANLINE_WEIGHT "Scanline weight" 6.0 0.0 15.0 0.1
#pragma parameter SCANLINE_GAP_BRIGHTNESS "Scanline gap brightness" 0.12 0.0 1.0 0.01
#pragma parameter BLOOM_FACTOR "Bloom factor" 1.5 0.0 5.0 0.01
#pragma parameter INPUT_GAMMA "Input gamma" 2.4 0.0 5.0 0.01
#pragma parameter OUTPUT_GAMMA "Output gamma" 2.2 0.0 5.0 0.01
// Haven't put these as parameters as it would slow the code down.
#define SCANLINES
#define MULTISAMPLE
#define GAMMA
//#define FAKE_GAMMA
//#define CURVATURE
//#define SHARPER
// MASK_TYPE: 0 = none, 1 = green/magenta, 2 = trinitron(ish)
#define MASK_TYPE 2
#ifdef GL_ES
#define COMPAT_PRECISION mediump
precision mediump float;
#else
#define COMPAT_PRECISION
#endif
#ifdef PARAMETER_UNIFORM
uniform COMPAT_PRECISION float CURVATURE_X;
uniform COMPAT_PRECISION float CURVATURE_Y;
uniform COMPAT_PRECISION float MASK_BRIGHTNESS;
uniform COMPAT_PRECISION float SCANLINE_WEIGHT;
uniform COMPAT_PRECISION float SCANLINE_GAP_BRIGHTNESS;
uniform COMPAT_PRECISION float BLOOM_FACTOR;
uniform COMPAT_PRECISION float INPUT_GAMMA;
uniform COMPAT_PRECISION float OUTPUT_GAMMA;
#else
#define CURVATURE_X 0.05
#define CURVATURE_Y 0.1
#define MASK_BRIGHTNESS 0.80
#define SCANLINE_WEIGHT 6.0
#define SCANLINE_GAP_BRIGHTNESS 0.12
#define BLOOM_FACTOR 3.5
#define INPUT_GAMMA 2.4
#define OUTPUT_GAMMA 2.2
#endif
/* COMPATIBILITY
- GLSL compilers
*/
//uniform vec2 TextureSize;
#if defined(CURVATURE)
varying vec2 screenScale;
#endif
varying vec2 TEX0;
varying float filterWidth;
#if defined(VERTEX)
//uniform mat4 MVPMatrix;
//attribute vec4 VertexCoord;
//attribute vec2 TexCoord;
//uniform vec2 InputSize;
//uniform vec2 OutputSize;
void main()
{
#if defined(CURVATURE)
screenScale = vec2(1.0, 1.0); //TextureSize / InputSize;
#endif
filterWidth = (768.0 / 240.0) / 3.0;
TEX0 = vec2(gl_MultiTexCoord0.x, 1.0-gl_MultiTexCoord0.y)*1.0001;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#elif defined(FRAGMENT)
uniform sampler2D Texture;
#if defined(CURVATURE)
vec2 Distort(vec2 coord)
{
vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y);
// Barrel distortion shrinks the display area a bit, this will allow us to counteract that.
vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION);
coord *= screenScale;
coord -= vec2(0.5);
float rsq = coord.x * coord.x + coord.y * coord.y;
coord += coord * (CURVATURE_DISTORTION * rsq);
coord *= barrelScale;
if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
coord = vec2(-1.0); // If out of bounds, return an invalid value.
else
{
coord += vec2(0.5);
coord /= screenScale;
}
return coord;
}
#endif
float CalcScanLineWeight(float dist)
{
return max(1.0-dist*dist*SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
}
float CalcScanLine(float dy)
{
float scanLineWeight = CalcScanLineWeight(dy);
#if defined(MULTISAMPLE)
scanLineWeight += CalcScanLineWeight(dy-filterWidth);
scanLineWeight += CalcScanLineWeight(dy+filterWidth);
scanLineWeight *= 0.3333333;
#endif
return scanLineWeight;
}
void main()
{
vec2 TextureSize = vec2(320.0, 240.0);
#if defined(CURVATURE)
vec2 texcoord = Distort(TEX0);
if (texcoord.x < 0.0)
gl_FragColor = vec4(0.0);
else
#else
vec2 texcoord = TEX0;
#endif
{
vec2 texcoordInPixels = texcoord * TextureSize;
#if defined(SHARPER)
vec2 tempCoord = floor(texcoordInPixels) + 0.5;
vec2 coord = tempCoord / TextureSize;
vec2 deltas = texcoordInPixels - tempCoord;
float scanLineWeight = CalcScanLine(deltas.y);
vec2 signs = sign(deltas);
deltas.x *= 2.0;
deltas = deltas * deltas;
deltas.y = deltas.y * deltas.y;
deltas.x *= 0.5;
deltas.y *= 8.0;
deltas /= TextureSize;
deltas *= signs;
vec2 tc = coord + deltas;
#else
float tempY = floor(texcoordInPixels.y) + 0.5;
float yCoord = tempY / TextureSize.y;
float dy = texcoordInPixels.y - tempY;
float scanLineWeight = CalcScanLine(dy);
float signY = sign(dy);
dy = dy * dy;
dy = dy * dy;
dy *= 8.0;
dy /= TextureSize.y;
dy *= signY;
vec2 tc = vec2(texcoord.x, yCoord + dy);
#endif
vec3 colour = texture2D(Texture, tc).rgb;
#if defined(SCANLINES)
#if defined(GAMMA)
#if defined(FAKE_GAMMA)
colour = colour * colour;
#else
colour = pow(colour, vec3(INPUT_GAMMA));
#endif
#endif
scanLineWeight *= BLOOM_FACTOR;
colour *= scanLineWeight;
#if defined(GAMMA)
#if defined(FAKE_GAMMA)
colour = sqrt(colour);
#else
colour = pow(colour, vec3(1.0/OUTPUT_GAMMA));
#endif
#endif
#endif
#if MASK_TYPE == 0
gl_FragColor = vec4(colour, 1.0);
#else
#if MASK_TYPE == 1
float whichMask = fract((gl_FragCoord.x*1.0001) * 0.5);
vec3 mask;
if (whichMask < 0.5)
mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
else
mask = vec3(1.0, MASK_BRIGHTNESS, 1.0);
#elif MASK_TYPE == 2
float whichMask = fract((gl_FragCoord.x*1.0001) * 0.3333333);
vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
if (whichMask < 0.3333333)
mask.x = 1.0;
else if (whichMask < 0.6666666)
mask.y = 1.0;
else
mask.z = 1.0;
#endif
gl_FragColor = vec4(colour * mask, 1.0);
#endif
}
}
#endif

View File

@@ -1,234 +0,0 @@
/*
crt-pi - A Raspberry Pi friendly CRT shader.
Copyright (C) 2015-2016 davej
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
Notes:
This shader is designed to work well on Raspberry Pi GPUs (i.e. 1080P @ 60Hz on a game with a 4:3 aspect ratio). It pushes the Pi's GPU hard and enabling some features will slow it down so that it is no longer able to match 1080P @ 60Hz. You will need to overclock your Pi to the fastest setting in raspi-config to get the best results from this shader: 'Pi2' for Pi2 and 'Turbo' for original Pi and Pi Zero. Note: Pi2s are slower at running the shader than other Pis, this seems to be down to Pi2s lower maximum memory speed. Pi2s don't quite manage 1080P @ 60Hz - they drop about 1 in 1000 frames. You probably won't notice this, but if you do, try enabling FAKE_GAMMA.
SCANLINES enables scanlines. You'll almost certainly want to use it with MULTISAMPLE to reduce moire effects. SCANLINE_WEIGHT defines how wide scanlines are (it is an inverse value so a higher number = thinner lines). SCANLINE_GAP_BRIGHTNESS defines how dark the gaps between the scan lines are. Darker gaps between scan lines make moire effects more likely.
GAMMA enables gamma correction using the values in INPUT_GAMMA and OUTPUT_GAMMA. FAKE_GAMMA causes it to ignore the values in INPUT_GAMMA and OUTPUT_GAMMA and approximate gamma correction in a way which is faster than true gamma whilst still looking better than having none. You must have GAMMA defined to enable FAKE_GAMMA.
CURVATURE distorts the screen by CURVATURE_X and CURVATURE_Y. Curvature slows things down a lot.
By default the shader uses linear blending horizontally. If you find this too blury, enable SHARPER.
BLOOM_FACTOR controls the increase in width for bright scanlines.
MASK_TYPE defines what, if any, shadow mask to use. MASK_BRIGHTNESS defines how much the mask type darkens the screen.
*/
#pragma parameter CURVATURE_X "Screen curvature - horizontal" 0.10 0.0 1.0 0.01
#pragma parameter CURVATURE_Y "Screen curvature - vertical" 0.15 0.0 1.0 0.01
#pragma parameter MASK_BRIGHTNESS "Mask brightness" 0.70 0.0 1.0 0.01
#pragma parameter SCANLINE_WEIGHT "Scanline weight" 6.0 0.0 15.0 0.1
#pragma parameter SCANLINE_GAP_BRIGHTNESS "Scanline gap brightness" 0.12 0.0 1.0 0.01
#pragma parameter BLOOM_FACTOR "Bloom factor" 1.5 0.0 5.0 0.01
#pragma parameter INPUT_GAMMA "Input gamma" 2.4 0.0 5.0 0.01
#pragma parameter OUTPUT_GAMMA "Output gamma" 2.2 0.0 5.0 0.01
// Haven't put these as parameters as it would slow the code down.
#define SCANLINES
#define MULTISAMPLE
#define GAMMA
//#define FAKE_GAMMA
//#define CURVATURE
//#define SHARPER
// MASK_TYPE: 0 = none, 1 = green/magenta, 2 = trinitron(ish)
#define MASK_TYPE 2
#ifdef GL_ES
#define COMPAT_PRECISION mediump
precision mediump float;
#else
#define COMPAT_PRECISION
#endif
#ifdef PARAMETER_UNIFORM
uniform COMPAT_PRECISION float CURVATURE_X;
uniform COMPAT_PRECISION float CURVATURE_Y;
uniform COMPAT_PRECISION float MASK_BRIGHTNESS;
uniform COMPAT_PRECISION float SCANLINE_WEIGHT;
uniform COMPAT_PRECISION float SCANLINE_GAP_BRIGHTNESS;
uniform COMPAT_PRECISION float BLOOM_FACTOR;
uniform COMPAT_PRECISION float INPUT_GAMMA;
uniform COMPAT_PRECISION float OUTPUT_GAMMA;
#else
#define CURVATURE_X 0.05
#define CURVATURE_Y 0.1
#define MASK_BRIGHTNESS 0.80
#define SCANLINE_WEIGHT 6.0
#define SCANLINE_GAP_BRIGHTNESS 0.12
#define BLOOM_FACTOR 3.5
#define INPUT_GAMMA 2.4
#define OUTPUT_GAMMA 2.2
#endif
/* COMPATIBILITY
- GLSL compilers
*/
//uniform vec2 TextureSize;
#if defined(CURVATURE)
varying vec2 screenScale;
#endif
varying vec2 TEX0;
varying float filterWidth;
#if defined(VERTEX)
//uniform mat4 MVPMatrix;
//attribute vec4 VertexCoord;
//attribute vec2 TexCoord;
//uniform vec2 InputSize;
//uniform vec2 OutputSize;
void main()
{
#if defined(CURVATURE)
screenScale = vec2(1.0, 1.0); //TextureSize / InputSize;
#endif
filterWidth = (768.0 / 256.0) / 3.0;
TEX0 = vec2(gl_MultiTexCoord0.x, 1.0-gl_MultiTexCoord0.y)*1.0001;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#elif defined(FRAGMENT)
uniform sampler2D Texture;
#if defined(CURVATURE)
vec2 Distort(vec2 coord)
{
vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y);
// Barrel distortion shrinks the display area a bit, this will allow us to counteract that.
vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION);
coord *= screenScale;
coord -= vec2(0.5);
float rsq = coord.x * coord.x + coord.y * coord.y;
coord += coord * (CURVATURE_DISTORTION * rsq);
coord *= barrelScale;
if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
coord = vec2(-1.0); // If out of bounds, return an invalid value.
else
{
coord += vec2(0.5);
coord /= screenScale;
}
return coord;
}
#endif
float CalcScanLineWeight(float dist)
{
return max(1.0-dist*dist*SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
}
float CalcScanLine(float dy)
{
float scanLineWeight = CalcScanLineWeight(dy);
#if defined(MULTISAMPLE)
scanLineWeight += CalcScanLineWeight(dy-filterWidth);
scanLineWeight += CalcScanLineWeight(dy+filterWidth);
scanLineWeight *= 0.3333333;
#endif
return scanLineWeight;
}
void main()
{
vec2 TextureSize = vec2(320.0, 256.0);
#if defined(CURVATURE)
vec2 texcoord = Distort(TEX0);
if (texcoord.x < 0.0)
gl_FragColor = vec4(0.0);
else
#else
vec2 texcoord = TEX0;
#endif
{
vec2 texcoordInPixels = texcoord * TextureSize;
#if defined(SHARPER)
vec2 tempCoord = floor(texcoordInPixels) + 0.5;
vec2 coord = tempCoord / TextureSize;
vec2 deltas = texcoordInPixels - tempCoord;
float scanLineWeight = CalcScanLine(deltas.y);
vec2 signs = sign(deltas);
deltas.x *= 2.0;
deltas = deltas * deltas;
deltas.y = deltas.y * deltas.y;
deltas.x *= 0.5;
deltas.y *= 8.0;
deltas /= TextureSize;
deltas *= signs;
vec2 tc = coord + deltas;
#else
float tempY = floor(texcoordInPixels.y) + 0.5;
float yCoord = tempY / TextureSize.y;
float dy = texcoordInPixels.y - tempY;
float scanLineWeight = CalcScanLine(dy);
float signY = sign(dy);
dy = dy * dy;
dy = dy * dy;
dy *= 8.0;
dy /= TextureSize.y;
dy *= signY;
vec2 tc = vec2(texcoord.x, yCoord + dy);
#endif
vec3 colour = texture2D(Texture, tc).rgb;
#if defined(SCANLINES)
#if defined(GAMMA)
#if defined(FAKE_GAMMA)
colour = colour * colour;
#else
colour = pow(colour, vec3(INPUT_GAMMA));
#endif
#endif
scanLineWeight *= BLOOM_FACTOR;
colour *= scanLineWeight;
#if defined(GAMMA)
#if defined(FAKE_GAMMA)
colour = sqrt(colour);
#else
colour = pow(colour, vec3(1.0/OUTPUT_GAMMA));
#endif
#endif
#endif
#if MASK_TYPE == 0
gl_FragColor = vec4(colour, 1.0);
#else
#if MASK_TYPE == 1
float whichMask = fract((gl_FragCoord.x*1.0001) * 0.5);
vec3 mask;
if (whichMask < 0.5)
mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
else
mask = vec3(1.0, MASK_BRIGHTNESS, 1.0);
#elif MASK_TYPE == 2
float whichMask = fract((gl_FragCoord.x*1.0001) * 0.3333333);
vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
if (whichMask < 0.3333333)
mask.x = 1.0;
else if (whichMask < 0.6666666)
mask.y = 1.0;
else
mask.z = 1.0;
#endif
gl_FragColor = vec4(colour * mask, 1.0);
#endif
}
}
#endif

View File

@@ -0,0 +1,157 @@
#version 330 core
// Configuración
#define SCANLINES
#define MULTISAMPLE
#define GAMMA
//#define FAKE_GAMMA
//#define CURVATURE
//#define SHARPER
#define MASK_TYPE 2
#define CURVATURE_X 0.05
#define CURVATURE_Y 0.1
#define MASK_BRIGHTNESS 0.80
#define SCANLINE_WEIGHT 6.0
#define SCANLINE_GAP_BRIGHTNESS 0.12
#define BLOOM_FACTOR 3.5
#define INPUT_GAMMA 2.4
#define OUTPUT_GAMMA 2.2
// Inputs desde vertex shader
in vec2 vTexCoord;
in float vFilterWidth;
#if defined(CURVATURE)
in vec2 vScreenScale;
#endif
// Output
out vec4 FragColor;
// Uniforms
uniform sampler2D Texture;
uniform vec2 TextureSize;
#if defined(CURVATURE)
vec2 Distort(vec2 coord)
{
vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y);
vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION);
coord *= vScreenScale;
coord -= vec2(0.5);
float rsq = coord.x * coord.x + coord.y * coord.y;
coord += coord * (CURVATURE_DISTORTION * rsq);
coord *= barrelScale;
if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
coord = vec2(-1.0);
else
{
coord += vec2(0.5);
coord /= vScreenScale;
}
return coord;
}
#endif
float CalcScanLineWeight(float dist)
{
return max(1.0 - dist * dist * SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
}
float CalcScanLine(float dy)
{
float scanLineWeight = CalcScanLineWeight(dy);
#if defined(MULTISAMPLE)
scanLineWeight += CalcScanLineWeight(dy - vFilterWidth);
scanLineWeight += CalcScanLineWeight(dy + vFilterWidth);
scanLineWeight *= 0.3333333;
#endif
return scanLineWeight;
}
void main()
{
#if defined(CURVATURE)
vec2 texcoord = Distort(vTexCoord);
if (texcoord.x < 0.0) {
FragColor = vec4(0.0);
return;
}
#else
vec2 texcoord = vTexCoord;
#endif
vec2 texcoordInPixels = texcoord * TextureSize;
#if defined(SHARPER)
vec2 tempCoord = floor(texcoordInPixels) + 0.5;
vec2 coord = tempCoord / TextureSize;
vec2 deltas = texcoordInPixels - tempCoord;
float scanLineWeight = CalcScanLine(deltas.y);
vec2 signs = sign(deltas);
deltas.x *= 2.0;
deltas = deltas * deltas;
deltas.y = deltas.y * deltas.y;
deltas.x *= 0.5;
deltas.y *= 8.0;
deltas /= TextureSize;
deltas *= signs;
vec2 tc = coord + deltas;
#else
float tempY = floor(texcoordInPixels.y) + 0.5;
float yCoord = tempY / TextureSize.y;
float dy = texcoordInPixels.y - tempY;
float scanLineWeight = CalcScanLine(dy);
float signY = sign(dy);
dy = dy * dy;
dy = dy * dy;
dy *= 8.0;
dy /= TextureSize.y;
dy *= signY;
vec2 tc = vec2(texcoord.x, yCoord + dy);
#endif
vec3 colour = texture(Texture, tc).rgb;
#if defined(SCANLINES)
#if defined(GAMMA)
#if defined(FAKE_GAMMA)
colour = colour * colour;
#else
colour = pow(colour, vec3(INPUT_GAMMA));
#endif
#endif
scanLineWeight *= BLOOM_FACTOR;
colour *= scanLineWeight;
#if defined(GAMMA)
#if defined(FAKE_GAMMA)
colour = sqrt(colour);
#else
colour = pow(colour, vec3(1.0 / OUTPUT_GAMMA));
#endif
#endif
#endif
#if MASK_TYPE == 0
FragColor = vec4(colour, 1.0);
#elif MASK_TYPE == 1
float whichMask = fract(gl_FragCoord.x * 0.5);
vec3 mask;
if (whichMask < 0.5)
mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
else
mask = vec3(1.0, MASK_BRIGHTNESS, 1.0);
FragColor = vec4(colour * mask, 1.0);
#elif MASK_TYPE == 2
float whichMask = fract(gl_FragCoord.x * 0.3333333);
vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
if (whichMask < 0.3333333)
mask.x = 1.0;
else if (whichMask < 0.6666666)
mask.y = 1.0;
else
mask.z = 1.0;
FragColor = vec4(colour * mask, 1.0);
#endif
}

View File

@@ -0,0 +1,152 @@
//
// CRT-Pi Fragment Shader - Metal Shading Language
// Portado desde GLSL a MSL para macOS
//
#include <metal_stdlib>
using namespace metal;
// Configuración (equivalente a los #define en GLSL)
constant bool SCANLINES = true;
constant bool MULTISAMPLE = true;
constant bool GAMMA = true;
constant bool FAKE_GAMMA = false;
constant bool CURVATURE = false;
constant bool SHARPER = false;
constant int MASK_TYPE = 2; // 0=none, 1=green/magenta, 2=trinitron
constant float CURVATURE_X = 0.05;
constant float CURVATURE_Y = 0.1;
constant float MASK_BRIGHTNESS = 0.80;
constant float SCANLINE_WEIGHT = 6.0;
constant float SCANLINE_GAP_BRIGHTNESS = 0.12;
constant float BLOOM_FACTOR = 3.5;
constant float INPUT_GAMMA = 2.4;
constant float OUTPUT_GAMMA = 2.2;
// Estructura de entrada (salida del vertex shader)
struct VertexOut {
float4 position [[position]];
float2 texCoord;
float filterWidth;
// float2 screenScale; // Solo si CURVATURE está activo
};
// Uniforms
struct Uniforms {
float2 textureSize;
};
// Función para calcular el peso de la scanline
float CalcScanLineWeight(float dist) {
return max(1.0 - dist * dist * SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
}
// Función para calcular scanline con multisampling
float CalcScanLine(float dy, float filterWidth) {
float scanLineWeight = CalcScanLineWeight(dy);
if (MULTISAMPLE) {
scanLineWeight += CalcScanLineWeight(dy - filterWidth);
scanLineWeight += CalcScanLineWeight(dy + filterWidth);
scanLineWeight *= 0.3333333;
}
return scanLineWeight;
}
// Entry point del fragment shader
fragment float4 fragment_main(VertexOut in [[stage_in]],
texture2d<float> colorTexture [[texture(0)]],
constant Uniforms& uniforms [[buffer(1)]],
sampler textureSampler [[sampler(0)]]) {
float2 texcoord = in.texCoord;
// Si CURVATURE estuviera activo, aquí iría la función Distort()
float2 texcoordInPixels = texcoord * uniforms.textureSize;
float2 tc;
float scanLineWeight;
if (!SHARPER) {
// Modo normal (no SHARPER)
float tempY = floor(texcoordInPixels.y) + 0.5;
float yCoord = tempY / uniforms.textureSize.y;
float dy = texcoordInPixels.y - tempY;
scanLineWeight = CalcScanLine(dy, in.filterWidth);
float signY = sign(dy);
dy = dy * dy;
dy = dy * dy;
dy *= 8.0;
dy /= uniforms.textureSize.y;
dy *= signY;
tc = float2(texcoord.x, yCoord + dy);
} else {
// Modo SHARPER
float2 tempCoord = floor(texcoordInPixels) + 0.5;
float2 coord = tempCoord / uniforms.textureSize;
float2 deltas = texcoordInPixels - tempCoord;
scanLineWeight = CalcScanLine(deltas.y, in.filterWidth);
float2 signs = sign(deltas);
deltas.x *= 2.0;
deltas = deltas * deltas;
deltas.y = deltas.y * deltas.y;
deltas.x *= 0.5;
deltas.y *= 8.0;
deltas /= uniforms.textureSize;
deltas *= signs;
tc = coord + deltas;
}
// Muestrear textura (texture() en GLSL = sample() en MSL)
float3 colour = colorTexture.sample(textureSampler, tc).rgb;
if (SCANLINES) {
if (GAMMA) {
if (FAKE_GAMMA) {
colour = colour * colour;
} else {
colour = pow(colour, float3(INPUT_GAMMA));
}
}
scanLineWeight *= BLOOM_FACTOR;
colour *= scanLineWeight;
if (GAMMA) {
if (FAKE_GAMMA) {
colour = sqrt(colour);
} else {
colour = pow(colour, float3(1.0 / OUTPUT_GAMMA));
}
}
}
// Aplicar máscara CRT
if (MASK_TYPE == 0) {
return float4(colour, 1.0);
} else if (MASK_TYPE == 1) {
// Máscara verde/magenta
float whichMask = fract(in.position.x * 0.5);
float3 mask;
if (whichMask < 0.5) {
mask = float3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
} else {
mask = float3(1.0, MASK_BRIGHTNESS, 1.0);
}
return float4(colour * mask, 1.0);
} else if (MASK_TYPE == 2) {
// Máscara trinitron
float whichMask = fract(in.position.x * 0.3333333);
float3 mask = float3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
if (whichMask < 0.3333333) {
mask.x = 1.0;
} else if (whichMask < 0.6666666) {
mask.y = 1.0;
} else {
mask.z = 1.0;
}
return float4(colour * mask, 1.0);
}
return float4(colour, 1.0);
}

View File

@@ -0,0 +1,48 @@
#version 330 core
// Configuración
#define SCANLINES
#define MULTISAMPLE
#define GAMMA
//#define FAKE_GAMMA
//#define CURVATURE
//#define SHARPER
#define MASK_TYPE 2
#define CURVATURE_X 0.05
#define CURVATURE_Y 0.1
#define MASK_BRIGHTNESS 0.80
#define SCANLINE_WEIGHT 6.0
#define SCANLINE_GAP_BRIGHTNESS 0.12
#define BLOOM_FACTOR 3.5
#define INPUT_GAMMA 2.4
#define OUTPUT_GAMMA 2.2
// Inputs (desde VAO)
layout(location = 0) in vec2 aPosition;
layout(location = 1) in vec2 aTexCoord;
// Outputs al fragment shader
out vec2 vTexCoord;
out float vFilterWidth;
#if defined(CURVATURE)
out vec2 vScreenScale;
#endif
// Uniforms
uniform vec2 TextureSize;
void main()
{
#if defined(CURVATURE)
vScreenScale = vec2(1.0, 1.0);
#endif
// Calcula filterWidth dinámicamente basándose en la altura de la textura
vFilterWidth = (768.0 / TextureSize.y) / 3.0;
// Pasar coordenadas de textura (invertir Y para SDL)
vTexCoord = vec2(aTexCoord.x, 1.0 - aTexCoord.y) * 1.0001;
// Posición del vértice (ya en espacio de clip [-1, 1])
gl_Position = vec4(aPosition, 0.0, 1.0);
}

View File

@@ -0,0 +1,46 @@
//
// CRT-Pi Vertex Shader - Metal Shading Language
// Portado desde GLSL a MSL para macOS
//
#include <metal_stdlib>
using namespace metal;
// Estructura de entrada del vertex shader (desde el buffer de vértices)
struct VertexIn {
float2 position [[attribute(0)]]; // Posición del vértice
float2 texCoord [[attribute(1)]]; // Coordenadas de textura
};
// Estructura de salida del vertex shader (entrada al fragment shader)
struct VertexOut {
float4 position [[position]]; // Posición en clip space
float2 texCoord; // Coordenadas de textura
float filterWidth; // Ancho del filtro calculado
// float2 screenScale; // Solo si CURVATURE está activo
};
// Uniforms (constantes del shader)
struct Uniforms {
float2 textureSize; // Tamaño de la textura (width, height)
};
// Entry point del vertex shader
vertex VertexOut vertex_main(VertexIn in [[stage_in]],
constant Uniforms& uniforms [[buffer(1)]]) {
VertexOut out;
// Posición del vértice (ya está en espacio de clip [-1, 1])
out.position = float4(in.position, 0.0, 1.0);
// Pasar coordenadas de textura (invertir Y para SDL, igual que en GLSL)
out.texCoord = float2(in.texCoord.x, 1.0 - in.texCoord.y) * 1.0001;
// Calcular filterWidth dinámicamente basándose en la altura de la textura
out.filterWidth = (768.0 / uniforms.textureSize.y) / 3.0;
// Si CURVATURE estuviera activo:
// out.screenScale = float2(1.0, 1.0);
return out;
}

BIN
data/sound/bullet2p.wav Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -2,11 +2,13 @@
#include <SDL3/SDL.h> // Para SDL_LogCategory, SDL_LogInfo, SDL_LogError, SDL_LogWarn
#include <cstddef> // Para size_t
#include <exception> // Para exception
#include <fstream> // Para basic_istream, basic_ifstream, ifstream, istringstream
#include <sstream> // Para basic_istringstream
#include <stdexcept> // Para runtime_error
#include <algorithm> // Para std::sort
#include <cstddef> // Para size_t
#include <exception> // Para exception
#include <filesystem> // Para std::filesystem
#include <fstream> // Para basic_istream, basic_ifstream, ifstream, istringstream
#include <sstream> // Para basic_istringstream
#include <stdexcept> // Para runtime_error
#include "resource_helper.h" // Para ResourceHelper
#include "utils.h" // Para getFileName
@@ -205,25 +207,28 @@ auto Asset::check() const -> bool {
}
// Comprueba que existe un fichero
auto Asset::checkFile(const std::string &path) -> bool {
// Intentar primero con ResourceHelper
auto data = ResourceHelper::loadFile(path);
bool success = !data.empty();
auto Asset::checkFile(const std::string &path) const -> bool {
// Construir ruta del pack usando executable_path_
std::string pack_path = executable_path_ + "resources.pack";
bool pack_exists = std::filesystem::exists(pack_path);
// Si no se encuentra en el pack, intentar con filesystem directo
if (!success) {
if (pack_exists) {
// MODO PACK: Usar ResourceHelper (igual que la carga real)
auto data = ResourceHelper::loadFile(path);
return !data.empty();
} else {
// MODO FILESYSTEM: Verificación directa (modo desarrollo)
std::ifstream file(path);
success = file.good();
bool success = file.good();
file.close();
}
if (!success) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Checking file: %s [ ERROR ]",
getFileName(path).c_str());
}
if (!success) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error: Could not open file: %s", path.c_str());
}
return success;
return success;
}
}
// Parsea string a Type
@@ -295,6 +300,9 @@ auto Asset::getListByType(Type type) const -> std::vector<std::string> {
}
}
// Ordenar alfabéticamente para garantizar orden consistente
std::sort(list.begin(), list.end());
return list;
}

View File

@@ -57,7 +57,7 @@ class Asset {
std::string executable_path_; // Ruta del ejecutable
// --- Métodos internos ---
[[nodiscard]] static auto checkFile(const std::string &path) -> bool; // Verifica si un archivo existe
[[nodiscard]] auto checkFile(const std::string &path) const -> bool; // Verifica si un archivo existe
[[nodiscard]] static auto getTypeName(Type type) -> std::string; // Obtiene el nombre del tipo
[[nodiscard]] static auto parseAssetType(const std::string &type_str) -> Type; // Convierte string a tipo
void addToMap(const std::string &file_path, Type type, bool required, bool absolute); // Añade archivo al mapa

View File

@@ -4,9 +4,7 @@
#include <algorithm> // Para clamp
#ifndef NO_AUDIO
#include "external/jail_audio.h" // Para JA_FadeOutMusic, JA_Init, JA_PauseM...
#endif
#include "options.h" // Para AudioOptions, audio, MusicOptions
#include "resource.h" // Para Resource
@@ -27,9 +25,7 @@ Audio::Audio() { initSDLAudio(); }
// Destructor
Audio::~Audio() {
#ifndef NO_AUDIO
JA_Quit();
#endif
}
// Método principal
@@ -43,9 +39,7 @@ void Audio::playMusic(const std::string &name, const int loop) {
music_.loop = (loop != 0);
if (music_enabled_ && music_.state != MusicState::PLAYING) {
#ifndef NO_AUDIO
JA_PlayMusic(Resource::get()->getMusic(name), loop);
#endif
music_.state = MusicState::PLAYING;
}
}
@@ -53,9 +47,7 @@ void Audio::playMusic(const std::string &name, const int loop) {
// Pausa la música
void Audio::pauseMusic() {
if (music_enabled_ && music_.state == MusicState::PLAYING) {
#ifndef NO_AUDIO
JA_PauseMusic();
#endif
music_.state = MusicState::PAUSED;
}
}
@@ -63,9 +55,7 @@ void Audio::pauseMusic() {
// Continua la música pausada
void Audio::resumeMusic() {
if (music_enabled_ && music_.state == MusicState::PAUSED) {
#ifndef NO_AUDIO
JA_ResumeMusic();
#endif
music_.state = MusicState::PLAYING;
}
}
@@ -73,9 +63,7 @@ void Audio::resumeMusic() {
// Detiene la música
void Audio::stopMusic() {
if (music_enabled_) {
#ifndef NO_AUDIO
JA_StopMusic();
#endif
music_.state = MusicState::STOPPED;
}
}
@@ -83,27 +71,37 @@ void Audio::stopMusic() {
// Reproduce un sonido
void Audio::playSound(const std::string &name, Group group) const {
if (sound_enabled_) {
#ifndef NO_AUDIO
JA_PlaySound(Resource::get()->getSound(name), 0, static_cast<int>(group));
#endif
}
}
// Detiene todos los sonidos
void Audio::stopAllSounds() const {
if (sound_enabled_) {
#ifndef NO_AUDIO
JA_StopChannel(-1);
#endif
}
}
// Realiza un fundido de salida de la música
void Audio::fadeOutMusic(int milliseconds) const {
if (music_enabled_ && music_.state == MusicState::PLAYING) {
#ifndef NO_AUDIO
if (music_enabled_ && getRealMusicState() == MusicState::PLAYING) {
JA_FadeOutMusic(milliseconds);
#endif
}
}
// Consulta directamente el estado real de la música en jailaudio
auto Audio::getRealMusicState() const -> MusicState {
JA_Music_state ja_state = JA_GetMusicState();
switch (ja_state) {
case JA_MUSIC_PLAYING:
return MusicState::PLAYING;
case JA_MUSIC_PAUSED:
return MusicState::PAUSED;
case JA_MUSIC_STOPPED:
case JA_MUSIC_INVALID:
case JA_MUSIC_DISABLED:
default:
return MusicState::STOPPED;
}
}
@@ -111,10 +109,8 @@ void Audio::fadeOutMusic(int milliseconds) const {
void Audio::setSoundVolume(int sound_volume, Group group) const {
if (sound_enabled_) {
sound_volume = std::clamp(sound_volume, MIN_VOLUME, MAX_VOLUME);
#ifndef NO_AUDIO
const float CONVERTED_VOLUME = (sound_volume / 100.0F) * (Options::audio.volume / 100.0F);
JA_SetSoundVolume(CONVERTED_VOLUME, static_cast<int>(group));
#endif
}
}
@@ -122,10 +118,8 @@ void Audio::setSoundVolume(int sound_volume, Group group) const {
void Audio::setMusicVolume(int music_volume) const {
if (music_enabled_) {
music_volume = std::clamp(music_volume, MIN_VOLUME, MAX_VOLUME);
#ifndef NO_AUDIO
const float CONVERTED_VOLUME = (music_volume / 100.0F) * (Options::audio.volume / 100.0F);
JA_SetMusicVolume(CONVERTED_VOLUME);
#endif
}
}
@@ -144,7 +138,6 @@ void Audio::enable(bool value) {
// Inicializa SDL Audio
void Audio::initSDLAudio() {
#ifndef NO_AUDIO
if (!SDL_Init(SDL_INIT_AUDIO)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_AUDIO could not initialize! SDL Error: %s", SDL_GetError());
} else {
@@ -153,7 +146,4 @@ void Audio::initSDLAudio() {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "** Audio system initialized successfully");
}
#else
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "** Audio system disabled");
#endif
}

View File

@@ -13,6 +13,12 @@ class Audio {
INTERFACE = 1 // Sonidos de la interfaz
};
enum class MusicState {
PLAYING, // Reproduciendo música
PAUSED, // Música pausada
STOPPED, // Música detenida
};
// --- Constantes ---
static constexpr int MAX_VOLUME = 100; // Volumen máximo
static constexpr int MIN_VOLUME = 0; // Volumen mínimo
@@ -60,14 +66,15 @@ class Audio {
void setSoundVolume(int volume, Group group = Group::ALL) const; // Ajustar volumen de efectos
void setMusicVolume(int volume) const; // Ajustar volumen de música
private:
// --- Enums privados ---
enum class MusicState {
PLAYING, // Reproduciendo música
PAUSED, // Música pausada
STOPPED, // Música detenida
};
// --- Getters para debug ---
bool isEnabled() const { return enabled_; }
bool isSoundEnabled() const { return sound_enabled_; }
bool isMusicEnabled() const { return music_enabled_; }
MusicState getMusicState() const { return music_.state; }
MusicState getRealMusicState() const; // Consulta directamente a jailaudio
const std::string& getCurrentMusicName() const { return music_.name; }
private:
// --- Estructuras privadas ---
struct Music {
MusicState state; // Estado actual de la música (reproduciendo, detenido, en pausa)

View File

@@ -122,6 +122,8 @@ class Balloon {
// --- Setters ---
void setVelY(float vel_y) { vy_ = vel_y; }
void setVelX(float vel_x) { vx_ = vel_x; }
void alterVelX(float percent) {vx_ *= percent; }
void setGameTempo(float tempo) { game_tempo_ = tempo; }
void setInvulnerable(bool value) { invulnerable_ = value; }
void setBouncingSound(bool value) { sound_.bouncing_enabled = value; }

View File

@@ -16,7 +16,7 @@
#include "utils.h"
// Constructor
BalloonManager::BalloonManager(IStageInfo *stage_info)
BalloonManager::BalloonManager(IStageInfo* stage_info)
: explosions_(std::make_unique<Explosions>()),
balloon_formations_(std::make_unique<BalloonFormations>()),
stage_info_(stage_info) { init(); }
@@ -64,7 +64,7 @@ void BalloonManager::init() {
// Actualiza (time-based)
void BalloonManager::update(float deltaTime) {
for (const auto &balloon : balloons_) {
for (const auto& balloon : balloons_) {
balloon->update(deltaTime);
}
updateBalloonDeployCounter(deltaTime);
@@ -73,7 +73,7 @@ void BalloonManager::update(float deltaTime) {
// Renderiza los objetos
void BalloonManager::render() {
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->render();
}
explosions_->render();
@@ -158,7 +158,7 @@ void BalloonManager::deployFormation(int formation_id, float y) {
// Vacia del vector de globos los globos que ya no sirven
void BalloonManager::freeBalloons() {
auto result = std::ranges::remove_if(balloons_, [](const auto &balloon) { return !balloon->isEnabled(); });
auto result = std::ranges::remove_if(balloons_, [](const auto& balloon) { return !balloon->isEnabled(); });
balloons_.erase(result.begin(), balloons_.end());
}
@@ -173,7 +173,7 @@ auto BalloonManager::canPowerBallBeCreated() -> bool { return (!power_ball_enabl
// Calcula el poder actual de los globos en pantalla
auto BalloonManager::calculateScreenPower() -> int {
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto &balloon) { return sum + (balloon->isEnabled() ? balloon->getPower() : 0); });
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto& balloon) { return sum + (balloon->isEnabled() ? balloon->getPower() : 0); });
}
// Crea un globo nuevo en el vector de globos
@@ -194,7 +194,7 @@ auto BalloonManager::createBalloon(Balloon::Config config) -> std::shared_ptr<Ba
}
// Crea un globo a partir de otro globo
void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon> &balloon, const std::string &direction) {
void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon>& balloon, const std::string& direction) {
if (can_deploy_balloons_) {
// Calcula parametros
const int PARENT_HEIGHT = balloon->getHeight();
@@ -208,6 +208,7 @@ void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon> &balloon,
Balloon::Config config = {
.x = std::clamp(X - (CHILD_WIDTH / 2), MIN_X, MAX_X),
.y = balloon->getPosY() + ((PARENT_HEIGHT - CHILD_HEIGHT) / 2),
.type = balloon->getType(),
.size = static_cast<Balloon::Size>(static_cast<int>(balloon->getSize()) - 1),
.vel_x = direction == "LEFT" ? Balloon::VELX_NEGATIVE : Balloon::VELX_POSITIVE,
.game_tempo = balloon_speed_,
@@ -216,9 +217,22 @@ void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon> &balloon,
// Crea el globo
auto b = createBalloon(config);
// Establece parametros (deltaTime en segundos - velocidades en pixels/segundo)
constexpr float VEL_Y_BALLOON_PER_S = -150.0F; // -2.50 pixels/frame convertido a pixels/segundo (-2.50 * 60 = -150)
b->setVelY(b->getType() == Balloon::Type::BALLOON ? VEL_Y_BALLOON_PER_S : Balloon::VELX_NEGATIVE * 2.0F);
// Establece parametros
constexpr float VEL_Y_BALLOON_PER_S = -150.0F;
switch (b->getType()) {
case Balloon::Type::BALLOON: {
b->setVelY(VEL_Y_BALLOON_PER_S);
break;
}
case Balloon::Type::FLOATER: {
const float MODIFIER = (rand() % 2 == 0) ? 1.0F : 1.0F;
b->setVelY(Balloon::VELX_NEGATIVE * 2.0F * MODIFIER);
(rand() % 2 == 0) ? b->alterVelX(1.0F) : b->alterVelX(1.0F);
break;
}
default:
break;
}
// Herencia de estados
if (balloon->isStopped()) { b->stop(); }
@@ -266,13 +280,13 @@ void BalloonManager::createPowerBall() {
// Establece la velocidad de los globos
void BalloonManager::setBalloonSpeed(float speed) {
balloon_speed_ = speed;
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->setGameTempo(speed);
}
}
// Explosiona un globo. Lo destruye y crea otros dos si es el caso
auto BalloonManager::popBalloon(const std::shared_ptr<Balloon> &balloon) -> int {
auto BalloonManager::popBalloon(const std::shared_ptr<Balloon>& balloon) -> int {
stage_info_->addPower(1);
int score = 0;
@@ -297,7 +311,7 @@ auto BalloonManager::popBalloon(const std::shared_ptr<Balloon> &balloon) -> int
}
// Explosiona un globo. Lo destruye = no crea otros globos
auto BalloonManager::destroyBalloon(std::shared_ptr<Balloon> &balloon) -> int {
auto BalloonManager::destroyBalloon(std::shared_ptr<Balloon>& balloon) -> int {
int score = 0;
// Calcula la puntuación y el poder que generaria el globo en caso de romperlo a él y a sus hijos
@@ -332,7 +346,7 @@ auto BalloonManager::destroyBalloon(std::shared_ptr<Balloon> &balloon) -> int {
// Destruye todos los globos
auto BalloonManager::destroyAllBalloons() -> int {
int score = 0;
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
score += destroyBalloon(balloon);
}
@@ -345,7 +359,7 @@ auto BalloonManager::destroyAllBalloons() -> int {
// Detiene todos los globos
void BalloonManager::stopAllBalloons() {
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
if (!balloon->isBeingCreated()) {
balloon->stop();
}
@@ -354,7 +368,7 @@ void BalloonManager::stopAllBalloons() {
// Pone en marcha todos los globos
void BalloonManager::startAllBalloons() {
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
if (!balloon->isBeingCreated()) {
balloon->start();
}
@@ -363,7 +377,7 @@ void BalloonManager::startAllBalloons() {
// Cambia el color de todos los globos
void BalloonManager::reverseColorsToAllBalloons() {
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
if (balloon->isStopped()) {
balloon->useReverseColor();
}
@@ -372,7 +386,7 @@ void BalloonManager::reverseColorsToAllBalloons() {
// Cambia el color de todos los globos
void BalloonManager::normalColorsToAllBalloons() {
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->useNormalColor();
}
}
@@ -384,13 +398,13 @@ void BalloonManager::createTwoBigBalloons() {
// Obtiene el nivel de ameza actual generado por los globos
auto BalloonManager::getMenace() -> int {
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto &balloon) { return sum + (balloon->isEnabled() ? balloon->getMenace() : 0); });
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto& balloon) { return sum + (balloon->isEnabled() ? balloon->getMenace() : 0); });
}
// Establece el sonido de los globos
void BalloonManager::setSounds(bool value) {
sound_enabled_ = value;
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->setSound(value);
}
}
@@ -398,14 +412,14 @@ void BalloonManager::setSounds(bool value) {
// Activa o desactiva los sonidos de rebote los globos
void BalloonManager::setBouncingSounds(bool value) {
bouncing_sound_enabled_ = value;
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->setBouncingSound(value);
}
}
// Activa o desactiva los sonidos de los globos al explotar
void BalloonManager::setPoppingSounds(bool value) {
poping_sound_enabled_ = value;
for (auto &balloon : balloons_) {
for (auto& balloon : balloons_) {
balloon->setPoppingSound(value);
}
}

View File

@@ -4,46 +4,64 @@
#include <string> // Para char_traits, basic_string, operator+, string
#include "param.h" // Para Param, ParamGame, param
#include "player.h" // Para Player::Id
#include "resource.h" // Para Resource
// Constructor
Bullet::Bullet(float x, float y, BulletType bullet_type, bool powered, Player::Id owner)
Bullet::Bullet(float x, float y, Type type, Color color, int owner)
: sprite_(std::make_unique<AnimatedSprite>(Resource::get()->getTexture("bullet.png"), Resource::get()->getAnimation("bullet.ani"))),
bullet_type_(bullet_type),
type_(type),
owner_(owner),
pos_x_(x),
pos_y_(y) {
vel_x_ = calculateVelocity(bullet_type_);
sprite_->setCurrentAnimation(buildAnimationString(bullet_type_, powered));
vel_x_ = calculateVelocity(type_);
sprite_->setCurrentAnimation(buildAnimationString(type_, color));
collider_.r = WIDTH / 2;
shiftColliders();
}
// Calcula la velocidad horizontal de la bala basada en su tipo
auto Bullet::calculateVelocity(BulletType bullet_type) -> float {
switch (bullet_type) {
case BulletType::LEFT:
auto Bullet::calculateVelocity(Type type) -> float {
switch (type) {
case Type::LEFT:
return VEL_X_LEFT;
case BulletType::RIGHT:
case Type::RIGHT:
return VEL_X_RIGHT;
default:
return VEL_X_CENTER;
}
}
// Construye el string de animación basado en el tipo de bala y si está potenciada
auto Bullet::buildAnimationString(BulletType bullet_type, bool powered) -> std::string {
std::string animation_string = powered ? "powered_" : "normal_";
// Construye el string de animación basado en el tipo de bala y color específico
auto Bullet::buildAnimationString(Type type, Color color) -> std::string {
std::string animation_string;
switch (bullet_type) {
case BulletType::UP:
// Mapear color a string específico
switch (color) {
case Color::YELLOW:
animation_string = "yellow_";
break;
case Color::GREEN:
animation_string = "green_";
break;
case Color::RED:
animation_string = "red_";
break;
case Color::PURPLE:
animation_string = "purple_";
break;
}
// Añadir dirección
switch (type) {
case Type::UP:
animation_string += "up";
break;
case BulletType::LEFT:
case Type::LEFT:
animation_string += "left";
break;
case BulletType::RIGHT:
case Type::RIGHT:
animation_string += "right";
break;
default:
@@ -53,49 +71,48 @@ auto Bullet::buildAnimationString(BulletType bullet_type, bool powered) -> std::
return animation_string;
}
// Implementación de render (llama al render del sprite_)
// Implementación de render
void Bullet::render() {
if (bullet_type_ != BulletType::NONE) {
if (type_ != Type::NONE) {
sprite_->render();
}
}
// Actualiza el estado del objeto (time-based)
auto Bullet::update(float deltaTime) -> BulletMoveStatus {
// Actualiza el estado del objeto
auto Bullet::update(float deltaTime) -> MoveStatus {
sprite_->update(deltaTime);
return move(deltaTime);
}
// Implementación del movimiento usando BulletMoveStatus (time-based)
auto Bullet::move(float deltaTime) -> BulletMoveStatus {
// DeltaTime puro: velocidad (pixels/segundo) * tiempo (segundos)
// Implementación del movimiento usando MoveStatus
auto Bullet::move(float deltaTime) -> MoveStatus {
pos_x_ += vel_x_ * deltaTime;
if (pos_x_ < param.game.play_area.rect.x - WIDTH || pos_x_ > param.game.play_area.rect.w) {
disable();
return BulletMoveStatus::OUT;
return MoveStatus::OUT;
}
pos_y_ += VEL_Y * deltaTime;
if (pos_y_ < param.game.play_area.rect.y - HEIGHT) {
disable();
return BulletMoveStatus::OUT;
return MoveStatus::OUT;
}
shiftSprite();
shiftColliders();
return BulletMoveStatus::OK;
return MoveStatus::OK;
}
auto Bullet::isEnabled() const -> bool {
return bullet_type_ != BulletType::NONE;
return type_ != Type::NONE;
}
void Bullet::disable() {
bullet_type_ = BulletType::NONE;
type_ = Type::NONE;
}
auto Bullet::getOwner() const -> Player::Id {
auto Bullet::getOwner() const -> int {
return owner_;
}

View File

@@ -6,22 +6,8 @@
#include <string> // Para string
#include "animated_sprite.h" // Para AnimatedSprite
#include "player.h" // Para Player
#include "utils.h" // Para Circle
// --- Enums ---
enum class BulletType : Uint8 {
UP, // Bala hacia arriba
LEFT, // Bala hacia la izquierda
RIGHT, // Bala hacia la derecha
NONE // Sin bala
};
enum class BulletMoveStatus : Uint8 {
OK = 0, // Movimiento normal
OUT = 1 // Fuera de los límites
};
// --- Clase Bullet: representa una bala del jugador ---
class Bullet {
public:
@@ -29,19 +15,39 @@ class Bullet {
static constexpr float WIDTH = 12.0F; // Anchura de la bala
static constexpr float HEIGHT = 12.0F; // Altura de la bala
// --- Enums ---
enum class Type : Uint8 {
UP, // Bala hacia arriba
LEFT, // Bala hacia la izquierda
RIGHT, // Bala hacia la derecha
NONE // Sin bala
};
enum class MoveStatus : Uint8 {
OK = 0, // Movimiento normal
OUT = 1 // Fuera de los límites
};
enum class Color : Uint8 {
YELLOW,
GREEN,
RED,
PURPLE
};
// --- Constructor y destructor ---
Bullet(float x, float y, BulletType bullet_type, bool powered, Player::Id owner); // Constructor principal
~Bullet() = default; // Destructor
Bullet(float x, float y, Type type, Color color, int owner); // Constructor principal
~Bullet() = default; // Destructor
// --- Métodos principales ---
void render(); // Dibuja la bala en pantalla
auto update(float deltaTime) -> BulletMoveStatus; // Actualiza el estado del objeto (time-based)
void disable(); // Desactiva la bala
void render(); // Dibuja la bala en pantalla
auto update(float deltaTime) -> MoveStatus; // Actualiza el estado del objeto (time-based)
void disable(); // Desactiva la bala
// --- Getters ---
[[nodiscard]] auto isEnabled() const -> bool; // Comprueba si está activa
[[nodiscard]] auto getOwner() const -> Player::Id; // Devuelve el identificador del dueño
auto getCollider() -> Circle &; // Devuelve el círculo de colisión
[[nodiscard]] auto isEnabled() const -> bool; // Comprueba si está activa
[[nodiscard]] auto getOwner() const -> int; // Devuelve el identificador del dueño
auto getCollider() -> Circle&; // Devuelve el círculo de colisión
private:
// --- Constantes ---
@@ -54,17 +60,17 @@ class Bullet {
std::unique_ptr<AnimatedSprite> sprite_; // Sprite con los gráficos
// --- Variables de estado ---
Circle collider_; // Círculo de colisión
BulletType bullet_type_; // Tipo de bala
Player::Id owner_; // Identificador del dueño
float pos_x_; // Posición en el eje X
float pos_y_; // Posición en el eje Y
float vel_x_; // Velocidad en el eje X
Circle collider_; // Círculo de colisión
Type type_; // Tipo de bala
int owner_; // Identificador del jugador
float pos_x_; // Posición en el eje X
float pos_y_; // Posición en el eje Y
float vel_x_; // Velocidad en el eje X
// --- Métodos internos ---
void shiftColliders(); // Ajusta el círculo de colisión
void shiftSprite(); // Ajusta el sprite
auto move(float deltaTime) -> BulletMoveStatus; // Mueve la bala y devuelve su estado (time-based)
static auto calculateVelocity(BulletType bullet_type) -> float; // Calcula la velocidad horizontal de la bala
static auto buildAnimationString(BulletType bullet_type, bool powered) -> std::string; // Construye el string de animación
void shiftColliders(); // Ajusta el círculo de colisión
void shiftSprite(); // Ajusta el sprite
auto move(float deltaTime) -> MoveStatus; // Mueve la bala y devuelve su estado (time-based)
static auto calculateVelocity(Type type) -> float; // Calcula la velocidad horizontal de la bala
static auto buildAnimationString(Type type, Color color) -> std::string; // Construye el string de animación
};

109
source/bullet_manager.cpp Normal file
View File

@@ -0,0 +1,109 @@
#include "bullet_manager.h"
#include <algorithm> // Para remove_if
#include "bullet.h" // Para Bullet
#include "param.h" // Para param
#include "player.h" // Para Player
// Constructor
BulletManager::BulletManager()
: play_area_(param.game.play_area.rect) {
}
// Actualiza el estado de todas las balas
void BulletManager::update(float deltaTime) {
for (auto& bullet : bullets_) {
if (bullet->isEnabled()) {
processBulletUpdate(bullet, deltaTime);
}
}
}
// Renderiza todas las balas activas
void BulletManager::render() {
for (auto& bullet : bullets_) {
if (bullet->isEnabled()) {
bullet->render();
}
}
}
// Crea una nueva bala
void BulletManager::createBullet(int x, int y, Bullet::Type type, Bullet::Color color, int owner) {
bullets_.emplace_back(std::make_shared<Bullet>(x, y, type, color, owner));
}
// Libera balas que ya no están habilitadas
void BulletManager::freeBullets() {
if (!bullets_.empty()) {
// Elimina las balas deshabilitadas del vector
bullets_.erase(
std::remove_if(bullets_.begin(), bullets_.end(),
[](const std::shared_ptr<Bullet>& bullet) {
return !bullet->isEnabled();
}),
bullets_.end());
}
}
// Elimina todas las balas
void BulletManager::clearAllBullets() {
bullets_.clear();
}
// Verifica colisiones de todas las balas
void BulletManager::checkCollisions() {
for (auto& bullet : bullets_) {
if (!bullet->isEnabled()) {
continue;
}
// Verifica colisión con Tabe
if (tabe_collision_callback_ && tabe_collision_callback_(bullet)) {
break; // Sale del bucle si hubo colisión
}
// Verifica colisión con globos
if (balloon_collision_callback_ && balloon_collision_callback_(bullet)) {
break; // Sale del bucle si hubo colisión
}
}
}
// Establece el callback para colisión con Tabe
void BulletManager::setTabeCollisionCallback(CollisionCallback callback) {
tabe_collision_callback_ = callback;
}
// Establece el callback para colisión con globos
void BulletManager::setBalloonCollisionCallback(CollisionCallback callback) {
balloon_collision_callback_ = callback;
}
// Establece el callback para balas fuera de límites
void BulletManager::setOutOfBoundsCallback(OutOfBoundsCallback callback) {
out_of_bounds_callback_ = callback;
}
// --- Métodos privados ---
// Procesa la actualización individual de una bala
void BulletManager::processBulletUpdate(const std::shared_ptr<Bullet>& bullet, float deltaTime) {
auto status = bullet->update(deltaTime);
// Si la bala salió de los límites, llama al callback
if (status == Bullet::MoveStatus::OUT && out_of_bounds_callback_) {
out_of_bounds_callback_(bullet);
}
}
// Verifica si la bala está fuera de los límites del área de juego
auto BulletManager::isBulletOutOfBounds(const std::shared_ptr<Bullet>& bullet) -> bool {
auto collider = bullet->getCollider();
return (collider.x < play_area_.x ||
collider.x > play_area_.x + play_area_.w ||
collider.y < play_area_.y ||
collider.y > play_area_.y + play_area_.h);
}

79
source/bullet_manager.h Normal file
View File

@@ -0,0 +1,79 @@
#pragma once
#include <SDL3/SDL.h> // Para SDL_FRect
#include <functional> // Para function
#include <memory> // Para shared_ptr, unique_ptr
#include <vector> // Para vector
#include "bullet.h" // Para Bullet
#include "utils.h" // Para Circle
// --- Types ---
using Bullets = std::vector<std::shared_ptr<Bullet>>;
// --- Forward declarations ---
class Player;
// --- Clase BulletManager: gestiona todas las balas del juego ---
//
// Esta clase se encarga de la gestión completa de las balas del juego,
// incluyendo su creación, actualización, renderizado y colisiones.
//
// Funcionalidades principales:
// • Gestión del ciclo de vida: creación, actualización y destrucción de balas
// • Renderizado: dibuja todas las balas activas en pantalla
// • Detección de colisiones: mediante sistema de callbacks
// • Limpieza automática: elimina balas deshabilitadas del contenedor
// • Configuración flexible: permite ajustar parámetros de las balas
//
// La clase utiliza un sistema de callbacks para manejar las colisiones,
// permitiendo que la lógica específica del juego permanezca en Game.
class BulletManager {
public:
// --- Types para callbacks ---
using CollisionCallback = std::function<bool(const std::shared_ptr<Bullet>&)>;
using OutOfBoundsCallback = std::function<void(const std::shared_ptr<Bullet>&)>;
// --- Constructor y destructor ---
BulletManager();
~BulletManager() = default;
// --- Métodos principales ---
void update(float deltaTime); // Actualiza el estado de las balas (time-based)
void render(); // Renderiza las balas en pantalla
// --- Gestión de balas ---
void createBullet(int x, int y, Bullet::Type type, Bullet::Color color, int owner); // Crea una nueva bala
void freeBullets(); // Libera balas que ya no sirven
void clearAllBullets(); // Elimina todas las balas
// --- Detección de colisiones ---
void checkCollisions(); // Verifica colisiones de todas las balas
void setTabeCollisionCallback(CollisionCallback callback); // Establece callback para colisión con Tabe
void setBalloonCollisionCallback(CollisionCallback callback); // Establece callback para colisión con globos
void setOutOfBoundsCallback(OutOfBoundsCallback callback); // Establece callback para balas fuera de límites
// --- Configuración ---
void setPlayArea(SDL_FRect play_area) { play_area_ = play_area; }; // Define el área de juego
// --- Getters ---
auto getBullets() -> Bullets& { return bullets_; } // Obtiene referencia al vector de balas
[[nodiscard]] auto getNumBullets() const -> int { return bullets_.size(); } // Obtiene el número de balas activas
private:
// --- Objetos y punteros ---
Bullets bullets_; // Vector con las balas activas
// --- Variables de configuración ---
SDL_FRect play_area_; // Área de juego para límites
// --- Callbacks para colisiones ---
CollisionCallback tabe_collision_callback_; // Callback para colisión con Tabe
CollisionCallback balloon_collision_callback_; // Callback para colisión con globos
OutOfBoundsCallback out_of_bounds_callback_; // Callback para balas fuera de límites
// --- Métodos internos ---
void processBulletUpdate(const std::shared_ptr<Bullet>& bullet, float deltaTime); // Procesa actualización individual
auto isBulletOutOfBounds(const std::shared_ptr<Bullet>& bullet) -> bool; // Verifica si la bala está fuera de límites
};

View File

@@ -95,6 +95,29 @@ struct Color {
return Color(new_r, new_g, new_b, new_a);
}
// Interpolación lineal hacia otro color (t=0.0: this, t=1.0: target)
[[nodiscard]] constexpr auto LERP(const Color &target, float t) const -> Color {
// Asegurar que t esté en el rango [0.0, 1.0]
t = std::clamp(t, 0.0f, 1.0f);
// Interpolación lineal para cada componente
auto lerp_component = [t](Uint8 start, Uint8 end) -> Uint8 {
return static_cast<Uint8>(start + (end - start) * t);
};
return Color(
lerp_component(r, target.r),
lerp_component(g, target.g),
lerp_component(b, target.b),
lerp_component(a, target.a)
);
}
// Sobrecarga para aceptar componentes RGBA directamente
[[nodiscard]] constexpr auto LERP(Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha, float t) const -> Color {
return LERP(Color(red, green, blue, alpha), t);
}
// Convierte el color a un entero de 32 bits en formato RGBA
[[nodiscard]] constexpr auto TO_UINT32() const -> Uint32 {
return (static_cast<Uint32>(r) << 24) |

View File

@@ -6,6 +6,7 @@
#include "color.h"
#include "ui/notifier.h" // Para Notifier::Position
#include "version.h" // Para Version::APP_NAME
// --- Namespace GameDefaults: configuración centralizada con valores por defecto del juego ---
namespace GameDefaults {
@@ -14,7 +15,6 @@ namespace GameDefaults {
namespace Game {
constexpr float WIDTH = 320.0F;
constexpr float HEIGHT = 256.0F;
constexpr float ITEM_SIZE = 20.0F;
constexpr int NAME_ENTRY_IDLE_TIME = 10;
constexpr int NAME_ENTRY_TOTAL_TIME = 60;
constexpr bool HIT_STOP = false;
@@ -211,7 +211,7 @@ constexpr const char* PLAYER1 = "422028FF";
// --- OPTIONS ---
namespace Options {
// Window
constexpr const char* WINDOW_CAPTION = "Coffee Crisis Arcade Edition";
constexpr const char* WINDOW_CAPTION = Version::APP_NAME;
constexpr int WINDOW_ZOOM = 2;
constexpr int WINDOW_MAX_ZOOM = 2;

70
source/demo.cpp Normal file
View File

@@ -0,0 +1,70 @@
#include "demo.h"
#include <SDL3/SDL.h> // Para SDL_IOStream, SDL_IOFromConstMem, SDL_IOFromFile, SDL_ReadIO, SDL_WriteIO, SDL_CloseIO
#include <stdexcept> // Para runtime_error
#include "resource_helper.h" // Para ResourceHelper
#include "utils.h" // Para printWithDots, getFileName
// Carga el fichero de datos para la demo
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData {
DemoData dd;
SDL_IOStream *file = nullptr;
// Intentar cargar desde ResourceHelper primero
auto resource_data = ResourceHelper::loadFile(file_path);
if (!resource_data.empty()) {
file = SDL_IOFromConstMem(resource_data.data(), resource_data.size());
} else {
// Fallback a filesystem directo
file = SDL_IOFromFile(file_path.c_str(), "r+b");
}
if (file == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
throw std::runtime_error("Fichero no encontrado: " + file_path);
}
printWithDots("DemoData : ", getFileName(file_path), "[ LOADED ]");
// Lee todos los datos del fichero y los deja en el destino
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
DemoKeys dk = DemoKeys();
SDL_ReadIO(file, &dk, sizeof(DemoKeys));
dd.push_back(dk);
}
// Cierra el fichero
SDL_CloseIO(file);
return dd;
}
#ifdef RECORDING
// Guarda el fichero de datos para la demo
bool saveDemoFile(const std::string &file_path, const DemoData &dd) {
auto success = true;
auto file = SDL_IOFromFile(file_path.c_str(), "w+b");
if (file) {
// Guarda los datos
for (const auto &data : dd) {
if (SDL_WriteIO(file, &data, sizeof(DemoKeys)) != sizeof(DemoKeys)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al escribir el fichero %s", getFileName(file_path).c_str());
success = false;
break;
}
}
if (success) {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file %s", getFileName(file_path).c_str());
}
// Cierra el fichero
SDL_CloseIO(file);
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to save %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
success = false;
}
return success;
}
#endif // RECORDING

54
source/demo.h Normal file
View File

@@ -0,0 +1,54 @@
#pragma once
#include <SDL3/SDL.h> // Para Uint8
#include <string> // Para string
#include <vector> // Para vector
// --- Constantes ---
constexpr int TOTAL_DEMO_DATA = 2000;
// --- Estructuras ---
struct DemoKeys {
Uint8 left;
Uint8 right;
Uint8 no_input;
Uint8 fire;
Uint8 fire_left;
Uint8 fire_right;
explicit DemoKeys(Uint8 l = 0, Uint8 r = 0, Uint8 ni = 0, Uint8 f = 0, Uint8 fl = 0, Uint8 fr = 0)
: left(l),
right(r),
no_input(ni),
fire(f),
fire_left(fl),
fire_right(fr) {}
};
// --- Tipos ---
using DemoData = std::vector<DemoKeys>;
struct Demo {
bool enabled = false; // Indica si está activo el modo demo
bool recording = false; // Indica si está activado el modo para grabar la demo
float elapsed_s = 0.0F; // Segundos transcurridos de demo
int index = 0; // Contador para el modo demo
DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo
std::vector<DemoData> data; // Vector con diferentes sets de datos con los movimientos para la demo
Demo() = default;
Demo(bool e, bool r, int c, const DemoKeys& k, const std::vector<DemoData>& d)
: enabled(e),
recording(r),
index(c),
keys(k),
data(d) {}
};
// --- Funciones ---
auto loadDemoDataFromFile(const std::string& file_path) -> DemoData;
#ifdef RECORDING
bool saveDemoFile(const std::string& file_path, const DemoData& dd);
#endif

View File

@@ -42,7 +42,7 @@ Director::Director(int argc, std::span<char *> argv) {
Section::name = Section::Name::GAME;
Section::options = Section::Options::GAME_PLAY_1P;
#elif _DEBUG
Section::name = Section::Name::TITLE;
Section::name = Section::Name::GAME;
Section::options = Section::Options::GAME_PLAY_1P;
#else // NORMAL GAME
Section::name = Section::Name::LOGO;
@@ -80,13 +80,13 @@ void Director::init() {
Asset::init(executable_path_); // Inicializa el sistema de gestión de archivos
#ifdef MACOS_BUNDLE
ResourceHelper::initializeResourceSystem(executable_path_ + "/../Resources/resources.pack");
ResourceHelper::initializeResourceSystem(executable_path_ + "../Resources/resources.pack");
#else
ResourceHelper::initializeResourceSystem("resources.pack");
ResourceHelper::initializeResourceSystem(executable_path_ + "resources.pack");
#endif
loadAssets(); // Crea el índice de archivos
Input::init(Asset::get()->get("gamecontrollerdb.txt"), Asset::get()->get("controllers.json")); // Carga configuración de controles
Options::setConfigFile(Asset::get()->get("config.txt")); // Establece el fichero de configuración
Options::setConfigFile(Asset::get()->get("config_v2.txt")); // Establece el fichero de configuración
Options::setControllersFile(Asset::get()->get("controllers.json")); // Establece el fichero de configuración de mandos
Options::loadFromFile(); // Carga el archivo de configuración
loadParams(); // Carga los parámetros del programa
@@ -174,8 +174,14 @@ void Director::loadAssets() {
// Comprueba los parametros del programa
void Director::checkProgramArguments(int argc, std::span<char *> argv) {
// Establece la ruta del programa
executable_path_ = getPath(argv[0]);
// Obtener la ruta absoluta del ejecutable
std::filesystem::path exe_path = std::filesystem::absolute(argv[0]);
executable_path_ = exe_path.parent_path().string();
// Asegurar que termine con separador de directorio
if (!executable_path_.empty() && executable_path_.back() != '/' && executable_path_.back() != '\\') {
executable_path_ += "/";
}
// Comprueba el resto de parámetros
for (int i = 1; i < argc; ++i) {

View File

@@ -1,147 +1,96 @@
#include "enter_name.h"
#include <cstddef> // Para size_t
#include <array> // Para array
#include <cstdlib> // Para rand
#include <string_view> // Para basic_string_view, string_view
#include "utils.h" // Para trim
// Constructor
EnterName::EnterName()
: character_list_(" ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789."),
character_index_{0} {}
: character_list_("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789."),
selected_index_(0) {}
// Inicializa el objeto
void EnterName::init(const std::string &name) {
// No se pasa ningún nombre
if (name.empty()) {
name_ = "A";
position_ = 0;
position_overflow_ = false;
}
// Se pasa un nombre
else {
name_ = name;
position_ = name_.length();
position_overflow_ = position_ >= NAME_SIZE;
}
// Inicializa el vector de indices con el nombre y espacios
initCharacterIndex(name_);
name_ = sanitizeName(name);
selected_index_ = 0;
}
// Incrementa la posición
void EnterName::incPosition() {
if (position_overflow_) {
// Si ya estamos en overflow, no incrementamos más.
return;
}
++position_;
if (position_ >= NAME_SIZE) {
position_ = NAME_SIZE; // Mantenemos en el índice máximo válido.
position_overflow_ = true; // Activamos el flag de overflow.
} else if (position_ > 0) // No es necesario verificar position_ < MAX_NAME_LENGTH
{
// Copiamos el índice del carácter anterior si es posible.
// character_index_[position_] = character_index_[position_ - 1];
// Ponemos el caracter "espacio"
character_index_[position_] = 0;
} else {
// Si position_ es 0, inicializamos el carácter actual.
character_index_[position_] = 0;
}
updateNameFromCharacterIndex();
}
// Decrementa la posición
void EnterName::decPosition() {
if (position_overflow_) {
// Si estaba en overflow, lo desactivamos y mantenemos position_ en el máximo.
position_overflow_ = false;
position_ = NAME_SIZE - 1;
} else {
if (position_ > 0) {
--position_;
// Limpiamos el carácter siguiente si el índice es válido.
if (position_ + 1 < NAME_SIZE) {
character_index_[position_ + 1] = 0;
}
} else {
// Si position_ es 0, aseguramos que no vaya a ser negativo y limpiamos el carácter actual.
position_ = 0;
// character_index_[position_] = 0;
}
// Si position_ es menor que NAME_LENGTH, aseguramos que el overflow esté desactivado.
if (position_ < NAME_SIZE) {
position_overflow_ = false;
}
}
updateNameFromCharacterIndex();
}
// Incrementa el índice
// Incrementa el índice del carácter seleccionado
void EnterName::incIndex() {
if (position_overflow_) {
return;
++selected_index_;
if (selected_index_ >= static_cast<int>(character_list_.size())) {
selected_index_ = 0;
}
++character_index_[position_];
if (character_index_[position_] >= static_cast<int>(character_list_.size())) {
character_index_[position_] = 0;
}
updateNameFromCharacterIndex();
}
// Decrementa el índice
// Decrementa el índice del carácter seleccionado
void EnterName::decIndex() {
if (position_overflow_) {
return;
}
--character_index_[position_];
if (character_index_[position_] < 0) {
character_index_[position_] = character_list_.size() - 1;
}
updateNameFromCharacterIndex();
}
// Actualiza el nombre a partir de la lista de índices
void EnterName::updateNameFromCharacterIndex() {
name_.clear();
for (size_t i = 0; i < NAME_SIZE; ++i) {
name_.push_back(character_list_[character_index_[i]]);
}
name_ = trim(name_);
}
// Actualiza la variable
void EnterName::initCharacterIndex(const std::string &name) {
// Rellena de espacios
for (size_t i = 0; i < NAME_SIZE; ++i) {
character_index_[i] = 0;
}
// Coloca los índices en función de los caracteres que forman el nombre
for (size_t i = 0; i < name.substr(0, NAME_SIZE).size(); ++i) {
character_index_[i] = findIndex(name.at(i));
--selected_index_;
if (selected_index_ < 0) {
selected_index_ = character_list_.size() - 1;
}
}
// Encuentra el indice de un caracter en "character_list_"
auto EnterName::findIndex(char character) const -> int {
for (size_t i = 0; i < character_list_.size(); ++i) {
if (character == character_list_.at(i)) {
return i;
// Añade el carácter seleccionado al nombre
void EnterName::addCharacter() {
if (name_.length() < MAX_NAME_SIZE) {
name_.push_back(character_list_[selected_index_]);
}
}
// Elimina el último carácter del nombre
void EnterName::removeLastCharacter() {
if (!name_.empty()) {
name_.pop_back();
}
}
// Devuelve el carácter seleccionado con offset relativo como string
auto EnterName::getSelectedCharacter(int offset) const -> std::string {
// Calcular el índice con offset, con wrap-around circular
int size = static_cast<int>(character_list_.size());
int index = (selected_index_ + offset) % size;
// Manejar índices negativos (hacer wrap-around hacia atrás)
if (index < 0) {
index += size;
}
return std::string(1, character_list_[index]);
}
// Devuelve el carrusel completo de caracteres centrado en el seleccionado
auto EnterName::getCarousel(int size) const -> std::string {
// Asegurar que el tamaño sea impar para tener un centro claro
if (size % 2 == 0) {
++size;
}
std::string carousel;
carousel.reserve(size); // Optimización: reservar memoria de antemano
int half = size / 2;
// Construir desde -half hasta +half (inclusive)
for (int offset = -half; offset <= half; ++offset) {
carousel += getSelectedCharacter(offset);
}
return carousel;
}
// Valida y limpia el nombre: solo caracteres legales y longitud máxima
auto EnterName::sanitizeName(const std::string &name) const -> std::string {
std::string sanitized;
for (size_t i = 0; i < name.length() && sanitized.length() < MAX_NAME_SIZE; ++i) {
// Verifica si el carácter está en la lista permitida
if (character_list_.find(name[i]) != std::string::npos) {
sanitized.push_back(name[i]);
}
}
return 0;
return sanitized;
}
// Devuelve un nombre al azar
@@ -157,12 +106,11 @@ auto EnterName::getRandomName() -> std::string {
"PEPE"};
return std::string(NAMES[rand() % NAMES.size()]);
}
// Obtiene el nombre final introducido
auto EnterName::getFinalName() -> std::string {
auto name = trim(name_.substr(0, position_ + 1)); // Devuelve el texto intruducido incluyendo el del selector
if (name.empty()) {
name = getRandomName();
if (name_.empty()) {
name_ = getRandomName();
}
name_ = name;
return name_;
}

View File

@@ -1,43 +1,38 @@
#pragma once
#include <array> // Para array
#include <cstddef> // Para size_t
#include <string> // Para allocator, string
#include "utils.h" // Para trim
// --- Constantes ---
constexpr size_t NAME_SIZE = 5; // Tamaño máximo del nombre
// --- Clase EnterName: gestor de entrada de nombre del jugador ---
class EnterName {
public:
// --- Constantes ---
static constexpr size_t MAX_NAME_SIZE = 6; // Tamaño máximo del nombre
EnterName();
~EnterName() = default;
void init(const std::string &name = ""); // Inicializa con un nombre opcional
void init(const std::string &name = ""); // Inicializa con nombre opcional (vacío por defecto)
void incPosition(); // Incrementa la posición del carácter actual
void decPosition(); // Decrementa la posición del carácter actual
void incIndex(); // Incrementa el índice del carácter en la lista
void decIndex(); // Decrementa el índice del carácter en la lista
void incIndex(); // Incrementa el índice del carácter seleccionado en la lista
void decIndex(); // Decrementa el índice del carácter seleccionado en la lista
auto getFinalName() -> std::string; // Obtiene el nombre final introducido
[[nodiscard]] auto getCurrentName() const -> std::string { return trim(name_); } // Obtiene el nombre actual en proceso
void addCharacter(); // Añade el carácter seleccionado al nombre
void removeLastCharacter(); // Elimina el último carácter del nombre
[[nodiscard]] auto getPosition() const -> int { return position_; } // Posición actual del carácter editado
[[nodiscard]] auto getPositionOverflow() const -> bool { return position_overflow_; } // Indica si la posición excede el límite
auto getFinalName() -> std::string; // Obtiene el nombre final (o aleatorio si vacío)
[[nodiscard]] auto getCurrentName() const -> std::string { return name_; } // Obtiene el nombre actual en proceso
[[nodiscard]] auto getSelectedCharacter(int offset = 0) const -> std::string; // Devuelve el carácter seleccionado con offset relativo
[[nodiscard]] auto getCarousel(int size) const -> std::string; // Devuelve el carrusel de caracteres (size debe ser impar)
[[nodiscard]] auto getSelectedIndex() const -> int { return selected_index_; } // Obtiene el índice del carácter seleccionado
[[nodiscard]] auto getCharacterList() const -> const std::string& { return character_list_; } // Obtiene la lista completa de caracteres
private:
// --- Variables de estado ---
std::string character_list_; // Lista de caracteres permitidos
std::string name_; // Nombre en proceso
std::array<int, NAME_SIZE> character_index_; // Índices a "character_list_"
size_t position_ = 0; // Índice del carácter que se edita
bool position_overflow_ = false; // Flag para exceder límite
std::string character_list_; // Lista de caracteres permitidos
std::string name_; // Nombre en proceso
int selected_index_ = 0; // Índice del carácter seleccionado en "character_list_"
void updateNameFromCharacterIndex(); // Actualiza "name_" según "character_index_"
void initCharacterIndex(const std::string &name); // Inicializa índices desde el nombre
[[nodiscard]] auto findIndex(char character) const -> int; // Busca el índice de un carácter en "character_list_"
static auto getRandomName() -> std::string; // Devuelve un nombre al azar
[[nodiscard]] auto sanitizeName(const std::string &name) const -> std::string; // Valida y limpia el nombre
static auto getRandomName() -> std::string; // Devuelve un nombre al azar
};

View File

@@ -1,415 +0,0 @@
#include "jail_shader.h"
#include <SDL3/SDL.h> // Para SDL_GL_GetProcAddress, SDL_LogError
#include <stdint.h> // Para uintptr_t
#include <cstring> // Para strncmp
#include <stdexcept> // Para runtime_error
#include <vector> // Para vector
#ifdef __APPLE__
#include <OpenGL/OpenGL.h> // Para OpenGL en macOS
#include "CoreFoundation/CoreFoundation.h" // Para Core Foundation en macOS
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#include <OpenGL/gl3.h> // Para OpenGL 3 en macOS
#else // NO ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#include <OpenGL/gl.h> // Para OpenGL (compatibilidad) en macOS
#endif // ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#else // SI NO ES __APPLE__
#include <SDL3/SDL_opengl.h> // Para GLuint, GLint, glTexCoord2f, glVertex2f
#endif // __APPLE__
namespace shader {
// Constantes
const GLuint INVALID_SHADER_ID = 0;
const GLuint INVALID_PROGRAM_ID = 0;
const GLuint DEFAULT_TEXTURE_ID = 1;
// Variables globales
SDL_Window *win = nullptr;
SDL_Renderer *renderer = nullptr;
GLuint programId = 0;
SDL_Texture *backBuffer = nullptr;
SDL_Point win_size = {320 * 4, 256 * 4};
SDL_FPoint tex_size = {320, 256};
bool usingOpenGL = false;
#ifndef __APPLE__
// Declaración de funciones de extensión de OpenGL (evitando GLEW)
PFNGLCREATESHADERPROC glCreateShader;
PFNGLSHADERSOURCEPROC glShaderSource;
PFNGLCOMPILESHADERPROC glCompileShader;
PFNGLGETSHADERIVPROC glGetShaderiv;
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
PFNGLDELETESHADERPROC glDeleteShader;
PFNGLATTACHSHADERPROC glAttachShader;
PFNGLCREATEPROGRAMPROC glCreateProgram;
PFNGLLINKPROGRAMPROC glLinkProgram;
PFNGLVALIDATEPROGRAMPROC glValidateProgram;
PFNGLGETPROGRAMIVPROC glGetProgramiv;
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
PFNGLUSEPROGRAMPROC glUseProgram;
PFNGLDELETEPROGRAMPROC glDeleteProgram;
bool initGLExtensions() {
glCreateShader = (PFNGLCREATESHADERPROC)SDL_GL_GetProcAddress("glCreateShader");
glShaderSource = (PFNGLSHADERSOURCEPROC)SDL_GL_GetProcAddress("glShaderSource");
glCompileShader = (PFNGLCOMPILESHADERPROC)SDL_GL_GetProcAddress("glCompileShader");
glGetShaderiv = (PFNGLGETSHADERIVPROC)SDL_GL_GetProcAddress("glGetShaderiv");
glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)SDL_GL_GetProcAddress("glGetShaderInfoLog");
glDeleteShader = (PFNGLDELETESHADERPROC)SDL_GL_GetProcAddress("glDeleteShader");
glAttachShader = (PFNGLATTACHSHADERPROC)SDL_GL_GetProcAddress("glAttachShader");
glCreateProgram = (PFNGLCREATEPROGRAMPROC)SDL_GL_GetProcAddress("glCreateProgram");
glLinkProgram = (PFNGLLINKPROGRAMPROC)SDL_GL_GetProcAddress("glLinkProgram");
glValidateProgram = (PFNGLVALIDATEPROGRAMPROC)SDL_GL_GetProcAddress("glValidateProgram");
glGetProgramiv = (PFNGLGETPROGRAMIVPROC)SDL_GL_GetProcAddress("glGetProgramiv");
glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)SDL_GL_GetProcAddress("glGetProgramInfoLog");
glUseProgram = (PFNGLUSEPROGRAMPROC)SDL_GL_GetProcAddress("glUseProgram");
glDeleteProgram = (PFNGLDELETEPROGRAMPROC)SDL_GL_GetProcAddress("glDeleteProgram");
return glCreateShader && glShaderSource && glCompileShader && glGetShaderiv &&
glGetShaderInfoLog && glDeleteShader && glAttachShader && glCreateProgram &&
glLinkProgram && glValidateProgram && glGetProgramiv && glGetProgramInfoLog &&
glUseProgram && glDeleteProgram;
}
#endif
// Función para verificar errores de OpenGL
void checkGLError(const char *operation) {
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error OpenGL en %s: 0x%x",
operation,
error);
}
}
// Función para compilar un shader a partir de un std::string
GLuint compileShader(const std::string &source, GLuint shader_type) {
if (source.empty()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "ERROR FATAL: El código fuente del shader está vacío.");
throw std::runtime_error("ERROR FATAL: El código fuente del shader está vacío.");
}
// Crear identificador del shader
GLuint shader_id = glCreateShader(shader_type);
if (shader_id == INVALID_SHADER_ID) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al crear el shader.");
checkGLError("glCreateShader");
return INVALID_SHADER_ID;
}
// Agregar una directiva según el tipo de shader
std::string directive = (shader_type == GL_VERTEX_SHADER)
? "#define VERTEX\n"
: "#define FRAGMENT\n";
const char *sources[2] = {directive.c_str(), source.c_str()};
// Especificar el código fuente del shader
glShaderSource(shader_id, 2, sources, nullptr);
checkGLError("glShaderSource");
// Compilar el shader
glCompileShader(shader_id);
checkGLError("glCompileShader");
// Verificar si la compilación fue exitosa
GLint compiled_ok = GL_FALSE;
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &compiled_ok);
if (compiled_ok != GL_TRUE) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error en la compilación del shader (%d)!", shader_id);
GLint log_length;
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 0) {
std::vector<GLchar> log(log_length);
glGetShaderInfoLog(shader_id, log_length, &log_length, log.data());
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Registro de compilación del shader: %s", log.data());
}
glDeleteShader(shader_id);
return INVALID_SHADER_ID;
}
return shader_id;
}
// Función para compilar un programa de shaders (vertex y fragment) a partir de std::string
GLuint compileProgram(const std::string &vertex_shader_source, const std::string &fragment_shader_source) {
GLuint program_id = glCreateProgram();
if (program_id == INVALID_PROGRAM_ID) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al crear el programa de shaders.");
checkGLError("glCreateProgram");
return INVALID_PROGRAM_ID;
}
// Si el fragment shader está vacío, reutilizamos el código del vertex shader
GLuint vertex_shader_id = compileShader(vertex_shader_source, GL_VERTEX_SHADER);
GLuint fragment_shader_id = compileShader(fragment_shader_source.empty() ? vertex_shader_source : fragment_shader_source, GL_FRAGMENT_SHADER);
if (vertex_shader_id != INVALID_SHADER_ID && fragment_shader_id != INVALID_SHADER_ID) {
// Asociar los shaders al programa
glAttachShader(program_id, vertex_shader_id);
checkGLError("glAttachShader vertex");
glAttachShader(program_id, fragment_shader_id);
checkGLError("glAttachShader fragment");
glLinkProgram(program_id);
checkGLError("glLinkProgram");
// Verificar el estado del enlace
GLint isLinked = GL_FALSE;
glGetProgramiv(program_id, GL_LINK_STATUS, &isLinked);
if (isLinked == GL_FALSE) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al enlazar el programa de shaders.");
GLint log_length;
glGetProgramiv(program_id, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 0) {
std::vector<char> log(log_length);
glGetProgramInfoLog(program_id, log_length, &log_length, log.data());
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Registro de enlace del programa: %s", log.data());
}
glDeleteProgram(program_id);
program_id = INVALID_PROGRAM_ID;
} else {
glValidateProgram(program_id);
checkGLError("glValidateProgram");
// Log de información del programa (solo si hay información)
GLint log_length;
glGetProgramiv(program_id, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 1) // > 1 porque algunos drivers devuelven 1 para cadena vacía
{
std::vector<char> log(log_length);
glGetProgramInfoLog(program_id, log_length, &log_length, log.data());
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Registro de información del programa:\n%s", log.data());
}
}
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudieron compilar los shaders.");
glDeleteProgram(program_id);
program_id = INVALID_PROGRAM_ID;
}
// Limpiar los shaders (ya no son necesarios después del enlace)
if (vertex_shader_id != INVALID_SHADER_ID) {
glDeleteShader(vertex_shader_id);
}
if (fragment_shader_id != INVALID_SHADER_ID) {
glDeleteShader(fragment_shader_id);
}
return program_id;
}
// Función para obtener el ID de textura OpenGL desde SDL3
GLuint getTextureID(SDL_Texture *texture) {
if (!texture)
return DEFAULT_TEXTURE_ID;
// Intentar obtener el ID de textura OpenGL desde las propiedades de SDL3
SDL_PropertiesID props = SDL_GetTextureProperties(texture);
GLuint textureId = 0;
// Intentar diferentes nombres de propiedades según la versión de SDL3
textureId = (GLuint)(uintptr_t)SDL_GetPointerProperty(props, "SDL.texture.opengl.texture", nullptr);
// Si la primera no funciona, intentar con el nombre alternativo
if (textureId == 0) {
textureId = (GLuint)(uintptr_t)SDL_GetPointerProperty(props, "texture.opengl.texture", nullptr);
}
// Si aún no funciona, intentar obtener como número
if (textureId == 0) {
textureId = (GLuint)SDL_GetNumberProperty(props, "SDL.texture.opengl.texture", DEFAULT_TEXTURE_ID);
}
// Si ninguna funciona, usar el método manual de bindeo de textura
if (textureId == 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"No se pudo obtener el ID de textura OpenGL, usando ID por defecto (%d)",
DEFAULT_TEXTURE_ID);
textureId = DEFAULT_TEXTURE_ID;
}
return textureId;
}
bool init(SDL_Window *window, SDL_Texture *back_buffer_texture, const std::string &vertex_shader, const std::string &fragment_shader) {
shader::win = window;
shader::renderer = SDL_GetRenderer(window);
shader::backBuffer = back_buffer_texture;
if (!shader::renderer) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo obtener el renderer de la ventana.");
return false;
}
SDL_GetWindowSize(window, &win_size.x, &win_size.y);
SDL_GetTextureSize(back_buffer_texture, &tex_size.x, &tex_size.y);
const auto render_name = SDL_GetRendererName(renderer);
if (!render_name) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo obtener el nombre del renderer.");
return false;
}
// Verificar que el renderer sea OpenGL
if (!strncmp(render_name, "opengl", 6)) {
#ifndef __APPLE__
if (!initGLExtensions()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "ERROR: No se han podido inicializar las extensiones de OpenGL.");
usingOpenGL = false;
return false;
}
#endif
// Compilar el programa de shaders utilizando std::string
programId = compileProgram(vertex_shader, fragment_shader);
if (programId == INVALID_PROGRAM_ID) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "ERROR: No se pudo compilar el programa de shaders.");
usingOpenGL = false;
return false;
}
} else {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "ADVERTENCIA: El driver del renderer no es OpenGL (%s).", render_name);
usingOpenGL = false;
return false;
}
usingOpenGL = true;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "** Shader system initialized successfully");
return true;
}
void render() {
// Establece el color de fondo
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_SetRenderTarget(renderer, nullptr);
SDL_RenderClear(renderer);
if (usingOpenGL && programId != INVALID_PROGRAM_ID) {
// Guardar estados de OpenGL
GLint oldProgramId;
glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgramId);
GLint oldViewport[4];
glGetIntegerv(GL_VIEWPORT, oldViewport);
GLboolean wasTextureEnabled = glIsEnabled(GL_TEXTURE_2D);
GLint oldTextureId;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTextureId);
// Obtener y bindear la textura
GLuint textureId = getTextureID(backBuffer);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureId);
checkGLError("glBindTexture");
// Usar nuestro programa de shaders
glUseProgram(programId);
checkGLError("glUseProgram");
// Recupera el tamaño lógico configurado con SDL_RenderSetLogicalSize
int logicalW, logicalH;
SDL_RendererLogicalPresentation mode;
SDL_GetRenderLogicalPresentation(renderer, &logicalW, &logicalH, &mode);
if (logicalW == 0 || logicalH == 0) {
logicalW = win_size.x;
logicalH = win_size.y;
}
// Cálculo del viewport
int viewportX = 0, viewportY = 0, viewportW = win_size.x, viewportH = win_size.y;
const bool USE_INTEGER_SCALE = mode == SDL_LOGICAL_PRESENTATION_INTEGER_SCALE;
if (USE_INTEGER_SCALE) {
// Calcula el factor de escalado entero máximo que se puede aplicar
int scaleX = win_size.x / logicalW;
int scaleY = win_size.y / logicalH;
int scale = (scaleX < scaleY ? scaleX : scaleY);
if (scale < 1) {
scale = 1;
}
viewportW = logicalW * scale;
viewportH = logicalH * scale;
viewportX = (win_size.x - viewportW) / 2;
viewportY = (win_size.y - viewportH) / 2;
} else {
// Letterboxing: preserva la relación de aspecto usando una escala flotante
float windowAspect = static_cast<float>(win_size.x) / win_size.y;
float logicalAspect = static_cast<float>(logicalW) / logicalH;
if (windowAspect > logicalAspect) {
viewportW = static_cast<int>(logicalAspect * win_size.y);
viewportX = (win_size.x - viewportW) / 2;
} else {
viewportH = static_cast<int>(win_size.x / logicalAspect);
viewportY = (win_size.y - viewportH) / 2;
}
}
glViewport(viewportX, viewportY, viewportW, viewportH);
checkGLError("glViewport");
// Configurar la proyección ortográfica usando el espacio lógico
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Queremos que el origen esté en la esquina superior izquierda del espacio lógico.
glOrtho(0, static_cast<GLdouble>(logicalW), static_cast<GLdouble>(logicalH), 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Dibuja el quad con las coordenadas ajustadas.
// Se asignan las coordenadas de textura "normales" para que no quede espejado horizontalmente,
// y se mantiene el flip vertical para que la imagen no aparezca volteada.
glBegin(GL_TRIANGLE_STRIP);
// Vértice superior izquierdo
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 0.0f);
// Vértice superior derecho
glTexCoord2f(1.0f, 1.0f);
glVertex2f(static_cast<GLfloat>(logicalW), 0.0f);
// Vértice inferior izquierdo
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, static_cast<GLfloat>(logicalH));
// Vértice inferior derecho
glTexCoord2f(1.0f, 0.0f);
glVertex2f(static_cast<GLfloat>(logicalW), static_cast<GLfloat>(logicalH));
glEnd();
checkGLError("render quad");
SDL_GL_SwapWindow(win);
// Restaurar estados de OpenGL
glUseProgram(oldProgramId);
glBindTexture(GL_TEXTURE_2D, oldTextureId);
if (!wasTextureEnabled) {
glDisable(GL_TEXTURE_2D);
}
glViewport(oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
} else {
// Fallback a renderizado normal de SDL
SDL_RenderTexture(renderer, backBuffer, nullptr, nullptr);
SDL_RenderPresent(renderer);
}
}
void cleanup() {
if (programId != INVALID_PROGRAM_ID) {
glDeleteProgram(programId);
programId = INVALID_PROGRAM_ID;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Programa de shaders liberado.");
}
// Reinicializar variables
win = nullptr;
renderer = nullptr;
backBuffer = nullptr;
usingOpenGL = false;
}
bool isUsingOpenGL() {
return usingOpenGL;
}
GLuint getProgramId() {
return programId;
}
} // namespace shader

View File

@@ -1,9 +0,0 @@
#pragma once
#include <SDL3/SDL.h> // Para SDL_Texture, SDL_Window
#include <string> // Para basic_string, string
namespace shader {
bool init(SDL_Window *ventana, SDL_Texture *texturaBackBuffer, const std::string &vertexShader, const std::string &fragmentShader = "");
void render();
} // namespace shader

View File

@@ -37,19 +37,19 @@ class Fade {
~Fade();
// --- Métodos principales ---
void reset(); // Resetea variables para reutilizar el fade
void render(); // Dibuja la transición en pantalla
void update(); // Actualiza el estado interno (ya usa tiempo real)
void update(float delta_time); // Compatibilidad delta-time (ignora el parámetro)
void activate(); // Activa el fade
void reset(); // Resetea variables para reutilizar el fade
void render(); // Dibuja la transición en pantalla
void update(); // Actualiza el estado interno (ya usa tiempo real)
void update(float delta_time); // Compatibilidad delta-time (ignora el parámetro)
void activate(); // Activa el fade
// --- Configuración ---
void setColor(Uint8 r, Uint8 g, Uint8 b); // Establece el color RGB del fade
void setColor(Color color); // Establece el color del fade
void setType(Type type) { type_ = type; } // Establece el tipo de fade
void setMode(Mode mode) { mode_ = mode; } // Establece el modo de fade
void setPostDuration(int value) { post_duration_ = value; } // Duración posterior al fade en milisegundos
void setPreDuration(int value) { pre_duration_ = value; } // Duración previa al fade en milisegundos
void setColor(Uint8 r, Uint8 g, Uint8 b); // Establece el color RGB del fade
void setColor(Color color); // Establece el color del fade
void setType(Type type) { type_ = type; } // Establece el tipo de fade
void setMode(Mode mode) { mode_ = mode; } // Establece el modo de fade
void setPostDuration(int milliseconds) { post_duration_ = milliseconds; } // Duración posterior al fade en milisegundos
void setPreDuration(int milliseconds) { pre_duration_ = milliseconds; } // Duración previa al fade en milisegundos
// --- Getters ---
[[nodiscard]] auto getValue() const -> int { return value_; }

View File

@@ -9,7 +9,7 @@
class Texture; // lines 6-6
Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::shared_ptr<Texture> &texture, const std::vector<std::string> &animation)
Item::Item(ItemType type, float x, float y, SDL_FRect& play_area, const std::shared_ptr<Texture>& texture, const std::vector<std::string>& animation)
: sprite_(std::make_unique<AnimatedSprite>(texture, animation)),
play_area_(play_area),
type_(type) {
@@ -26,8 +26,6 @@ Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::sha
break;
}
default: {
width_ = param.game.item_size;
height_ = param.game.item_size;
pos_x_ = x;
pos_y_ = y;
// 6 velocidades: 3 negativas (-1.0, -0.66, -0.33) y 3 positivas (0.33, 0.66, 1.0)
@@ -35,13 +33,17 @@ Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::sha
if (direction < 3) {
// Velocidades negativas: -1.0, -0.66, -0.33
vel_x_ = -ITEM_VEL_X_BASE + (direction * ITEM_VEL_X_STEP);
rotate_speed_ = -720.0F;
} else {
// Velocidades positivas: 0.33, 0.66, 1.0
vel_x_ = ITEM_VEL_X_STEP + ((direction - 3) * ITEM_VEL_X_STEP);
rotate_speed_ = 720.0F;
}
vel_y_ = ITEM_VEL_Y;
accel_y_ = ITEM_ACCEL_Y;
collider_.r = width_ / 2;
sprite_->startRotate();
sprite_->setRotateAmount(rotate_speed_);
break;
}
}
@@ -103,7 +105,8 @@ void Item::move(float deltaTime) {
// Si toca el borde lateral
if (pos_x_ == MIN_X || pos_x_ == MAX_X) {
vel_x_ = -vel_x_; // Invierte la velocidad horizontal
vel_x_ = -vel_x_; // Invierte la velocidad horizontal
sprite_->scaleRotateAmount(-1.0F); // Invierte la rotación
}
// Si colisiona por arriba, rebota (excepto la máquina de café)
@@ -117,8 +120,9 @@ void Item::move(float deltaTime) {
// Si colisiona con la parte inferior
if (pos_y_ > play_area_.h - height_) {
// Corrige la posición
pos_y_ = play_area_.h - height_;
pos_y_ = play_area_.h - height_; // Corrige la posición
sprite_->scaleRotateAmount(0.5F); // Reduce la rotación
sprite_->stopRotate(300.0F); // Detiene la rotacion
switch (type_) {
case ItemType::COFFEE_MACHINE:

View File

@@ -27,8 +27,10 @@ enum class ItemType : int {
class Item {
public:
// --- Constantes ---
static constexpr int COFFEE_MACHINE_WIDTH = 30; // Anchura de la máquina de café
static constexpr int COFFEE_MACHINE_HEIGHT = 39; // Altura de la máquina de café
static constexpr float WIDTH = 20.0F; // Anchura del item
static constexpr float HEIGHT = 20.0F; // ALtura del item
static constexpr int COFFEE_MACHINE_WIDTH = 30; // Anchura de la máquina de café
static constexpr int COFFEE_MACHINE_HEIGHT = 39; // Altura de la máquina de café
static constexpr float LIFETIME_DURATION_S = 10.0f; // Duración de vida del ítem en segundos
// Velocidades base (pixels/segundo) - Coffee Machine
@@ -37,10 +39,10 @@ class Item {
static constexpr float COFFEE_MACHINE_ACCEL_Y = 360.0F; // Aceleración Y de máquina de café (0.1*60²fps = 360 pixels/segundo²)
// Velocidades base (pixels/segundo) - Items normales
static constexpr float ITEM_VEL_X_BASE = 60.0F; // Velocidad X base para items (1.0F*60fps)
static constexpr float ITEM_VEL_X_STEP = 20.0F; // Incremento de velocidad X (0.33F*60fps)
static constexpr float ITEM_VEL_Y = -240.0F; // Velocidad Y inicial de items (-4.0F*60fps)
static constexpr float ITEM_ACCEL_Y = 720.0F; // Aceleración Y de items (0.2*60²fps = 720 pixels/segundo²)
static constexpr float ITEM_VEL_X_BASE = 60.0F; // Velocidad X base para items (1.0F*60fps)
static constexpr float ITEM_VEL_X_STEP = 20.0F; // Incremento de velocidad X (0.33F*60fps)
static constexpr float ITEM_VEL_Y = -240.0F; // Velocidad Y inicial de items (-4.0F*60fps)
static constexpr float ITEM_ACCEL_Y = 720.0F; // Aceleración Y de items (0.2*60²fps = 720 pixels/segundo²)
// Constantes de física de rebote
static constexpr float BOUNCE_VEL_THRESHOLD = 60.0F; // Umbral de velocidad para parar (1.0F*60fps)
@@ -49,14 +51,14 @@ class Item {
static constexpr float HORIZONTAL_DAMPING = 0.75F; // Factor de amortiguación horizontal
// --- Constructor y destructor ---
Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::shared_ptr<Texture> &texture, const std::vector<std::string> &animation); // Constructor principal
Item(ItemType type, float x, float y, SDL_FRect& play_area, const std::shared_ptr<Texture>& texture, const std::vector<std::string>& animation); // Constructor principal
~Item() = default; // Destructor
// --- Métodos principales ---
void alignTo(int x); // Centra el objeto en la posición X indicada
void render(); // Renderiza el objeto en pantalla
void disable(); // Desactiva el objeto
void update(float deltaTime); // Actualiza la posición, animación y contadores (time-based)
void alignTo(int x); // Centra el objeto en la posición X indicada
void render(); // Renderiza el objeto en pantalla
void disable(); // Desactiva el objeto
void update(float deltaTime); // Actualiza la posición, animación y contadores (time-based)
// --- Getters ---
[[nodiscard]] auto getPosX() const -> float { return pos_x_; } // Obtiene la posición X
@@ -66,7 +68,7 @@ class Item {
[[nodiscard]] auto getType() const -> ItemType { return type_; } // Obtiene el tipo
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; } // Verifica si está habilitado
[[nodiscard]] auto isOnFloor() const -> bool { return floor_collision_; } // Verifica si está en el suelo
auto getCollider() -> Circle & { return collider_; } // Obtiene el colisionador
auto getCollider() -> Circle& { return collider_; } // Obtiene el colisionador
private:
// --- Objetos y punteros ---
@@ -76,15 +78,16 @@ class Item {
SDL_FRect play_area_; // Rectángulo con la zona de juego
Circle collider_; // Círculo de colisión del objeto
ItemType type_; // Tipo de objeto
float pos_x_; // Posición X del objeto
float pos_y_; // Posición Y del objeto
float vel_x_; // Velocidad en el eje X
float vel_y_; // Velocidad en el eje Y
float pos_x_ = 0.0F; // Posición X del objeto
float pos_y_ = 0.0F; // Posición Y del objeto
float vel_x_ = 0.0F; // Velocidad en el eje X
float vel_y_ = 0.0F; // Velocidad en el eje Y
float accel_x_ = 0.0F; // Aceleración en el eje X
float accel_y_; // Aceleración en el eje Y
int width_; // Ancho del objeto
int height_; // Alto del objeto
float lifetime_timer_ = 0.0f; // Acumulador de tiempo de vida del ítem (segundos)
float accel_y_ = 0.0F; // Aceleración en el eje Y
float width_ = WIDTH; // Ancho del objeto
float height_ = HEIGHT; // Alto del objeto
float rotate_speed_ = 0.0F; // Velocidad de rotacion
float lifetime_timer_ = 0.0f; // Acumulador de tiempo de vida del ítem (segundos)
bool floor_collision_ = false; // Indica si el objeto colisiona con el suelo
bool enabled_ = true; // Indica si el objeto está habilitado
@@ -92,6 +95,6 @@ class Item {
void shiftColliders(); // Alinea el círculo de colisión con la posición del objeto
void shiftSprite(); // Coloca el sprite en la posición del objeto
void move(float deltaTime); // Actualiza la posición y estados del objeto (time-based)
void updateTimeToLive(float deltaTime); // Actualiza el contador de tiempo de vida (time-based)
void updateTimeToLive(float deltaTime); // Actualiza el contador de tiempo de vida (time-based)
static auto getCoffeeMachineSpawn(int player_x, int item_width, int area_width, int margin = 2) -> int; // Calcula la zona de aparición de la máquina de café
};

View File

@@ -24,6 +24,32 @@ void ManageHiScoreTable::clear() {
table_.emplace_back("PACMQ", 200);
table_.emplace_back("PELEC", 100);
/*
table_.emplace_back("BRY", 1000);
table_.emplace_back("USUFO", 500);
table_.emplace_back("GLUCA", 100);
table_.emplace_back("PARRA", 50);
table_.emplace_back("CAGAM", 10);
table_.emplace_back("PEPE", 5);
table_.emplace_back("ROSIT", 4);
table_.emplace_back("SAM", 3);
table_.emplace_back("PACMQ", 2);
table_.emplace_back("PELEC", 1);
*/
/*
table_.emplace_back("BRY", 5000000);
table_.emplace_back("USUFO", 5000000);
table_.emplace_back("GLUCA", 5000000);
table_.emplace_back("PARRA", 5000000);
table_.emplace_back("CAGAM", 5000000);
table_.emplace_back("PEPE", 5000000);
table_.emplace_back("ROSIT", 5000000);
table_.emplace_back("SAM", 5000000);
table_.emplace_back("PACMQ", 5000000);
table_.emplace_back("PELEC", 5000000);
*/
sort();
}

View File

@@ -1,5 +1,6 @@
#include "moving_sprite.h"
#include <cmath> // Para std::abs
#include <utility>
#include "texture.h" // Para Texture
@@ -90,6 +91,21 @@ void MovingSprite::setRotate(bool enable) {
rotate_.enabled = enable;
}
// Habilita la rotación y establece el centro en el centro del sprite
void MovingSprite::startRotate() {
rotate_.enabled = true;
rotate_.center.x = pos_.w / 2.0F;
rotate_.center.y = pos_.h / 2.0F;
}
// Detiene la rotación y resetea el ángulo a cero
void MovingSprite::stopRotate(float threshold) {
if (threshold == 0.0F || std::abs(rotate_.amount) <= threshold) {
rotate_.enabled = false;
rotate_.angle = 0.0;
}
}
// Establece la posición y_ el tamaño del objeto
void MovingSprite::setPos(SDL_FRect rect) {
x_ = rect.x;

View File

@@ -15,7 +15,6 @@ class MovingSprite : public Sprite {
// --- Estructuras ---
struct Rotate {
bool enabled{false}; // Indica si ha de rotar
int speed{1}; // Velocidad de giro
double angle{0.0}; // Ángulo para dibujarlo
float amount{0.0F}; // Cantidad de grados a girar en cada iteración
SDL_FPoint center{.x = 0.0F, .y = 0.0F}; // Centro de rotación
@@ -28,10 +27,10 @@ class MovingSprite : public Sprite {
~MovingSprite() override = default;
// --- Métodos principales ---
virtual void update(float deltaTime); // Actualiza las variables internas del objeto (time-based)
void clear() override; // Reinicia todas las variables a cero
void stop(); // Elimina el movimiento del sprite
void render() override; // Muestra el sprite por pantalla
virtual void update(float deltaTime); // Actualiza las variables internas del objeto (time-based)
void clear() override; // Reinicia todas las variables a cero
void stop(); // Elimina el movimiento del sprite
void render() override; // Muestra el sprite por pantalla
// --- Configuración ---
void setPos(SDL_FRect rect); // Establece la posición y el tamaño del objeto
@@ -47,8 +46,10 @@ class MovingSprite : public Sprite {
void setAngle(double value) { rotate_.angle = value; } // Establece el ángulo
void setRotatingCenter(SDL_FPoint point) { rotate_.center = point; } // Establece el centro de rotación
void setRotate(bool enable); // Activa o desactiva el efecto de rotación
void setRotateSpeed(int value) { rotate_.speed = std::max(1, value); } // Establece la velocidad de rotación
void setRotateAmount(double value) { rotate_.amount = value; } // Establece la cantidad de rotación
void startRotate(); // Habilita la rotación con centro automático
void stopRotate(float threshold = 0.0F); // Detiene la rotación y resetea ángulo
void setRotateAmount(double value) { rotate_.amount = value; } // Establece la velocidad de rotación
void scaleRotateAmount(float value) { rotate_.amount *= value; } // Modifica la velocidad de rotacion
void switchRotate() { rotate_.amount *= -1; } // Cambia el sentido de la rotación
void setFlip(SDL_FlipMode flip) { flip_ = flip; } // Establece el flip
void flip() { flip_ = (flip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL; } // Cambia el flip

View File

@@ -6,6 +6,7 @@
#include <cstddef> // Para size_t
#include <fstream> // Para basic_ostream, operator<<, basic_ostream::operator<<, basic_ofstream, basic_istream, basic_ifstream, ifstream, ofstream
#include <functional> // Para function
#include <sstream> // Para istringstream
#include <map> // Para map, operator==, _Rb_tree_const_iterator
#include <ranges> // Para std::ranges::any_of
#include <stdexcept> // Para invalid_argument, out_of_range
@@ -64,11 +65,27 @@ auto loadFromFile() -> bool {
// --- CASO: EL FICHERO EXISTE ---
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nReading file: %s", getFileName(settings.config_file).c_str());
std::string line;
std::string param_name;
std::string param_value;
while (std::getline(file, line)) {
if (line.substr(0, 1) != "#") {
int pos = line.find('=');
if (!set(line.substr(0, pos), line.substr(pos + 1, line.length()))) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Unknown parameter: %s", line.substr(0, pos).c_str());
// Elimina comentarios
auto comment_pos = line.find('#');
if (comment_pos != std::string::npos) {
line.resize(comment_pos);
}
// Si la línea contiene '=', lo reemplazamos por un espacio para compatibilidad
auto equals_pos = line.find('=');
if (equals_pos != std::string::npos) {
line[equals_pos] = ' ';
}
// Usa un stream para separar palabras (elimina automáticamente espacios extra)
std::istringstream iss(line);
if (iss >> param_name >> param_value) {
if (!set(param_name, param_value)) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Unknown parameter: %s", param_name.c_str());
}
}
}
@@ -100,49 +117,51 @@ auto saveToFile() -> bool {
applyPendingChanges();
// Versión del archivo
file << "# Coffee Crisis Arcade Edition - Configuration File\n";
file << "# Format: key value\n";
file << "config.version " << settings.config_version << "\n";
// Opciones de ventana
file << "## WINDOW\n";
file << "window.zoom=" << window.zoom << "\n";
file << "\n# WINDOW\n";
file << "window.zoom " << window.zoom << "\n";
// Opciones de video
file << "\n## VIDEO\n";
file << "## video.scale_mode [" << static_cast<int>(SDL_ScaleMode::SDL_SCALEMODE_NEAREST) << ": nearest, " << static_cast<int>(SDL_ScaleMode::SDL_SCALEMODE_LINEAR) << ": lineal]\n";
file << "video.fullscreen=" << boolToString(video.fullscreen) << "\n";
file << "video.scale_mode=" << static_cast<int>(video.scale_mode) << "\n";
file << "video.vsync=" << boolToString(video.vsync) << "\n";
file << "video.integer_scale=" << boolToString(video.integer_scale) << "\n";
file << "video.shaders=" << boolToString(video.shaders) << "\n";
file << "\n# VIDEO\n";
file << "# video.scale_mode [" << static_cast<int>(SDL_ScaleMode::SDL_SCALEMODE_NEAREST) << ": nearest, " << static_cast<int>(SDL_ScaleMode::SDL_SCALEMODE_LINEAR) << ": linear]\n";
file << "video.fullscreen " << boolToString(video.fullscreen) << "\n";
file << "video.scale_mode " << static_cast<int>(video.scale_mode) << "\n";
file << "video.vsync " << boolToString(video.vsync) << "\n";
file << "video.integer_scale " << boolToString(video.integer_scale) << "\n";
file << "video.shaders " << boolToString(video.shaders) << "\n";
// Opciones de audio
file << "\n## AUDIO\n";
file << "## volume [0 .. 100]\n";
file << "audio.enabled=" << boolToString(audio.enabled) << "\n";
file << "audio.volume=" << audio.volume << "\n";
file << "audio.music.enabled=" << boolToString(audio.music.enabled) << "\n";
file << "audio.music.volume=" << audio.music.volume << "\n";
file << "audio.sound.enabled=" << boolToString(audio.sound.enabled) << "\n";
file << "audio.sound.volume=" << audio.sound.volume << "\n";
file << "\n# AUDIO\n";
file << "# volume range: [0 .. 100]\n";
file << "audio.enabled " << boolToString(audio.enabled) << "\n";
file << "audio.volume " << audio.volume << "\n";
file << "audio.music.enabled " << boolToString(audio.music.enabled) << "\n";
file << "audio.music.volume " << audio.music.volume << "\n";
file << "audio.sound.enabled " << boolToString(audio.sound.enabled) << "\n";
file << "audio.sound.volume " << audio.sound.volume << "\n";
// Opciones del juego
file << "\n## GAME\n";
file << "## game.language [0: spanish, 1: valencian, 2: english]\n";
file << "## game.difficulty [" << static_cast<int>(Difficulty::Code::EASY) << ": easy, " << static_cast<int>(Difficulty::Code::NORMAL) << ": normal, " << static_cast<int>(Difficulty::Code::HARD) << ": hard]\n";
file << "game.language=" << static_cast<int>(settings.language) << "\n";
file << "game.difficulty=" << static_cast<int>(settings.difficulty) << "\n";
file << "game.autofire=" << boolToString(settings.autofire) << "\n";
file << "game.shutdown_enabled=" << boolToString(settings.shutdown_enabled) << "\n";
file << "game.params_file=" << settings.params_file << "\n";
file << "\n# GAME\n";
file << "# game.language [0: spanish, 1: valencian, 2: english]\n";
file << "# game.difficulty [" << static_cast<int>(Difficulty::Code::EASY) << ": easy, " << static_cast<int>(Difficulty::Code::NORMAL) << ": normal, " << static_cast<int>(Difficulty::Code::HARD) << ": hard]\n";
file << "game.language " << static_cast<int>(settings.language) << "\n";
file << "game.difficulty " << static_cast<int>(settings.difficulty) << "\n";
file << "game.autofire " << boolToString(settings.autofire) << "\n";
file << "game.shutdown_enabled " << boolToString(settings.shutdown_enabled) << "\n";
file << "game.params_file " << settings.params_file << "\n";
// Opciones de mandos
file << "\n## CONTROLLERS\n";
file << "\n# CONTROLLERS\n";
gamepad_manager.saveToFile(file);
// Opciones de teclado
file << "\n## KEYBOARD\n";
file << "keyboard.player=" << static_cast<int>(keyboard.player_id) << "\n";
file << "\n# KEYBOARD\n";
file << "keyboard.player " << static_cast<int>(keyboard.player_id) << "\n";
// Cierra el fichero
file.close();
@@ -177,6 +196,8 @@ auto set(const std::string& var, const std::string& value) -> bool {
// Un mapa estático asegura que se inicializa solo una vez
static const std::map<std::string, std::function<void(const std::string&)>> SETTINGS_MAP = {
// Configuración
{"config.version", [](const auto& val) { settings.config_version = std::stoi(val); }},
// Ventana
{"window.zoom", [](const auto& val) { window.zoom = std::stoi(val); }},
// Vídeo

View File

@@ -58,6 +58,7 @@ struct Audio {
};
struct Settings {
int config_version = 2; // Versión del archivo de configuración
Difficulty::Code difficulty = Difficulty::Code::NORMAL; // Dificultad del juego
Lang::Code language = Lang::Code::VALENCIAN; // Idioma usado en el juego
bool autofire = GameDefaults::Options::SETTINGS_AUTOFIRE; // Indicador de autofire
@@ -158,12 +159,12 @@ class GamepadManager {
const auto& gamepad = gamepads_[i];
// Guardar el nombre solo si hay path (mando real asignado)
if (!gamepad.path.empty()) {
file << "controller." << i << ".name=" << gamepad.name << "\n";
file << "controller." << i << ".name " << gamepad.name << "\n";
} else {
file << "controller." << i << ".name=\n"; // vacío
file << "controller." << i << ".name \n"; // vacío
}
file << "controller." << i << ".path=" << gamepad.path << "\n";
file << "controller." << i << ".player=" << static_cast<int>(gamepad.player_id) << "\n";
file << "controller." << i << ".path " << gamepad.path << "\n";
file << "controller." << i << ".player " << static_cast<int>(gamepad.player_id) << "\n";
}
}

View File

@@ -87,7 +87,6 @@ auto setParams(const std::string& var, const std::string& value) -> bool {
static const std::unordered_map<std::string, std::function<void(const std::string&)>> INT_PARAMS = {
{"game.width", [](const std::string& v) { param.game.width = std::stoi(v); }},
{"game.height", [](const std::string& v) { param.game.height = std::stoi(v); }},
{"game.item_size", [](const std::string& v) { param.game.item_size = std::stoi(v); }},
{"game.play_area.rect.x", [](const std::string& v) { param.game.play_area.rect.x = std::stoi(v); }},
{"game.play_area.rect.y", [](const std::string& v) { param.game.play_area.rect.y = std::stoi(v); }},
{"game.play_area.rect.w", [](const std::string& v) { param.game.play_area.rect.w = std::stoi(v); }},

View File

@@ -14,7 +14,6 @@
struct ParamGame {
float width = GameDefaults::Game::WIDTH;
float height = GameDefaults::Game::HEIGHT;
float item_size = GameDefaults::Game::ITEM_SIZE;
Zone play_area{}; // Se inicializa en el constructor de Param
Zone game_area{}; // Se inicializa en el constructor de Param
int name_entry_idle_time = GameDefaults::Game::NAME_ENTRY_IDLE_TIME;

View File

@@ -22,7 +22,7 @@
#endif
// Constructor
Player::Player(const Config &config)
Player::Player(const Config& config)
: player_sprite_(std::make_unique<AnimatedSprite>(config.texture.at(0), config.animations.at(0))),
power_sprite_(std::make_unique<AnimatedSprite>(config.texture.at(4), config.animations.at(1))),
enter_name_(std::make_unique<EnterName>()),
@@ -128,16 +128,16 @@ void Player::setInputPlaying(Input::Action action) {
// Procesa inputs para cuando está introduciendo el nombre
void Player::setInputEnteringName(Input::Action action) {
switch (action) {
case Input::Action::LEFT:
enter_name_->decPosition();
case Input::Action::FIRE_LEFT:
enter_name_->addCharacter();
break;
case Input::Action::FIRE_CENTER:
enter_name_->removeLastCharacter();
break;
case Input::Action::RIGHT:
enter_name_->incPosition();
break;
case Input::Action::UP:
enter_name_->incIndex();
break;
case Input::Action::DOWN:
case Input::Action::LEFT:
enter_name_->decIndex();
break;
case Input::Action::START:
@@ -149,20 +149,20 @@ void Player::setInputEnteringName(Input::Action action) {
name_entry_idle_time_accumulator_ = 0.0f;
}
// Fase 1: Sistema de movimiento time-based
// Sistema de movimiento
void Player::move(float deltaTime) {
switch (playing_state_) {
case State::PLAYING:
handlePlayingMovement(deltaTime);
break;
case State::ROLLING:
handleRollingMovement();
handleRollingMovement();
break;
case State::TITLE_ANIMATION:
handleTitleAnimation(deltaTime);
break;
case State::CONTINUE_TIME_OUT:
handleContinueTimeOut();
handleContinueTimeOut();
break;
case State::LEAVING_SCREEN:
updateStepCounter(deltaTime);
@@ -173,13 +173,13 @@ void Player::move(float deltaTime) {
handleEnteringScreen(deltaTime);
break;
case State::CREDITS:
handleCreditsMovement(deltaTime);
handleCreditsMovement(deltaTime);
break;
case State::WAITING:
handleWaitingMovement(deltaTime);
handleWaitingMovement(deltaTime);
break;
case State::RECOVER:
handleRecoverMovement();
handleRecoverMovement();
break;
default:
break;
@@ -356,7 +356,7 @@ void Player::handleCreditsLeftMovement() {
// Controla la animación del jugador saludando (time-based)
void Player::handleWaitingMovement(float deltaTime) {
waiting_time_accumulator_ += deltaTime;
const float WAITING_DURATION_S = static_cast<float>(WAITING_COUNTER) / 60.0f; // Convert frames to seconds
const float WAITING_DURATION_S = static_cast<float>(WAITING_COUNTER) / 60.0f; // Convert frames to seconds
if (waiting_time_accumulator_ >= WAITING_DURATION_S) {
waiting_time_accumulator_ = 0.0f;
player_sprite_->resetAnimation();
@@ -387,7 +387,7 @@ void Player::setInputBasedOnPlayerId() {
// Incrementa o ajusta el contador de pasos (time-based)
void Player::updateStepCounter(float deltaTime) {
step_time_accumulator_ += deltaTime;
const float STEP_INTERVAL_S = 10.0f / 60.0f; // 10 frames converted to seconds
const float STEP_INTERVAL_S = 10.0f / 60.0f; // 10 frames converted to seconds
if (step_time_accumulator_ >= STEP_INTERVAL_S) {
step_time_accumulator_ = 0.0f;
playSound("walk.wav");
@@ -396,24 +396,8 @@ void Player::updateStepCounter(float deltaTime) {
// Pinta el jugador en pantalla
void Player::render() {
if (power_up_ && isPlaying()) {
// Convertir lógica de parpadeo a deltaTime en segundos
const float TOTAL_POWERUP_TIME_S = static_cast<float>(POWERUP_COUNTER) / 60.0f; // Total time in seconds
const float QUARTER_TIME_S = TOTAL_POWERUP_TIME_S / 4.0f; // 25% del tiempo total
if (power_up_time_accumulator_ > QUARTER_TIME_S) {
// En los primeros 75% del tiempo, siempre visible
power_sprite_->render();
} else {
// En el último 25%, parpadea cada 20 frames (≈0.333s)
constexpr float BLINK_PERIOD_S = 20.0f / 60.0f; // 20 frames in seconds
constexpr float VISIBLE_PROPORTION = 4.0f / 20.0f; // 4 frames visible de 20 total
float cycle_position = fmod(power_up_time_accumulator_, BLINK_PERIOD_S) / BLINK_PERIOD_S;
if (cycle_position >= VISIBLE_PROPORTION) {
power_sprite_->render();
}
}
if (power_sprite_visible_ && isPlaying()) {
power_sprite_->render();
}
if (isRenderable()) {
@@ -475,11 +459,10 @@ auto Player::computeAnimation() const -> std::pair<std::string, SDL_FlipMode> {
return {anim_name, flip_mode};
}
// Fase 1: Establece la animación correspondiente al estado (time-based)
// Establece la animación correspondiente al estado
void Player::setAnimation(float deltaTime) {
switch (playing_state_) {
case State::PLAYING:
case State::ENTERING_NAME_GAME_COMPLETED:
case State::ENTERING_SCREEN:
case State::LEAVING_SCREEN:
case State::TITLE_ANIMATION:
@@ -505,6 +488,7 @@ void Player::setAnimation(float deltaTime) {
case State::CONTINUE:
player_sprite_->setCurrentAnimation("dizzy");
break;
case State::ENTERING_NAME_GAME_COMPLETED:
case State::CELEBRATING:
player_sprite_->setCurrentAnimation("celebration");
break;
@@ -519,16 +503,16 @@ void Player::setAnimation(float deltaTime) {
// Actualiza al jugador con deltaTime (time-based)
void Player::update(float deltaTime) {
move(deltaTime); // Sistema de movimiento time-based
setAnimation(deltaTime); // Animaciones time-based
shiftColliders(); // Sin cambios (posicional)
updateFireSystem(deltaTime); // Sistema de disparo de dos líneas
updatePowerUp(deltaTime); // Sistema de power-up time-based
updateInvulnerable(deltaTime); // Sistema de invulnerabilidad time-based
updateScoreboard(); // Sin cambios (no temporal)
updateContinueCounter(deltaTime); // Sistema de continue time-based
updateEnterNameCounter(deltaTime); // Sistema de name entry time-based
updateShowingName(deltaTime); // Sistema de showing name time-based
move(deltaTime); // Sistema de movimiento time-based
setAnimation(deltaTime); // Animaciones time-based
shiftColliders(); // Sin cambios (posicional)
updateFireSystem(deltaTime); // Sistema de disparo de dos líneas
updatePowerUp(deltaTime); // Sistema de power-up time-based
updateInvulnerable(deltaTime); // Sistema de invulnerabilidad time-based
updateScoreboard(); // Sin cambios (no temporal)
updateContinueCounter(deltaTime); // Sistema de continue time-based
updateEnterNameCounter(deltaTime); // Sistema de name entry time-based
updateShowingName(deltaTime); // Sistema de showing name time-based
}
void Player::passShowingName() {
@@ -556,8 +540,9 @@ void Player::updateScoreboard() {
}
case State::ENTERING_NAME:
case State::ENTERING_NAME_GAME_COMPLETED: {
Scoreboard::get()->setRecordName(scoreboard_panel_, enter_name_->getCurrentName());
Scoreboard::get()->setSelectorPos(scoreboard_panel_, getRecordNamePos());
Scoreboard::get()->setEnterName(scoreboard_panel_, enter_name_->getCurrentName());
Scoreboard::get()->setCharacterSelected(scoreboard_panel_, enter_name_->getSelectedCharacter());
Scoreboard::get()->setCarouselAnimation(scoreboard_panel_, enter_name_->getSelectedIndex(), enter_name_.get());
break;
}
default:
@@ -599,7 +584,7 @@ void Player::setPlayingState(State state) {
case State::CONTINUE: {
// Inicializa el contador de continuar
continue_counter_ = 9;
continue_time_accumulator_ = 0.0f; // Initialize time accumulator
continue_time_accumulator_ = 0.0f; // Initialize time accumulator
playSound("continue_clock.wav");
setScoreboardMode(Scoreboard::Mode::CONTINUE);
break;
@@ -618,7 +603,7 @@ void Player::setPlayingState(State state) {
}
pos_y_ = default_pos_y_;
waiting_counter_ = 0;
waiting_time_accumulator_ = 0.0f; // Initialize time accumulator
waiting_time_accumulator_ = 0.0f; // Initialize time accumulator
shiftSprite();
player_sprite_->setCurrentAnimation("hello");
player_sprite_->animtionPause();
@@ -626,23 +611,23 @@ void Player::setPlayingState(State state) {
break;
}
case State::ENTERING_NAME: {
setScoreboardMode(Scoreboard::Mode::ENTER_NAME);
setScoreboardMode(Scoreboard::Mode::SCORE_TO_ENTER_NAME); // Iniciar animación de transición
break;
}
case State::SHOWING_NAME: {
showing_name_time_accumulator_ = 0.0f; // Inicializar acumulador time-based
setScoreboardMode(Scoreboard::Mode::SHOW_NAME);
Scoreboard::get()->setRecordName(scoreboard_panel_, last_enter_name_);
setScoreboardMode(Scoreboard::Mode::ENTER_TO_SHOW_NAME); // Iniciar animación de transición
Scoreboard::get()->setEnterName(scoreboard_panel_, last_enter_name_);
addScoreToScoreBoard();
break;
}
case State::ROLLING: {
// Activa la animación de rodar dando botes
player_sprite_->setCurrentAnimation("rolling");
player_sprite_->setAnimationSpeed(4.0f / 60.0f); // 4 frames convertido a segundos
player_sprite_->setVelY(-396.0F); // Velocidad inicial (6.6 * 60 = 396 pixels/s)
player_sprite_->setAccelY(720.0F); // Gravedad (0.2 * 60² = 720 pixels/s²)
player_sprite_->setPosY(pos_y_ - 2); // Para "sacarlo" del suelo, ya que está hundido un pixel para ocultar el outline de los pies
player_sprite_->setAnimationSpeed(4.0f / 60.0f); // 4 frames convertido a segundos
player_sprite_->setVelY(-396.0F); // Velocidad inicial (6.6 * 60 = 396 pixels/s)
player_sprite_->setAccelY(720.0F); // Gravedad (0.2 * 60² = 720 pixels/s²)
player_sprite_->setPosY(pos_y_ - 2); // Para "sacarlo" del suelo, ya que está hundido un pixel para ocultar el outline de los pies
(rand() % 2 == 0) ? player_sprite_->setVelX(198.0F) : player_sprite_->setVelX(-198.0F); // 3.3 * 60 = 198 pixels/s
break;
}
@@ -659,8 +644,8 @@ void Player::setPlayingState(State state) {
}
case State::CONTINUE_TIME_OUT: {
// Activa la animación de sacar al jugador de la zona de juego
player_sprite_->setAccelY(720.0F); // 0.2 * 60² = 720 pixels/s²
player_sprite_->setVelY(-240.0F); // -4.0 * 60 = -240 pixels/s
player_sprite_->setAccelY(720.0F); // 0.2 * 60² = 720 pixels/s²
player_sprite_->setVelY(-240.0F); // -4.0 * 60 = -240 pixels/s
player_sprite_->setVelX(0.0F);
player_sprite_->setCurrentAnimation("rolling");
player_sprite_->setAnimationSpeed(5.0f / 60.0f); // 5 frames convertido a segundos
@@ -679,21 +664,21 @@ void Player::setPlayingState(State state) {
break;
}
case State::ENTERING_NAME_GAME_COMPLETED: {
setWalkingState(State::WALKING_STOP);
setFiringState(State::FIRING_NONE);
setScoreboardMode(Scoreboard::Mode::ENTER_NAME);
// setWalkingState(State::WALKING_STOP);
// setFiringState(State::FIRING_NONE);
setScoreboardMode(Scoreboard::Mode::SCORE_TO_ENTER_NAME); // Iniciar animación de transición
break;
}
case State::LEAVING_SCREEN: {
step_counter_ = 0;
step_time_accumulator_ = 0.0f; // Initialize time accumulator
step_time_accumulator_ = 0.0f; // Initialize time accumulator
setScoreboardMode(Scoreboard::Mode::GAME_COMPLETED);
break;
}
case State::ENTERING_SCREEN: {
init();
step_counter_ = 0;
step_time_accumulator_ = 0.0f; // Initialize time accumulator
step_time_accumulator_ = 0.0f; // Initialize time accumulator
setScoreboardMode(Scoreboard::Mode::SCORE);
switch (id_) {
case Id::PLAYER1:
@@ -734,7 +719,7 @@ void Player::decScoreMultiplier() {
void Player::setInvulnerable(bool value) {
invulnerable_ = value;
invulnerable_counter_ = invulnerable_ ? INVULNERABLE_COUNTER : 0;
invulnerable_time_accumulator_ = invulnerable_ ? static_cast<float>(INVULNERABLE_COUNTER) / 60.0f : 0.0f; // Convert frames to seconds
invulnerable_time_accumulator_ = invulnerable_ ? static_cast<float>(INVULNERABLE_COUNTER) / 60.0f : 0.0f; // Convert frames to seconds
}
// Monitoriza el estado (time-based)
@@ -744,12 +729,12 @@ void Player::updateInvulnerable(float deltaTime) {
invulnerable_time_accumulator_ -= deltaTime;
// Frecuencia fija de parpadeo adaptada a deltaTime (en segundos)
constexpr float BLINK_PERIOD_S = 8.0f / 60.0f; // 8 frames convertidos a segundos
constexpr float BLINK_PERIOD_S = 8.0f / 60.0f; // 8 frames convertidos a segundos
// Calcula proporción decreciente basada en tiempo restante
const float TOTAL_INVULNERABLE_TIME_S = static_cast<float>(INVULNERABLE_COUNTER) / 60.0f;
float progress = 1.0f - (invulnerable_time_accumulator_ / TOTAL_INVULNERABLE_TIME_S);
float white_proportion = 0.5f - progress * 0.2f; // Menos blanco hacia el final
float white_proportion = 0.5f - progress * 0.2f; // Menos blanco hacia el final
// Calcula si debe mostrar textura de invulnerabilidad basado en el ciclo temporal
float cycle_position = fmod(invulnerable_time_accumulator_, BLINK_PERIOD_S) / BLINK_PERIOD_S;
@@ -773,7 +758,10 @@ void Player::updateInvulnerable(float deltaTime) {
void Player::setPowerUp() {
power_up_ = true;
power_up_counter_ = POWERUP_COUNTER;
power_up_time_accumulator_ = static_cast<float>(POWERUP_COUNTER) / 60.0f; // Convert frames to seconds
power_up_time_accumulator_ = static_cast<float>(POWERUP_COUNTER) / 60.0f; // Convert frames to seconds
power_sprite_visible_ = true; // Inicialmente visible cuando se activa el power-up
in_power_up_ending_phase_ = false; // Empezar en fase normal
bullet_color_toggle_ = false; // Resetear toggle
}
// Actualiza el valor de la variable (time-based)
@@ -784,7 +772,33 @@ void Player::updatePowerUp(float deltaTime) {
power_up_ = power_up_time_accumulator_ > 0;
if (!power_up_) {
power_up_time_accumulator_ = 0;
power_sprite_visible_ = false;
in_power_up_ending_phase_ = false;
bullet_color_toggle_ = false;
// Los colores ahora se manejan dinámicamente en getNextBulletColor()
} else {
// Calcular visibilidad del power sprite
const float TOTAL_POWERUP_TIME_S = static_cast<float>(POWERUP_COUNTER) / 60.0f;
const float QUARTER_TIME_S = TOTAL_POWERUP_TIME_S / 4.0f;
if (power_up_time_accumulator_ > QUARTER_TIME_S) {
// En los primeros 75% del tiempo, siempre visible
power_sprite_visible_ = true;
in_power_up_ending_phase_ = false;
} else {
// En el último 25%, parpadea cada 20 frames (≈0.333s)
constexpr float BLINK_PERIOD_S = 20.0f / 60.0f;
constexpr float VISIBLE_PROPORTION = 4.0f / 20.0f;
float cycle_position = fmod(power_up_time_accumulator_, BLINK_PERIOD_S) / BLINK_PERIOD_S;
power_sprite_visible_ = cycle_position >= VISIBLE_PROPORTION;
in_power_up_ending_phase_ = true; // Activar modo alternancia de colores de balas
}
}
} else {
power_sprite_visible_ = false;
in_power_up_ending_phase_ = false;
bullet_color_toggle_ = false;
}
}
}
@@ -816,7 +830,7 @@ void Player::shiftColliders() {
}
// Pone las texturas del jugador
void Player::setPlayerTextures(const std::vector<std::shared_ptr<Texture>> &texture) {
void Player::setPlayerTextures(const std::vector<std::shared_ptr<Texture>>& texture) {
player_sprite_->setTexture(texture[0]);
power_sprite_->setTexture(texture[1]);
}
@@ -825,7 +839,7 @@ void Player::setPlayerTextures(const std::vector<std::shared_ptr<Texture>> &text
void Player::updateContinueCounter(float deltaTime) {
if (playing_state_ == State::CONTINUE) {
continue_time_accumulator_ += deltaTime;
constexpr float CONTINUE_INTERVAL_S = 1.0f; // 1 segundo
constexpr float CONTINUE_INTERVAL_S = 1.0f; // 1 segundo
if (continue_time_accumulator_ >= CONTINUE_INTERVAL_S) {
continue_time_accumulator_ -= CONTINUE_INTERVAL_S;
decContinueCounter();
@@ -837,7 +851,7 @@ void Player::updateContinueCounter(float deltaTime) {
void Player::updateEnterNameCounter(float deltaTime) {
if (playing_state_ == State::ENTERING_NAME || playing_state_ == State::ENTERING_NAME_GAME_COMPLETED) {
name_entry_time_accumulator_ += deltaTime;
constexpr float NAME_ENTRY_INTERVAL_S = 1.0f; // 1 segundo
constexpr float NAME_ENTRY_INTERVAL_S = 1.0f; // 1 segundo
if (name_entry_time_accumulator_ >= NAME_ENTRY_INTERVAL_S) {
name_entry_time_accumulator_ -= NAME_ENTRY_INTERVAL_S;
decNameEntryCounter();
@@ -849,7 +863,7 @@ void Player::updateEnterNameCounter(float deltaTime) {
void Player::updateShowingName(float deltaTime) {
if (playing_state_ == State::SHOWING_NAME) {
showing_name_time_accumulator_ += deltaTime;
constexpr float SHOWING_NAME_DURATION_S = 5.0f; // 5 segundos
constexpr float SHOWING_NAME_DURATION_S = 5.0f; // 5 segundos
if (showing_name_time_accumulator_ >= SHOWING_NAME_DURATION_S) {
game_completed_ ? setPlayingState(State::LEAVING_SCREEN) : setPlayingState(State::CONTINUE);
}
@@ -858,7 +872,7 @@ void Player::updateShowingName(float deltaTime) {
// Decrementa el contador de continuar
void Player::decContinueCounter() {
continue_time_accumulator_ = 0.0f; // Reset time accumulator
continue_time_accumulator_ = 0.0f; // Reset time accumulator
--continue_counter_;
if (continue_counter_ < 0) {
setPlayingState(State::CONTINUE_TIME_OUT);
@@ -869,7 +883,7 @@ void Player::decContinueCounter() {
// Decrementa el contador de entrar nombre
void Player::decNameEntryCounter() {
name_entry_time_accumulator_ = 0.0f; // Reset time accumulator
name_entry_time_accumulator_ = 0.0f; // Reset time accumulator
// Incrementa acumuladores de tiempo (1 segundo)
name_entry_idle_time_accumulator_ += 1.0f;
@@ -888,15 +902,6 @@ void Player::decNameEntryCounter() {
}
}
// Obtiene la posición que se está editando del nombre del jugador para la tabla de mejores puntuaciones
auto Player::getRecordNamePos() const -> int {
if (enter_name_) {
return enter_name_->getPosition();
}
return 0;
}
// Recoloca los sprites
void Player::shiftSprite() {
player_sprite_->setPosX(pos_x_);
@@ -905,12 +910,12 @@ void Player::shiftSprite() {
}
// Hace sonar un sonido
void Player::playSound(const std::string &name) const {
void Player::playSound(const std::string& name) const {
if (demo_) {
return;
}
static auto *audio_ = Audio::get();
static auto* audio_ = Audio::get();
audio_->playSound(name);
}
@@ -919,6 +924,33 @@ auto Player::isRenderable() const -> bool {
return !isTitleHidden();
};
// Devuelve el color actual de bala según el estado
auto Player::getBulletColor() const -> Bullet::Color {
return power_up_ ? bullet_colors_.powered_color : bullet_colors_.normal_color;
}
// Devuelve el color para la próxima bala (alterna si está en modo toggle)
auto Player::getNextBulletColor() -> Bullet::Color {
if (in_power_up_ending_phase_) {
// En fase final: alternar entre colores powered y normal
bullet_color_toggle_ = !bullet_color_toggle_;
return bullet_color_toggle_ ? bullet_colors_.powered_color : bullet_colors_.normal_color;
}
// Modo normal: sin power-up = normal_color, con power-up = powered_color
return power_up_ ? bullet_colors_.powered_color : bullet_colors_.normal_color;
}
// Establece los colores de bala para este jugador
void Player::setBulletColors(Bullet::Color normal, Bullet::Color powered) {
bullet_colors_.normal_color = normal;
bullet_colors_.powered_color = powered;
}
// Establece el archivo de sonido de bala para este jugador
void Player::setBulletSoundFile(const std::string& filename) {
bullet_sound_file_ = filename;
}
// Añade una puntuación a la tabla de records
void Player::addScoreToScoreBoard() const {
if (hi_score_table_ == nullptr) {
@@ -1007,7 +1039,7 @@ void Player::startFiringSystem(int cooldown_frames) {
visual_fire_state_ = VisualFireState::AIMING;
visual_state_timer_ = aiming_duration_;
updateFiringStateFromVisual(); // Sincroniza firing_state_ para animaciones
updateFiringStateFromVisual(); // Sincroniza firing_state_ para animaciones
}
// Sincroniza firing_state_ con visual_fire_state_
@@ -1034,19 +1066,35 @@ void Player::updateFiringStateFromVisual() {
case VisualFireState::RECOILING:
switch (base_state) {
case State::FIRING_LEFT: firing_state_ = State::RECOILING_LEFT; break;
case State::FIRING_RIGHT: firing_state_ = State::RECOILING_RIGHT; break;
case State::FIRING_UP: firing_state_ = State::RECOILING_UP; break;
default: firing_state_ = State::RECOILING_UP; break;
case State::FIRING_LEFT:
firing_state_ = State::RECOILING_LEFT;
break;
case State::FIRING_RIGHT:
firing_state_ = State::RECOILING_RIGHT;
break;
case State::FIRING_UP:
firing_state_ = State::RECOILING_UP;
break;
default:
firing_state_ = State::RECOILING_UP;
break;
}
break;
case VisualFireState::THREAT_POSE:
switch (base_state) {
case State::FIRING_LEFT: firing_state_ = State::COOLING_LEFT; break;
case State::FIRING_RIGHT: firing_state_ = State::COOLING_RIGHT; break;
case State::FIRING_UP: firing_state_ = State::COOLING_UP; break;
default: firing_state_ = State::COOLING_UP; break;
case State::FIRING_LEFT:
firing_state_ = State::COOLING_LEFT;
break;
case State::FIRING_RIGHT:
firing_state_ = State::COOLING_RIGHT;
break;
case State::FIRING_UP:
firing_state_ = State::COOLING_UP;
break;
default:
firing_state_ = State::COOLING_UP;
break;
}
break;
}
@@ -1064,8 +1112,8 @@ void Player::transitionToThreatPose() {
// Calcular threat_pose_duration ajustada:
// Duración original (833ms) menos el tiempo extra que ahora dura recoiling
float original_recoiling_duration = fire_cooldown_timer_; // Era 100% del cooldown
float new_recoiling_duration = aiming_duration_ * RECOILING_DURATION_MULTIPLIER; // Ahora es más del cooldown
float original_recoiling_duration = fire_cooldown_timer_; // Era 100% del cooldown
float new_recoiling_duration = aiming_duration_ * RECOILING_DURATION_MULTIPLIER; // Ahora es más del cooldown
float extra_recoiling_time = new_recoiling_duration - original_recoiling_duration;
float adjusted_threat_duration = THREAT_POSE_DURATION - extra_recoiling_time;

View File

@@ -8,6 +8,7 @@
#include <vector> // Para vector
#include "animated_sprite.h" // Para AnimatedSprite
#include "bullet.h" // Para Bullet
#include "enter_name.h" // Para EnterName
#include "input.h" // Para Input
#include "manage_hiscore_table.h" // Para Table
@@ -38,6 +39,12 @@ class Player {
static constexpr int WIDTH = 32; // Anchura
static constexpr int HEIGHT = 32; // Altura
// --- Estructuras ---
struct BulletColorPair {
Bullet::Color normal_color; // Color de bala sin power-up
Bullet::Color powered_color; // Color de bala con power-up
};
// --- Enums ---
enum class Id : int {
NO_PLAYER = -1, // Sin jugador
@@ -95,22 +102,22 @@ class Player {
float x; // Posición X inicial
int y; // Posición Y inicial
bool demo; // Modo demo
SDL_FRect *play_area; // Área de juego (puntero para mantener referencia)
SDL_FRect* play_area; // Área de juego (puntero para mantener referencia)
std::vector<std::shared_ptr<Texture>> texture; // Texturas del jugador
std::vector<std::vector<std::string>> animations; // Animaciones del jugador
Table *hi_score_table; // Tabla de puntuaciones (puntero para referencia)
int *glowing_entry; // Entrada brillante (puntero para mantener referencia)
IStageInfo *stage_info; // Gestor de pantallas (puntero)
Table* hi_score_table; // Tabla de puntuaciones (puntero para referencia)
int* glowing_entry; // Entrada brillante (puntero para mantener referencia)
IStageInfo* stage_info; // Gestor de pantallas (puntero)
};
// --- Constructor y destructor ---
Player(const Config &config);
Player(const Config& config);
~Player() = default;
// --- Inicialización y ciclo de vida ---
void init(); // Inicializa el jugador
void update(float deltaTime); // Actualiza estado, animación y contadores (time-based)
void render(); // Dibuja el jugador en pantalla
void init(); // Inicializa el jugador
void update(float deltaTime); // Actualiza estado, animación y contadores (time-based)
void render(); // Dibuja el jugador en pantalla
// --- Entrada y control ---
void setInput(Input::Action action); // Procesa entrada general
@@ -118,32 +125,32 @@ class Player {
void setInputEnteringName(Input::Action action); // Procesa entrada al introducir nombre
// --- Movimiento y animación ---
void move(float deltaTime); // Mueve el jugador (time-based)
void setAnimation(float deltaTime); // Establece la animación según el estado (time-based)
void move(float deltaTime); // Mueve el jugador (time-based)
void setAnimation(float deltaTime); // Establece la animación según el estado (time-based)
// --- Texturas y animaciones ---
void setPlayerTextures(const std::vector<std::shared_ptr<Texture>> &texture); // Cambia las texturas del jugador
void setPlayerTextures(const std::vector<std::shared_ptr<Texture>>& texture); // Cambia las texturas del jugador
// --- Estados y contadores ---
// --- Puntuación y marcador ---
// --- Gameplay: Puntuación y power-ups ---
void addScore(int score, int lowest_hi_score_entry); // Añade puntos
void incScoreMultiplier(); // Incrementa el multiplicador
void decScoreMultiplier(); // Decrementa el multiplicador
// --- Estados de juego ---
void setPlayingState(State state); // Cambia el estado de juego
void setInvulnerable(bool value); // Establece el valor del estado de invulnerabilidad
void setPowerUp(); // Activa el modo PowerUp
void updatePowerUp(float deltaTime); // Actualiza el valor de PowerUp (time-based)
void giveExtraHit(); // Concede un toque extra al jugador
void removeExtraHit(); // Quita el toque extra al jugador
void decContinueCounter(); // Decrementa el contador de continuar
void setPlayingState(State state); // Cambia el estado de juego
void setInvulnerable(bool value); // Establece el valor del estado de invulnerabilidad
void setPowerUp(); // Activa el modo PowerUp
void updatePowerUp(float deltaTime); // Actualiza el valor de PowerUp
void giveExtraHit(); // Concede un toque extra al jugador
void removeExtraHit(); // Quita el toque extra al jugador
void decContinueCounter(); // Decrementa el contador de continuar
void setWalkingState(State state) { walking_state_ = state; } // Establece el estado de caminar
void startFiringSystem(int cooldown_frames); // Inicia el sistema de disparo
void setScoreBoardPanel(Scoreboard::Id panel) { scoreboard_panel_ = panel; } // Establece el panel del marcador
void addCredit();
void passShowingName();
// --- Getters y comprobaciones de estado ---
[[nodiscard]] auto getRecordNamePos() const -> int; // Obtiene la posición que se está editando del nombre del jugador para la tabla de mejores puntuaciones
// Comprobación de playing_state
// --- Estado del juego: Consultas (is* methods) ---
[[nodiscard]] auto isLyingOnTheFloorForever() const -> bool { return playing_state_ == State::LYING_ON_THE_FLOOR_FOREVER; }
[[nodiscard]] auto isCelebrating() const -> bool { return playing_state_ == State::CELEBRATING; }
[[nodiscard]] auto isContinue() const -> bool { return playing_state_ == State::CONTINUE; }
@@ -157,7 +164,7 @@ class Player {
[[nodiscard]] auto isWaiting() const -> bool { return playing_state_ == State::WAITING; }
[[nodiscard]] auto isTitleHidden() const -> bool { return playing_state_ == State::TITLE_HIDDEN; }
// Getters
// --- Estados específicos: Consultas adicionales ---
[[nodiscard]] auto canFire() const -> bool { return can_fire_new_system_; } // Usa nuevo sistema
[[nodiscard]] auto hasExtraHit() const -> bool { return extra_hit_; }
[[nodiscard]] auto isCooling() const -> bool { return firing_state_ == State::COOLING_LEFT || firing_state_ == State::COOLING_UP || firing_state_ == State::COOLING_RIGHT; }
@@ -165,49 +172,56 @@ class Player {
[[nodiscard]] auto qualifiesForHighScore() const -> bool { return qualifies_for_high_score_; }
[[nodiscard]] auto isInvulnerable() const -> bool { return invulnerable_; }
[[nodiscard]] auto isPowerUp() const -> bool { return power_up_; }
auto getCollider() -> Circle & { return collider_; }
[[nodiscard]] auto getScoreMultiplier() const -> float { return score_multiplier_; }
[[nodiscard]] auto getCoffees() const -> int { return coffees_; }
[[nodiscard]] auto getContinueCounter() const -> int { return continue_counter_; }
[[nodiscard]] auto getController() const -> int { return controller_index_; }
[[nodiscard]] static auto getHeight() -> int { return HEIGHT; }
[[nodiscard]] auto getId() const -> Player::Id { return id_; }
[[nodiscard]] auto getInvulnerableCounter() const -> int { return invulnerable_counter_; }
[[nodiscard]] auto isInBulletColorToggleMode() const -> bool { return in_power_up_ending_phase_; }
// --- Getters: Propiedades y valores ---
// Posición y dimensiones
[[nodiscard]] auto getPosX() const -> int { return static_cast<int>(pos_x_); }
[[nodiscard]] auto getPosY() const -> int { return pos_y_; }
[[nodiscard]] static auto getWidth() -> int { return WIDTH; }
[[nodiscard]] static auto getHeight() -> int { return HEIGHT; }
// Jugador y identificación
[[nodiscard]] auto getId() const -> Player::Id { return id_; }
[[nodiscard]] auto getName() const -> const std::string& { return name_; }
[[nodiscard]] auto getPlayingState() const -> State { return playing_state_; }
auto getCollider() -> Circle& { return collider_; }
// Puntuación y juego
[[nodiscard]] auto getScore() const -> int { return score_; }
[[nodiscard]] auto getScoreMultiplier() const -> float { return score_multiplier_; }
[[nodiscard]] auto get1CC() const -> bool { return game_completed_ && credits_used_ <= 1; }
[[nodiscard]] auto getScoreBoardPanel() const -> Scoreboard::Id { return scoreboard_panel_; }
// Power-ups y estado especial
[[nodiscard]] auto getCoffees() const -> int { return coffees_; }
[[nodiscard]] auto getPowerUpCounter() const -> int { return power_up_counter_; }
[[nodiscard]] auto getInvulnerableCounter() const -> int { return invulnerable_counter_; }
[[nodiscard]] auto getBulletColor() const -> Bullet::Color; // Devuelve el color actual de bala según el estado
auto getNextBulletColor() -> Bullet::Color; // Devuelve el color para la próxima bala (alterna si está en modo toggle)
void setBulletColors(Bullet::Color normal, Bullet::Color powered); // Establece los colores de bala para este jugador
[[nodiscard]] auto getBulletSoundFile() const -> std::string { return bullet_sound_file_; } // Devuelve el archivo de sonido de bala
void setBulletSoundFile(const std::string& filename); // Establece el archivo de sonido de bala para este jugador
// Contadores y timers
[[nodiscard]] auto getContinueCounter() const -> int { return continue_counter_; }
[[nodiscard]] auto getRecordName() const -> std::string { return enter_name_ ? enter_name_->getFinalName() : "xxx"; }
[[nodiscard]] auto getLastEnterName() const -> std::string { return last_enter_name_; }
[[nodiscard]] auto getScore() const -> int { return score_; }
[[nodiscard]] auto getScoreBoardPanel() const -> Scoreboard::Id { return scoreboard_panel_; }
[[nodiscard]] static auto getWidth() -> int { return WIDTH; }
[[nodiscard]] auto getPlayingState() const -> State { return playing_state_; }
[[nodiscard]] auto getName() const -> const std::string & { return name_; }
[[nodiscard]] auto get1CC() const -> bool { return game_completed_ && credits_used_ <= 1; }
[[nodiscard]] auto getEnterNamePositionOverflow() const -> bool { return enter_name_ ? enter_name_->getPositionOverflow() : false; }
// Setters inline
void setController(int index) { controller_index_ = index; }
void startFiringSystem(int cooldown_frames); // Método público para iniciar disparo
void setFiringState(State state) { firing_state_ = state; }
void setInvulnerableCounter(int value) { invulnerable_counter_ = value; }
void setName(const std::string &name) { name_ = name; }
void setPowerUpCounter(int value) { power_up_counter_ = value; }
void setScore(int score) { score_ = score; }
void setScoreBoardPanel(Scoreboard::Id panel) { scoreboard_panel_ = panel; }
void setScoreMultiplier(float value) { score_multiplier_ = value; }
void setWalkingState(State state) { walking_state_ = state; }
void addCredit();
void passShowingName();
// --- Configuración e interfaz externa ---
void setName(const std::string& name) { name_ = name; }
void setGamepad(std::shared_ptr<Input::Gamepad> gamepad) { gamepad_ = std::move(gamepad); }
[[nodiscard]] auto getGamepad() const -> std::shared_ptr<Input::Gamepad> { return gamepad_; }
void setUsesKeyboard(bool value) { uses_keyboard_ = value; }
[[nodiscard]] auto getUsesKeyboard() const -> bool { return uses_keyboard_; }
[[nodiscard]] auto getController() const -> int { return controller_index_; }
// Demo file management
[[nodiscard]] auto getDemoFile() const -> size_t { return demo_file_; }
void setDemoFile(size_t demo_file) { demo_file_ = demo_file; }
private:
// --- Constantes de física y movimiento ---
static constexpr float BASE_SPEED = 90.0f; // Velocidad base del jugador (pixels/segundo)
static constexpr float BASE_SPEED = 90.0f; // Velocidad base del jugador (pixels/segundo)
// --- Constantes de power-ups y estados especiales ---
static constexpr int POWERUP_COUNTER = 1500; // Duración del estado PowerUp (frames)
@@ -215,16 +229,16 @@ class Player {
static constexpr size_t INVULNERABLE_TEXTURE = 3; // Textura usada durante invulnerabilidad
// --- Constantes del sistema de disparo (obsoletas - usar nuevo sistema) ---
static constexpr int COOLING_DURATION = 50; // Duración del enfriamiento tras disparar
static constexpr int COOLING_COMPLETE = 0; // Valor que indica enfriamiento completado
static constexpr int COOLING_DURATION = 50; // Duración del enfriamiento tras disparar
static constexpr int COOLING_COMPLETE = 0; // Valor que indica enfriamiento completado
// --- Constantes de estados de espera ---
static constexpr int WAITING_COUNTER = 1000; // Tiempo de espera en estado de espera
static constexpr int WAITING_COUNTER = 1000; // Tiempo de espera en estado de espera
// --- Constantes del nuevo sistema de disparo de dos líneas ---
static constexpr float AIMING_DURATION_FACTOR = 0.5f; // 50% del cooldown funcional
static constexpr float RECOILING_DURATION_MULTIPLIER = 4.0f; // 4 veces la duración de aiming
static constexpr float THREAT_POSE_DURATION = 50.0f / 60.0f; // 50 frames = ~0.833s (duración base)
static constexpr float AIMING_DURATION_FACTOR = 0.5f; // 50% del cooldown funcional
static constexpr float RECOILING_DURATION_MULTIPLIER = 4.0f; // 4 veces la duración de aiming
static constexpr float THREAT_POSE_DURATION = 50.0f / 60.0f; // 50 frames = ~0.833s (duración base)
static constexpr float MIN_THREAT_POSE_DURATION = 6.0f / 60.0f; // 6 frames = ~0.1s (duración mínima)
// --- Objetos y punteros ---
@@ -232,57 +246,58 @@ class Player {
std::unique_ptr<AnimatedSprite> power_sprite_; // Sprite para dibujar el aura del jugador con el poder a tope
std::unique_ptr<EnterName> enter_name_; // Clase utilizada para introducir el nombre
std::shared_ptr<Input::Gamepad> gamepad_ = nullptr; // Dispositivo asociado
Table *hi_score_table_ = nullptr; // Tabla de máximas puntuaciones
int *glowing_entry_ = nullptr; // Entrada de la tabla de puntuaciones para hacerla brillar
IStageInfo *stage_info_; // Informacion de la pantalla actual
Table* hi_score_table_ = nullptr; // Tabla de máximas puntuaciones
int* glowing_entry_ = nullptr; // Entrada de la tabla de puntuaciones para hacerla brillar
IStageInfo* stage_info_; // Informacion de la pantalla actual
// --- Variables de estado ---
SDL_FRect play_area_; // Rectángulo con la zona de juego
Circle collider_ = Circle(0, 0, 9); // Círculo de colisión del jugador
std::string name_; // Nombre del jugador
std::string last_enter_name_; // Último nombre introducido en la tabla de puntuaciones
Scoreboard::Id scoreboard_panel_ = Scoreboard::Id::LEFT; // Panel del marcador asociado al jugador
Id id_; // Identificador para el jugador
State walking_state_ = State::WALKING_STOP; // Estado del jugador al moverse
State firing_state_ = State::FIRING_NONE; // Estado del jugador al disparar
State playing_state_ = State::WAITING; // Estado del jugador en el juego
SDL_FRect play_area_; // Rectángulo con la zona de juego
Circle collider_ = Circle(0, 0, 9); // Círculo de colisión del jugador
std::string name_; // Nombre del jugador
std::string last_enter_name_; // Último nombre introducido en la tabla de puntuaciones
Scoreboard::Id scoreboard_panel_ = Scoreboard::Id::LEFT; // Panel del marcador asociado al jugador
Id id_; // Identificador para el jugador
State walking_state_ = State::WALKING_STOP; // Estado del jugador al moverse
State firing_state_ = State::FIRING_NONE; // Estado del jugador al disparar
State playing_state_ = State::WAITING; // Estado del jugador en el juego
BulletColorPair bullet_colors_ = {Bullet::Color::YELLOW, Bullet::Color::GREEN}; // Par de colores de balas para este jugador
std::string bullet_sound_file_ = "bullet1p.wav"; // Archivo de sonido de bala para este jugador
float pos_x_ = 0.0F; // Posición en el eje X
float default_pos_x_; // Posición inicial para el jugador
float vel_x_ = 0.0F; // Cantidad de píxeles a desplazarse en el eje X
float score_multiplier_ = 1.0F; // Multiplicador de puntos
int pos_y_ = 0; // Posición en el eje Y
int default_pos_y_; // Posición inicial para el jugador
int vel_y_ = 0; // Cantidad de píxeles a desplazarse en el eje Y
float invulnerable_time_accumulator_ = 0.0f; // Acumulador de tiempo para invulnerabilidad (time-based)
float power_up_time_accumulator_ = 0.0f; // Acumulador de tiempo para power-up (time-based)
float continue_time_accumulator_ = 0.0f; // Acumulador de tiempo para continue counter (time-based)
float name_entry_time_accumulator_ = 0.0f; // Acumulador de tiempo para name entry counter (time-based)
float showing_name_time_accumulator_ = 0.0f; // Acumulador de tiempo para showing name (time-based)
float waiting_time_accumulator_ = 0.0f; // Acumulador de tiempo para waiting movement (time-based)
float step_time_accumulator_ = 0.0f; // Acumulador de tiempo para step counter (time-based)
float pos_x_ = 0.0F; // Posición en el eje X
float default_pos_x_; // Posición inicial para el jugador
float vel_x_ = 0.0F; // Cantidad de píxeles a desplazarse en el eje X
float score_multiplier_ = 1.0F; // Multiplicador de puntos
int pos_y_ = 0; // Posición en el eje Y
int default_pos_y_; // Posición inicial para el jugador
int vel_y_ = 0; // Cantidad de píxeles a desplazarse en el eje Y
float invulnerable_time_accumulator_ = 0.0f; // Acumulador de tiempo para invulnerabilidad (time-based)
float power_up_time_accumulator_ = 0.0f; // Acumulador de tiempo para power-up (time-based)
float continue_time_accumulator_ = 0.0f; // Acumulador de tiempo para continue counter (time-based)
float name_entry_time_accumulator_ = 0.0f; // Acumulador de tiempo para name entry counter (time-based)
float showing_name_time_accumulator_ = 0.0f; // Acumulador de tiempo para showing name (time-based)
float waiting_time_accumulator_ = 0.0f; // Acumulador de tiempo para waiting movement (time-based)
float step_time_accumulator_ = 0.0f; // Acumulador de tiempo para step counter (time-based)
// ========================================
// NUEVO SISTEMA DE DISPARO DE DOS LÍNEAS
// ========================================
// LÍNEA 1: SISTEMA FUNCIONAL (CanFire)
float fire_cooldown_timer_ = 0.0f; // Tiempo restante hasta poder disparar otra vez
bool can_fire_new_system_ = true; // true si puede disparar ahora mismo
float fire_cooldown_timer_ = 0.0f; // Tiempo restante hasta poder disparar otra vez
bool can_fire_new_system_ = true; // true si puede disparar ahora mismo
// LÍNEA 2: SISTEMA VISUAL (Animaciones)
enum class VisualFireState {
NORMAL, // Brazo en posición neutral
AIMING, // Brazo alzado (disparando)
RECOILING, // Brazo en retroceso
THREAT_POSE // Posición amenazante
NORMAL, // Brazo en posición neutral
AIMING, // Brazo alzado (disparando)
RECOILING, // Brazo en retroceso
THREAT_POSE // Posición amenazante
};
VisualFireState visual_fire_state_ = VisualFireState::NORMAL;
float visual_state_timer_ = 0.0f; // Tiempo en el estado visual actual
float aiming_duration_ = 0.0f; // Duración del estado AIMING
float recoiling_duration_ = 0.0f; // Duración del estado RECOILING
float visual_state_timer_ = 0.0f; // Tiempo en el estado visual actual
float aiming_duration_ = 0.0f; // Duración del estado AIMING
float recoiling_duration_ = 0.0f; // Duración del estado RECOILING
int invulnerable_counter_ = INVULNERABLE_COUNTER; // Contador para la invulnerabilidad
int score_ = 0; // Puntos del jugador
@@ -291,6 +306,7 @@ class Player {
int power_up_x_offset_ = 0; // Desplazamiento del sprite de PowerUp respecto al sprite del jugador
int continue_counter_ = 10; // Contador para poder continuar
int controller_index_ = 0; // Índice del array de mandos que utilizará para moverse
size_t demo_file_ = 0; // Indice del fichero de datos para el modo demo
float name_entry_idle_time_accumulator_ = 0.0f; // Tiempo idle acumulado para poner nombre (milisegundos)
float name_entry_total_time_accumulator_ = 0.0f; // Tiempo total acumulado poniendo nombre (milisegundos)
int step_counter_ = 0; // Cuenta los pasos para los estados en los que camina automáticamente
@@ -300,67 +316,76 @@ class Player {
bool invulnerable_ = true; // Indica si el jugador es invulnerable
bool extra_hit_ = false; // Indica si el jugador tiene un toque extra
bool power_up_ = false; // Indica si el jugador tiene activo el modo PowerUp
bool power_sprite_visible_ = false; // Indica si el sprite de power-up debe ser visible
bool in_power_up_ending_phase_ = false; // Indica si está en la fase final del power-up (alternando colores)
bool bullet_color_toggle_ = false; // Para alternar entre verde y amarillo en fase final
bool demo_ = false; // Para que el jugador sepa si está en el modo demostración
bool game_completed_ = false; // Indica si ha completado el juego
bool uses_keyboard_ = false; // Indica si usa el teclado como dispositivo de control
// --- Métodos internos ---
void shiftColliders(); // Actualiza el círculo de colisión a la posición del jugador
void shiftSprite(); // Recoloca el sprite
void updateInvulnerable(float deltaTime); // Monitoriza el estado de invulnerabilidad (time-based)
void updateContinueCounter(float deltaTime); // Actualiza el contador de continue (time-based)
void updateEnterNameCounter(float deltaTime); // Actualiza el contador de entrar nombre (time-based)
void updateShowingName(float deltaTime); // Actualiza el estado SHOWING_NAME (time-based)
void decNameEntryCounter(); // Decrementa el contador de entrar nombre
void updateScoreboard(); // Actualiza el panel del marcador
void setScoreboardMode(Scoreboard::Mode mode) const; // Cambia el modo del marcador
void playSound(const std::string &name) const; // Hace sonar un sonido
[[nodiscard]] auto isRenderable() const -> bool; // Indica si se puede dibujar el objeto
void addScoreToScoreBoard() const; // Añade una puntuación a la tabla de records
void shiftColliders(); // Actualiza el círculo de colisión a la posición del jugador
void shiftSprite(); // Recoloca el sprite
// --- Métodos del sistema de disparo de dos líneas ---
void updateFireSystem(float deltaTime); // Método principal del nuevo sistema de disparo
void updateFunctionalLine(float deltaTime); // Actualiza la línea funcional (CanFire)
void updateVisualLine(float deltaTime); // Actualiza la línea visual (Animaciones)
void startFiring(int cooldown_frames); // Inicia un nuevo disparo en ambas líneas
void updateFiringStateFromVisual(); // Sincroniza firing_state_ con visual_fire_state_
void transitionToRecoilingNew(); // Transición AIMING → RECOILING
void transitionToThreatPose(); // Transición RECOILING → THREAT_POSE
void transitionToNormalNew(); // Transición THREAT_POSE → NORMAL
// --- Setters internos ---
void setController(int index) { controller_index_ = index; }
void setFiringState(State state) { firing_state_ = state; }
void setInvulnerableCounter(int value) { invulnerable_counter_ = value; }
void setPowerUpCounter(int value) { power_up_counter_ = value; }
void setScore(int score) { score_ = score; }
void setScoreMultiplier(float value) { score_multiplier_ = value; }
// --- Métodos del sistema de disparo obsoleto ---
void handleFiringCooldown(); // Gestiona el tiempo de espera después de disparar (frame-based)
void handleFiringCooldown(float deltaTime); // Gestiona el tiempo de espera después de disparar (time-based)
void handleRecoilAndCooling(); // Procesa retroceso y enfriamiento (frame-based)
void handleRecoilAndCooling(float deltaTime); // Procesa retroceso y enfriamiento (time-based)
void handleCoolingState(); // Actualiza estado de enfriamiento (frame-based)
void handleCoolingState(float deltaTime); // Actualiza estado de enfriamiento (time-based)
void transitionToRecoiling(); // Transición a retroceso (sistema obsoleto)
void transitionToCooling(); // Transición a enfriamiento (sistema obsoleto)
void completeCooling(); // Finaliza enfriamiento (sistema obsoleto)
void handlePlayingMovement(); // Gestiona el movimiento del personaje u objeto durante el estado de juego activo (frame-based)
void handlePlayingMovement(float deltaTime); // Gestiona el movimiento del personaje u objeto durante el estado de juego activo (time-based)
void handleRecoverMovement(); // Comprueba si ha acabado la animación
void handleRollingMovement(); // Actualiza la lógica de movimiento de "rodar" (posiblemente tras impacto o acción especial)
void handleRollingBoundaryCollision(); // Detecta y maneja colisiones del objeto rodante con los límites de la pantalla
void handleRollingGroundCollision(); // Gestiona la interacción del objeto rodante con el suelo (rebotes, frenado, etc.)
void handleRollingStop(); // Detiene el movimiento del objeto rodante cuando se cumplen las condiciones necesarias
void handleRollingBounce(); // Aplica una lógica de rebote al colisionar con superficies durante el rodamiento
void handleTitleAnimation(float deltaTime); // Ejecuta la animación del título en pantalla (ej. entrada, parpadeo o desplazamiento)
void handleContinueTimeOut(); // Gestiona el tiempo de espera en la pantalla de "Continuar" y decide si pasar a otro estado
void handleLeavingScreen(float deltaTime); // Lógica para salir de la pantalla actual (transición visual o cambio de escena)
void handleEnteringScreen(float deltaTime); // Lógica para entrar en una nueva pantalla, posiblemente con animación o retraso
void handlePlayer1Entering(float deltaTime); // Controla la animación o posición de entrada del Jugador 1 en pantalla
void handlePlayer2Entering(float deltaTime); // Controla la animación o posición de entrada del Jugador 2 en pantalla
void handleCreditsMovement(); // Movimiento general en la pantalla de créditos (frame-based)
void handleCreditsMovement(float deltaTime); // Movimiento general en la pantalla de créditos (time-based)
void handleCreditsRightMovement(); // Lógica específica para mover los créditos hacia la derecha
void handleCreditsLeftMovement(); // Lógica específica para mover los créditos hacia la izquierda
void handleWaitingMovement(); // Controla la animación del jugador saludando (frame-based)
void handleWaitingMovement(float deltaTime); // Controla la animación del jugador saludando (time-based)
void updateWalkingStateForCredits(); // Actualiza el estado de caminata de algún personaje u elemento animado en los créditos
void setInputBasedOnPlayerId(); // Asocia las entradas de control en función del identificador del jugador (teclas, mando, etc.)
void updateStepCounter(); // Incrementa o ajusta el contador de pasos (frame-based)
void updateStepCounter(float deltaTime); // Incrementa o ajusta el contador de pasos (time-based)
[[nodiscard]] auto computeAnimation() const -> std::pair<std::string, SDL_FlipMode>; // Calcula la animacion de moverse y disparar del jugador
// --- Actualizadores de estado (time-based) ---
void updateInvulnerable(float deltaTime); // Monitoriza el estado de invulnerabilidad
void updateContinueCounter(float deltaTime); // Actualiza el contador de continue
void updateEnterNameCounter(float deltaTime); // Actualiza el contador de entrar nombre
void updateShowingName(float deltaTime); // Actualiza el estado SHOWING_NAME
void decNameEntryCounter(); // Decrementa el contador de entrar nombre
// --- Utilidades generales ---
void updateScoreboard(); // Actualiza el panel del marcador
void setScoreboardMode(Scoreboard::Mode mode) const; // Cambia el modo del marcador
void playSound(const std::string& name) const; // Hace sonar un sonido
[[nodiscard]] auto isRenderable() const -> bool; // Indica si se puede dibujar el objeto
void addScoreToScoreBoard() const; // Añade una puntuación a la tabla de records
// --- Sistema de disparo (nuevo - dos líneas) ---
void updateFireSystem(float deltaTime); // Método principal del nuevo sistema de disparo
void updateFunctionalLine(float deltaTime); // Actualiza la línea funcional (CanFire)
void updateVisualLine(float deltaTime); // Actualiza la línea visual (Animaciones)
void updateFiringStateFromVisual(); // Sincroniza firing_state_ con visual_fire_state_
void transitionToRecoilingNew(); // Transición AIMING → RECOILING
void transitionToThreatPose(); // Transición RECOILING → THREAT_POSE
void transitionToNormalNew(); // Transición THREAT_POSE → NORMAL
// --- Manejadores de movimiento ---
void handlePlayingMovement(float deltaTime); // Gestiona el movimiento durante el juego
void handleRecoverMovement(); // Comprueba si ha acabado la animación de recuperación
void updateStepCounter(float deltaTime); // Incrementa o ajusta el contador de pasos
void setInputBasedOnPlayerId(); // Asocia las entradas de control según el jugador
// --- Manejadores de estados especiales ---
void handleRollingMovement(); // Actualiza la lógica de movimiento de "rodar"
void handleRollingBoundaryCollision(); // Detecta colisiones con límites durante rodamiento
void handleRollingGroundCollision(); // Gestiona interacción con el suelo durante rodamiento
void handleRollingStop(); // Detiene el movimiento del objeto rodante
void handleRollingBounce(); // Aplica lógica de rebote durante rodamiento
void handleContinueTimeOut(); // Gestiona tiempo de espera en pantalla "Continuar"
// --- Manejadores de transiciones de pantalla ---
void handleTitleAnimation(float deltaTime); // Ejecuta animación del título
void handleLeavingScreen(float deltaTime); // Lógica para salir de pantalla
void handleEnteringScreen(float deltaTime); // Lógica para entrar en pantalla
void handlePlayer1Entering(float deltaTime); // Entrada del Jugador 1
void handlePlayer2Entering(float deltaTime); // Entrada del Jugador 2
// --- Manejadores de pantallas especiales ---
void handleCreditsMovement(float deltaTime); // Movimiento en pantalla de créditos
void handleCreditsRightMovement(); // Movimiento hacia la derecha en créditos
void handleCreditsLeftMovement(); // Movimiento hacia la izquierda en créditos
void handleWaitingMovement(float deltaTime); // Animación del jugador saludando
void updateWalkingStateForCredits(); // Actualiza estado de caminata en créditos
// --- Utilidades de animación ---
[[nodiscard]] auto computeAnimation() const -> std::pair<std::string, SDL_FlipMode>; // Calcula animación de movimiento y disparo
};

View File

@@ -0,0 +1,166 @@
# Metal Shader Backend - Notas de Implementación
## Estado Actual
**Completado:**
- Shaders MSL (Metal Shading Language) portados desde GLSL
- Estructura básica de `MetalShader` class
- Inicialización de Metal device y command queue
- Compilación de shaders en runtime
- Creación de pipeline state
- Buffers de vértices, índices y uniforms
**Pendiente:**
- **Render loop completo** (la parte más crítica)
- Obtener textura Metal desde SDL_Texture
- Gestión de drawables y presentation
## Diferencias GLSL vs MSL
| Concepto | GLSL (OpenGL) | MSL (Metal) |
|----------|---------------|-------------|
| Entrada vertex | `layout(location = 0) in vec2` | `[[attribute(0)]]` |
| Salida vertex | `out vec2` | Struct con `[[position]]` |
| Uniforms | `uniform vec2` | `constant` struct en `[[buffer(N)]]` |
| Sampling | `texture(sampler2D, vec2)` | `texture.sample(sampler, float2)` |
| Entry point | `void main()` | `vertex/fragment function_name()` |
| Vector types | `vec2, vec3, vec4` | `float2, float3, float4` |
## Pasos para Completar el Render Loop
El método `MetalShader::render()` necesita:
```objc
1. Obtener drawable del CAMetalLayer:
id<CAMetalDrawable> drawable = [metal_layer_ nextDrawable];
2. Crear command buffer:
id<MTLCommandBuffer> command_buffer = [command_queue_ commandBuffer];
3. Crear render pass descriptor:
MTLRenderPassDescriptor* pass_descriptor = [MTLRenderPassDescriptor renderPassDescriptor];
pass_descriptor.colorAttachments[0].texture = drawable.texture;
pass_descriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
pass_descriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
pass_descriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 1);
4. Crear render command encoder:
id<MTLRenderCommandEncoder> encoder =
[command_buffer renderCommandEncoderWithDescriptor:pass_descriptor];
5. Configurar pipeline y buffers:
[encoder setRenderPipelineState:pipeline_state_];
[encoder setVertexBuffer:vertex_buffer_ offset:0 atIndex:0];
[encoder setVertexBuffer:uniforms_buffer_ offset:0 atIndex:1];
[encoder setFragmentBuffer:uniforms_buffer_ offset:0 atIndex:1];
[encoder setFragmentTexture:game_texture offset:0 atIndex:0];
[encoder setFragmentSamplerState:sampler_state_ atIndex:0];
6. Dibujar:
[encoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle
indexCount:6
indexType:MTLIndexTypeUInt16
indexBuffer:index_buffer_
indexBufferOffset:0];
7. Finalizar:
[encoder endEncoding];
[command_buffer presentDrawable:drawable];
[command_buffer commit];
```
## Problema Crítico: Obtener MTLTexture desde SDL_Texture
SDL3 renderiza el juego a `back_buffer_` (SDL_Texture). Necesitamos obtener
la textura Metal subyacente para pasarla al fragment shader.
**Opciones:**
1. **SDL_GetProperty()** - Usar SDL3 properties system:
```cpp
id<MTLTexture> metal_texture = (__bridge id<MTLTexture>)SDL_GetProperty(
SDL_GetTextureProperties(back_buffer_),
"SDL.texture.metal.texture",
nullptr
);
```
2. **Render to Metal texture directamente** - En lugar de usar SDL_Texture,
crear una MTLTexture directamente y renderizar el juego ahí. Más trabajo
pero más control.
3. **Copiar SDL texture a Metal texture** - Menos eficiente pero más simple.
## Sampler State
Falta crear el sampler state (equivalente a glTexParameteri en OpenGL):
```objc
MTLSamplerDescriptor* sampler_descriptor = [[MTLSamplerDescriptor alloc] init];
sampler_descriptor.minFilter = MTLSamplerMinMagFilterLinear;
sampler_descriptor.magFilter = MTLSamplerMinMagFilterLinear;
sampler_descriptor.sAddressMode = MTLSamplerAddressModeClampToEdge;
sampler_descriptor.tAddressMode = MTLSamplerAddressModeClampToEdge;
id<MTLSamplerState> sampler_state = [device_ newSamplerStateWithDescriptor:sampler_descriptor];
```
## Build Configuration
Para compilar en Xcode/CMake, necesitarás:
1. **CMakeLists.txt** - Añadir metal_shader.mm:
```cmake
if(APPLE)
set(RENDERING_SOURCES
${RENDERING_SOURCES}
source/rendering/metal/metal_shader.mm
)
target_link_libraries(${PROJECT_NAME}
"-framework Metal"
"-framework QuartzCore"
)
endif()
```
2. **Compilar shaders .metal** - Opcionalmente pre-compilar:
```bash
xcrun -sdk macosx metal -c crtpi_vertex.metal -o crtpi_vertex.air
xcrun -sdk macosx metal -c crtpi_fragment.metal -o crtpi_fragment.air
xcrun -sdk macosx metallib crtpi_*.air -o crtpi.metallib
```
3. **Cargar .metallib** en código:
```objc
NSString* path = [[NSBundle mainBundle] pathForResource:@"crtpi" ofType:@"metallib"];
id<MTLLibrary> library = [device_ newLibraryWithFile:path error:&error];
```
## Testing en macOS
Cuando pruebes en macOS:
1. Verifica que `SDL_WINDOW_METAL` está activo en screen.cpp
2. Compila con `-DCMAKE_BUILD_TYPE=Debug` para ver logs
3. Usa Xcode Instruments (Metal Debugger) para inspeccionar frames
4. Compara rendimiento con/sin shaders
## Referencias Útiles
- [Metal Programming Guide](https://developer.apple.com/metal/)
- [Metal Shading Language Specification](https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf)
- [SDL3 Metal Integration](https://github.com/libsdl-org/SDL/blob/main/docs/README-metal.md)
## Próximos Pasos
1. Implementar `render()` completo
2. Resolver obtención de textura desde SDL
3. Crear sampler state
4. Testear en macOS real
5. Optimizar si es necesario (probablemente ya será rápido)
---
**Nota importante**: Metal es significativamente más verboso que OpenGL pero
también más eficiente. Una vez que funcione el render loop, el rendimiento
debería ser excelente en macOS.

View File

@@ -0,0 +1,76 @@
#pragma once
#include "../shader_backend.h"
#ifdef __APPLE__
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
namespace Rendering {
/**
* @brief Backend de shaders usando Metal para macOS
*
* Implementa el renderizado de shaders usando Metal API nativo de Apple:
* - MTLDevice para acceso a GPU
* - MTLRenderPipelineState para configuración del pipeline
* - MTLCommandQueue para enviar comandos a GPU
* - MSL (Metal Shading Language) para shaders
*/
class MetalShader : public ShaderBackend {
public:
MetalShader() = default;
~MetalShader() override;
bool init(SDL_Window* window,
SDL_Texture* texture,
const std::string& vertex_source,
const std::string& fragment_source) override;
void render() override;
void setTextureSize(float width, float height) override;
void cleanup() override;
bool isHardwareAccelerated() const override { return is_initialized_; }
private:
// Funciones auxiliares
bool createMetalDevice();
bool compileShaders(const std::string& vertex_source,
const std::string& fragment_source);
bool createPipeline();
bool createBuffers();
id<MTLTexture> getMetalTexture(SDL_Texture* texture);
// Estado SDL
SDL_Window* window_ = nullptr;
SDL_Renderer* renderer_ = nullptr;
SDL_Texture* back_buffer_ = nullptr;
// Estado Metal
id<MTLDevice> device_ = nil; // GPU device
id<MTLCommandQueue> command_queue_ = nil; // Cola de comandos
id<MTLRenderPipelineState> pipeline_state_ = nil; // Estado del pipeline
id<MTLBuffer> vertex_buffer_ = nil; // Buffer de vértices
id<MTLBuffer> index_buffer_ = nil; // Buffer de índices
id<MTLBuffer> uniforms_buffer_ = nil; // Buffer de uniforms
CAMetalLayer* metal_layer_ = nil; // Layer de Metal
// Uniforms
struct Uniforms {
float textureSize[2]; // width, height
} uniforms_;
// Tamaños
int window_width_ = 0;
int window_height_ = 0;
float texture_width_ = 0.0f;
float texture_height_ = 0.0f;
// Estado
bool is_initialized_ = false;
};
} // namespace Rendering
#endif // __APPLE__

View File

@@ -0,0 +1,301 @@
// Usar .mm (Objective-C++) en lugar de .cpp para código Metal en macOS
#include "metal_shader.h"
#ifdef __APPLE__
#import <SDL3/SDL.h>
#import <SDL3/SDL_metal.h>
#include <vector>
namespace Rendering {
MetalShader::~MetalShader() {
cleanup();
}
bool MetalShader::createMetalDevice() {
// Obtener el device Metal por defecto (GPU del sistema)
device_ = MTLCreateSystemDefaultDevice();
if (!device_) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: No se pudo crear Metal device");
return false;
}
// Crear command queue
command_queue_ = [device_ newCommandQueue];
if (!command_queue_) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: No se pudo crear Metal command queue");
return false;
}
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Metal device creado: %s", [[device_ name] UTF8String]);
return true;
}
bool MetalShader::compileShaders(const std::string& vertex_source,
const std::string& fragment_source) {
// En Metal, los shaders se pueden compilar de dos formas:
// 1. Runtime compilation desde strings (lo que haremos aquí)
// 2. Pre-compilados a .metallib (más eficiente pero requiere build step)
NSError* error = nil;
// Convertir sources a NSString
NSString* vertex_code = [NSString stringWithUTF8String:vertex_source.c_str()];
NSString* fragment_code = [NSString stringWithUTF8String:fragment_source.c_str()];
// Combinar vertex + fragment en un solo source (Metal permite múltiples shaders por library)
NSString* combined_source = [NSString stringWithFormat:@"%@\n\n%@",
vertex_code, fragment_code];
// Crear library desde source code
id<MTLLibrary> library = [device_ newLibraryWithSource:combined_source
options:nil
error:&error];
if (!library) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: Fallo al compilar shaders Metal: %s",
[[error localizedDescription] UTF8String]);
return false;
}
// Obtener funciones del library
id<MTLFunction> vertex_function = [library newFunctionWithName:@"vertex_main"];
id<MTLFunction> fragment_function = [library newFunctionWithName:@"fragment_main"];
if (!vertex_function || !fragment_function) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: No se encontraron las funciones vertex_main o fragment_main");
return false;
}
// Crear pipeline descriptor
MTLRenderPipelineDescriptor* pipeline_descriptor = [[MTLRenderPipelineDescriptor alloc] init];
pipeline_descriptor.vertexFunction = vertex_function;
pipeline_descriptor.fragmentFunction = fragment_function;
// Configurar formato de color (BGRA8Unorm es común en macOS)
pipeline_descriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
// Configurar vertex descriptor (cómo están organizados los vértices)
MTLVertexDescriptor* vertex_descriptor = [[MTLVertexDescriptor alloc] init];
// Atributo 0: position (2 floats)
vertex_descriptor.attributes[0].format = MTLVertexFormatFloat2;
vertex_descriptor.attributes[0].offset = 0;
vertex_descriptor.attributes[0].bufferIndex = 0;
// Atributo 1: texCoord (2 floats)
vertex_descriptor.attributes[1].format = MTLVertexFormatFloat2;
vertex_descriptor.attributes[1].offset = 2 * sizeof(float);
vertex_descriptor.attributes[1].bufferIndex = 0;
// Layout del buffer (stride = 4 floats por vértice)
vertex_descriptor.layouts[0].stride = 4 * sizeof(float);
vertex_descriptor.layouts[0].stepRate = 1;
vertex_descriptor.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
pipeline_descriptor.vertexDescriptor = vertex_descriptor;
// Crear pipeline state
pipeline_state_ = [device_ newRenderPipelineStateWithDescriptor:pipeline_descriptor
error:&error];
if (!pipeline_state_) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: Fallo al crear pipeline state: %s",
[[error localizedDescription] UTF8String]);
return false;
}
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Shaders Metal compilados correctamente");
return true;
}
bool MetalShader::createBuffers() {
// Crear quad de pantalla completa (igual que en OpenGL)
// Formato: x, y, u, v
float vertices[] = {
-1.0f, -1.0f, 0.0f, 0.0f, // Inferior izquierda
1.0f, -1.0f, 1.0f, 0.0f, // Inferior derecha
1.0f, 1.0f, 1.0f, 1.0f, // Superior derecha
-1.0f, 1.0f, 0.0f, 1.0f // Superior izquierda
};
uint16_t indices[] = {
0, 1, 2, // Primer triángulo
2, 3, 0 // Segundo triángulo
};
// Crear vertex buffer
vertex_buffer_ = [device_ newBufferWithBytes:vertices
length:sizeof(vertices)
options:MTLResourceStorageModeShared];
if (!vertex_buffer_) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: Fallo al crear vertex buffer");
return false;
}
// Crear index buffer
index_buffer_ = [device_ newBufferWithBytes:indices
length:sizeof(indices)
options:MTLResourceStorageModeShared];
if (!index_buffer_) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: Fallo al crear index buffer");
return false;
}
// Crear uniforms buffer
uniforms_buffer_ = [device_ newBufferWithLength:sizeof(Uniforms)
options:MTLResourceStorageModeShared];
if (!uniforms_buffer_) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: Fallo al crear uniforms buffer");
return false;
}
return true;
}
bool MetalShader::init(SDL_Window* window,
SDL_Texture* texture,
const std::string& vertex_source,
const std::string& fragment_source) {
window_ = window;
back_buffer_ = texture;
renderer_ = SDL_GetRenderer(window);
if (!renderer_) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: No se pudo obtener el renderer");
return false;
}
// Verificar que es Metal renderer
const char* renderer_name = SDL_GetRendererName(renderer_);
if (!renderer_name || strncmp(renderer_name, "metal", 5) != 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Renderer no es Metal: %s", renderer_name ? renderer_name : "unknown");
return false;
}
// Obtener tamaños
SDL_GetWindowSize(window_, &window_width_, &window_height_);
SDL_GetTextureSize(back_buffer_, &texture_width_, &texture_height_);
// Crear Metal device
if (!createMetalDevice()) {
return false;
}
// Compilar shaders
if (!compileShaders(vertex_source, fragment_source)) {
return false;
}
// Crear buffers
if (!createBuffers()) {
return false;
}
// Inicializar uniforms
uniforms_.textureSize[0] = texture_width_;
uniforms_.textureSize[1] = texture_height_;
// Copiar uniforms al buffer
memcpy([uniforms_buffer_ contents], &uniforms_, sizeof(Uniforms));
// Obtener Metal layer de SDL
metal_layer_ = (__bridge CAMetalLayer*)SDL_GetProperty(
SDL_GetWindowProperties(window_),
SDL_PROP_WINDOW_METAL_LAYER_POINTER,
nullptr
);
if (!metal_layer_) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: No se pudo obtener Metal layer");
return false;
}
metal_layer_.device = device_;
metal_layer_.pixelFormat = MTLPixelFormatBGRA8Unorm;
is_initialized_ = true;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"** Metal Shader Backend inicializado correctamente");
return true;
}
void MetalShader::render() {
if (!is_initialized_ || !pipeline_state_) {
// Fallback a renderizado normal
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
SDL_SetRenderTarget(renderer_, nullptr);
SDL_RenderClear(renderer_);
SDL_RenderTexture(renderer_, back_buffer_, nullptr, nullptr);
SDL_RenderPresent(renderer_);
return;
}
// TODO: Implementar render loop de Metal
// 1. Obtener drawable del layer
// 2. Crear command buffer
// 3. Crear render pass descriptor
// 4. Encodear comandos de dibujo
// 5. Commit y presentar
// NOTA: Esta es la parte más compleja y requiere más trabajo
// Por ahora dejamos un stub para que compile
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Metal render() no implementado completamente todavía");
}
void MetalShader::setTextureSize(float width, float height) {
if (!is_initialized_) {
return;
}
texture_width_ = width;
texture_height_ = height;
// Actualizar uniforms
uniforms_.textureSize[0] = width;
uniforms_.textureSize[1] = height;
memcpy([uniforms_buffer_ contents], &uniforms_, sizeof(Uniforms));
}
void MetalShader::cleanup() {
// En Objective-C++ con ARC, no necesitamos release manual
// pero limpiamos las referencias
pipeline_state_ = nil;
command_queue_ = nil;
vertex_buffer_ = nil;
index_buffer_ = nil;
uniforms_buffer_ = nil;
device_ = nil;
metal_layer_ = nil;
is_initialized_ = false;
}
id<MTLTexture> MetalShader::getMetalTexture(SDL_Texture* texture) {
// TODO: Obtener la textura Metal desde SDL_Texture
// Esto requiere acceder a las properties de SDL3
return nil;
}
} // namespace Rendering
#endif // __APPLE__

View File

@@ -0,0 +1,455 @@
#include "opengl_shader.h"
#include <SDL3/SDL.h>
#include <cstring>
#include <stdexcept>
#include <vector>
namespace Rendering {
OpenGLShader::~OpenGLShader() {
cleanup();
}
#ifndef __APPLE__
bool OpenGLShader::initGLExtensions() {
glCreateShader = (PFNGLCREATESHADERPROC)SDL_GL_GetProcAddress("glCreateShader");
glShaderSource = (PFNGLSHADERSOURCEPROC)SDL_GL_GetProcAddress("glShaderSource");
glCompileShader = (PFNGLCOMPILESHADERPROC)SDL_GL_GetProcAddress("glCompileShader");
glGetShaderiv = (PFNGLGETSHADERIVPROC)SDL_GL_GetProcAddress("glGetShaderiv");
glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)SDL_GL_GetProcAddress("glGetShaderInfoLog");
glDeleteShader = (PFNGLDELETESHADERPROC)SDL_GL_GetProcAddress("glDeleteShader");
glAttachShader = (PFNGLATTACHSHADERPROC)SDL_GL_GetProcAddress("glAttachShader");
glCreateProgram = (PFNGLCREATEPROGRAMPROC)SDL_GL_GetProcAddress("glCreateProgram");
glLinkProgram = (PFNGLLINKPROGRAMPROC)SDL_GL_GetProcAddress("glLinkProgram");
glValidateProgram = (PFNGLVALIDATEPROGRAMPROC)SDL_GL_GetProcAddress("glValidateProgram");
glGetProgramiv = (PFNGLGETPROGRAMIVPROC)SDL_GL_GetProcAddress("glGetProgramiv");
glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)SDL_GL_GetProcAddress("glGetProgramInfoLog");
glUseProgram = (PFNGLUSEPROGRAMPROC)SDL_GL_GetProcAddress("glUseProgram");
glDeleteProgram = (PFNGLDELETEPROGRAMPROC)SDL_GL_GetProcAddress("glDeleteProgram");
glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)SDL_GL_GetProcAddress("glGetUniformLocation");
glUniform2f = (PFNGLUNIFORM2FPROC)SDL_GL_GetProcAddress("glUniform2f");
glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)SDL_GL_GetProcAddress("glGenVertexArrays");
glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)SDL_GL_GetProcAddress("glBindVertexArray");
glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC)SDL_GL_GetProcAddress("glDeleteVertexArrays");
glGenBuffers = (PFNGLGENBUFFERSPROC)SDL_GL_GetProcAddress("glGenBuffers");
glBindBuffer = (PFNGLBINDBUFFERPROC)SDL_GL_GetProcAddress("glBindBuffer");
glBufferData = (PFNGLBUFFERDATAPROC)SDL_GL_GetProcAddress("glBufferData");
glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)SDL_GL_GetProcAddress("glDeleteBuffers");
glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)SDL_GL_GetProcAddress("glVertexAttribPointer");
glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)SDL_GL_GetProcAddress("glEnableVertexAttribArray");
return glCreateShader && glShaderSource && glCompileShader && glGetShaderiv &&
glGetShaderInfoLog && glDeleteShader && glAttachShader && glCreateProgram &&
glLinkProgram && glValidateProgram && glGetProgramiv && glGetProgramInfoLog &&
glUseProgram && glDeleteProgram && glGetUniformLocation && glUniform2f &&
glGenVertexArrays && glBindVertexArray && glDeleteVertexArrays &&
glGenBuffers && glBindBuffer && glBufferData && glDeleteBuffers &&
glVertexAttribPointer && glEnableVertexAttribArray;
}
#endif
void OpenGLShader::checkGLError(const char* operation) {
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error OpenGL en %s: 0x%x", operation, error);
}
}
GLuint OpenGLShader::compileShader(const std::string& source, GLenum shader_type) {
if (source.empty()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"ERROR: El código fuente del shader está vacío");
return 0;
}
GLuint shader_id = glCreateShader(shader_type);
if (shader_id == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al crear shader");
checkGLError("glCreateShader");
return 0;
}
const char* sources[1] = {source.c_str()};
glShaderSource(shader_id, 1, sources, nullptr);
checkGLError("glShaderSource");
glCompileShader(shader_id);
checkGLError("glCompileShader");
// Verificar compilación
GLint compiled = GL_FALSE;
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &compiled);
if (compiled != GL_TRUE) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error en compilación del shader");
GLint log_length;
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 0) {
std::vector<char> log(log_length);
glGetShaderInfoLog(shader_id, log_length, &log_length, log.data());
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Log de compilación: %s", log.data());
}
glDeleteShader(shader_id);
return 0;
}
return shader_id;
}
GLuint OpenGLShader::linkProgram(GLuint vertex_shader, GLuint fragment_shader) {
GLuint program = glCreateProgram();
if (program == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error al crear programa de shaders");
return 0;
}
glAttachShader(program, vertex_shader);
checkGLError("glAttachShader(vertex)");
glAttachShader(program, fragment_shader);
checkGLError("glAttachShader(fragment)");
glLinkProgram(program);
checkGLError("glLinkProgram");
// Verificar enlace
GLint linked = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (linked != GL_TRUE) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error al enlazar programa");
GLint log_length;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 0) {
std::vector<char> log(log_length);
glGetProgramInfoLog(program, log_length, &log_length, log.data());
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Log de enlace: %s", log.data());
}
glDeleteProgram(program);
return 0;
}
glValidateProgram(program);
checkGLError("glValidateProgram");
return program;
}
void OpenGLShader::createQuadGeometry() {
// Datos del quad: posición (x, y) + coordenadas de textura (u, v)
// Formato: x, y, u, v
float vertices[] = {
// Posición // TexCoords
-1.0f, -1.0f, 0.0f, 0.0f, // Inferior izquierda
1.0f, -1.0f, 1.0f, 0.0f, // Inferior derecha
1.0f, 1.0f, 1.0f, 1.0f, // Superior derecha
-1.0f, 1.0f, 0.0f, 1.0f // Superior izquierda
};
// Índices para dibujar el quad con dos triángulos
unsigned int indices[] = {
0, 1, 2, // Primer triángulo
2, 3, 0 // Segundo triángulo
};
// Generar y configurar VAO
glGenVertexArrays(1, &vao_);
glBindVertexArray(vao_);
checkGLError("glBindVertexArray");
// Generar y configurar VBO
glGenBuffers(1, &vbo_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
checkGLError("glBufferData(VBO)");
// Generar y configurar EBO
glGenBuffers(1, &ebo_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
checkGLError("glBufferData(EBO)");
// Atributo 0: Posición (2 floats)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
checkGLError("glVertexAttribPointer(position)");
// Atributo 1: Coordenadas de textura (2 floats)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
checkGLError("glVertexAttribPointer(texcoord)");
// Desvincular
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
GLuint OpenGLShader::getTextureID(SDL_Texture* texture) {
if (!texture) return 1;
SDL_PropertiesID props = SDL_GetTextureProperties(texture);
GLuint texture_id = 0;
// Intentar obtener ID de textura OpenGL
texture_id = (GLuint)(uintptr_t)SDL_GetPointerProperty(props, "SDL.texture.opengl.texture", nullptr);
if (texture_id == 0) {
texture_id = (GLuint)(uintptr_t)SDL_GetPointerProperty(props, "texture.opengl.texture", nullptr);
}
if (texture_id == 0) {
texture_id = (GLuint)SDL_GetNumberProperty(props, "SDL.texture.opengl.texture", 1);
}
if (texture_id == 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"No se pudo obtener ID de textura OpenGL, usando 1 por defecto");
texture_id = 1;
}
return texture_id;
}
bool OpenGLShader::init(SDL_Window* window,
SDL_Texture* texture,
const std::string& vertex_source,
const std::string& fragment_source) {
window_ = window;
back_buffer_ = texture;
renderer_ = SDL_GetRenderer(window);
if (!renderer_) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error: No se pudo obtener el renderer");
return false;
}
// Obtener tamaños
SDL_GetWindowSize(window_, &window_width_, &window_height_);
SDL_GetTextureSize(back_buffer_, &texture_width_, &texture_height_);
// Verificar que es OpenGL
const char* renderer_name = SDL_GetRendererName(renderer_);
if (!renderer_name || strncmp(renderer_name, "opengl", 6) != 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Renderer no es OpenGL: %s", renderer_name ? renderer_name : "unknown");
return false;
}
#ifndef __APPLE__
// Inicializar extensiones OpenGL en Windows/Linux
if (!initGLExtensions()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error al inicializar extensiones OpenGL");
return false;
}
#endif
// Limpiar shader anterior si existe
if (program_id_ != 0) {
glDeleteProgram(program_id_);
program_id_ = 0;
}
// Compilar shaders
GLuint vertex_shader = compileShader(vertex_source, GL_VERTEX_SHADER);
GLuint fragment_shader = compileShader(fragment_source, GL_FRAGMENT_SHADER);
if (vertex_shader == 0 || fragment_shader == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error al compilar shaders");
if (vertex_shader != 0) glDeleteShader(vertex_shader);
if (fragment_shader != 0) glDeleteShader(fragment_shader);
return false;
}
// Enlazar programa
program_id_ = linkProgram(vertex_shader, fragment_shader);
// Limpiar shaders (ya no necesarios tras el enlace)
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
if (program_id_ == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error al crear programa de shaders");
return false;
}
// Crear geometría del quad
createQuadGeometry();
// Obtener ubicación del uniform TextureSize
glUseProgram(program_id_);
texture_size_location_ = glGetUniformLocation(program_id_, "TextureSize");
if (texture_size_location_ != -1) {
glUniform2f(texture_size_location_, texture_width_, texture_height_);
checkGLError("glUniform2f(TextureSize)");
} else {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Uniform 'TextureSize' no encontrado en shader");
}
glUseProgram(0);
is_initialized_ = true;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"** OpenGL 3.3 Shader Backend inicializado correctamente");
return true;
}
void OpenGLShader::render() {
if (!is_initialized_ || program_id_ == 0) {
// Fallback: renderizado SDL normal
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
SDL_SetRenderTarget(renderer_, nullptr);
SDL_RenderClear(renderer_);
SDL_RenderTexture(renderer_, back_buffer_, nullptr, nullptr);
SDL_RenderPresent(renderer_);
return;
}
// Obtener tamaño actual de ventana (puede haber cambiado)
int current_width, current_height;
SDL_GetWindowSize(window_, &current_width, &current_height);
// Guardar estados OpenGL
GLint old_program;
glGetIntegerv(GL_CURRENT_PROGRAM, &old_program);
GLint old_viewport[4];
glGetIntegerv(GL_VIEWPORT, old_viewport);
GLboolean was_texture_enabled = glIsEnabled(GL_TEXTURE_2D);
GLint old_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture);
GLint old_vao;
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &old_vao);
// Preparar renderizado
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
SDL_SetRenderTarget(renderer_, nullptr);
SDL_RenderClear(renderer_);
// Obtener y bindear textura
GLuint texture_id = getTextureID(back_buffer_);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_id);
checkGLError("glBindTexture");
// Usar nuestro programa
glUseProgram(program_id_);
checkGLError("glUseProgram");
// Configurar viewport (obtener tamaño lógico de SDL)
int logical_w, logical_h;
SDL_RendererLogicalPresentation mode;
SDL_GetRenderLogicalPresentation(renderer_, &logical_w, &logical_h, &mode);
if (logical_w == 0 || logical_h == 0) {
logical_w = current_width;
logical_h = current_height;
}
// Calcular viewport considerando aspect ratio
int viewport_x = 0, viewport_y = 0;
int viewport_w = current_width, viewport_h = current_height;
if (mode == SDL_LOGICAL_PRESENTATION_INTEGER_SCALE) {
int scale_x = current_width / logical_w;
int scale_y = current_height / logical_h;
int scale = (scale_x < scale_y) ? scale_x : scale_y;
if (scale < 1) scale = 1;
viewport_w = logical_w * scale;
viewport_h = logical_h * scale;
viewport_x = (current_width - viewport_w) / 2;
viewport_y = (current_height - viewport_h) / 2;
} else {
float window_aspect = static_cast<float>(current_width) / current_height;
float logical_aspect = static_cast<float>(logical_w) / logical_h;
if (window_aspect > logical_aspect) {
viewport_w = static_cast<int>(logical_aspect * current_height);
viewport_x = (current_width - viewport_w) / 2;
} else {
viewport_h = static_cast<int>(current_width / logical_aspect);
viewport_y = (current_height - viewport_h) / 2;
}
}
glViewport(viewport_x, viewport_y, viewport_w, viewport_h);
checkGLError("glViewport");
// Dibujar quad usando VAO
glBindVertexArray(vao_);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
checkGLError("glDrawElements");
// Presentar
SDL_GL_SwapWindow(window_);
// Restaurar estados OpenGL
glUseProgram(old_program);
glBindTexture(GL_TEXTURE_2D, old_texture);
if (!was_texture_enabled) {
glDisable(GL_TEXTURE_2D);
}
glBindVertexArray(old_vao);
glViewport(old_viewport[0], old_viewport[1], old_viewport[2], old_viewport[3]);
}
void OpenGLShader::setTextureSize(float width, float height) {
if (!is_initialized_ || program_id_ == 0) {
return;
}
texture_width_ = width;
texture_height_ = height;
GLint old_program;
glGetIntegerv(GL_CURRENT_PROGRAM, &old_program);
glUseProgram(program_id_);
if (texture_size_location_ != -1) {
glUniform2f(texture_size_location_, width, height);
checkGLError("glUniform2f(TextureSize)");
}
glUseProgram(old_program);
}
void OpenGLShader::cleanup() {
if (vao_ != 0) {
glDeleteVertexArrays(1, &vao_);
vao_ = 0;
}
if (vbo_ != 0) {
glDeleteBuffers(1, &vbo_);
vbo_ = 0;
}
if (ebo_ != 0) {
glDeleteBuffers(1, &ebo_);
ebo_ = 0;
}
if (program_id_ != 0) {
glDeleteProgram(program_id_);
program_id_ = 0;
}
is_initialized_ = false;
window_ = nullptr;
renderer_ = nullptr;
back_buffer_ = nullptr;
}
} // namespace Rendering

View File

@@ -0,0 +1,98 @@
#pragma once
#include "../shader_backend.h"
#ifdef __APPLE__
#include <OpenGL/gl3.h>
#else
#include <SDL3/SDL_opengl.h>
#endif
namespace Rendering {
/**
* @brief Backend de shaders usando OpenGL 3.3 Core Profile
*
* Implementa el renderizado de shaders usando APIs modernas de OpenGL:
* - VAO (Vertex Array Objects)
* - VBO (Vertex Buffer Objects)
* - Shaders GLSL #version 330 core
*/
class OpenGLShader : public ShaderBackend {
public:
OpenGLShader() = default;
~OpenGLShader() override;
bool init(SDL_Window* window,
SDL_Texture* texture,
const std::string& vertex_source,
const std::string& fragment_source) override;
void render() override;
void setTextureSize(float width, float height) override;
void cleanup() override;
bool isHardwareAccelerated() const override { return is_initialized_; }
private:
// Funciones auxiliares
bool initGLExtensions();
GLuint compileShader(const std::string& source, GLenum shader_type);
GLuint linkProgram(GLuint vertex_shader, GLuint fragment_shader);
void createQuadGeometry();
GLuint getTextureID(SDL_Texture* texture);
void checkGLError(const char* operation);
// Estado SDL
SDL_Window* window_ = nullptr;
SDL_Renderer* renderer_ = nullptr;
SDL_Texture* back_buffer_ = nullptr;
// Estado OpenGL
GLuint program_id_ = 0;
GLuint vao_ = 0; // Vertex Array Object
GLuint vbo_ = 0; // Vertex Buffer Object
GLuint ebo_ = 0; // Element Buffer Object
// Ubicaciones de uniforms
GLint texture_size_location_ = -1;
// Tamaños
int window_width_ = 0;
int window_height_ = 0;
float texture_width_ = 0.0f;
float texture_height_ = 0.0f;
// Estado
bool is_initialized_ = false;
#ifndef __APPLE__
// Punteros a funciones OpenGL en Windows/Linux
PFNGLCREATESHADERPROC glCreateShader = nullptr;
PFNGLSHADERSOURCEPROC glShaderSource = nullptr;
PFNGLCOMPILESHADERPROC glCompileShader = nullptr;
PFNGLGETSHADERIVPROC glGetShaderiv = nullptr;
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog = nullptr;
PFNGLDELETESHADERPROC glDeleteShader = nullptr;
PFNGLATTACHSHADERPROC glAttachShader = nullptr;
PFNGLCREATEPROGRAMPROC glCreateProgram = nullptr;
PFNGLLINKPROGRAMPROC glLinkProgram = nullptr;
PFNGLVALIDATEPROGRAMPROC glValidateProgram = nullptr;
PFNGLGETPROGRAMIVPROC glGetProgramiv = nullptr;
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog = nullptr;
PFNGLUSEPROGRAMPROC glUseProgram = nullptr;
PFNGLDELETEPROGRAMPROC glDeleteProgram = nullptr;
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = nullptr;
PFNGLUNIFORM2FPROC glUniform2f = nullptr;
PFNGLGENVERTEXARRAYSPROC glGenVertexArrays = nullptr;
PFNGLBINDVERTEXARRAYPROC glBindVertexArray = nullptr;
PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays = nullptr;
PFNGLGENBUFFERSPROC glGenBuffers = nullptr;
PFNGLBINDBUFFERPROC glBindBuffer = nullptr;
PFNGLBUFFERDATAPROC glBufferData = nullptr;
PFNGLDELETEBUFFERSPROC glDeleteBuffers = nullptr;
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer = nullptr;
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray = nullptr;
#endif
};
} // namespace Rendering

View File

@@ -0,0 +1,55 @@
#pragma once
#include <SDL3/SDL.h>
#include <string>
namespace Rendering {
/**
* @brief Interfaz abstracta para backends de renderizado con shaders
*
* Esta interfaz define el contrato que todos los backends de shaders
* deben cumplir (OpenGL, Metal, Vulkan, etc.)
*/
class ShaderBackend {
public:
virtual ~ShaderBackend() = default;
/**
* @brief Inicializa el backend de shaders
* @param window Ventana SDL
* @param texture Textura de backbuffer a la que aplicar shaders
* @param vertex_source Código fuente del vertex shader
* @param fragment_source Código fuente del fragment shader
* @return true si la inicialización fue exitosa
*/
virtual bool init(SDL_Window* window,
SDL_Texture* texture,
const std::string& vertex_source,
const std::string& fragment_source) = 0;
/**
* @brief Renderiza la textura con los shaders aplicados
*/
virtual void render() = 0;
/**
* @brief Establece el tamaño de la textura como parámetro del shader
* @param width Ancho de la textura
* @param height Alto de la textura
*/
virtual void setTextureSize(float width, float height) = 0;
/**
* @brief Limpia y libera recursos del backend
*/
virtual void cleanup() = 0;
/**
* @brief Verifica si el backend está usando aceleración por hardware
* @return true si usa aceleración (OpenGL/Metal/Vulkan)
*/
virtual bool isHardwareAccelerated() const = 0;
};
} // namespace Rendering

View File

@@ -10,23 +10,22 @@
#include <stdexcept> // Para runtime_error
#include <utility> // Para move
#include "asset.h" // Para Asset
#include "color.h" // Para Color
#ifndef NO_AUDIO
#include "asset.h" // Para Asset
#include "color.h" // Para Color
#include "external/jail_audio.h" // Para JA_LoadMusic, JA_LoadSound, JA_DeleteMusic, JA_DeleteSound
#endif
#include "lang.h" // Para getText
#include "param.h" // Para Param, param, ParamResource, ParamGame
#include "resource_helper.h" // Para ResourceHelper
#include "screen.h" // Para Screen
#include "text.h" // Para Text
#include "lang.h" // Para getText
#include "param.h" // Para Param, param, ParamResource, ParamGame
#include "resource_helper.h" // Para ResourceHelper
#include "screen.h" // Para Screen
#include "text.h" // Para Text
#include "version.h" // Para Version::APP_NAME y Version::GIT_HASH
struct JA_Music_t; // lines 11-11
struct JA_Sound_t; // lines 12-12
// Helper para cargar archivos de audio desde pack o filesystem
namespace {
std::string createTempAudioFile(const std::string &file_path, std::vector<std::string> &temp_files_tracker) {
std::string createTempAudioFile(const std::string& file_path, std::vector<std::string>& temp_files_tracker) {
auto resource_data = ResourceHelper::loadFile(file_path);
if (!resource_data.empty()) {
// Crear archivo temporal
@@ -42,7 +41,7 @@ std::string createTempAudioFile(const std::string &file_path, std::vector<std::s
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Cannot create temp file %s", temp_path.c_str());
return file_path;
}
temp_file.write(reinterpret_cast<const char *>(resource_data.data()), resource_data.size());
temp_file.write(reinterpret_cast<const char*>(resource_data.data()), resource_data.size());
temp_file.close();
// Agregar a la lista de archivos temporales para limpieza posterior
@@ -57,7 +56,7 @@ std::string createTempAudioFile(const std::string &file_path, std::vector<std::s
// Declaraciones de funciones que necesitas implementar en otros archivos
// Singleton
Resource *Resource::instance = nullptr;
Resource* Resource::instance = nullptr;
// Inicializa la instancia única del singleton con modo de carga
void Resource::init(LoadingMode mode) {
@@ -71,7 +70,7 @@ void Resource::destroy() {
}
// Obtiene la instancia
auto Resource::get() -> Resource * { return Resource::instance; }
auto Resource::get() -> Resource* { return Resource::instance; }
// Constructor con modo de carga
Resource::Resource(LoadingMode mode)
@@ -112,10 +111,10 @@ void Resource::loadTextFilesQuiet() {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXT FILES (quiet load)");
auto list = Asset::get()->getListByType(Asset::Type::FONT);
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
// Buscar en nuestra lista y cargar directamente
auto it = std::ranges::find_if(text_files_, [&name](const auto &t) { return t.name == name; });
auto it = std::ranges::find_if(text_files_, [&name](const auto& t) { return t.name == name; });
if (it != text_files_.end()) {
it->text_file = Text::loadFile(l);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Text file loaded: %s", name.c_str());
@@ -141,12 +140,12 @@ void Resource::loadEssentialTextures() {
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
for (const auto &file : texture_list) {
for (const auto& file : texture_list) {
auto name = getFileName(file);
// Solo cargar texturas esenciales
if (std::ranges::find(ESSENTIAL_TEXTURES, name) != ESSENTIAL_TEXTURES.end()) {
// Buscar en nuestra lista y cargar
auto it = std::ranges::find_if(textures_, [&name](const auto &t) { return t.name == name; });
auto it = std::ranges::find_if(textures_, [&name](const auto& t) { return t.name == name; });
if (it != textures_.end()) {
it->texture = std::make_shared<Texture>(Screen::get()->getRenderer(), file);
}
@@ -161,35 +160,35 @@ void Resource::initResourceLists() {
// Inicializa lista de sonidos
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
sounds_.clear();
for (const auto &file : sound_list) {
for (const auto& file : sound_list) {
sounds_.emplace_back(getFileName(file));
}
// Inicializa lista de músicas
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
musics_.clear();
for (const auto &file : music_list) {
for (const auto& file : music_list) {
musics_.emplace_back(getFileName(file));
}
// Inicializa lista de texturas
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
textures_.clear();
for (const auto &file : texture_list) {
for (const auto& file : texture_list) {
textures_.emplace_back(getFileName(file));
}
// Inicializa lista de ficheros de texto
auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
text_files_.clear();
for (const auto &file : text_file_list) {
for (const auto& file : text_file_list) {
text_files_.emplace_back(getFileName(file));
}
// Inicializa lista de animaciones
auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
animations_.clear();
for (const auto &file : animation_list) {
for (const auto& file : animation_list) {
animations_.emplace_back(getFileName(file));
}
@@ -212,7 +211,7 @@ void Resource::initResourceLists() {
"smb2_grad"};
texts_.clear();
for (const auto &text_name : TEXT_OBJECTS) {
for (const auto& text_name : TEXT_OBJECTS) {
texts_.emplace_back(text_name); // Constructor con nullptr por defecto
}
@@ -220,8 +219,8 @@ void Resource::initResourceLists() {
}
// Obtiene el sonido a partir de un nombre (con carga perezosa)
auto Resource::getSound(const std::string &name) -> JA_Sound_t * {
auto it = std::ranges::find_if(sounds_, [&name](const auto &s) { return s.name == name; });
auto Resource::getSound(const std::string& name) -> JA_Sound_t* {
auto it = std::ranges::find_if(sounds_, [&name](const auto& s) { return s.name == name; });
if (it != sounds_.end()) {
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
@@ -236,8 +235,8 @@ auto Resource::getSound(const std::string &name) -> JA_Sound_t * {
}
// Obtiene la música a partir de un nombre (con carga perezosa)
auto Resource::getMusic(const std::string &name) -> JA_Music_t * {
auto it = std::ranges::find_if(musics_, [&name](const auto &m) { return m.name == name; });
auto Resource::getMusic(const std::string& name) -> JA_Music_t* {
auto it = std::ranges::find_if(musics_, [&name](const auto& m) { return m.name == name; });
if (it != musics_.end()) {
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
@@ -252,8 +251,8 @@ auto Resource::getMusic(const std::string &name) -> JA_Music_t * {
}
// Obtiene la textura a partir de un nombre (con carga perezosa)
auto Resource::getTexture(const std::string &name) -> std::shared_ptr<Texture> {
auto it = std::ranges::find_if(textures_, [&name](const auto &t) { return t.name == name; });
auto Resource::getTexture(const std::string& name) -> std::shared_ptr<Texture> {
auto it = std::ranges::find_if(textures_, [&name](const auto& t) { return t.name == name; });
if (it != textures_.end()) {
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
@@ -268,8 +267,8 @@ auto Resource::getTexture(const std::string &name) -> std::shared_ptr<Texture> {
}
// Obtiene el fichero de texto a partir de un nombre (con carga perezosa)
auto Resource::getTextFile(const std::string &name) -> std::shared_ptr<Text::File> {
auto it = std::ranges::find_if(text_files_, [&name](const auto &t) { return t.name == name; });
auto Resource::getTextFile(const std::string& name) -> std::shared_ptr<Text::File> {
auto it = std::ranges::find_if(text_files_, [&name](const auto& t) { return t.name == name; });
if (it != text_files_.end()) {
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
@@ -284,8 +283,8 @@ auto Resource::getTextFile(const std::string &name) -> std::shared_ptr<Text::Fil
}
// Obtiene el objeto de texto a partir de un nombre (con carga perezosa)
auto Resource::getText(const std::string &name) -> std::shared_ptr<Text> {
auto it = std::ranges::find_if(texts_, [&name](const auto &t) { return t.name == name; });
auto Resource::getText(const std::string& name) -> std::shared_ptr<Text> {
auto it = std::ranges::find_if(texts_, [&name](const auto& t) { return t.name == name; });
if (it != texts_.end()) {
// Si está en modo lazy y no se ha cargado aún, lo carga ahora
@@ -300,8 +299,8 @@ auto Resource::getText(const std::string &name) -> std::shared_ptr<Text> {
}
// Obtiene la animación a partir de un nombre (con carga perezosa)
auto Resource::getAnimation(const std::string &name) -> AnimationsFileBuffer & {
auto it = std::ranges::find_if(animations_, [&name](const auto &a) { return a.name == name; });
auto Resource::getAnimation(const std::string& name) -> AnimationsFileBuffer& {
auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; });
if (it != animations_.end()) {
// Si está en modo lazy y no se ha cargado aún (vector vacío), lo carga ahora
@@ -316,44 +315,45 @@ auto Resource::getAnimation(const std::string &name) -> AnimationsFileBuffer & {
}
// Obtiene el fichero con los datos para el modo demostración a partir de un índice
auto Resource::getDemoData(int index) -> DemoData & {
auto Resource::getDemoData(int index) -> DemoData& {
if (index < 0 || index >= static_cast<int>(demos_.size())) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index %d out of range for demo data (size: %d)", index, static_cast<int>(demos_.size()));
static DemoData empty_demo;
return empty_demo;
}
return demos_.at(index);
}
// --- Métodos de carga perezosa ---
auto Resource::loadSoundLazy(const std::string &name) -> JA_Sound_t * {
auto Resource::loadSoundLazy(const std::string& name) -> JA_Sound_t* {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading sound lazily: %s", name.c_str());
#ifndef NO_AUDIO
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
for (const auto &file : sound_list) {
for (const auto& file : sound_list) {
if (getFileName(file) == name) {
std::string audio_path = createTempAudioFile(file, Resource::get()->temp_audio_files_);
return JA_LoadSound(audio_path.c_str());
}
}
#endif
return nullptr;
}
auto Resource::loadMusicLazy(const std::string &name) -> JA_Music_t * {
auto Resource::loadMusicLazy(const std::string& name) -> JA_Music_t* {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading music lazily: %s", name.c_str());
#ifndef NO_AUDIO
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
for (const auto &file : music_list) {
for (const auto& file : music_list) {
if (getFileName(file) == name) {
std::string audio_path = createTempAudioFile(file, Resource::get()->temp_audio_files_);
return JA_LoadMusic(audio_path.c_str());
}
}
#endif
return nullptr;
}
auto Resource::loadTextureLazy(const std::string &name) -> std::shared_ptr<Texture> {
auto Resource::loadTextureLazy(const std::string& name) -> std::shared_ptr<Texture> {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading texture lazily: %s", name.c_str());
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
for (const auto &file : texture_list) {
for (const auto& file : texture_list) {
if (getFileName(file) == name) {
return std::make_shared<Texture>(Screen::get()->getRenderer(), file);
}
@@ -361,10 +361,10 @@ auto Resource::loadTextureLazy(const std::string &name) -> std::shared_ptr<Textu
return nullptr;
}
auto Resource::loadTextFileLazy(const std::string &name) -> std::shared_ptr<Text::File> {
auto Resource::loadTextFileLazy(const std::string& name) -> std::shared_ptr<Text::File> {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading text file lazily: %s", name.c_str());
auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
for (const auto &file : text_file_list) {
for (const auto& file : text_file_list) {
if (getFileName(file) == name) {
return Text::loadFile(file);
}
@@ -372,7 +372,7 @@ auto Resource::loadTextFileLazy(const std::string &name) -> std::shared_ptr<Text
return nullptr;
}
auto Resource::loadTextLazy(const std::string &name) -> std::shared_ptr<Text> {
auto Resource::loadTextLazy(const std::string& name) -> std::shared_ptr<Text> {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading text object lazily: %s", name.c_str());
// Mapeo de objetos de texto a sus recursos
@@ -396,7 +396,7 @@ auto Resource::loadTextLazy(const std::string &name) -> std::shared_ptr<Text> {
{.key = "smb2", .texture_file = "smb2.png", .text_file = "smb2.txt"},
{.key = "smb2_grad", .texture_file = "smb2_grad.png", .text_file = "smb2.txt"}};
for (const auto &mapping : TEXT_MAPPINGS) {
for (const auto& mapping : TEXT_MAPPINGS) {
if (mapping.key == name) {
// Cargar las dependencias automáticamente
auto texture = getTexture(mapping.texture_file); // Esto cargará la textura si no está cargada
@@ -411,10 +411,10 @@ auto Resource::loadTextLazy(const std::string &name) -> std::shared_ptr<Text> {
return nullptr;
}
auto Resource::loadAnimationLazy(const std::string &name) -> AnimationsFileBuffer {
auto Resource::loadAnimationLazy(const std::string& name) -> AnimationsFileBuffer {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading animation lazily: %s", name.c_str());
auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
for (const auto &file : animation_list) {
for (const auto& file : animation_list) {
if (getFileName(file) == name) {
return loadAnimationsFromFile(file);
}
@@ -425,10 +425,8 @@ auto Resource::loadAnimationLazy(const std::string &name) -> AnimationsFileBuffe
// Vacia todos los vectores de recursos
void Resource::clear() {
#ifndef NO_AUDIO
clearSounds();
clearMusics();
#endif
textures_.clear();
text_files_.clear();
texts_.clear();
@@ -443,15 +441,13 @@ void Resource::load() {
initProgressBar();
// Muerstra la ventana y desactiva el sincronismo vertical
auto *screen = Screen::get();
auto* screen = Screen::get();
auto vsync = Screen::getVSync();
screen->setVSync(false);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** LOADING RESOURCES");
#ifndef NO_AUDIO
loadSounds(); // Carga sonidos
loadMusics(); // Carga músicas
#endif
loadSounds(); // Carga sonidos
loadMusics(); // Carga músicas
loadTextures(); // Carga texturas
loadTextFiles(); // Carga ficheros de texto
loadAnimations(); // Carga animaciones
@@ -481,15 +477,11 @@ void Resource::loadSounds() {
auto list = Asset::get()->getListByType(Asset::Type::SOUND);
sounds_.clear();
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
#ifndef NO_AUDIO
std::string audio_path = createTempAudioFile(l, temp_audio_files_);
sounds_.emplace_back(name, JA_LoadSound(audio_path.c_str()));
#else
sounds_.emplace_back(name, nullptr);
#endif
printWithDots("Sound : ", name, "[ LOADED ]");
}
}
@@ -500,15 +492,11 @@ void Resource::loadMusics() {
auto list = Asset::get()->getListByType(Asset::Type::MUSIC);
musics_.clear();
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
#ifndef NO_AUDIO
std::string audio_path = createTempAudioFile(l, temp_audio_files_);
musics_.emplace_back(name, JA_LoadMusic(audio_path.c_str()));
#else
musics_.emplace_back(name, nullptr);
#endif
printWithDots("Music : ", name, "[ LOADED ]");
}
}
@@ -519,7 +507,7 @@ void Resource::loadTextures() {
auto list = Asset::get()->getListByType(Asset::Type::BITMAP);
textures_.clear();
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
textures_.emplace_back(name, std::make_shared<Texture>(Screen::get()->getRenderer(), l));
@@ -532,7 +520,7 @@ void Resource::loadTextFiles() {
auto list = Asset::get()->getListByType(Asset::Type::FONT);
text_files_.clear();
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
text_files_.emplace_back(name, Text::loadFile(l));
@@ -545,7 +533,7 @@ void Resource::loadAnimations() {
auto list = Asset::get()->getListByType(Asset::Type::ANIMATION);
animations_.clear();
for (const auto &l : list) {
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
animations_.emplace_back(name, loadAnimationsFromFile(l));
@@ -555,12 +543,13 @@ void Resource::loadAnimations() {
// Carga los datos para el modo demostración
void Resource::loadDemoData() {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES");
auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
demos_.clear();
constexpr std::array<const char *, 2> DEMO_FILES = {"demo1.bin", "demo2.bin"};
for (const auto &file : DEMO_FILES) {
updateLoadingProgress(file);
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get(file)));
for (const auto& l : list) {
auto name = getFileName(l);
updateLoadingProgress(name);
demos_.emplace_back(loadDemoDataFromFile(l));
}
}
@@ -581,12 +570,12 @@ void Resource::createPlayerTextures() {
// Bucle principal
for (size_t player_idx = 0; player_idx < players.size(); ++player_idx) {
const auto &player = players[player_idx]; // Obtenemos el jugador actual
const auto& player = players[player_idx]; // Obtenemos el jugador actual
// Encontrar el archivo original de la textura
std::string texture_file_path;
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
for (const auto &file : texture_list) {
for (const auto& file : texture_list) {
if (getFileName(file) == player.base_texture) {
texture_file_path = file;
break;
@@ -667,7 +656,7 @@ void Resource::createTextTextures() {
{"game_text_1000000_points", Lang::getText("[GAME_TEXT] 8")}};
auto text1 = getText("04b_25_enhanced");
for (const auto &s : strings1) {
for (const auto& s : strings1) {
textures_.emplace_back(s.name, text1->writeDXToTexture(Text::STROKE, s.text, -2, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
printWithDots("Texture : ", s.name, "[ DONE ]");
}
@@ -678,10 +667,11 @@ void Resource::createTextTextures() {
{"game_text_get_ready", Lang::getText("[GAME_TEXT] 7")},
{"game_text_last_stage", Lang::getText("[GAME_TEXT] 3")},
{"game_text_congratulations", Lang::getText("[GAME_TEXT] 1")},
{"game_text_new_record", Lang::getText("[GAME_TEXT] NEW_RECORD")},
{"game_text_game_over", "Game Over"}};
auto text2 = getText("04b_25_2x_enhanced");
for (const auto &s : strings2) {
for (const auto& s : strings2) {
textures_.emplace_back(s.name, text2->writeDXToTexture(Text::STROKE, s.text, -4, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
printWithDots("Texture : ", s.name, "[ DONE ]");
}
@@ -721,7 +711,7 @@ void Resource::createText() {
{"smb2", "smb2.png", "smb2.txt"},
{"smb2_grad", "smb2_grad.png", "smb2.txt"}};
for (const auto &resource : resources) {
for (const auto& resource : resources) {
if (!resource.white_texture_file.empty()) {
// Crear texto con textura blanca
texts_.emplace_back(resource.key, std::make_shared<Text>(getTexture(resource.texture_file), getTexture(resource.white_texture_file), getTextFile(resource.text_file)));
@@ -735,11 +725,9 @@ void Resource::createText() {
// Vacía el vector de sonidos y libera la memoria asociada
void Resource::clearSounds() {
for (auto &sound : sounds_) {
for (auto& sound : sounds_) {
if (sound.sound != nullptr) {
#ifndef NO_AUDIO
JA_DeleteSound(sound.sound);
#endif
sound.sound = nullptr;
}
}
@@ -748,11 +736,9 @@ void Resource::clearSounds() {
// Vacía el vector de músicas y libera la memoria asociada
void Resource::clearMusics() {
for (auto &music : musics_) {
for (auto& music : musics_) {
if (music.music != nullptr) {
#ifndef NO_AUDIO
JA_DeleteMusic(music.music);
#endif
music.music = nullptr;
}
}
@@ -770,7 +756,7 @@ void Resource::calculateTotalResources() {
Asset::Type::DEMODATA};
size_t total = 0;
for (const auto &asset_type : ASSET_TYPES) {
for (const auto& asset_type : ASSET_TYPES) {
auto list = Asset::get()->getListByType(asset_type);
total += list.size();
}
@@ -781,8 +767,8 @@ void Resource::calculateTotalResources() {
// Muestra el progreso de carga en pantalla (barra y texto)
void Resource::renderProgress() {
// Obtiene la pantalla y el renderer
auto *screen = Screen::get();
auto *renderer = screen->getRenderer();
auto* screen = Screen::get();
auto* renderer = screen->getRenderer();
// Actualiza la lógica principal de la pantalla (input, etc.)
screen->coreUpdate();
@@ -808,6 +794,30 @@ void Resource::renderProgress() {
Lang::getText("[RESOURCE] LOADING") + " : " + loading_resource_name_,
param.resource.color);
// Muestra nombre de la aplicación y versión
loading_text_->writeColored(
X_PADDING,
Y_PADDING,
std::string(Version::APP_NAME) + " (" + Version::GIT_HASH + ")",
param.resource.color);
// Muestra información del monitor desplazada hacia abajo
loading_text_->writeColored(
X_PADDING,
Y_PADDING + 18,
screen->getDisplayMonitorName(),
param.resource.color);
loading_text_->writeColored(
X_PADDING,
Y_PADDING + 27,
std::to_string(screen->getDisplayMonitorWidth()) + "x" + std::to_string(screen->getDisplayMonitorHeight()),
param.resource.color);
loading_text_->writeColored(
X_PADDING,
Y_PADDING + 36,
std::to_string(screen->getDisplayMonitorRefreshRate()) + "Hz",
param.resource.color);
// Renderiza el frame en pantalla
screen->coreRender();
}
@@ -832,20 +842,16 @@ void Resource::checkEvents() {
// Carga los datos para el modo demostración (sin mostrar progreso)
void Resource::loadDemoDataQuiet() {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES (quiet load)");
auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
demos_.clear();
constexpr std::array<const char *, 2> DEMO_FILES = {"demo1.bin", "demo2.bin"};
for (const auto &file : DEMO_FILES) {
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get(file)));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Demo file loaded: %s", file);
for (const auto& l : list) {
demos_.emplace_back(loadDemoDataFromFile(l));
}
}
// Inicializa los rectangulos que definen la barra de progreso
void Resource::initProgressBar() {
constexpr float X_PADDING = 20.0F;
constexpr float Y_PADDING = 20.0F;
constexpr float BAR_HEIGHT = 10.0F;
const float BAR_Y_POSITION = param.game.height - BAR_HEIGHT - Y_PADDING;
const float WIRED_BAR_WIDTH = param.game.width - (X_PADDING * 2);
@@ -871,13 +877,13 @@ void Resource::updateProgressBar() {
// Limpia archivos temporales de audio
void Resource::cleanupTempAudioFiles() {
for (const auto &temp_path : temp_audio_files_) {
for (const auto& temp_path : temp_audio_files_) {
try {
if (std::filesystem::exists(temp_path)) {
std::filesystem::remove(temp_path);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Removed temp audio file: %s", temp_path.c_str());
}
} catch (const std::exception &e) {
} catch (const std::exception& e) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to remove temp audio file %s: %s", temp_path.c_str(), e.what());
}
}

View File

@@ -11,7 +11,7 @@
#include "animated_sprite.h" // Para AnimationsFileBuffer
#include "text.h" // Para Text, TextFile
#include "texture.h" // Para Texture
#include "utils.h" // Para DemoData
#include "demo.h" // Para DemoData
struct JA_Music_t;
struct JA_Sound_t;
@@ -120,6 +120,11 @@ class Resource {
}
};
// --- Constantes para la barra de progreso ---
static constexpr float X_PADDING = 20.0F;
static constexpr float Y_PADDING = 20.0F;
static constexpr float BAR_HEIGHT = 10.0F;
// --- Modo de carga ---
LoadingMode loading_mode_;

View File

@@ -16,9 +16,10 @@
#include "sprite.h" // Para Sprite
#include "text.h" // Para Text, Text::CENTER, Text::COLOR
#include "texture.h" // Para Texture
#include "utils.h" // Para easeOutCubic
// .at(SINGLETON) Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Scoreboard *Scoreboard::instance = nullptr;
Scoreboard* Scoreboard::instance = nullptr;
// .at(SINGLETON) Crearemos el objeto score_board con esta función estática
void Scoreboard::init() {
@@ -31,7 +32,7 @@ void Scoreboard::destroy() {
}
// .at(SINGLETON) Con este método obtenemos el objeto score_board y podemos trabajar con él
auto Scoreboard::get() -> Scoreboard * {
auto Scoreboard::get() -> Scoreboard* {
return Scoreboard::instance;
}
@@ -40,16 +41,18 @@ Scoreboard::Scoreboard()
: renderer_(Screen::get()->getRenderer()),
game_power_meter_texture_(Resource::get()->getTexture("game_power_meter.png")),
power_meter_sprite_(std::make_unique<Sprite>(game_power_meter_texture_)),
text_(Resource::get()->getText("8bithud")),
enter_name_text_(Resource::get()->getText("smb2")) {
text_(Resource::get()->getText("8bithud")) {
// Inicializa variables
for (size_t i = 0; i < static_cast<size_t>(Id::SIZE); ++i) {
name_.at(i).clear();
record_name_.at(i).clear();
enter_name_.at(i).clear();
selector_pos_.at(i) = 0;
score_.at(i) = 0;
mult_.at(i) = 0;
continue_counter_.at(i) = 0;
carousel_prev_index_.at(i) = -1; // Inicializar a -1 para detectar primera inicialización
enter_name_ref_.at(i) = nullptr;
text_slide_offset_.at(i) = 0.0f;
}
panel_.at(static_cast<size_t>(Id::LEFT)).mode = Mode::SCORE;
@@ -74,8 +77,9 @@ Scoreboard::Scoreboard()
// Rellena la textura de fondo
fillBackgroundTexture();
// Inicializa el vector de colores para el nombre
iniNameColors();
// Inicializa el ciclo de colores para el nombre
name_color_cycle_ = Colors::generateMirroredCycle(color_.INVERSE(), ColorCycleStyle::VIBRANT);
animated_color_ = name_color_cycle_.at(0);
}
Scoreboard::~Scoreboard() {
@@ -83,13 +87,104 @@ Scoreboard::~Scoreboard() {
SDL_DestroyTexture(background_);
}
for (auto *texture : panel_texture_) {
for (auto* texture : panel_texture_) {
if (texture != nullptr) {
SDL_DestroyTexture(texture);
}
}
}
// Configura la animación del carrusel
void Scoreboard::setCarouselAnimation(Id id, int selected_index, EnterName* enter_name_ptr) {
size_t idx = static_cast<size_t>(id);
// Guardar referencia a EnterName
enter_name_ref_.at(idx) = enter_name_ptr;
if (!enter_name_ptr || selected_index < 0) {
return;
}
// Primera inicialización: posicionar directamente sin animar
if (carousel_prev_index_.at(idx) == -1) {
carousel_position_.at(idx) = static_cast<float>(selected_index);
carousel_target_.at(idx) = static_cast<float>(selected_index);
carousel_prev_index_.at(idx) = selected_index;
} else {
// Detectar cambio en el índice del carácter seleccionado
int prev_index = carousel_prev_index_.at(idx);
if (selected_index != prev_index) {
// Calcular dirección del movimiento
int direction = selected_index - prev_index;
// Obtener tamaño de la lista para manejar wrap-around
const int LIST_SIZE = enter_name_ptr->getCharacterList().size();
// Manejar wrap-around circular
if (direction > LIST_SIZE / 2) {
direction = -(LIST_SIZE - direction); // Wrap backward (ej: Z → A)
} else if (direction < -LIST_SIZE / 2) {
direction = LIST_SIZE + direction; // Wrap forward (ej: A → Z)
}
// Normalizar a -1 o +1
direction = (direction > 0) ? 1 : ((direction < 0) ? -1 : 0);
if (direction != 0) {
// Actualizar target con movimiento relativo
carousel_target_.at(idx) = carousel_position_.at(idx) + static_cast<float>(direction);
// Guardar nuevo índice
carousel_prev_index_.at(idx) = selected_index;
}
}
}
}
// Establece el modo del panel y gestiona transiciones
void Scoreboard::setMode(Id id, Mode mode) {
size_t idx = static_cast<size_t>(id);
// Cambiar el modo
panel_.at(idx).mode = mode;
// Gestionar inicialización/transiciones según el nuevo modo
switch (mode) {
case Mode::SCORE_TO_ENTER_NAME:
// Iniciar animación de transición SCORE → ENTER_NAME
text_slide_offset_.at(idx) = 0.0f;
// Resetear carrusel para que se inicialice correctamente en ENTER_NAME
if (carousel_prev_index_.at(idx) != -1) {
carousel_prev_index_.at(idx) = -1;
}
break;
case Mode::ENTER_NAME:
// Resetear carrusel al entrar en modo de entrada de nombre
// Esto fuerza una reinicialización en la próxima llamada a setCarouselAnimation()
if (carousel_prev_index_.at(idx) != -1) {
carousel_prev_index_.at(idx) = -1;
}
text_slide_offset_.at(idx) = 0.0f;
break;
case Mode::ENTER_TO_SHOW_NAME:
// Iniciar animación de transición ENTER_NAME → SHOW_NAME
text_slide_offset_.at(idx) = 0.0f;
break;
case Mode::SHOW_NAME:
// Asegurar que la animación está completa
text_slide_offset_.at(idx) = 1.0f;
break;
// Otros modos no requieren inicialización especial
default:
break;
}
}
// Transforma un valor numérico en una cadena de 7 cifras
auto Scoreboard::updateScoreText(int num) -> std::string {
std::ostringstream oss;
@@ -107,11 +202,80 @@ void Scoreboard::updateTimeCounter() {
}
}
// Actualiza el índice del color animado del nombre
void Scoreboard::updateNameColorIndex() {
constexpr Uint64 COLOR_UPDATE_INTERVAL = 100; // 100ms entre cambios de color
if (SDL_GetTicks() - name_color_last_update_ >= COLOR_UPDATE_INTERVAL) {
++name_color_index_;
name_color_last_update_ = SDL_GetTicks();
}
// Precalcular el color actual del ciclo
animated_color_ = name_color_cycle_.at(name_color_index_ % name_color_cycle_.size());
}
// Actualiza la animación del carrusel
void Scoreboard::updateCarouselAnimation(float deltaTime) {
constexpr float CAROUSEL_SPEED = 8.0f; // Posiciones por segundo
for (size_t i = 0; i < carousel_position_.size(); ++i) {
// Solo animar si no hemos llegado al target
if (std::abs(carousel_position_.at(i) - carousel_target_.at(i)) > 0.01f) {
// Determinar dirección
float direction = (carousel_target_.at(i) > carousel_position_.at(i)) ? 1.0f : -1.0f;
// Calcular movimiento
float movement = CAROUSEL_SPEED * deltaTime * direction;
// Mover, pero no sobrepasar el target
float new_position = carousel_position_.at(i) + movement;
// Clamp para no sobrepasar
if (direction > 0) {
carousel_position_.at(i) = std::min(new_position, carousel_target_.at(i));
} else {
carousel_position_.at(i) = std::max(new_position, carousel_target_.at(i));
}
} else {
// Forzar al target exacto cuando estamos muy cerca
carousel_position_.at(i) = carousel_target_.at(i);
}
}
}
// Actualiza las animaciones de deslizamiento de texto
void Scoreboard::updateTextSlideAnimation(float deltaTime) {
for (size_t i = 0; i < static_cast<size_t>(Id::SIZE); ++i) {
Mode current_mode = panel_.at(i).mode;
if (current_mode == Mode::SCORE_TO_ENTER_NAME) {
// Incrementar progreso de animación SCORE → ENTER_NAME (0.0 a 1.0)
text_slide_offset_.at(i) += deltaTime / TEXT_SLIDE_DURATION;
// Terminar animación y cambiar a ENTER_NAME cuando se complete
if (text_slide_offset_.at(i) >= 1.0f) {
setMode(static_cast<Id>(i), Mode::ENTER_NAME);
}
} else if (current_mode == Mode::ENTER_TO_SHOW_NAME) {
// Incrementar progreso de animación ENTER_NAME → SHOW_NAME (0.0 a 1.0)
text_slide_offset_.at(i) += deltaTime / TEXT_SLIDE_DURATION;
// Terminar animación y cambiar a SHOW_NAME cuando se complete
if (text_slide_offset_.at(i) >= 1.0f) {
setMode(static_cast<Id>(i), Mode::SHOW_NAME);
}
}
}
}
// Actualiza la lógica del marcador
void Scoreboard::update() {
fillBackgroundTexture();
void Scoreboard::update(float deltaTime) {
updateTimeCounter();
++loop_counter_;
updateNameColorIndex();
updateCarouselAnimation(deltaTime);
updateTextSlideAnimation(deltaTime);
fillBackgroundTexture(); // Renderizar DESPUÉS de actualizar
}
// Pinta el marcador
@@ -129,7 +293,7 @@ void Scoreboard::setColor(Color color) {
// Aplica los colores
power_meter_sprite_->getTexture()->setColor(text_color2_);
fillBackgroundTexture();
iniNameColors();
name_color_cycle_ = Colors::generateMirroredCycle(color_.INVERSE(), ColorCycleStyle::VIBRANT);
}
// Establece el valor de la variable
@@ -145,7 +309,7 @@ void Scoreboard::setPos(SDL_FRect rect) {
// Rellena los diferentes paneles del marcador
void Scoreboard::fillPanelTextures() {
// Guarda a donde apunta actualmente el renderizador
auto *temp = SDL_GetRenderTarget(renderer_);
auto* temp = SDL_GetRenderTarget(renderer_);
// Genera el contenido de cada panel_
for (size_t i = 0; i < static_cast<int>(Id::SIZE); ++i) {
@@ -183,9 +347,15 @@ void Scoreboard::renderPanelContent(size_t panel_index) {
case Mode::CONTINUE:
renderContinueMode(panel_index);
break;
case Mode::SCORE_TO_ENTER_NAME:
renderScoreToEnterNameMode(panel_index);
break;
case Mode::ENTER_NAME:
renderEnterNameMode(panel_index);
break;
case Mode::ENTER_TO_SHOW_NAME:
renderEnterToShowNameMode(panel_index);
break;
case Mode::SHOW_NAME:
renderShowNameMode(panel_index);
break;
@@ -266,7 +436,40 @@ void Scoreboard::renderContinueMode(size_t panel_index) {
text_->writeDX(Text::CENTER | Text::COLOR, slot4_4_.x, slot4_4_.y, std::to_string(continue_counter_.at(panel_index)), 1, text_color2_);
}
void Scoreboard::renderScoreToEnterNameMode(size_t panel_index) {
// Calcular progreso suavizado de la animación (0.0 a 1.0)
const float t = static_cast<float>(easeInOutSine(text_slide_offset_.at(panel_index)));
// Calcular desplazamientos reales entre slots (no son exactamente ROW_SIZE)
const float delta_1_to_2 = slot4_2_.y - slot4_1_.y; // Diferencia real entre ROW1 y ROW2
const float delta_2_to_3 = slot4_3_.y - slot4_2_.y; // Diferencia real entre ROW2 y ROW3
const float delta_3_to_4 = slot4_4_.y - slot4_3_.y; // Diferencia real entre ROW3 y ROW4
// ========== Texto que SALE hacia arriba ==========
// name_ (sale desde ROW1 hacia arriba)
text_->writeDX(Text::CENTER | Text::COLOR, slot4_1_.x, slot4_1_.y - t * delta_1_to_2,
name_.at(panel_index), 1, text_color1_);
// ========== Textos que SE MUEVEN hacia arriba ==========
// score_ (se mueve de ROW2 a ROW1)
text_->writeDX(Text::CENTER | Text::COLOR, slot4_2_.x, slot4_2_.y - t * delta_1_to_2,
updateScoreText(score_.at(panel_index)), 1, text_color2_);
// "ENTER NAME" (se mueve de ROW3 a ROW2)
text_->writeDX(Text::CENTER | Text::COLOR, slot4_3_.x, slot4_3_.y - t * delta_2_to_3,
Lang::getText("[SCOREBOARD] 11"), 1, text_color1_);
// enter_name_ (se mueve de ROW4 a ROW3)
text_->writeDX(Text::CENTER | Text::COLOR, slot4_4_.x, slot4_4_.y - t * delta_3_to_4,
enter_name_.at(panel_index), 1, text_color2_);
// ========== Elemento que ENTRA desde abajo ==========
// CARRUSEL (entra desde debajo de ROW4 hacia ROW4)
renderCarousel(panel_index, slot4_4_.x, static_cast<int>(slot4_4_.y + delta_3_to_4 - t * delta_3_to_4));
}
void Scoreboard::renderEnterNameMode(size_t panel_index) {
/*
// SCORE
text_->writeDX(Text::CENTER | Text::COLOR, slot4_1_.x, slot4_1_.y, name_.at(panel_index), 1, text_color1_);
text_->writeDX(Text::CENTER | Text::COLOR, slot4_2_.x, slot4_2_.y, updateScoreText(score_.at(panel_index)), 1, text_color2_);
@@ -275,41 +478,66 @@ void Scoreboard::renderEnterNameMode(size_t panel_index) {
text_->writeDX(Text::CENTER | Text::COLOR, slot4_3_.x, slot4_3_.y, Lang::getText("[SCOREBOARD] 11"), 1, text_color1_);
renderNameInputField(panel_index);
*/
// SCORE
text_->writeDX(Text::CENTER | Text::COLOR, slot4_1_.x, slot4_1_.y, updateScoreText(score_.at(panel_index)), 1, text_color2_);
// ENTER NAME
text_->writeDX(Text::CENTER | Text::COLOR, slot4_2_.x, slot4_2_.y, Lang::getText("[SCOREBOARD] 11"), 1, text_color1_);
// NAME
text_->writeDX(Text::CENTER | Text::COLOR, slot4_3_.x, slot4_3_.y, enter_name_.at(panel_index), 1, text_color2_);
// CARRUSEL
renderCarousel(panel_index, slot4_4_.x, slot4_4_.y);
}
void Scoreboard::renderNameInputField(size_t panel_index) {
SDL_FRect rect = {
.x = enter_name_pos_.x,
.y = enter_name_pos_.y,
.w = static_cast<float>(enter_name_text_->getCharacterSize() - 2),
.h = static_cast<float>(enter_name_text_->getCharacterSize())};
void Scoreboard::renderEnterToShowNameMode(size_t panel_index) {
// Calcular progreso suavizado de la animación (0.0 a 1.0)
const float t = static_cast<float>(easeInOutSine(text_slide_offset_.at(panel_index)));
// Recorre todos los slots de letras del nombre
for (size_t j = 0; j < NAME_SIZE; ++j) {
// Dibuja la linea. Si coincide con el selector solo se dibuja 2 de cada 4 veces
if (j != selector_pos_.at(panel_index) || time_counter_ % 4 >= 2) {
SDL_SetRenderDrawColor(renderer_, text_color1_.r, text_color1_.g, text_color1_.b, 255);
SDL_RenderLine(renderer_, rect.x, rect.y + rect.h, rect.x + rect.w, rect.y + rect.h);
}
// Calcular desplazamientos reales entre slots (no son exactamente ROW_SIZE)
const float delta_1_to_2 = slot4_2_.y - slot4_1_.y; // Diferencia real entre ROW1 y ROW2
const float delta_2_to_3 = slot4_3_.y - slot4_2_.y; // Diferencia real entre ROW2 y ROW3
const float delta_3_to_4 = slot4_4_.y - slot4_3_.y; // Diferencia real entre ROW3 y ROW4
// Dibuja la letra
if (j < record_name_.at(panel_index).size()) {
enter_name_text_->writeColored(rect.x, rect.y, record_name_.at(panel_index).substr(j, 1), text_color2_);
}
rect.x += enter_name_text_->getCharacterSize();
}
// ========== Texto que ENTRA desde arriba ==========
// name_ (entra desde arriba hacia ROW1)
// Debe venir desde donde estaría ROW0, que está a delta_1_to_2 píxeles arriba de ROW1
text_->writeDX(Text::CENTER | Text::COLOR, slot4_1_.x, slot4_1_.y + t * delta_1_to_2 - delta_1_to_2,
name_.at(panel_index), 1, text_color1_);
// ========== Textos que SE MUEVEN (renderizar UNA sola vez) ==========
// SCORE (se mueve de ROW1 a ROW2)
text_->writeDX(Text::CENTER | Text::COLOR, slot4_1_.x, slot4_1_.y + t * delta_1_to_2,
updateScoreText(score_.at(panel_index)), 1, text_color2_);
// "ENTER NAME" (se mueve de ROW2 a ROW3)
text_->writeDX(Text::CENTER | Text::COLOR, slot4_2_.x, slot4_2_.y + t * delta_2_to_3,
Lang::getText("[SCOREBOARD] 11"), 1, text_color1_);
// enter_name_ (se mueve de ROW3 a ROW4)
text_->writeDX(Text::CENTER | Text::COLOR, slot4_3_.x, slot4_3_.y + t * delta_3_to_4,
enter_name_.at(panel_index), 1, text_color2_);
// ========== Elemento que SALE hacia abajo ==========
// CARRUSEL (sale desde ROW4 hacia abajo, fuera de pantalla)
renderCarousel(panel_index, slot4_4_.x, static_cast<int>(slot4_4_.y + t * delta_3_to_4));
}
void Scoreboard::renderShowNameMode(size_t panel_index) {
// SCORE
// NOMBRE DEL JUGADOR
text_->writeDX(Text::CENTER | Text::COLOR, slot4_1_.x, slot4_1_.y, name_.at(panel_index), 1, text_color1_);
// SCORE
text_->writeDX(Text::CENTER | Text::COLOR, slot4_2_.x, slot4_2_.y, updateScoreText(score_.at(panel_index)), 1, text_color2_);
// NAME
// "ENTER NAME"
text_->writeDX(Text::CENTER | Text::COLOR, slot4_3_.x, slot4_3_.y, Lang::getText("[SCOREBOARD] 11"), 1, text_color1_);
// NOMBRE INTRODUCIDO
enter_name_text_->writeDX(Text::CENTER | Text::COLOR, slot4_4_.x, slot4_4_.y, record_name_.at(panel_index), 1, Colors::getColorLikeKnightRider(name_colors_, loop_counter_ / 5));
// NOMBRE INTRODUCIDO (con color animado)
text_->writeDX(Text::CENTER | Text::COLOR, slot4_4_.x, slot4_4_.y, enter_name_.at(panel_index), 1, animated_color_);
}
void Scoreboard::renderGameCompletedMode(size_t panel_index) {
@@ -329,7 +557,7 @@ void Scoreboard::fillBackgroundTexture() {
fillPanelTextures();
// Cambia el destino del renderizador
SDL_Texture *temp = SDL_GetRenderTarget(renderer_);
SDL_Texture* temp = SDL_GetRenderTarget(renderer_);
SDL_SetRenderTarget(renderer_, background_);
// Dibuja el fondo del marcador
@@ -379,7 +607,7 @@ void Scoreboard::recalculateAnchors() {
slot4_4_ = {.x = COL, .y = ROW4};
// Primer cuadrado para poner el nombre de record
const int ENTER_NAME_LENGTH = enter_name_text_->length(std::string(NAME_SIZE, 'A'));
const int ENTER_NAME_LENGTH = text_->length(std::string(EnterName::MAX_NAME_SIZE, 'A'));
enter_name_pos_.x = COL - (ENTER_NAME_LENGTH / 2);
enter_name_pos_.y = ROW4;
@@ -405,7 +633,7 @@ void Scoreboard::createBackgroundTexture() {
// Crea las texturas de los paneles
void Scoreboard::createPanelTextures() {
// Elimina las texturas en caso de existir
for (auto *texture : panel_texture_) {
for (auto* texture : panel_texture_) {
if (texture != nullptr) {
SDL_DestroyTexture(texture);
}
@@ -413,8 +641,8 @@ void Scoreboard::createPanelTextures() {
panel_texture_.clear();
// Crea las texturas para cada panel_
for (auto &i : panel_) {
SDL_Texture *tex = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, i.pos.w, i.pos.h);
for (auto& i : panel_) {
SDL_Texture* tex = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, i.pos.w, i.pos.h);
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
panel_texture_.push_back(tex);
}
@@ -428,13 +656,87 @@ void Scoreboard::renderSeparator() {
SDL_RenderLine(renderer_, 0, 0, rect_.w, 0);
}
// Inicializa el vector de colores para el nombre
void Scoreboard::iniNameColors() {
Color color = color_.INVERSE();
// Pinta el carrusel de caracteres con efecto de color LERP y animación suave
void Scoreboard::renderCarousel(size_t panel_index, int center_x, int y) {
// Obtener referencia a EnterName
EnterName* enter_name = enter_name_ref_.at(panel_index);
if (!enter_name) {
return;
}
name_colors_.clear();
name_colors_.emplace_back(color.LIGHTEN(50));
name_colors_.emplace_back(color.LIGHTEN(25));
name_colors_.emplace_back(color);
name_colors_.emplace_back(color.DARKEN(25));
// Obtener la lista completa de caracteres
const std::string& char_list = enter_name->getCharacterList();
if (char_list.empty()) {
return;
}
// Espacio extra entre letras
constexpr int EXTRA_SPACING = 2;
// Carrusel extendido: usar constante de clase
constexpr int HALF_VISIBLE = CAROUSEL_VISIBLE_LETTERS / 2; // 4
// Posición flotante actual del carrusel (índice en character_list_)
const float carousel_pos = carousel_position_.at(panel_index);
// Calcular ancho promedio de una letra (asumimos ancho uniforme)
std::string sample_char(1, char_list[0]);
const int AVG_CHAR_WIDTH = text_->length(sample_char, 1);
const int CHAR_STEP = AVG_CHAR_WIDTH + EXTRA_SPACING;
// Calcular offset de píxeles basado en la parte fraccionaria de carousel_pos
const float fractional_offset = carousel_pos - std::floor(carousel_pos);
const int pixel_offset = static_cast<int>(fractional_offset * CHAR_STEP);
// Índice base en character_list_ (centro del carrusel)
const int base_index = static_cast<int>(std::floor(carousel_pos));
const int char_list_size = static_cast<int>(char_list.size());
// Calcular posición X inicial (centrar el conjunto de 9 letras)
int start_x = center_x - (HALF_VISIBLE * CHAR_STEP) - (AVG_CHAR_WIDTH / 2) - pixel_offset;
// Renderizar las 9 letras visibles
for (int i = -HALF_VISIBLE; i <= HALF_VISIBLE; ++i) {
// Índice real en character_list_ (con wrap-around circular)
int char_index = base_index + i;
// Wrap-around circular
char_index = char_index % char_list_size;
if (char_index < 0) {
char_index += char_list_size;
}
// Obtener el carácter directamente de character_list_
std::string single_char(1, char_list[char_index]);
// Calcular distancia flotante al centro visual basada en posición real del carácter
float distance_from_center = std::abs(static_cast<float>(char_index) - carousel_pos);
// Manejar wrap-around circular: elegir el camino más corto
if (distance_from_center > static_cast<float>(char_list_size) / 2.0f) {
distance_from_center = static_cast<float>(char_list_size) - distance_from_center;
}
// Calcular color con LERP dinámico continuo
Color letter_color;
if (distance_from_center < 0.5f) {
// Letra cerca del centro: LERP hacia animated_color_
// distance_from_center va de 0.0 (centro exacto) a 0.5 (borde)
float lerp_to_animated = distance_from_center / 0.5f; // 0.0 a 1.0
letter_color = animated_color_.LERP(text_color1_, lerp_to_animated);
} else {
// Letras alejadas: LERP hacia color_ (fade out)
float base_lerp = (distance_from_center - 0.5f) / (HALF_VISIBLE - 0.5f);
base_lerp = std::min(base_lerp, 1.0f);
const float LERP_FACTOR = base_lerp * 0.85f;
letter_color = text_color1_.LERP(color_, LERP_FACTOR);
}
// Calcular posición X de esta letra
const int letter_x = start_x + (i + HALF_VISIBLE) * CHAR_STEP;
// Pintar la letra
text_->writeDX(Text::COLOR, letter_x, y, single_char, 1, letter_color);
}
}

View File

@@ -8,6 +8,9 @@
#include <string> // Para string, basic_string
#include <vector> // Para vector
// Forward declarations
class EnterName;
#include "color.h" // Para Color
class Sprite;
@@ -32,7 +35,9 @@ class Scoreboard {
WAITING,
GAME_OVER,
DEMO,
SCORE_TO_ENTER_NAME, // Transición animada: SCORE → ENTER_NAME
ENTER_NAME,
ENTER_TO_SHOW_NAME, // Transición animada: ENTER_NAME → SHOW_NAME
SHOW_NAME,
GAME_COMPLETED,
NUM_MODES,
@@ -45,57 +50,70 @@ class Scoreboard {
};
// --- Métodos de singleton ---
static void init(); // Crea el objeto Scoreboard
static void destroy(); // Libera el objeto Scoreboard
static auto get() -> Scoreboard *; // Obtiene el puntero al objeto Scoreboard
static void init(); // Crea el objeto Scoreboard
static void destroy(); // Libera el objeto Scoreboard
static auto get() -> Scoreboard*; // Obtiene el puntero al objeto Scoreboard
// --- Métodos principales ---
void update(); // Actualiza la lógica del marcador
void render(); // Pinta el marcador
void update(float deltaTime); // Actualiza la lógica del marcador
void render(); // Pinta el marcador
// --- Setters ---
void setColor(Color color); // Establece el color del marcador
void setPos(SDL_FRect rect); // Establece la posición y tamaño del marcador
void setContinue(Id id, int continue_counter) { continue_counter_.at(static_cast<size_t>(id)) = continue_counter; }
void setHiScore(int hi_score) { hi_score_ = hi_score; }
void setHiScoreName(const std::string &name) { hi_score_name_ = name; }
void setMode(Id id, Mode mode) { panel_.at(static_cast<size_t>(id)).mode = mode; }
void setHiScoreName(const std::string& name) { hi_score_name_ = name; }
void setMode(Id id, Mode mode); // Establece el modo del panel y gestiona transiciones
void setMult(Id id, float mult) { mult_.at(static_cast<size_t>(id)) = mult; }
void setName(Id id, const std::string &name) { name_.at(static_cast<size_t>(id)) = name; }
void setName(Id id, const std::string& name) { name_.at(static_cast<size_t>(id)) = name; }
void setPower(float power) { power_ = power; }
void setRecordName(Id id, const std::string &record_name) { record_name_.at(static_cast<size_t>(id)) = record_name; }
void setEnterName(Id id, const std::string& enter_name) { enter_name_.at(static_cast<size_t>(id)) = enter_name; }
void setCharacterSelected(Id id, const std::string& character_selected) { character_selected_.at(static_cast<size_t>(id)) = character_selected; }
void setCarouselAnimation(Id id, int selected_index, EnterName* enter_name_ptr); // Configura la animación del carrusel
void setScore(Id id, int score) { score_.at(static_cast<size_t>(id)) = score; }
void setSelectorPos(Id id, int pos) { selector_pos_.at(static_cast<size_t>(id)) = pos; }
void setStage(int stage) { stage_ = stage; }
private:
// --- Objetos y punteros ---
SDL_Renderer *renderer_; // El renderizador de la ventana
SDL_Renderer* renderer_; // El renderizador de la ventana
std::shared_ptr<Texture> game_power_meter_texture_; // Textura con el marcador de poder de la fase
std::unique_ptr<Sprite> power_meter_sprite_; // Sprite para el medidor de poder de la fase
std::shared_ptr<Text> text_; // Fuente para el marcador del juego
std::shared_ptr<Text> enter_name_text_; // Fuente para la introducción de nombre del jugador
SDL_Texture *background_ = nullptr; // Textura para dibujar el marcador
std::vector<SDL_Texture *> panel_texture_; // Texturas para dibujar cada panel
SDL_Texture* background_ = nullptr; // Textura para dibujar el marcador
std::vector<SDL_Texture*> panel_texture_; // Texturas para dibujar cada panel
// --- Variables de estado ---
std::array<std::string, static_cast<int>(Id::SIZE)> name_ = {}; // Nombre de cada jugador
std::array<std::string, static_cast<int>(Id::SIZE)> record_name_ = {}; // Nombre introducido para la tabla de records
std::array<Panel, static_cast<int>(Id::SIZE)> panel_ = {}; // Lista con todos los paneles del marcador
std::vector<Color> name_colors_; // Colores para destacar el nombre una vez introducido
std::string hi_score_name_; // Nombre del jugador con la máxima puntuación
SDL_FRect rect_ = {0, 0, 320, 40}; // Posición y dimensiones del marcador
Color color_; // Color del marcador
std::array<size_t, static_cast<int>(Id::SIZE)> selector_pos_ = {}; // Posición del selector de letra para introducir el nombre
std::array<int, static_cast<int>(Id::SIZE)> score_ = {}; // Puntuación de los jugadores
std::array<int, static_cast<int>(Id::SIZE)> continue_counter_ = {}; // Tiempo para continuar de los jugadores
std::array<float, static_cast<int>(Id::SIZE)> mult_ = {}; // Multiplicador de los jugadores
Uint64 ticks_ = SDL_GetTicks(); // Variable donde almacenar el valor de SDL_GetTicks()
int stage_ = 1; // Número de fase actual
int hi_score_ = 0; // Máxima puntuación
int time_counter_ = 0; // Contador de segundos
int loop_counter_ = 0; // Contador de bucle
float power_ = 0; // Poder actual de la fase
std::array<std::string, static_cast<int>(Id::SIZE)> name_ = {}; // Nombre de cada jugador
std::array<std::string, static_cast<int>(Id::SIZE)> enter_name_ = {}; // Nombre introducido para la tabla de records
std::array<std::string, static_cast<int>(Id::SIZE)> character_selected_ = {}; // Caracter seleccionado
std::array<EnterName*, static_cast<int>(Id::SIZE)> enter_name_ref_ = {}; // Referencias a EnterName para obtener character_list_
std::array<float, static_cast<int>(Id::SIZE)> carousel_position_ = {}; // Posición actual del carrusel (índice en character_list_)
std::array<float, static_cast<int>(Id::SIZE)> carousel_target_ = {}; // Posición objetivo del carrusel
std::array<int, static_cast<int>(Id::SIZE)> carousel_prev_index_ = {}; // Índice previo para detectar cambios
std::array<float, static_cast<int>(Id::SIZE)> text_slide_offset_ = {}; // Progreso de animación de deslizamiento (0.0 a 1.0)
std::array<Panel, static_cast<int>(Id::SIZE)> panel_ = {}; // Lista con todos los paneles del marcador
Colors::Cycle name_color_cycle_; // Ciclo de colores para destacar el nombre una vez introducido
Color animated_color_; // Color actual animado (ciclo automático cada 100ms)
std::string hi_score_name_; // Nombre del jugador con la máxima puntuación
SDL_FRect rect_ = {0, 0, 320, 40}; // Posición y dimensiones del marcador
Color color_; // Color del marcador
std::array<size_t, static_cast<int>(Id::SIZE)> selector_pos_ = {}; // Posición del selector de letra para introducir el nombre
std::array<int, static_cast<int>(Id::SIZE)> score_ = {}; // Puntuación de los jugadores
std::array<int, static_cast<int>(Id::SIZE)> continue_counter_ = {}; // Tiempo para continuar de los jugadores
std::array<float, static_cast<int>(Id::SIZE)> mult_ = {}; // Multiplicador de los jugadores
Uint64 ticks_ = SDL_GetTicks(); // Variable donde almacenar el valor de SDL_GetTicks()
int stage_ = 1; // Número de fase actual
int hi_score_ = 0; // Máxima puntuación
int time_counter_ = 0; // Contador de segundos
Uint32 name_color_index_ = 0; // Índice actual del color en el ciclo de animación del nombre
Uint64 name_color_last_update_ = 0; // Último tick de actualización del color del nombre
float power_ = 0; // Poder actual de la fase
// --- Constantes ---
static constexpr int CAROUSEL_VISIBLE_LETTERS = 9;
static constexpr float TEXT_SLIDE_DURATION = 0.3f; // Duración de la animación de deslizamiento en segundos
// --- Variables de aspecto ---
Color text_color1_, text_color2_; // Colores para los marcadores del texto;
@@ -112,8 +130,10 @@ class Scoreboard {
void fillPanelTextures(); // Rellena los diferentes paneles del marcador
void fillBackgroundTexture(); // Rellena la textura de fondo
void updateTimeCounter(); // Actualiza el contador
void updateNameColorIndex(); // Actualiza el índice del color animado del nombre
void updateCarouselAnimation(float deltaTime); // Actualiza la animación del carrusel
void updateTextSlideAnimation(float deltaTime); // Actualiza la animación de deslizamiento de texto
void renderSeparator(); // Dibuja la línea que separa la zona de juego del marcador
void iniNameColors(); // Inicializa el vector de colores para el nombre
void renderPanelContent(size_t panel_index);
void renderScoreMode(size_t panel_index);
void renderDemoMode();
@@ -121,15 +141,18 @@ class Scoreboard {
void renderGameOverMode();
void renderStageInfoMode();
void renderContinueMode(size_t panel_index);
void renderScoreToEnterNameMode(size_t panel_index); // Renderiza la transición SCORE → ENTER_NAME
void renderEnterNameMode(size_t panel_index);
void renderNameInputField(size_t panel_index);
void renderEnterToShowNameMode(size_t panel_index); // Renderiza la transición ENTER_NAME → SHOW_NAME
void renderShowNameMode(size_t panel_index);
void renderGameCompletedMode(size_t panel_index);
void renderCarousel(size_t panel_index, int center_x, int y); // Pinta el carrusel de caracteres con colores LERP
// --- Constructores y destructor privados (singleton) ---
Scoreboard(); // Constructor privado
~Scoreboard(); // Destructor privado
// --- Instancia singleton ---
static Scoreboard *instance; // Instancia única de Scoreboard
static Scoreboard* instance; // Instancia única de Scoreboard
};

View File

@@ -8,9 +8,12 @@
#include <memory> // Para allocator, shared_ptr, make_shared, __shared_ptr_access
#include <string> // Para operator+, char_traits, to_string, string
#include "asset.h" // Para Asset
#include "external/jail_shader.h" // Para init, render
#include "mouse.h" // Para updateCursorVisibility
#include "asset.h" // Para Asset
#include "mouse.h" // Para updateCursorVisibility
#include "rendering/opengl/opengl_shader.h" // Para OpenGLShader
#ifdef __APPLE__
// #include "rendering/metal/metal_shader.h" // Para MetalShader (TODO: descomentar cuando esté completo)
#endif
#include "options.h" // Para VideoOptions, video, WindowOptions, window
#include "param.h" // Para Param, param, ParamGame, ParamDebug
#include "text.h" // Para Text, Text::COLOR, Text::STROKE
@@ -55,10 +58,12 @@ Screen::Screen()
setDebugInfoEnabled(true);
#endif
// Inicializa los shaders
SDL_RenderTexture(renderer_, game_canvas_, nullptr, nullptr);
// Inicializa los shaders (solo en plataformas no-macOS por ahora)
#ifndef __APPLE__
loadShaders();
shader::init(window_, game_canvas_, shader_source_);
shader_backend_ = std::make_unique<Rendering::OpenGLShader>();
shader_backend_->init(window_, game_canvas_, vertex_shader_source_, fragment_shader_source_);
#endif
}
// Destructor
@@ -86,10 +91,10 @@ void Screen::render() {
// Vuelca el contenido del renderizador en pantalla exceptuando ciertas partes
void Screen::coreRender() {
fps_.increment();
/*fps_.increment();
#ifdef _DEBUG
renderInfo();
#endif
#endif*/
renderPresent(); // Renderiza el contenido del game_canvas_
}
@@ -98,8 +103,8 @@ void Screen::renderPresent() {
SDL_SetRenderTarget(renderer_, nullptr);
clean();
if (Options::video.shaders) {
shader::render();
if (Options::video.shaders && shader_backend_) {
shader_backend_->render();
} else {
SDL_RenderTexture(renderer_, game_canvas_, nullptr, nullptr);
SDL_RenderPresent(renderer_);
@@ -217,26 +222,47 @@ void Screen::renderInfo() {
// FPS
const std::string FPS_TEXT = std::to_string(fps_.last_value) + " FPS";
debug_info_.text->writeDX(Text::COLOR | Text::STROKE, param.game.width - debug_info_.text->length(FPS_TEXT) - 2, 1 + debug_info_.text->getCharacterSize(), FPS_TEXT, 1, param.debug.color, 1, param.debug.color.DARKEN(150));
#ifdef RECORDING
// RECORDING
debug_info_.text->writeDX(Text::COLOR | Text::STROKE, param.game.width - debug_info_.text->length("RECORDING"), 2*(1 + debug_info_.text->getCharacterSize()), "RECORDING", 1, param.debug.color, 1, param.debug.color.DARKEN(150));
#endif
}
}
#endif
// Carga el contenido del archivo GLSL
// Carga el contenido de los archivos GLSL
void Screen::loadShaders() {
if (shader_source_.empty()) {
const std::string GLSL_FILE = param.game.game_area.rect.h == 256 ? "crtpi_256.glsl" : "crtpi_240.glsl";
auto data = Asset::get()->loadData(GLSL_FILE);
if (vertex_shader_source_.empty()) {
const std::string VERTEX_FILE = "crtpi_vertex.glsl";
auto data = Asset::get()->loadData(VERTEX_FILE);
if (!data.empty()) {
shader_source_ = std::string(data.begin(), data.end());
vertex_shader_source_ = std::string(data.begin(), data.end());
}
}
if (fragment_shader_source_.empty()) {
const std::string FRAGMENT_FILE = "crtpi_fragment.glsl";
auto data = Asset::get()->loadData(FRAGMENT_FILE);
if (!data.empty()) {
fragment_shader_source_ = std::string(data.begin(), data.end());
}
}
}
// Inicializa los shaders
void Screen::initShaders() {
#ifndef __APPLE__
if (Options::video.shaders) {
loadShaders();
shader::init(window_, game_canvas_, shader_source_);
if (!shader_backend_) {
shader_backend_ = std::make_unique<Rendering::OpenGLShader>();
}
shader_backend_->init(window_, game_canvas_, vertex_shader_source_, fragment_shader_source_);
}
#else
// En macOS, OpenGL está deprecated y rinde mal
// TODO: Implementar backend de Metal para shaders en macOS
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Shaders no disponibles en macOS (OpenGL deprecated). Usa Metal backend.");
#endif
}
// Calcula el tamaño de la ventana
@@ -296,14 +322,29 @@ auto Screen::initSDLVideo() -> bool {
// Obtener información de la pantalla
getDisplayInfo();
// Configurar hint para OpenGL
// Configurar hint para renderizado
#ifdef __APPLE__
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal")) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Warning: Failed to set Metal hint!");
}
#else
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl")) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Warning: Failed to set OpenGL hint!");
}
// Configurar contexto OpenGL 3.3 Core Profile
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#endif
// Crear ventana
#ifdef __APPLE__
SDL_WindowFlags window_flags = SDL_WINDOW_METAL;
#else
SDL_WindowFlags window_flags = SDL_WINDOW_OPENGL;
#endif
if (Options::video.fullscreen) {
window_flags |= SDL_WINDOW_FULLSCREEN;
}
@@ -358,6 +399,13 @@ void Screen::getDisplayInfo() {
const auto *dm = SDL_GetCurrentDisplayMode(displays[0]);
// Guarda información del monitor en display_monitor_
const char *first_display_name = SDL_GetDisplayName(displays[0]);
display_monitor_.name = (first_display_name != nullptr) ? first_display_name : "Unknown";
display_monitor_.width = static_cast<int>(dm->w);
display_monitor_.height = static_cast<int>(dm->h);
display_monitor_.refresh_rate = static_cast<int>(dm->refresh_rate);
// Calcula el máximo factor de zoom que se puede aplicar a la pantalla
Options::window.max_zoom = std::min(dm->w / param.game.width, dm->h / param.game.height);
Options::window.zoom = std::min(Options::window.zoom, Options::window.max_zoom);

View File

@@ -8,10 +8,15 @@
#include "color.h" // Para Color
#include "options.h" // Para VideoOptions, video
// Forward declarations
class Notifier;
class ServiceMenu;
class Text;
namespace Rendering {
class ShaderBackend;
}
// --- Clase Screen: gestiona la ventana, el renderizador y los efectos visuales globales (singleton) ---
class Screen {
public:
@@ -54,6 +59,12 @@ class Screen {
[[nodiscard]] static auto getVSync() -> bool { return Options::video.vsync; } // Obtiene el valor de V-Sync
[[nodiscard]] auto getText() const -> std::shared_ptr<Text> { return text_; } // Obtiene el puntero al texto de Screen
// --- Display Monitor getters ---
[[nodiscard]] auto getDisplayMonitorName() const -> std::string { return display_monitor_.name; }
[[nodiscard]] auto getDisplayMonitorWidth() const -> int { return display_monitor_.width; }
[[nodiscard]] auto getDisplayMonitorHeight() const -> int { return display_monitor_.height; }
[[nodiscard]] auto getDisplayMonitorRefreshRate() const -> int { return display_monitor_.refresh_rate; }
#ifdef _DEBUG
// --- Debug ---
void toggleDebugInfo() { debug_info_.show = !debug_info_.show; }
@@ -65,6 +76,12 @@ class Screen {
static constexpr int WINDOWS_DECORATIONS = 35; // Decoraciones de la ventana
// --- Estructuras privadas ---
struct DisplayMonitor {
std::string name;
int width;
int height;
int refresh_rate;
};
struct FPS {
Uint32 ticks{0}; // Tiempo en milisegundos desde que se comenzó a contar.
int frame_count{0}; // Número acumulado de frames en el intervalo.
@@ -191,21 +208,24 @@ class Screen {
#endif
// --- Objetos y punteros ---
SDL_Window *window_; // Ventana de la aplicación
SDL_Renderer *renderer_; // El renderizador de la ventana
SDL_Texture *game_canvas_; // Textura donde se dibuja todo antes de volcarse al renderizador
ServiceMenu *service_menu_; // Objeto para mostrar el menú de servicio
Notifier *notifier_; // Objeto para mostrar las notificaciones por pantalla
std::shared_ptr<Text> text_; // Objeto para escribir texto en pantalla
SDL_Window *window_; // Ventana de la aplicación
SDL_Renderer *renderer_; // El renderizador de la ventana
SDL_Texture *game_canvas_; // Textura donde se dibuja todo antes de volcarse al renderizador
ServiceMenu *service_menu_; // Objeto para mostrar el menú de servicio
Notifier *notifier_; // Objeto para mostrar las notificaciones por pantalla
std::shared_ptr<Text> text_; // Objeto para escribir texto en pantalla
std::unique_ptr<Rendering::ShaderBackend> shader_backend_; // Backend de shaders (OpenGL/Metal/Vulkan)
// --- Variables de estado ---
SDL_FRect src_rect_; // Coordenadas de origen para dibujar la textura del juego
SDL_FRect dst_rect_; // Coordenadas destino para dibujar la textura del juego
std::string shader_source_; // Almacena el contenido del archivo GLSL
std::string vertex_shader_source_; // Almacena el vertex shader
std::string fragment_shader_source_; // Almacena el fragment shader
FPS fps_; // Gestión de frames por segundo
FlashEffect flash_effect_; // Efecto de flash en pantalla
ShakeEffect shake_effect_; // Efecto de agitar la pantalla
bool attenuate_effect_ = false; // Indica si la pantalla ha de estar atenuada
DisplayMonitor display_monitor_; // Información del monitor actual
#ifdef _DEBUG
Debug debug_info_; // Información de debug
#endif
@@ -218,7 +238,7 @@ class Screen {
void renderPresent(); // Selecciona y ejecuta el método de renderizado adecuado
void loadShaders(); // Carga el contenido del archivo GLSL
void adjustWindowSize(); // Calcula el tamaño de la ventana
static void getDisplayInfo(); // Obtiene información sobre la pantalla
void getDisplayInfo(); // Obtiene información sobre la pantalla
void renderOverlays(); // Renderiza todos los overlays y efectos
void renderAttenuate(); // Atenúa la pantalla
void createText(); // Crea el objeto de texto

View File

@@ -49,13 +49,13 @@ Credits::Credits()
fade_in_->setColor(param.fade.color);
fade_in_->setType(Fade::Type::FULLSCREEN);
fade_in_->setPostDuration(static_cast<int>(50 * (1000.0f / 60.0f))); // 50 frames = ~833ms
fade_in_->setPostDuration(800);
fade_in_->setMode(Fade::Mode::IN);
fade_in_->activate();
fade_out_->setColor(0, 0, 0);
fade_out_->setType(Fade::Type::FULLSCREEN);
fade_out_->setPostDuration(static_cast<int>(400 * (1000.0f / 60.0f))); // 400 frames = ~6667ms
fade_out_->setPostDuration(7000);
updateRedRect();
tiled_bg_->setColor(Color(255, 96, 96));
@@ -293,11 +293,10 @@ void Credits::fillCanvas() {
// Actualiza el destino de los rectangulos de las texturas (time-based)
void Credits::updateTextureDstRects(float deltaTime) {
constexpr float TEXTURE_UPDATE_INTERVAL_S = 10.0f / 60.0f; // ~0.167s (cada 10 frames)
static float texture_accumulator = 0.0f;
texture_accumulator += deltaTime;
credits_state_.texture_accumulator += deltaTime;
if (texture_accumulator >= TEXTURE_UPDATE_INTERVAL_S) {
texture_accumulator -= TEXTURE_UPDATE_INTERVAL_S;
if (credits_state_.texture_accumulator >= TEXTURE_UPDATE_INTERVAL_S) {
credits_state_.texture_accumulator -= TEXTURE_UPDATE_INTERVAL_S;
// Comprueba la posición de la textura con los titulos de credito
if (credits_rect_dst_.y + credits_rect_dst_.h > play_area_.y) {
@@ -336,20 +335,17 @@ void Credits::throwBalloons(float deltaTime) {
return;
}
static float balloon_accumulator = 0.0f;
static float powerball_accumulator = 0.0f;
credits_state_.balloon_accumulator += deltaTime;
credits_state_.powerball_accumulator += deltaTime;
balloon_accumulator += deltaTime;
powerball_accumulator += deltaTime;
if (balloon_accumulator >= BALLOON_INTERVAL_S) {
balloon_accumulator -= BALLOON_INTERVAL_S;
if (credits_state_.balloon_accumulator >= BALLOON_INTERVAL_S) {
credits_state_.balloon_accumulator -= BALLOON_INTERVAL_S;
const int INDEX = (static_cast<int>(counter_ / SPEED)) % SETS.size();
balloon_manager_->deployFormation(SETS.at(INDEX), -60);
}
if (powerball_accumulator >= POWERBALL_INTERVAL_S && counter_ > 0) {
powerball_accumulator -= POWERBALL_INTERVAL_S;
if (credits_state_.powerball_accumulator >= POWERBALL_INTERVAL_S && counter_ > 0) {
credits_state_.powerball_accumulator -= POWERBALL_INTERVAL_S;
balloon_manager_->createPowerBall();
}
}
@@ -425,13 +421,11 @@ void Credits::initPlayers() {
void Credits::updateBlackRects(float deltaTime) {
static float current_step_ = static_cast<float>(steps_);
constexpr float BLACK_RECT_INTERVAL_S = 4.0f / 60.0f; // ~0.067s (cada 4 frames)
static float black_rect_accumulator = 0.0f;
if (top_black_rect_.h != param.game.game_area.center_y - 1 && bottom_black_rect_.y != param.game.game_area.center_y + 1) {
// Si los rectangulos superior e inferior no han llegado al centro
black_rect_accumulator += deltaTime;
if (black_rect_accumulator >= BLACK_RECT_INTERVAL_S) {
black_rect_accumulator -= BLACK_RECT_INTERVAL_S;
credits_state_.black_rect_accumulator += deltaTime;
if (credits_state_.black_rect_accumulator >= BLACK_RECT_INTERVAL_S) {
credits_state_.black_rect_accumulator -= BLACK_RECT_INTERVAL_S;
// Incrementa la altura del rectangulo superior
top_black_rect_.h = std::min(top_black_rect_.h + 1, param.game.game_area.center_y - 1);
@@ -487,12 +481,12 @@ void Credits::updateAllFades(float deltaTime) {
updateRedRect();
}
fade_in_->update(); // Fade ya usa tiempo interno
if (fade_in_->hasEnded()) {
fade_in_->update();
if (fade_in_->hasEnded() && Audio::get()->getMusicState() != Audio::MusicState::PLAYING) {
Audio::get()->playMusic("credits.ogg");
}
fade_out_->update(); // Fade ya usa tiempo interno
fade_out_->update();
if (fade_out_->hasEnded()) {
Section::name = Section::Name::HI_SCORE_TABLE;
}
@@ -515,33 +509,33 @@ void Credits::cycleColors() {
constexpr int UPPER_LIMIT = 140; // Límite superior
constexpr int LOWER_LIMIT = 30; // Límite inferior
static auto r_ = static_cast<float>(UPPER_LIMIT);
static auto g_ = static_cast<float>(LOWER_LIMIT);
static auto b_ = static_cast<float>(LOWER_LIMIT);
static float step_r_ = -0.5F; // Paso flotante para transiciones suaves
static float step_g_ = 0.3F;
static float step_b_ = 0.1F;
// Inicializar valores RGB si es la primera vez
if (credits_state_.r == 255.0f && credits_state_.g == 0.0f && credits_state_.b == 0.0f && credits_state_.step_r == -0.5f) {
credits_state_.r = static_cast<float>(UPPER_LIMIT);
credits_state_.g = static_cast<float>(LOWER_LIMIT);
credits_state_.b = static_cast<float>(LOWER_LIMIT);
}
// Ajustar valores de R
r_ += step_r_;
if (r_ >= UPPER_LIMIT || r_ <= LOWER_LIMIT) {
step_r_ = -step_r_; // Cambia de dirección al alcanzar los límites
credits_state_.r += credits_state_.step_r;
if (credits_state_.r >= UPPER_LIMIT || credits_state_.r <= LOWER_LIMIT) {
credits_state_.step_r = -credits_state_.step_r; // Cambia de dirección al alcanzar los límites
}
// Ajustar valores de G
g_ += step_g_;
if (g_ >= UPPER_LIMIT || g_ <= LOWER_LIMIT) {
step_g_ = -step_g_; // Cambia de dirección al alcanzar los límites
credits_state_.g += credits_state_.step_g;
if (credits_state_.g >= UPPER_LIMIT || credits_state_.g <= LOWER_LIMIT) {
credits_state_.step_g = -credits_state_.step_g; // Cambia de dirección al alcanzar los límites
}
// Ajustar valores de B
b_ += step_b_;
if (b_ >= UPPER_LIMIT || b_ <= LOWER_LIMIT) {
step_b_ = -step_b_; // Cambia de dirección al alcanzar los límites
credits_state_.b += credits_state_.step_b;
if (credits_state_.b >= UPPER_LIMIT || credits_state_.b <= LOWER_LIMIT) {
credits_state_.step_b = -credits_state_.step_b; // Cambia de dirección al alcanzar los límites
}
// Aplicar el color, redondeando a enteros antes de usar
color_ = Color(static_cast<int>(r_), static_cast<int>(g_), static_cast<int>(b_));
color_ = Color(static_cast<int>(credits_state_.r), static_cast<int>(credits_state_.g), static_cast<int>(credits_state_.b));
tiled_bg_->setColor(color_);
}

View File

@@ -65,6 +65,20 @@ class Credits {
int initial_volume_ = Options::audio.music.volume; // Volumen inicial
int steps_ = 0; // Pasos para reducir audio
// --- Estado de acumuladores para animaciones ---
struct CreditsState {
float texture_accumulator = 0.0f;
float balloon_accumulator = 0.0f;
float powerball_accumulator = 0.0f;
float black_rect_accumulator = 0.0f;
float r = 255.0f; // UPPER_LIMIT
float g = 0.0f; // LOWER_LIMIT
float b = 0.0f; // LOWER_LIMIT
float step_r = -0.5f;
float step_g = 0.3f;
float step_b = 0.1f;
} credits_state_;
// --- Rectángulos de renderizado ---
// Texto de créditos
SDL_FRect credits_rect_src_ = param.game.game_area.rect;

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,8 @@
#include <string> // Para string
#include <vector> // Para vector
#include "bullet.h" // Para Bullet
#include "bullet_manager.h" // Para BulletManager
#include "hit.h" // Para Hit
#include "item.h" // Para Item, ItemType
#include "manage_hiscore_table.h" // Para HiScoreEntry
@@ -14,12 +16,13 @@
#include "player.h" // Para Player
#include "smart_sprite.h" // Para SmartSprite
#include "stage.h" // Para StageManager
#include "utils.h" // Para Demo
#include "demo.h" // Para Demo
#include "utils.h" // Para otras utilidades
class Background;
class Balloon;
class BalloonManager;
class Bullet;
class BulletManager;
class Fade;
class Input;
class PauseManager;
@@ -27,7 +30,6 @@ class Scoreboard;
class Screen;
class Tabe;
class Texture;
enum class BulletType : Uint8;
namespace Difficulty {
enum class Code;
@@ -56,8 +58,8 @@ class Game {
static constexpr bool DEMO_ON = true; // Modo demo activado
// --- Constructor y destructor ---
Game(Player::Id player_id, int current_stage, bool demo); // Constructor principal
~Game(); // Destructor
Game(Player::Id player_id, int current_stage, bool demo_enabled); // Constructor principal
~Game(); // Destructor
// --- Bucle principal ---
void run(); // Ejecuta el bucle principal del juego
@@ -75,11 +77,11 @@ class Game {
// --- Constantes de tiempo (en segundos) ---
static constexpr float HELP_COUNTER_S = 16.667f; // Contador de ayuda (1000 frames a 60fps → segundos)
static constexpr float GAME_COMPLETED_START_FADE_S = 8.333f; // Inicio del fade al completar (500 frames → segundos)
static constexpr float GAME_COMPLETED_END_S = 11.667f; // Fin del juego completado (700 frames → segundos)
static constexpr float GAME_OVER_DURATION_S = 5.833f; // Duración game over (350 frames → segundos)
static constexpr float TIME_STOPPED_DURATION_S = 6.0f; // Duración del tiempo detenido (360 frames → segundos)
static constexpr float DEMO_FADE_PRE_DURATION_S = 0.5f; // Pre-duración del fade en modo demo
static constexpr float GAME_COMPLETED_START_FADE_S = 8.333f; // Inicio del fade al completar (500 frames → segundos)
static constexpr float GAME_COMPLETED_END_S = 11.667f; // Fin del juego completado (700 frames → segundos)
static constexpr float GAME_OVER_DURATION_S = 8.5f;
static constexpr float TIME_STOPPED_DURATION_S = 6.0f;
static constexpr float DEMO_FADE_PRE_DURATION_S = 0.5f;
static constexpr int ITEM_POINTS_1_DISK_ODDS = 10;
static constexpr int ITEM_POINTS_2_GAVINA_ODDS = 6;
static constexpr int ITEM_POINTS_3_PACMAR_ODDS = 3;
@@ -112,15 +114,14 @@ class Game {
};
// --- Objetos y punteros ---
SDL_Renderer *renderer_; // El renderizador de la ventana
Screen *screen_; // Objeto encargado de dibujar en pantalla
Input *input_; // Manejador de entrada
Scoreboard *scoreboard_; // Objeto para dibujar el marcador
SDL_Renderer* renderer_; // El renderizador de la ventana
Screen* screen_; // Objeto encargado de dibujar en pantalla
Input* input_; // Manejador de entrada
Scoreboard* scoreboard_; // Objeto para dibujar el marcador
SDL_Texture *canvas_; // Textura para dibujar la zona de juego
SDL_Texture* canvas_; // Textura para dibujar la zona de juego
std::vector<std::shared_ptr<Player>> players_; // Vector con los jugadores
std::vector<std::shared_ptr<Bullet>> bullets_; // Vector con las balas
std::vector<std::unique_ptr<Item>> items_; // Vector con los items
std::vector<std::unique_ptr<SmartSprite>> smart_sprites_; // Vector con los smartsprites
std::vector<std::unique_ptr<PathSprite>> path_sprites_; // Vector con los pathsprites
@@ -136,6 +137,7 @@ class Game {
std::unique_ptr<PauseManager> pause_manager_; // Objeto para gestionar la pausa
std::unique_ptr<StageManager> stage_manager_; // Objeto para gestionar las fases
std::unique_ptr<BalloonManager> balloon_manager_; // Objeto para gestionar los globos
std::unique_ptr<BulletManager> bullet_manager_; // Objeto para gestionar las balas
std::unique_ptr<Background> background_; // Objeto para dibujar el fondo del juego
std::unique_ptr<Fade> fade_in_; // Objeto para renderizar fades
std::unique_ptr<Fade> fade_out_; // Objeto para renderizar fades
@@ -165,6 +167,39 @@ class Game {
std::vector<std::shared_ptr<Player>> players_to_put_at_front_;
Hit hit_; // Para representar colisiones en pantalla
// Estructuras para gestionar flags de eventos basados en tiempo
struct GameOverFlags {
bool music_fade_triggered = false;
bool message_triggered = false;
bool fade_out_triggered = false;
void reset() {
music_fade_triggered = false;
message_triggered = false;
fade_out_triggered = false;
}
} game_over_flags_;
struct GameCompletedFlags {
bool start_celebrations_triggered = false;
bool end_celebrations_triggered = false;
void reset() {
start_celebrations_triggered = false;
end_celebrations_triggered = false;
}
} game_completed_flags_;
struct TimeStoppedFlags {
bool color_flash_sound_played = false;
bool warning_phase_started = false;
void reset() {
color_flash_sound_played = false;
warning_phase_started = false;
}
} time_stopped_flags_;
#ifdef _DEBUG
bool auto_pop_balloons_ = false; // Si es true, incrementa automaticamente los globos explotados
#endif
@@ -185,7 +220,7 @@ class Game {
void updateGameStateShowingGetReadyMessage(float deltaTime); // Gestiona el estado de mensaje "preparado"
void updateGameStatePlaying(float deltaTime); // Gestiona el estado de juego activo
void updateGameStateCompleted(float deltaTime); // Gestiona el estado de juego completado
void updateGameStateGameOver(float deltaTime); // Gestiona el estado de fin de partida
void updateGameStateGameOver(float deltaTime); // Gestiona las actualizaciones continuas del estado de fin de partida
// --- Gestión de jugadores ---
void initPlayers(Player::Id player_id); // Inicializa los datos de los jugadores
@@ -203,42 +238,38 @@ class Game {
auto allPlayersAreNotPlaying() -> bool; // Verifica si ningún jugador está activo
// --- Colisiones de jugadores ---
void handlePlayerCollision(std::shared_ptr<Player> &player, std::shared_ptr<Balloon> &balloon); // Procesa colisión de jugador con globo
auto checkPlayerBalloonCollision(std::shared_ptr<Player> &player) -> std::shared_ptr<Balloon>; // Detecta colisión jugador-globo
void checkPlayerItemCollision(std::shared_ptr<Player> &player); // Detecta colisión jugador-ítem
void handlePlayerCollision(std::shared_ptr<Player>& player, std::shared_ptr<Balloon>& balloon); // Procesa colisión de jugador con globo
auto checkPlayerBalloonCollision(std::shared_ptr<Player>& player) -> std::shared_ptr<Balloon>; // Detecta colisión jugador-globo
void checkPlayerItemCollision(std::shared_ptr<Player>& player); // Detecta colisión jugador-ítem
// --- Sistema de entrada (input) ---
void checkInput(); // Gestiona toda la entrada durante el juego
void checkPauseInput(); // Verifica solicitudes de pausa de controladores
// --- Entrada de jugadores normales ---
void handlePlayersInput(); // Gestiona entrada de todos los jugadores
void handleNormalPlayerInput(const std::shared_ptr<Player> &player); // Procesa entrada de un jugador específico
void handleFireInput(const std::shared_ptr<Player> &player, BulletType bullet_type); // Gestiona disparo de jugador
void handleFireInputs(const std::shared_ptr<Player> &player, bool autofire); // Procesa disparos automáticos
void handlePlayerContinueInput(const std::shared_ptr<Player> &player); // Permite continuar al jugador
void handlePlayerWaitingInput(const std::shared_ptr<Player> &player); // Permite (re)entrar al jugador
void handleNameInput(const std::shared_ptr<Player> &player); // Gestiona entrada de nombre del jugador
void handlePlayersInput(); // Gestiona entrada de todos los jugadores
void handleNormalPlayerInput(const std::shared_ptr<Player>& player); // Procesa entrada de un jugador específico
void handleFireInput(const std::shared_ptr<Player>& player, Bullet::Type type); // Gestiona disparo de jugador
void handleFireInputs(const std::shared_ptr<Player>& player, bool autofire); // Procesa disparos automáticos
void handlePlayerContinueInput(const std::shared_ptr<Player>& player); // Permite continuar al jugador
void handlePlayerWaitingInput(const std::shared_ptr<Player>& player); // Permite (re)entrar al jugador
void handleNameInput(const std::shared_ptr<Player>& player); // Gestiona entrada de nombre del jugador
// --- Entrada en modo demo ---
void demoHandleInput(); // Gestiona entrada durante el modo demostración
void demoHandlePassInput(); // Permite saltar la demostración
void demoHandlePlayerInput(const std::shared_ptr<Player> &player, int index); // Procesa entrada de jugador en demo
void demoHandlePlayerInput(const std::shared_ptr<Player>& player, int index); // Procesa entrada de jugador en demo
// --- Sistema de balas y proyectiles ---
void updateBullets(float deltaTime); // Actualiza posición y estado de todas las balas (time-based)
void renderBullets(); // Renderiza todas las balas activas
void createBullet(int x, int y, BulletType kind, bool powered_up, Player::Id owner); // Crea una nueva bala
void checkBulletCollision(); // Verifica colisiones de todas las balas
void freeBullets(); // Libera memoria del vector de balas
void checkBulletCollision(); // Verifica colisiones de todas las balas (delegado a BulletManager)
// --- Colisiones específicas de balas ---
auto checkBulletTabeCollision(const std::shared_ptr<Bullet> &bullet) -> bool; // Detecta colisión bala-Tabe
auto checkBulletBalloonCollision(const std::shared_ptr<Bullet> &bullet) -> bool; // Detecta colisión bala-globo
void processBalloonHit(const std::shared_ptr<Bullet> &bullet, const std::shared_ptr<Balloon> &balloon); // Procesa impacto en globo
auto checkBulletTabeCollision(const std::shared_ptr<Bullet>& bullet) -> bool; // Detecta colisión bala-Tabe
auto checkBulletBalloonCollision(const std::shared_ptr<Bullet>& bullet) -> bool; // Detecta colisión bala-globo
void processBalloonHit(const std::shared_ptr<Bullet>& bullet, const std::shared_ptr<Balloon>& balloon); // Procesa impacto en globo
// --- Sistema de ítems y power-ups ---
void updateItems(float deltaTime); // Actualiza posición y estado de todos los ítems
void updateItems(float deltaTime); // Actualiza posición y estado de todos los ítems
void renderItems(); // Renderiza todos los ítems activos
auto dropItem() -> ItemType; // Determina aleatoriamente qué ítem soltar
void createItem(ItemType type, float x, float y); // Crea un nuevo ítem en posición específica
@@ -250,29 +281,29 @@ class Game {
void disableTimeStopItem(); // Desactiva el efecto de detener el tiempo
void updateTimeStopped(float deltaTime); // Actualiza el estado del tiempo detenido
void handleGameCompletedEvents(); // Maneja eventos del juego completado
void handleGameOverEvents(); // Maneja eventos de game over
void handleGameOverEvents(); // Maneja eventos discretos basados en tiempo durante game over
void throwCoffee(int x, int y); // Crea efecto de café arrojado al ser golpeado
// --- Gestión de caída de ítems ---
void handleItemDrop(const std::shared_ptr<Balloon> &balloon, const std::shared_ptr<Player> &player); // Gestiona caída de ítem desde globo
void handleItemDrop(const std::shared_ptr<Balloon>& balloon, const std::shared_ptr<Player>& player); // Gestiona caída de ítem desde globo
// --- Sprites inteligentes (smartsprites) ---
void updateSmartSprites(float deltaTime); // Actualiza todos los sprites con lógica propia (time-based)
void renderSmartSprites(); // Renderiza todos los sprites inteligentes
void freeSmartSprites(); // Libera memoria de sprites inteligentes
void renderSmartSprites(); // Renderiza todos los sprites inteligentes
void freeSmartSprites(); // Libera memoria de sprites inteligentes
// --- Sprites por ruta (pathsprites) ---
void updatePathSprites(float deltaTime); // Actualiza sprites que siguen rutas predefinidas
void renderPathSprites(); // Renderiza sprites animados por ruta
void freePathSprites(); // Libera memoria de sprites por ruta
void initPaths(); // Inicializa rutas predefinidas para animaciones
void renderPathSprites(); // Renderiza sprites animados por ruta
void freePathSprites(); // Libera memoria de sprites por ruta
void initPaths(); // Inicializa rutas predefinidas para animaciones
// --- Creación de sprites especiales ---
void createItemText(int x, const std::shared_ptr<Texture> &texture); // Crea texto animado para ítems
void createMessage(const std::vector<Path> &paths, const std::shared_ptr<Texture> &texture); // Crea mensaje con animación por ruta
void createItemText(int x, const std::shared_ptr<Texture>& texture); // Crea texto animado para ítems
void createMessage(const std::vector<Path>& paths, const std::shared_ptr<Texture>& texture); // Crea mensaje con animación por ruta
// --- Sistema de globos y enemigos ---
void handleBalloonDestruction(std::shared_ptr<Balloon> balloon, const std::shared_ptr<Player> &player); // Procesa destrucción de globo
void handleBalloonDestruction(std::shared_ptr<Balloon> balloon, const std::shared_ptr<Player>& player); // Procesa destrucción de globo
void handleTabeHitEffects(); // Gestiona efectos al golpear a Tabe
void checkAndUpdateBalloonSpeed(); // Ajusta velocidad de globos según progreso
@@ -285,39 +316,39 @@ class Game {
void setMenace(); // Calcula y establece amenaza según globos activos
// --- Puntuación y marcador ---
void updateHiScore(); // Actualiza el récord máximo si es necesario
void updateScoreboard(); // Actualiza la visualización del marcador
void updateHiScoreName(); // Pone en el marcador el nombre del primer jugador de la tabla
void updateHiScore(); // Actualiza el récord máximo si es necesario
void updateScoreboard(float deltaTime); // Actualiza la visualización del marcador
void updateHiScoreName(); // Pone en el marcador el nombre del primer jugador de la tabla
void initScoreboard(); // Inicializa el sistema de puntuación
// --- Modo demostración ---
void initDemo(Player::Id player_id); // Inicializa variables para el modo demostración
void updateDemo(); // Actualiza lógica específica del modo demo
void updateDemo(float deltaTime); // Actualiza lógica específica del modo demo
// --- Recursos y renderizado ---
void setResources(); // Asigna texturas y animaciones a los objetos
void setResources(); // Asigna texturas y animaciones a los objetos
void updateBackground(float deltaTime); // Actualiza elementos del fondo (time-based)
void fillCanvas(); // Renderiza elementos del área de juego en su textura
void updateHelper(); // Actualiza variables auxiliares de renderizado
void fillCanvas(); // Renderiza elementos del área de juego en su textura
void updateHelper(); // Actualiza variables auxiliares de renderizado
// --- Sistema de audio ---
static void playMusic(); // Reproduce la música de fondo
void stopMusic() const; // Detiene la reproducción de música
static void pauseMusic(); // Pausa la música
static void resumeMusic(); // Retoma la música que eestaba pausada
void playSound(const std::string &name) const; // Reproduce un efecto de sonido específico
static void playMusic(const std::string& music_file, int loop = -1); // Reproduce la música de fondo
void stopMusic() const; // Detiene la reproducción de música
static void pauseMusic(); // Pausa la música
static void resumeMusic(); // Retoma la música que eestaba pausada
void playSound(const std::string& name) const; // Reproduce un efecto de sonido específico
void sendPlayerToTheBack(const std::shared_ptr<Player> &player); // Mueve el jugador para pintarlo al fondo de la lista de jugadores
void sendPlayerToTheFront(const std::shared_ptr<Player> &player); // Mueve el jugador para pintarlo el primero de la lista de jugadores
void sendPlayerToTheBack(const std::shared_ptr<Player>& player); // Mueve el jugador para pintarlo al fondo de la lista de jugadores
void sendPlayerToTheFront(const std::shared_ptr<Player>& player); // Mueve el jugador para pintarlo el primero de la lista de jugadores
void onPauseStateChanged(bool is_paused);
// SISTEMA DE GRABACIÓN (CONDICIONAL)
#ifdef RECORDING
void updateRecording(); // Actualiza variables durante modo de grabación
void updateRecording(float deltaTime); // Actualiza variables durante modo de grabación
#endif
// --- Depuración (solo en modo DEBUG) ---
#ifdef _DEBUG
void handleDebugEvents(const SDL_Event &event); // Comprueba los eventos en el modo DEBUG
void handleDebugEvents(const SDL_Event& event); // Comprueba los eventos en el modo DEBUG
#endif
};

View File

@@ -368,17 +368,14 @@ void HiScoreTable::glowEntryNames() {
// Gestiona el contador
void HiScoreTable::updateCounter() {
static bool background_changed = false;
static bool fade_activated = false;
if (elapsed_time_ >= BACKGROUND_CHANGE_S && !background_changed) {
if (elapsed_time_ >= BACKGROUND_CHANGE_S && !hiscore_flags_.background_changed) {
background_->setColor(background_fade_color_.DARKEN());
background_->setAlpha(96);
background_changed = true;
hiscore_flags_.background_changed = true;
}
if (elapsed_time_ >= COUNTER_END_S && !fade_activated) {
if (elapsed_time_ >= COUNTER_END_S && !hiscore_flags_.fade_activated) {
fade_->activate();
fade_activated = true;
hiscore_flags_.fade_activated = true;
}
}

View File

@@ -56,6 +56,17 @@ class HiScoreTable {
Color background_fade_color_; // Color de atenuación del fondo
std::vector<Color> entry_colors_; // Colores para destacar las entradas en la tabla
// --- Flags para eventos basados en tiempo ---
struct HiScoreFlags {
bool background_changed = false;
bool fade_activated = false;
void reset() {
background_changed = false;
fade_activated = false;
}
} hiscore_flags_;
// --- Métodos internos ---
void update(float delta_time); // Actualiza las variables
void render(); // Pinta en pantalla

View File

@@ -14,6 +14,7 @@
#include "global_events.h" // Para check
#include "global_inputs.h" // Para check
#include "input.h" // Para Input
#include "item.h" // Para Item
#include "lang.h" // Para getText
#include "param.h" // Para Param, param, ParamGame, ParamFade, Param...
#include "resource.h" // Para Resource
@@ -78,43 +79,43 @@ void Instructions::iniSprites() {
// Inicializa los sprites
for (int i = 0; i < (int)item_textures_.size(); ++i) {
auto sprite = std::make_unique<Sprite>(item_textures_[i], 0, 0, param.game.item_size, param.game.item_size);
sprite->setPosition((SDL_FPoint){sprite_pos_.x, sprite_pos_.y + ((param.game.item_size + item_space_) * i)});
auto sprite = std::make_unique<Sprite>(item_textures_[i], 0, 0, Item::WIDTH, Item::HEIGHT);
sprite->setPosition((SDL_FPoint){sprite_pos_.x, sprite_pos_.y + ((Item::HEIGHT + item_space_) * i)});
sprites_.push_back(std::move(sprite));
}
}
// Actualiza los sprites
void Instructions::updateSprites() {
SDL_FRect src_rect = {0, 0, param.game.item_size, param.game.item_size};
SDL_FRect src_rect = {0, 0, Item::WIDTH, Item::HEIGHT};
// Disquito (desplazamiento 12/60 = 0.2s)
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.2f) / SPRITE_ANIMATION_CYCLE_S) % 2);
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.2f) / SPRITE_ANIMATION_CYCLE_S) % 2);
sprites_[0]->setSpriteClip(src_rect);
// Gavina (desplazamiento 9/60 = 0.15s)
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.15f) / SPRITE_ANIMATION_CYCLE_S) % 2);
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.15f) / SPRITE_ANIMATION_CYCLE_S) % 2);
sprites_[1]->setSpriteClip(src_rect);
// Pacmar (desplazamiento 6/60 = 0.1s)
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.1f) / SPRITE_ANIMATION_CYCLE_S) % 2);
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.1f) / SPRITE_ANIMATION_CYCLE_S) % 2);
sprites_[2]->setSpriteClip(src_rect);
// Time Stopper (desplazamiento 3/60 = 0.05s)
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.05f) / SPRITE_ANIMATION_CYCLE_S) % 2);
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.05f) / SPRITE_ANIMATION_CYCLE_S) % 2);
sprites_[3]->setSpriteClip(src_rect);
// Coffee (sin desplazamiento)
src_rect.y = param.game.item_size * (static_cast<int>(elapsed_time_ / SPRITE_ANIMATION_CYCLE_S) % 2);
src_rect.y = Item::HEIGHT * (static_cast<int>(elapsed_time_ / SPRITE_ANIMATION_CYCLE_S) % 2);
sprites_[4]->setSpriteClip(src_rect);
}
// Rellena la textura de texto
void Instructions::fillTexture() {
const int X_OFFSET = param.game.item_size + 8;
const int X_OFFSET = Item::WIDTH + 8;
// Modifica el renderizador para pintar en la textura
auto *temp = SDL_GetRenderTarget(renderer_);
auto* temp = SDL_GetRenderTarget(renderer_);
SDL_SetRenderTarget(renderer_, texture_);
// Limpia la textura
@@ -130,7 +131,7 @@ void Instructions::fillTexture() {
constexpr int SPACE_POST_HEADER = 20;
constexpr int SPACE_PRE_HEADER = 28;
const int SPACE_BETWEEN_LINES = text_->getCharacterSize() * 1.5F;
const int SPACE_BETWEEN_ITEM_LINES = param.game.item_size + item_space_;
const int SPACE_BETWEEN_ITEM_LINES = Item::HEIGHT + item_space_;
const int SPACE_NEW_PARAGRAPH = SPACE_BETWEEN_LINES * 0.5F;
const int SIZE = (NUM_LINES * SPACE_BETWEEN_LINES) + (NUM_ITEM_LINES * SPACE_BETWEEN_ITEM_LINES) + (NUM_POST_HEADERS * SPACE_POST_HEADER) + (NUM_PRE_HEADERS * SPACE_PRE_HEADER) + (SPACE_NEW_PARAGRAPH);
@@ -144,7 +145,7 @@ void Instructions::fillTexture() {
Lang::getText("[INSTRUCTIONS] 09"),
Lang::getText("[INSTRUCTIONS] 10"),
Lang::getText("[INSTRUCTIONS] 11")};
for (const auto &desc : ITEM_DESCRIPTIONS) {
for (const auto& desc : ITEM_DESCRIPTIONS) {
const int L = text_->length(desc);
length = L > length ? L : length;
}
@@ -178,13 +179,13 @@ void Instructions::fillTexture() {
// Da valor a la variable
sprite_pos_.x = ANCHOR_ITEM;
sprite_pos_.y = ANCHOR3 - ((param.game.item_size - text_->getCharacterSize()) / 2);
sprite_pos_.y = ANCHOR3 - ((Item::HEIGHT - text_->getCharacterSize()) / 2);
}
// Rellena el backbuffer
void Instructions::fillBackbuffer() {
// Modifica el renderizador para pintar en la textura
auto *temp = SDL_GetRenderTarget(renderer_);
auto* temp = SDL_GetRenderTarget(renderer_);
SDL_SetRenderTarget(renderer_, backbuffer_);
// Limpia la textura
@@ -195,7 +196,7 @@ void Instructions::fillBackbuffer() {
SDL_RenderTexture(renderer_, texture_, nullptr, nullptr);
// Dibuja los sprites
for (auto &sprite : sprites_) {
for (auto& sprite : sprites_) {
sprite->render();
}
@@ -207,7 +208,7 @@ void Instructions::fillBackbuffer() {
void Instructions::update(float delta_time) {
elapsed_time_ += delta_time; // Incrementa el tiempo transcurrido
static auto *const SCREEN = Screen::get();
static auto* const SCREEN = Screen::get();
SCREEN->update(delta_time); // Actualiza el objeto screen
Audio::update(); // Actualiza el objeto audio
@@ -220,7 +221,7 @@ void Instructions::update(float delta_time) {
// Pinta en pantalla
void Instructions::render() {
static auto *const SCREEN = Screen::get();
static auto* const SCREEN = Screen::get();
SCREEN->start(); // Prepara para empezar a dibujar en la textura de juego
SCREEN->clean(); // Limpia la pantalla
@@ -287,11 +288,11 @@ auto Instructions::initializeLines(int height) -> std::vector<Line> {
}
// Método para mover las líneas con suavizado
auto Instructions::moveLines(std::vector<Line> &lines, int width, float duration, Uint32 start_delay) -> bool {
auto Instructions::moveLines(std::vector<Line>& lines, int width, float duration, Uint32 start_delay) -> bool {
Uint32 current_time = SDL_GetTicks();
bool all_lines_off_screen = true;
for (auto &line : lines) {
for (auto& line : lines) {
// Establecer start_time en el primer cuadro de animación
if (line.start_time == 0) {
line.start_time = current_time + line.y * start_delay;
@@ -316,8 +317,8 @@ auto Instructions::moveLines(std::vector<Line> &lines, int width, float duration
}
// Método para renderizar las líneas
void Instructions::renderLines(SDL_Renderer *renderer, SDL_Texture *texture, const std::vector<Line> &lines) {
for (const auto &line : lines) {
void Instructions::renderLines(SDL_Renderer* renderer, SDL_Texture* texture, const std::vector<Line>& lines) {
for (const auto& line : lines) {
SDL_FRect src_rect = {0, static_cast<float>(line.y), 320, 1};
SDL_FRect dst_rect = {static_cast<float>(line.x), static_cast<float>(line.y), 320, 1};
SDL_RenderTexture(renderer, texture, &src_rect, &dst_rect);

View File

@@ -208,7 +208,7 @@ void Intro::switchText(int from_index, int to_index) {
// Actualiza las variables del objeto
void Intro::update(float delta_time) {
static auto *const SCREEN = Screen::get();
static auto* const SCREEN = Screen::get();
SCREEN->update(delta_time); // Actualiza el objeto screen
Audio::update(); // Actualiza el objeto Audio
@@ -229,7 +229,7 @@ void Intro::update(float delta_time) {
// Dibuja el objeto en pantalla
void Intro::render() {
static auto *const SCREEN = Screen::get();
static auto* const SCREEN = Screen::get();
SCREEN->start(); // Prepara para empezar a dibujar en la textura de juego
SCREEN->clean(); // Limpia la pantalla
@@ -302,7 +302,7 @@ void Intro::initSprites() {
card_texture->setBlendMode(SDL_BLENDMODE_BLEND);
// Apuntamos el renderizador a la textura
auto *temp = SDL_GetRenderTarget(Screen::get()->getRenderer());
auto* temp = SDL_GetRenderTarget(Screen::get()->getRenderer());
card_texture->setAsRenderTarget(Screen::get()->getRenderer());
// Limpia la textura
@@ -343,7 +343,7 @@ void Intro::initSprites() {
card_sprites_.at(2)->addPath(-CARD_HEIGHT, Y_DEST, PathType::VERTICAL, X_DEST, CARD_ANIM_DURATION_FAST, easeOutQuint, 0.0f);
card_sprites_.at(3)->addPath(param.game.height, Y_DEST, PathType::VERTICAL, X_DEST, CARD_ANIM_DURATION_VERY_SLOW, easeInOutExpo, 0.0f);
card_sprites_.at(4)->addPath(-CARD_HEIGHT, Y_DEST, PathType::VERTICAL, X_DEST, CARD_ANIM_DURATION_MEDIUM, easeOutElastic, 0.0f);
card_sprites_.at(5)->addPath(-CARD_HEIGHT, Y_DEST, PathType::VERTICAL, X_DEST, CARD_ANIM_DURATION_SLOW, easeOutQuad, CARD_ANIM_DELAY_LONG);
card_sprites_.at(5)->addPath(-CARD_HEIGHT, Y_DEST, PathType::VERTICAL, X_DEST, CARD_ANIM_DURATION_SLOW, easeOutQuad, CARD_ANIM_DELAY_LONG_S);
card_sprites_.at(5)->addPath(X_DEST, -CARD_WIDTH, PathType::HORIZONTAL, Y_DEST, CARD_ANIM_DURATION_SHORT, easeInElastic, 0.0f);
// Constantes
@@ -357,7 +357,7 @@ void Intro::initSprites() {
shadow_texture->setBlendMode(SDL_BLENDMODE_BLEND);
// Apuntamos el renderizador a la textura
auto *temp = SDL_GetRenderTarget(Screen::get()->getRenderer());
auto* temp = SDL_GetRenderTarget(Screen::get()->getRenderer());
shadow_texture->setAsRenderTarget(Screen::get()->getRenderer());
// Limpia la textura
@@ -394,7 +394,7 @@ void Intro::initSprites() {
shadow_sprites_.at(2)->addPath(-SHADOW_SPRITE_WIDTH, S_X_DEST, PathType::HORIZONTAL, S_Y_DEST, CARD_ANIM_DURATION_FAST, easeOutQuint, 0.0f);
shadow_sprites_.at(3)->addPath(-SHADOW_SPRITE_HEIGHT, S_Y_DEST, PathType::VERTICAL, S_X_DEST, CARD_ANIM_DURATION_VERY_SLOW, easeInOutExpo, 0.0f);
shadow_sprites_.at(4)->addPath(param.game.height, S_Y_DEST, PathType::VERTICAL, S_X_DEST, CARD_ANIM_DURATION_MEDIUM, easeOutElastic, 0.0f);
shadow_sprites_.at(5)->addPath(param.game.width, S_X_DEST, PathType::HORIZONTAL, S_Y_DEST, CARD_ANIM_DURATION_SLOW, easeOutQuad, CARD_ANIM_DELAY_LONG);
shadow_sprites_.at(5)->addPath(param.game.width, S_X_DEST, PathType::HORIZONTAL, S_Y_DEST, CARD_ANIM_DURATION_SLOW, easeOutQuad, CARD_ANIM_DELAY_LONG_S);
shadow_sprites_.at(5)->addPath(S_X_DEST, param.game.width, PathType::HORIZONTAL, S_Y_DEST, CARD_ANIM_DURATION_SHORT, easeInElastic, 0.0f);
}
@@ -447,25 +447,25 @@ void Intro::initTexts() {
texts_.at(8)->setCaption(Lang::getText("[INTRO] 9"));
texts_.at(8)->setSpeedS(TEXT_SPEED_ULTRA_FAST);
for (auto &text : texts_) {
for (auto& text : texts_) {
text->center(param.game.game_area.center_x);
}
}
// Actualiza los sprites
void Intro::updateSprites(float delta_time) {
for (auto &sprite : card_sprites_) {
for (auto& sprite : card_sprites_) {
sprite->update(delta_time);
}
for (auto &sprite : shadow_sprites_) {
for (auto& sprite : shadow_sprites_) {
sprite->update(delta_time);
}
}
// Actualiza los textos
void Intro::updateTexts(float delta_time) {
for (auto &text : texts_) {
for (auto& text : texts_) {
text->updateS(delta_time); // Usar updateS para delta_time en segundos
}
}
@@ -478,7 +478,7 @@ void Intro::renderSprites() {
// Dibuja los textos
void Intro::renderTexts() {
for (const auto &text : texts_) {
for (const auto& text : texts_) {
text->render();
}
}

View File

@@ -65,8 +65,8 @@ class Intro {
static constexpr float CARD_ANIM_DURATION_SLOW = 250.0f / 60.0f; // ≈ 4.1667 s
static constexpr float CARD_ANIM_DURATION_VERY_SLOW = 300.0f / 60.0f; // ≈ 5.0000 s
static constexpr float CARD_ANIM_DELAY_LONG = 0.45f; // Retraso largo antes de animación
static constexpr float CARD_OFFSET_MARGIN = 10.0f; // Margen fuera de pantalla
static constexpr float CARD_ANIM_DELAY_LONG_S = 7.5F; // Retraso largo antes de animación
static constexpr float CARD_OFFSET_MARGIN = 10.0F; // Margen fuera de pantalla
// --- Estados internos ---
enum class State {

View File

@@ -43,7 +43,11 @@ Title::Title()
game_logo_(std::make_unique<GameLogo>(param.game.game_area.center_x, param.title.title_c_c_position)),
mini_logo_sprite_(std::make_unique<Sprite>(Resource::get()->getTexture("logo_jailgames_mini.png"))),
state_(State::LOGO_ANIMATING),
num_controllers_(Input::get()->getNumGamepads()) {
num_controllers_(Input::get()->getNumGamepads())
#ifdef _DEBUG
, debug_color_(param.title.bg_color)
#endif
{
// Configura objetos
tiled_bg_->setColor(param.title.bg_color);
tiled_bg_->setSpeed(0.0F);
@@ -87,10 +91,7 @@ void Title::update(float deltaTime) {
updateFade();
updateState(deltaTime);
updateStartPrompt(deltaTime);
for (auto& player : players_) {
player->update(deltaTime);
}
updatePlayers(deltaTime);
}
// Calcula el tiempo transcurrido desde el último frame
@@ -141,13 +142,11 @@ void Title::handleKeyDownEvent(const SDL_Event& event) {
#ifdef _DEBUG
void Title::handleDebugColorKeys(SDL_Keycode key) {
static Color color_ = param.title.bg_color;
adjustColorComponent(key, color_);
adjustColorComponent(key, debug_color_);
counter_time_ = 0.0f;
tiled_bg_->setColor(color_);
printColorValue(color_);
tiled_bg_->setColor(debug_color_);
printColorValue(debug_color_);
}
void Title::adjustColorComponent(SDL_Keycode key, Color& color) {
@@ -567,6 +566,13 @@ void Title::initPlayers() {
}
}
// Actualiza los jugadores
void Title::updatePlayers(float deltaTime) {
for (auto& player : players_) {
player->update(deltaTime);
}
}
// Renderiza los jugadores
void Title::renderPlayers() {
for (auto const& player : players_) {

View File

@@ -99,6 +99,10 @@ class Title {
bool player1_start_pressed_ = false; // Indica si se ha pulsado el botón de empezar para el jugador 1
bool player2_start_pressed_ = false; // Indica si se ha pulsado el botón de empezar para el jugador 2
#ifdef _DEBUG
Color debug_color_; // Color para depuración en modo debug
#endif
// --- Ciclo de vida del título ---
void update(float deltaTime); // Actualiza las variables del objeto
float calculateDeltaTime(); // Calcula el tiempo transcurrido desde el último frame
@@ -121,7 +125,7 @@ class Title {
// --- Gestión de jugadores ---
void initPlayers(); // Inicializa los jugadores
void updatePlayers(); // Actualiza los jugadores
void updatePlayers(float deltaTime); // Actualiza los jugadores
void renderPlayers(); // Renderiza los jugadores
auto getPlayer(Player::Id id) -> std::shared_ptr<Player>; // Obtiene un jugador a partir de su "id"

View File

@@ -151,6 +151,51 @@ auto StageManager::jumpToStage(size_t target_stage_index) -> bool {
return true;
}
auto StageManager::setTotalPower(int target_total_power) -> bool {
if (target_total_power < 0) {
return false;
}
int total_power_needed = getTotalPowerNeededToCompleteGame();
if (target_total_power > total_power_needed) {
return false;
}
// Calcular en qué fase debería estar y cuánto poder de esa fase
int accumulated_power = 0;
size_t target_stage_index = 0;
int target_current_power = 0;
for (size_t i = 0; i < stages_.size(); ++i) {
int stage_power = stages_[i].getPowerToComplete();
if (accumulated_power + stage_power > target_total_power) {
// El objetivo está dentro de esta fase
target_stage_index = i;
target_current_power = target_total_power - accumulated_power;
break;
}
accumulated_power += stage_power;
if (accumulated_power == target_total_power) {
// El objetivo coincide exactamente con el final de esta fase
// Mover a la siguiente fase (si existe) con power 0
target_stage_index = (i + 1 < stages_.size()) ? i + 1 : i;
target_current_power = (i + 1 < stages_.size()) ? 0 : stage_power;
break;
}
}
// Actualizar estado
current_stage_index_ = target_stage_index;
current_power_ = target_current_power;
total_power_ = target_total_power;
updateStageStatuses();
return true;
}
auto StageManager::subtractPower(int amount) -> bool {
if (amount <= 0 || current_power_ < amount) {
return false;

View File

@@ -67,6 +67,7 @@ class StageManager : public IStageInfo {
// --- Navegación ---
auto jumpToStage(size_t target_stage_index) -> bool; // Salta a una fase específica
auto setTotalPower(int target_total_power) -> bool; // Establece el poder total y ajusta fase/progreso
// --- Consultas de estado ---
[[nodiscard]] auto getCurrentStage() const -> std::optional<StageData>; // Obtiene la fase actual

View File

@@ -38,7 +38,7 @@ Texture::Texture(SDL_Renderer *renderer, std::string path)
surface_ = loadSurface(path_);
// Añade la propia paleta del fichero a la lista
addPaletteFromGifFile(path_);
addPaletteFromGifFile(path_, true); // Usar modo silencioso
// Crea la textura, establece el BlendMode y copia la surface a la textura
createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
@@ -301,7 +301,7 @@ void Texture::setPaletteColor(int palette, int index, Uint32 color) {
}
// Carga una paleta desde un fichero
auto Texture::loadPaletteFromFile(const std::string &file_path) -> Palette {
auto Texture::loadPaletteFromFile(const std::string &file_path, bool quiet) -> Palette {
Palette palette;
std::vector<Uint8> buffer;
@@ -329,7 +329,9 @@ auto Texture::loadPaletteFromFile(const std::string &file_path) -> Palette {
}
}
printWithDots("Palette : ", getFileName(file_path), "[ LOADED ]");
if (!quiet) {
printWithDots("Palette : ", getFileName(file_path), "[ LOADED ]");
}
// Usar la nueva función loadPalette, que devuelve un vector<uint32_t>
GIF::Gif gif;
@@ -349,14 +351,14 @@ auto Texture::loadPaletteFromFile(const std::string &file_path) -> Palette {
}
// Añade una paleta a la lista
void Texture::addPaletteFromGifFile(const std::string &path) {
palettes_.emplace_back(loadPaletteFromFile(path));
void Texture::addPaletteFromGifFile(const std::string &path, bool quiet) {
palettes_.emplace_back(loadPaletteFromFile(path, quiet));
setPaletteColor(palettes_.size() - 1, 0, 0x00000000);
}
// Añade una paleta a la lista
void Texture::addPaletteFromPalFile(const std::string &path) {
palettes_.emplace_back(readPalFile(path));
palettes_.emplace_back(readPalFile(path, true)); // Usar modo silencioso
setPaletteColor(palettes_.size() - 1, 0, 0x00000000);
}
@@ -372,7 +374,7 @@ void Texture::setPalette(size_t palette) {
auto Texture::getRenderer() -> SDL_Renderer * { return renderer_; }
// Carga una paleta desde un archivo .pal
auto Texture::readPalFile(const std::string &file_path) -> Palette {
auto Texture::readPalFile(const std::string &file_path, bool quiet) -> Palette {
Palette palette{};
palette.fill(0); // Inicializar todo con 0 (transparente por defecto)

View File

@@ -49,7 +49,7 @@ class Texture {
void setAlpha(Uint8 alpha); // Establece el alpha para la modulación
// --- Paletas ---
void addPaletteFromGifFile(const std::string &path); // Añade una paleta a la lista
void addPaletteFromGifFile(const std::string &path, bool quiet = false); // Añade una paleta a la lista
void addPaletteFromPalFile(const std::string &path); // Añade una paleta a la lista
void setPaletteColor(int palette, int index, Uint32 color); // Establece un color de la paleta
void setPalette(size_t palette); // Cambia la paleta de la textura
@@ -76,8 +76,8 @@ class Texture {
// --- Métodos internos ---
auto loadSurface(const std::string &file_path) -> std::shared_ptr<Surface>; // Crea una surface desde un fichero .gif
void flipSurface(); // Vuelca la surface en la textura
static auto loadPaletteFromFile(const std::string &file_path) -> Palette; // Carga una paleta desde un fichero
static auto loadPaletteFromFile(const std::string &file_path, bool quiet = false) -> Palette; // Carga una paleta desde un fichero
void unloadTexture(); // Libera la memoria de la textura
void unloadSurface(); // Libera la surface actual
static auto readPalFile(const std::string &file_path) -> Palette; // Carga una paleta desde un archivo .pal
static auto readPalFile(const std::string &file_path, bool quiet = false) -> Palette; // Carga una paleta desde un archivo .pal
};

View File

@@ -320,68 +320,6 @@ void printWithDots(const std::string &text1, const std::string &text2, const std
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%s", formatted_text.c_str());
}
// Carga el fichero de datos para la demo
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData {
DemoData dd;
SDL_IOStream *file = nullptr;
// Intentar cargar desde ResourceHelper primero
auto resource_data = ResourceHelper::loadFile(file_path);
if (!resource_data.empty()) {
file = SDL_IOFromConstMem(resource_data.data(), resource_data.size());
} else {
// Fallback a filesystem directo
file = SDL_IOFromFile(file_path.c_str(), "r+b");
}
if (file == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
throw std::runtime_error("Fichero no encontrado: " + file_path);
}
printWithDots("DemoData : ", getFileName(file_path), "[ LOADED ]");
// Lee todos los datos del fichero y los deja en el destino
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
DemoKeys dk = DemoKeys();
SDL_ReadIO(file, &dk, sizeof(DemoKeys));
dd.push_back(dk);
}
// Cierra el fichero
SDL_CloseIO(file);
return dd;
}
#ifdef RECORDING
// Guarda el fichero de datos para la demo
bool saveDemoFile(const std::string &file_path, const DemoData &dd) {
auto success = true;
auto file = SDL_IOFromFile(file_path.c_str(), "w+b");
if (file) {
// Guarda los datos
for (const auto &data : dd) {
if (SDL_RWwrite(file, &data, sizeof(DemoKeys), 1) != 1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al escribir el fichero %s", getFileName(file_path).c_str());
success = false;
break;
}
}
if (success) {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file %s", getFileName(file_path).c_str());
}
// Cierra el fichero
SDL_CloseIO(file);
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to save %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
success = false;
}
return success;
}
#endif // RECORDING
// Obtiene el nombre de un fichero a partir de una ruta completa
auto getFileName(const std::string &path) -> std::string {

View File

@@ -10,7 +10,6 @@
// --- Constantes ---
constexpr int BLOCK = 8;
constexpr int TOTAL_DEMO_DATA = 2000;
// --- Estructuras ---
struct Overrides {
@@ -32,44 +31,6 @@ struct Circle {
r(radius) {}
};
struct DemoKeys {
Uint8 left;
Uint8 right;
Uint8 no_input;
Uint8 fire;
Uint8 fire_left;
Uint8 fire_right;
explicit DemoKeys(Uint8 l = 0, Uint8 r = 0, Uint8 ni = 0, Uint8 f = 0, Uint8 fl = 0, Uint8 fr = 0)
: left(l),
right(r),
no_input(ni),
fire(f),
fire_left(fl),
fire_right(fr) {}
};
// --- Tipos ---
using DemoData = std::vector<DemoKeys>;
struct Demo {
bool enabled; // Indica si está activo el modo demo
bool recording; // Indica si está activado el modo para grabar la demo
int counter; // Contador para el modo demo
DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo
std::vector<DemoData> data; // Vector con diferentes sets de datos con los movimientos para la demo
Demo()
: enabled(false),
recording(false),
counter(0) {}
Demo(bool e, bool r, int c, const DemoKeys &k, const std::vector<DemoData> &d)
: enabled(e),
recording(r),
counter(c),
keys(k),
data(d) {}
};
struct Zone {
SDL_FRect rect; // Rectangulo que define la zona
@@ -88,21 +49,21 @@ extern Overrides overrides; // Configuración global de overrides
// Colisiones y geometría
auto distanceSquared(int x1, int y1, int x2, int y2) -> double;
auto getCollisionPoint(const Circle &a, const Circle &b) -> SDL_FPoint;
auto checkCollision(const Circle &a, const Circle &b) -> bool;
auto checkCollision(const Circle &a, const SDL_FRect &b) -> bool;
auto checkCollision(const SDL_FRect &a, const SDL_FRect &b) -> bool;
auto checkCollision(const SDL_FPoint &p, const SDL_FRect &r) -> bool;
auto getCollisionPoint(const Circle& a, const Circle& b) -> SDL_FPoint;
auto checkCollision(const Circle& a, const Circle& b) -> bool;
auto checkCollision(const Circle& a, const SDL_FRect& b) -> bool;
auto checkCollision(const SDL_FRect& a, const SDL_FRect& b) -> bool;
auto checkCollision(const SDL_FPoint& p, const SDL_FRect& r) -> bool;
// Conversión y manipulación de cadenas
auto stringToBool(const std::string &str) -> bool;
auto stringToBool(const std::string& str) -> bool;
auto boolToString(bool value) -> std::string;
auto boolToOnOff(bool value) -> std::string;
auto toLower(const std::string &str) -> std::string;
auto trim(const std::string &str) -> std::string;
auto toLower(const std::string& str) -> std::string;
auto trim(const std::string& str) -> std::string;
// Dibujo
void drawCircle(SDL_Renderer *renderer, int32_t center_x, int32_t center_y, int32_t radius);
void drawCircle(SDL_Renderer* renderer, int32_t center_x, int32_t center_y, int32_t radius);
// Funciones de suavizado (easing)
auto easeOutQuint(double time) -> double;
@@ -123,17 +84,11 @@ auto easeOutCubic(double time) -> double;
auto easeInCubic(double time) -> double;
// Utilidades varias
auto stringInVector(const std::vector<std::string> &vec, const std::string &str) -> bool; // Comprueba si un vector contiene una cadena
void printWithDots(const std::string &text1, const std::string &text2, const std::string &text3); // Imprime una línea con puntos
auto truncateWithEllipsis(const std::string &input, size_t length) -> std::string; // Trunca un string y le añade puntos suspensivos
auto stringInVector(const std::vector<std::string>& vec, const std::string& str) -> bool; // Comprueba si un vector contiene una cadena
void printWithDots(const std::string& text1, const std::string& text2, const std::string& text3); // Imprime una línea con puntos
auto truncateWithEllipsis(const std::string& input, size_t length) -> std::string; // Trunca un string y le añade puntos suspensivos
// Demo
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData;
#ifdef RECORDING
bool saveDemoFile(const std::string &file_path, const DemoData &dd);
#endif
// Ficheros y rutas
auto getFileName(const std::string &path) -> std::string; // Obtiene el nombre de un fichero a partir de una ruta
auto getPath(const std::string &full_path) -> std::string; // Obtiene la ruta eliminando el nombre del fichero
auto getFileName(const std::string& path) -> std::string; // Obtiene el nombre de un fichero a partir de una ruta
auto getPath(const std::string& full_path) -> std::string; // Obtiene la ruta eliminando el nombre del fichero

6
source/version.h.in Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
namespace Version {
constexpr const char* GIT_HASH = "@GIT_HASH@";
constexpr const char* APP_NAME = "Coffee Crisis Arcade Edition";
}

Binary file not shown.

View File

@@ -1,9 +1,10 @@
#include "../source/resource_pack.h"
#include "../build/version.h" // Para Version::APP_NAME
#include <iostream>
#include <filesystem>
void showHelp() {
std::cout << "Coffee Crisis Arcade Edition - Resource Packer" << std::endl;
std::cout << Version::APP_NAME << " - Resource Packer" << std::endl;
std::cout << "===============================================" << std::endl;
std::cout << "Usage: pack_resources [options] [input_dir] [output_file]" << std::endl;
std::cout << std::endl;
@@ -43,7 +44,8 @@ int main(int argc, char* argv[]) {
std::string dataDir = "data";
std::string outputFile = "resources.pack";
bool listMode = false;
bool dataDirSet = false;
// Parse arguments
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
@@ -56,8 +58,9 @@ int main(int argc, char* argv[]) {
outputFile = argv[++i]; // Next argument is pack file to list
}
} else if (!arg.empty() && arg[0] != '-') {
if (dataDir == "data") {
if (!dataDirSet) {
dataDir = arg;
dataDirSet = true;
} else {
outputFile = arg;
}
@@ -69,7 +72,7 @@ int main(int argc, char* argv[]) {
return 0;
}
std::cout << "Coffee Crisis Arcade Edition - Resource Packer" << std::endl;
std::cout << Version::APP_NAME << " - Resource Packer" << std::endl;
std::cout << "===============================================" << std::endl;
std::cout << "Input directory: " << dataDir << std::endl;
std::cout << "Output file: " << outputFile << std::endl;