21 Commits

Author SHA1 Message Date
0faa605ad9 resource.pack 2026-04-15 23:26:43 +02:00
c3534ace9c fix: el fade en el titol al anar a jugar es podia interrumpir continuament 2026-04-15 18:36:09 +02:00
517bc2caa1 make controllerdb
nom del mando trimmed
2026-04-15 16:16:49 +02:00
f9b0f64b81 arreglos en screen 2026-04-15 06:30:23 +02:00
e0498d642d corregit bug de fullscreen en emscripten 2026-04-13 21:03:46 +02:00
ccdf9732d1 streaming de audio 2026-04-13 20:12:04 +02:00
1451327fcc fix: la powerball sonava en la demo 2026-04-13 19:58:55 +02:00
a035fecb04 emscripten: fix reset quan fas exit. Eliminades les opcions d'eixida 2026-04-13 17:57:54 +02:00
9d70138855 emscripten: per defecte integer scale false 2026-04-13 17:15:19 +02:00
dfe0a3d4e6 fix: corregit el tractament de mandos connectats 2026-04-13 17:11:27 +02:00
66c3e0089c fix: petada per tancar mal director (supose que introduit per Claude al pasar a sdl_callbacks)
eliminat codi mort d'screen
2026-04-13 16:44:27 +02:00
86323a0e56 afegit un mini-notificador 2026-04-13 16:27:57 +02:00
58cacf7bda - punter del mouse amagat soles
- canvas de wasm mes gran
2026-04-12 22:23:31 +02:00
978cbcc9fc desactivat eixir del joc en la versió WASM (milestone 5)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 21:03:09 +02:00
fb023df1e1 build wasm a build/wasm i output a dist/wasm
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 19:57:32 +02:00
555f347375 afegit suport Emscripten/WebAssembly al build system (milestone 4)
- createSystemFolder() adaptat per Emscripten (MEMFS, sense pwd.h/unistd.h)
- initOptions() amb windowSize=1 i videoMode=0 per Emscripten
- CMakeLists.txt: SDL3 via FetchContent per Emscripten, --preload-file data
- Makefile: target wasm amb Docker (emscripten/emsdk)
- Build de Linux verificat, segueix funcionant correctament

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 19:49:37 +02:00
85a47c1a2b corregits bugs dels sub-bucles aplanats
- Demo ja no entra en pausa ni game over (redirigeix a instruccions)
- Perdre el focus de la finestra només pausa durant el joc actiu (no en demo, game over ni pausa)
- Demo gestionat amb save/restore de section->name per evitar transició del Director

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 19:45:39 +02:00
06d4712493 migrat a SDL3 Callback API (SDL_AppInit/Iterate/Event/Quit) (milestone 3)
- main.cpp reescrit amb SDL_MAIN_USE_CALLBACKS
- Director convertit a màquina d'estats amb iterate() i handleEvent()
- Seccions (Logo, Intro, Title, Game) amb iterate() i handleEvent()
- Events SDL enrutats via SDL_AppEvent → Director → secció activa
- Eliminat SDL_PollEvent de iterate(), events via handleEvent()
- Transicions entre seccions gestionades per handleSectionTransition()
- Instructions i Game (demo) delegats frame a frame des de Title

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 19:32:31 +02:00
18c4d6032d aplanat sub-bucles anidats de pausa, game over, instruccions i demo (milestone 2)
- Game::runPausedGame() convertit a enterPausedGame() + despatx directe en run()
- Game::runGameOverScreen() convertit a enterGameOverScreen() + despatx directe
- Eliminada variable static postFade, convertida a membre gameOverPostFade
- Extret SDL_PollEvent de updateGameOverScreen() a checkGameOverEvents()
- Game::run() refactoritzat amb iterate() + hasFinished() per preparar callbacks
- Title::runInstructions() i runDemoGame() convertits a no-bloquejants
- Instructions ara usa finished/quitRequested en lloc de modificar section directament
- Instructions exposa start(), update(), checkEvents(), render(), hasFinished()

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 19:15:54 +02:00
9365f80e8b eliminats tots els SDL_Delay i bucles bloquejants (milestone 1)
- shakeScreen() convertit a màquina d'estats amb SDL_GetTicks (50ms per pas)
- killPlayer() convertit a seqüència de fases (Shaking → Waiting → Done)
- Fade FADE_FULLSCREEN convertit a per-frame amb alpha incremental
- Fade FADE_RANDOM_SQUARE convertit a per-frame (un quadrat cada 100ms)
- Title SUBSECTION_TITLE_2 convertit a no-bloquejant, variables static eliminades
- Corregit so duplicat del crashSound al títol
- Congelat input del jugador durant la seqüència de mort

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 19:02:44 +02:00
4bd07216f3 corregit make release de windows 2026-04-05 18:57:32 +02:00
53 changed files with 5345 additions and 2563 deletions

3
.gitignore vendored
View File

@@ -1,7 +1,6 @@
.vscode .vscode
build/ build/
dist/ dist/
data/config/config.txt
*.DS_Store *.DS_Store
thumbs.db thumbs.db
*.exe *.exe
@@ -14,3 +13,5 @@ thumbs.db
coffee_crisis coffee_crisis
coffee_crisis_debug coffee_crisis_debug
release/windows/coffee.res release/windows/coffee.res
resources.pack
tools/pack_resources/pack_resources

View File

@@ -72,7 +72,7 @@ Defined in `const.h`: block size (8px), virtual canvas (256×192), play area bou
- `data/font/` — bitmap font files - `data/font/` — bitmap font files
- `data/music/` and `data/sound/` — audio assets - `data/music/` and `data/sound/` — audio assets
- `data/lang/` — language files (es_ES, ba_BA, en_UK) - `data/lang/` — language files (es_ES, ba_BA, en_UK)
- `data/config/` — gamecontroller DB, demo recording data - `data/demo/` — demo recording data (gamecontrollerdb.txt vive en la raíz del proyecto, fuera del pack)
- `data/menu/` — menu definition files - `data/menu/` — menu definition files
## Language ## Language

View File

@@ -30,11 +30,30 @@ if(NOT SOURCES)
endif() endif()
# Configuración de SDL3 # Configuración de SDL3
if(EMSCRIPTEN)
# En Emscripten, SDL3 se compila desde source con FetchContent
include(FetchContent)
FetchContent_Declare(
SDL3
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG release-3.4.4
GIT_SHALLOW TRUE
)
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(SDL3)
message(STATUS "SDL3 compilado desde source para Emscripten")
else()
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}") message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}")
endif()
# Configuración común de salida de ejecutables en el directorio raíz # Configuración de salida de ejecutables
if(NOT EMSCRIPTEN)
# En desktop, el ejecutable va a la raíz del proyecto
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()
# Añadir ejecutable principal # Añadir ejecutable principal
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} ${SOURCES})
@@ -66,6 +85,15 @@ elseif(APPLE)
-rpath @executable_path/../Frameworks/ -rpath @executable_path/../Frameworks/
) )
endif() endif()
elseif(EMSCRIPTEN)
target_compile_definitions(${PROJECT_NAME} PRIVATE EMSCRIPTEN_BUILD)
target_link_options(${PROJECT_NAME} PRIVATE
--preload-file ${CMAKE_SOURCE_DIR}/resources.pack@/resources.pack
--preload-file ${CMAKE_SOURCE_DIR}/gamecontrollerdb.txt@/gamecontrollerdb.txt
-sALLOW_MEMORY_GROWTH=1
-sMAX_WEBGL_VERSION=2
)
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html")
elseif(UNIX AND NOT APPLE) elseif(UNIX AND NOT APPLE)
target_compile_definitions(${PROJECT_NAME} PRIVATE LINUX_BUILD) target_compile_definitions(${PROJECT_NAME} PRIVATE LINUX_BUILD)
target_link_options(${PROJECT_NAME} PRIVATE -Wl,--gc-sections) target_link_options(${PROJECT_NAME} PRIVATE -Wl,--gc-sections)

View File

@@ -3,6 +3,7 @@
# ============================================================================== # ==============================================================================
DIR_ROOT := $(dir $(abspath $(MAKEFILE_LIST))) DIR_ROOT := $(dir $(abspath $(MAKEFILE_LIST)))
DIR_BIN := $(addsuffix /, $(DIR_ROOT)) DIR_BIN := $(addsuffix /, $(DIR_ROOT))
DIR_PACK_TOOL := tools/pack_resources
# ============================================================================== # ==============================================================================
# TARGET NAMES # TARGET NAMES
@@ -60,14 +61,23 @@ endif
# ============================================================================== # ==============================================================================
# COMPILACIÓN CON CMAKE # COMPILACIÓN CON CMAKE
# ============================================================================== # ==============================================================================
all: all: resources.pack
@cmake -S . -B build -DCMAKE_BUILD_TYPE=Release @cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
@cmake --build build @cmake --build build
debug: debug: resources.pack
@cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug @cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
@cmake --build build @cmake --build build
# ==============================================================================
# REGLAS PARA HERRAMIENTA DE EMPAQUETADO Y RESOURCES.PACK
# ==============================================================================
pack_tool:
@$(MAKE) -C $(DIR_PACK_TOOL)
resources.pack:
@$(MAKE) -C $(DIR_PACK_TOOL) pack
# ============================================================================== # ==============================================================================
# RELEASE AUTOMÁTICO (detecta SO) # RELEASE AUTOMÁTICO (detecta SO)
# ============================================================================== # ==============================================================================
@@ -86,6 +96,7 @@ endif
# COMPILACIÓN PARA WINDOWS (RELEASE) # COMPILACIÓN PARA WINDOWS (RELEASE)
# ============================================================================== # ==============================================================================
windows_release: windows_release:
@$(MAKE) resources.pack
@echo off @echo off
@echo Creando release para Windows - Version: $(VERSION) @echo Creando release para Windows - Version: $(VERSION)
@@ -99,11 +110,12 @@ windows_release:
@powershell -Command "if (-not (Test-Path '$(RELEASE_FOLDER)')) {New-Item '$(RELEASE_FOLDER)' -ItemType Directory}" @powershell -Command "if (-not (Test-Path '$(RELEASE_FOLDER)')) {New-Item '$(RELEASE_FOLDER)' -ItemType Directory}"
# Copia ficheros # Copia ficheros
@powershell -Command "Copy-Item -Path 'data' -Destination '$(RELEASE_FOLDER)' -Recurse -Force" @powershell -Command "Copy-Item 'resources.pack' -Destination '$(RELEASE_FOLDER)'"
@powershell -Command "Copy-Item 'gamecontrollerdb.txt' -Destination '$(RELEASE_FOLDER)'"
@powershell -Command "Copy-Item 'LICENSE' -Destination '$(RELEASE_FOLDER)'" @powershell -Command "Copy-Item 'LICENSE' -Destination '$(RELEASE_FOLDER)'"
@powershell -Command "Copy-Item 'README.md' -Destination '$(RELEASE_FOLDER)'" @powershell -Command "Copy-Item 'README.md' -Destination '$(RELEASE_FOLDER)'"
@powershell -Command "Copy-Item 'release\windows\dll\*.dll' -Destination '$(RELEASE_FOLDER)'" @powershell -Command "Copy-Item 'release\windows\dll\*.dll' -Destination '$(RELEASE_FOLDER)'"
@powershell -Command "Copy-Item -Path '$(TARGET_FILE)' -Destination '\"$(WIN_RELEASE_FILE).exe\"'" @powershell -Command "Copy-Item -Path '$(TARGET_FILE).exe' -Destination '$(WIN_RELEASE_FILE).exe'"
strip -s -R .comment -R .gnu.version "$(WIN_RELEASE_FILE).exe" --strip-unneeded strip -s -R .comment -R .gnu.version "$(WIN_RELEASE_FILE).exe" --strip-unneeded
# Crea el fichero .zip # Crea el fichero .zip
@@ -118,6 +130,7 @@ windows_release:
# COMPILACIÓN PARA MACOS (RELEASE) # COMPILACIÓN PARA MACOS (RELEASE)
# ============================================================================== # ==============================================================================
macos_release: macos_release:
@$(MAKE) resources.pack
@echo "Creando release para macOS - Version: $(VERSION)" @echo "Creando release para macOS - Version: $(VERSION)"
# Verificar e instalar create-dmg si es necesario # Verificar e instalar create-dmg si es necesario
@@ -140,7 +153,8 @@ macos_release:
$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources" $(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
# Copia carpetas y ficheros # Copia carpetas y ficheros
cp -R data "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources" cp resources.pack "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
cp gamecontrollerdb.txt "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
cp -R release/macos/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks" cp -R release/macos/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks"
cp release/icons/*.icns "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources" cp release/icons/*.icns "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
cp release/macos/Info.plist "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents" cp release/macos/Info.plist "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents"
@@ -211,6 +225,7 @@ macos_release:
# COMPILACIÓN PARA LINUX (RELEASE) # COMPILACIÓN PARA LINUX (RELEASE)
# ============================================================================== # ==============================================================================
linux_release: linux_release:
@$(MAKE) resources.pack
@echo "Creando release para Linux - Version: $(VERSION)" @echo "Creando release para Linux - Version: $(VERSION)"
# Compila con cmake # Compila con cmake
@@ -222,7 +237,8 @@ linux_release:
$(MKDIR) "$(RELEASE_FOLDER)" $(MKDIR) "$(RELEASE_FOLDER)"
# Copia ficheros # Copia ficheros
cp -R data "$(RELEASE_FOLDER)" cp resources.pack "$(RELEASE_FOLDER)"
cp gamecontrollerdb.txt "$(RELEASE_FOLDER)"
cp LICENSE "$(RELEASE_FOLDER)" cp LICENSE "$(RELEASE_FOLDER)"
cp README.md "$(RELEASE_FOLDER)" cp README.md "$(RELEASE_FOLDER)"
cp "$(TARGET_FILE)" "$(RELEASE_FILE)" cp "$(TARGET_FILE)" "$(RELEASE_FILE)"
@@ -236,6 +252,38 @@ linux_release:
# Elimina la carpeta temporal # Elimina la carpeta temporal
$(RMDIR) "$(RELEASE_FOLDER)" $(RMDIR) "$(RELEASE_FOLDER)"
# ==============================================================================
# COMPILACIÓN PARA WEBASSEMBLY (requiere Docker)
# ==============================================================================
wasm:
@$(MAKE) resources.pack
@echo "Compilando para WebAssembly - Version: $(VERSION)"
docker run --rm \
--user $(shell id -u):$(shell id -g) \
-v $(DIR_ROOT):/src \
-w /src \
emscripten/emsdk:latest \
bash -c "emcmake cmake -S . -B build/wasm -DCMAKE_BUILD_TYPE=Release && cmake --build build/wasm"
$(MKDIR) "$(DIST_DIR)/wasm"
cp build/wasm/$(TARGET_NAME).html $(DIST_DIR)/wasm/
cp build/wasm/$(TARGET_NAME).js $(DIST_DIR)/wasm/
cp build/wasm/$(TARGET_NAME).wasm $(DIST_DIR)/wasm/
cp build/wasm/$(TARGET_NAME).data $(DIST_DIR)/wasm/
@echo "Output: $(DIST_DIR)/wasm/"
scp $(DIST_DIR)/wasm/$(TARGET_NAME).js $(DIST_DIR)/wasm/$(TARGET_NAME).wasm $(DIST_DIR)/wasm/$(TARGET_NAME).data \
maverick:/home/sergio/gitea/web_jailgames/static/games/coffee-crisis/wasm/
ssh maverick 'cd /home/sergio/gitea/web_jailgames && ./deploy.sh'
@echo "Deployed to maverick"
# ==============================================================================
# DESCARGA DE GAMECONTROLLERDB
# ==============================================================================
controllerdb:
@echo "Descargando gamecontrollerdb.txt..."
curl -fsSL https://raw.githubusercontent.com/mdqinc/SDL_GameControllerDB/master/gamecontrollerdb.txt \
-o gamecontrollerdb.txt
@echo "gamecontrollerdb.txt actualizado"
# ============================================================================== # ==============================================================================
# REGLAS ESPECIALES # REGLAS ESPECIALES
# ============================================================================== # ==============================================================================
@@ -255,9 +303,13 @@ help:
@echo " make windows_release - Crear release para Windows" @echo " make windows_release - Crear release para Windows"
@echo " make linux_release - Crear release para Linux" @echo " make linux_release - Crear release para Linux"
@echo " make macos_release - Crear release para macOS" @echo " make macos_release - Crear release para macOS"
@echo " make wasm - Compilar para WebAssembly (requiere Docker) y deploy a maverick"
@echo ""
@echo " Herramientas:"
@echo " make controllerdb - Descargar gamecontrollerdb.txt actualizado"
@echo "" @echo ""
@echo " Otros:" @echo " Otros:"
@echo " make show_version - Mostrar version actual ($(VERSION))" @echo " make show_version - Mostrar version actual ($(VERSION))"
@echo " make help - Mostrar esta ayuda" @echo " make help - Mostrar esta ayuda"
.PHONY: all debug release windows_release macos_release linux_release show_version help .PHONY: all debug release windows_release macos_release linux_release wasm controllerdb pack_tool resources.pack show_version help

File diff suppressed because it is too large Load Diff

View File

@@ -279,3 +279,9 @@ MODE FORA DE LINEA
## 93 - MENU OPCIONES ## 93 - MENU OPCIONES
TAULER DE PUNTS TAULER DE PUNTS
## 94 - NOTIFICACIO COMANDAMENT
CONNECTAT
## 95 - NOTIFICACIO COMANDAMENT
DESCONNECTAT

View File

@@ -279,3 +279,9 @@ OFFLINE MODE
## 93 - MENU OPCIONES ## 93 - MENU OPCIONES
HISCORE TABLE HISCORE TABLE
## 94 - GAMEPAD NOTIFICATION
CONNECTED
## 95 - GAMEPAD NOTIFICATION
DISCONNECTED

View File

@@ -279,3 +279,9 @@ MODO SIN CONEXION
## 93 - MENU OPCIONES ## 93 - MENU OPCIONES
TABLA DE PUNTUACIONES TABLA DE PUNTUACIONES
## 94 - NOTIFICACION MANDO
CONECTADO
## 95 - NOTIFICACION MANDO
DESCONECTADO

2231
gamecontrollerdb.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -6,28 +6,20 @@
#include "texture.h" // for Texture #include "texture.h" // for Texture
// Carga la animación desde un fichero // Parser compartido: lee un istream con el formato .ani
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose) { static animatedSprite_t parseAnimationStream(std::istream &file, Texture *texture, const std::string &filename, bool verbose) {
// Inicializa variables
animatedSprite_t as; animatedSprite_t as;
as.texture = texture; as.texture = texture;
int framesPerRow = 0; int framesPerRow = 0;
int frameWidth = 0; int frameWidth = 0;
int frameHeight = 0; int frameHeight = 0;
int maxTiles = 0; int maxTiles = 0;
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
std::ifstream file(filePath);
std::string line; std::string line;
// El fichero se puede abrir
if (file.good()) {
// Procesa el fichero linea a linea
if (verbose) { if (verbose) {
std::cout << "Animation loaded: " << filename << std::endl; std::cout << "Animation loaded: " << filename << std::endl;
} }
while (std::getline(file, line)) { while (std::getline(file, line)) {
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]") { if (line == "[animation]") {
animation_t buffer; animation_t buffer;
buffer.counter = 0; buffer.counter = 0;
@@ -36,76 +28,47 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, b
do { do {
std::getline(file, line); std::getline(file, line);
// Encuentra la posición del caracter '='
int pos = line.find("="); int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != (int)line.npos) { if (pos != (int)line.npos) {
if (line.substr(0, pos) == "name") { if (line.substr(0, pos) == "name") {
buffer.name = line.substr(pos + 1, line.length()); buffer.name = line.substr(pos + 1, line.length());
} } else if (line.substr(0, pos) == "speed") {
else if (line.substr(0, pos) == "speed") {
buffer.speed = std::stoi(line.substr(pos + 1, line.length())); buffer.speed = std::stoi(line.substr(pos + 1, line.length()));
} } else if (line.substr(0, pos) == "loop") {
else if (line.substr(0, pos) == "loop") {
buffer.loop = std::stoi(line.substr(pos + 1, line.length())); buffer.loop = std::stoi(line.substr(pos + 1, line.length()));
} } else if (line.substr(0, pos) == "frames") {
else if (line.substr(0, pos) == "frames") {
// Se introducen los valores separados por comas en un vector
std::stringstream ss(line.substr(pos + 1, line.length())); std::stringstream ss(line.substr(pos + 1, line.length()));
std::string tmp; std::string tmp;
SDL_Rect rect = {0, 0, frameWidth, frameHeight}; SDL_Rect rect = {0, 0, frameWidth, frameHeight};
while (getline(ss, tmp, ',')) { while (getline(ss, tmp, ',')) {
// Comprueba que el tile no sea mayor que el maximo indice permitido
const int numTile = std::stoi(tmp) > maxTiles ? 0 : std::stoi(tmp); const int numTile = std::stoi(tmp) > maxTiles ? 0 : std::stoi(tmp);
rect.x = (numTile % framesPerRow) * frameWidth; rect.x = (numTile % framesPerRow) * frameWidth;
rect.y = (numTile / framesPerRow) * frameHeight; rect.y = (numTile / framesPerRow) * frameHeight;
buffer.frames.push_back(rect); buffer.frames.push_back(rect);
} }
} } else {
else {
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
} }
} }
} while (line != "[/animation]"); } while (line != "[/animation]");
// Añade la animación al vector de animaciones
as.animations.push_back(buffer); as.animations.push_back(buffer);
} } else {
// En caso contrario se parsea el fichero para buscar las variables y los valores
else {
// Encuentra la posición del caracter '='
int pos = line.find("="); int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != (int)line.npos) { if (pos != (int)line.npos) {
if (line.substr(0, pos) == "framesPerRow") { if (line.substr(0, pos) == "framesPerRow") {
framesPerRow = std::stoi(line.substr(pos + 1, line.length())); framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
} } else if (line.substr(0, pos) == "frameWidth") {
else if (line.substr(0, pos) == "frameWidth") {
frameWidth = std::stoi(line.substr(pos + 1, line.length())); frameWidth = std::stoi(line.substr(pos + 1, line.length()));
} } else if (line.substr(0, pos) == "frameHeight") {
else if (line.substr(0, pos) == "frameHeight") {
frameHeight = std::stoi(line.substr(pos + 1, line.length())); frameHeight = std::stoi(line.substr(pos + 1, line.length()));
} } else {
else {
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl; std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
} }
// Normaliza valores
if (framesPerRow == 0 && frameWidth > 0) { if (framesPerRow == 0 && frameWidth > 0) {
framesPerRow = texture->getWidth() / frameWidth; framesPerRow = texture->getWidth() / frameWidth;
} }
if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0) { if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0) {
const int w = texture->getWidth() / frameWidth; const int w = texture->getWidth() / frameWidth;
const int h = texture->getHeight() / frameHeight; const int h = texture->getHeight() / frameHeight;
@@ -115,18 +78,35 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, b
} }
} }
// Cierra el fichero return as;
file.close();
} }
// El fichero no se puede abrir
else { // Carga la animación desde un fichero
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose) {
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
std::ifstream file(filePath);
if (!file.good()) {
if (verbose) { if (verbose) {
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
} }
animatedSprite_t as;
as.texture = texture;
return as;
}
return parseAnimationStream(file, texture, filename, verbose);
} }
// Carga la animación desde bytes en memoria
animatedSprite_t loadAnimationFromMemory(Texture *texture, const std::vector<uint8_t> &bytes, const std::string &nameForLogs, bool verbose) {
if (bytes.empty()) {
animatedSprite_t as;
as.texture = texture;
return as; return as;
} }
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
std::stringstream ss(content);
return parseAnimationStream(ss, texture, nameForLogs, verbose);
}
// Constructor // Constructor
AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::string file, std::vector<std::string> *buffer) { AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::string file, std::vector<std::string> *buffer) {

View File

@@ -2,6 +2,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <cstdint>
#include <string> // for string, basic_string #include <string> // for string, basic_string
#include <vector> // for vector #include <vector> // for vector
@@ -26,6 +27,9 @@ struct animatedSprite_t {
// Carga la animación desde un fichero // Carga la animación desde un fichero
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose = false); animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose = false);
// Carga la animación desde bytes en memoria
animatedSprite_t loadAnimationFromMemory(Texture *texture, const std::vector<uint8_t> &bytes, const std::string &nameForLogs = "", bool verbose = false);
class AnimatedSprite : public MovingSprite { class AnimatedSprite : public MovingSprite {
private: private:
// Variables // Variables

View File

@@ -5,6 +5,8 @@
#include <iostream> // for basic_ostream, operator<<, cout, endl #include <iostream> // for basic_ostream, operator<<, cout, endl
#include "resource_helper.h"
// Constructor // Constructor
Asset::Asset(std::string executablePath) { Asset::Asset(std::string executablePath) {
this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/")); this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/"));
@@ -96,15 +98,22 @@ bool Asset::checkFile(std::string path) {
bool success = false; bool success = false;
std::string result = "ERROR"; std::string result = "ERROR";
// Comprueba si existe el fichero // Comprueba si existe el fichero (pack o filesystem)
const std::string filename = path.substr(path.find_last_of("\\/") + 1); const std::string filename = path.substr(path.find_last_of("\\/") + 1);
if (ResourceHelper::shouldUseResourcePack(path)) {
auto bytes = ResourceHelper::loadFile(path);
if (!bytes.empty()) {
result = "OK";
success = true;
}
} else {
SDL_IOStream *file = SDL_IOFromFile(path.c_str(), "rb"); SDL_IOStream *file = SDL_IOFromFile(path.c_str(), "rb");
if (file != nullptr) { if (file != nullptr) {
result = "OK"; result = "OK";
success = true; success = true;
SDL_CloseIO(file); SDL_CloseIO(file);
} }
}
if (verbose) { if (verbose) {
std::cout.setf(std::ios::left, std::ios::adjustfield); std::cout.setf(std::ios::left, std::ios::adjustfield);

View File

@@ -18,15 +18,15 @@ enum assetType {
// Clase Asset // Clase Asset
class Asset { class Asset {
private: public:
// Estructura para definir un item // Estructura para definir un item
struct item_t { struct item_t {
std::string file; // Ruta del fichero desde la raiz del directorio std::string file; // Ruta del fichero desde la raiz del directorio
enum assetType type; // Indica el tipo de recurso enum assetType type; // Indica el tipo de recurso
bool required; // Indica si es un fichero que debe de existir bool required; // Indica si es un fichero que debe de existir
// bool absolute; // Indica si la ruta que se ha proporcionado es una ruta absoluta
}; };
private:
// Variables // Variables
int longestName; // Contiene la longitud del nombre de fichero mas largo int longestName; // Contiene la longitud del nombre de fichero mas largo
std::vector<item_t> fileList; // Listado con todas las rutas a los ficheros std::vector<item_t> fileList; // Listado con todas las rutas a los ficheros
@@ -49,6 +49,9 @@ class Asset {
// Devuelve un elemento de la lista a partir de una cadena // Devuelve un elemento de la lista a partir de una cadena
std::string get(std::string text); std::string get(std::string text);
// Devuelve toda la lista de items registrados
const std::vector<item_t> &getAll() const { return fileList; }
// Comprueba que existen todos los elementos // Comprueba que existen todos los elementos
bool check(); bool check();

View File

@@ -4,10 +4,13 @@
#include <errno.h> // for errno, EEXIST, EACCES, ENAMETOO... #include <errno.h> // for errno, EEXIST, EACCES, ENAMETOO...
#include <stdio.h> // for printf, perror #include <stdio.h> // for printf, perror
#include <string.h> // for strcmp #include <string.h> // for strcmp
#ifndef __EMSCRIPTEN__
#include <sys/stat.h> // for mkdir, stat, S_IRWXU #include <sys/stat.h> // for mkdir, stat, S_IRWXU
#include <unistd.h> // for getuid #include <unistd.h> // for getuid
#endif
#include <cstdlib> // for exit, EXIT_FAILURE, srand #include <cstdlib> // for exit, EXIT_FAILURE, srand
#include <filesystem>
#include <fstream> // for basic_ostream, operator<<, basi... #include <fstream> // for basic_ostream, operator<<, basi...
#include <iostream> // for cout #include <iostream> // for cout
#include <memory> #include <memory>
@@ -21,13 +24,16 @@
#include "intro.h" // for Intro #include "intro.h" // for Intro
#include "jail_audio.hpp" // for JA_Init #include "jail_audio.hpp" // for JA_Init
#include "lang.h" // for Lang, MAX_LANGUAGES, ba_BA, en_UK #include "lang.h" // for Lang, MAX_LANGUAGES, ba_BA, en_UK
#include "resource.h"
#include "resource_helper.h"
#include "logo.h" // for Logo #include "logo.h" // for Logo
#include "mouse.hpp" // for Mouse::handleEvent, Mouse::upda...
#include "screen.h" // for FILTER_NEAREST, Screen, FILTER_... #include "screen.h" // for FILTER_NEAREST, Screen, FILTER_...
#include "texture.h" // for Texture #include "texture.h" // for Texture
#include "title.h" // for Title #include "title.h" // for Title
#include "utils.h" // for options_t, input_t, boolToString #include "utils.h" // for options_t, input_t, boolToString
#ifndef _WIN32 #if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
#include <pwd.h> #include <pwd.h>
#endif #endif
@@ -52,6 +58,19 @@ Director::Director(int argc, const char *argv[]) {
createSystemFolder("jailgames/coffee_crisis_debug"); createSystemFolder("jailgames/coffee_crisis_debug");
#endif #endif
// Inicializa el sistema de recursos (pack + fallback)
{
#ifdef RELEASE_BUILD
const bool enable_fallback = false;
#else
const bool enable_fallback = true;
#endif
if (!ResourceHelper::initializeResourceSystem("resources.pack", enable_fallback)) {
std::cerr << "Fatal: resource system init failed (missing resources.pack?)" << std::endl;
exit(EXIT_FAILURE);
}
}
// Crea el objeto que controla los ficheros de recursos // Crea el objeto que controla los ficheros de recursos
asset = new Asset(executablePath); asset = new Asset(executablePath);
asset->setVerbose(options->console); asset->setVerbose(options->console);
@@ -77,18 +96,50 @@ Director::Director(int argc, const char *argv[]) {
lang = new Lang(asset); lang = new Lang(asset);
lang->setLang(options->language); lang->setLang(options->language);
input = new Input(asset->get("gamecontrollerdb.txt")); #ifdef __EMSCRIPTEN__
input = new Input("/gamecontrollerdb.txt");
#else
{
const std::string binDir = std::filesystem::path(executablePath).parent_path().string();
#ifdef MACOS_BUNDLE
input = new Input(binDir + "/../Resources/gamecontrollerdb.txt");
#else
input = new Input(binDir + "/gamecontrollerdb.txt");
#endif
}
#endif
initInput(); initInput();
// Precarga todos los recursos en memoria (texturas, sonidos, música, ...).
// Vivirán durante toda la vida de la app; las escenas leen handles compartidos.
// Debe ir antes de crear Screen porque Screen usa Text (propiedad de Resource).
Resource::init(renderer, asset, input);
screen = new Screen(window, renderer, asset, options); screen = new Screen(window, renderer, asset, options);
activeSection = ActiveSection::None;
} }
Director::~Director() { Director::~Director() {
saveConfigFile(); saveConfigFile();
// Libera las secciones primero: sus destructores tocan audio/render SDL
// (p.ej. Intro::~Intro llama a JA_DeleteMusic) y deben ejecutarse antes
// de SDL_Quit().
logo.reset();
intro.reset();
title.reset();
game.reset();
// Screen puede tener referencias a Text propiedad de Resource: destruir
// Screen antes que Resource.
delete screen;
// Libera todos los recursos precargados antes de cerrar SDL.
Resource::destroy();
delete asset; delete asset;
delete input; delete input;
delete screen;
delete lang; delete lang;
delete options; delete options;
delete section; delete section;
@@ -98,6 +149,8 @@ Director::~Director() {
SDL_Quit(); SDL_Quit();
ResourceHelper::shutdownResourceSystem();
std::cout << "\nBye!" << std::endl; std::cout << "\nBye!" << std::endl;
} }
@@ -137,8 +190,8 @@ void Director::initInput() {
input->bindGameControllerButton(input_fire_right, SDL_GAMEPAD_BUTTON_EAST); input->bindGameControllerButton(input_fire_right, SDL_GAMEPAD_BUTTON_EAST);
// Mando - Otros // Mando - Otros
// SOUTH queda sin asignar para evitar salidas accidentales: pausa/cancel se hace con START/BACK.
input->bindGameControllerButton(input_accept, SDL_GAMEPAD_BUTTON_EAST); input->bindGameControllerButton(input_accept, SDL_GAMEPAD_BUTTON_EAST);
input->bindGameControllerButton(input_cancel, SDL_GAMEPAD_BUTTON_SOUTH);
#ifdef GAME_CONSOLE #ifdef GAME_CONSOLE
input->bindGameControllerButton(input_pause, SDL_GAMEPAD_BUTTON_BACK); input->bindGameControllerButton(input_pause, SDL_GAMEPAD_BUTTON_BACK);
input->bindGameControllerButton(input_exit, SDL_GAMEPAD_BUTTON_START); input->bindGameControllerButton(input_exit, SDL_GAMEPAD_BUTTON_START);
@@ -227,8 +280,7 @@ bool Director::setFileList() {
// Ficheros de configuración // Ficheros de configuración
asset->add(systemFolder + "/config.txt", t_data, false, true); asset->add(systemFolder + "/config.txt", t_data, false, true);
asset->add(systemFolder + "/score.bin", t_data, false, true); asset->add(systemFolder + "/score.bin", t_data, false, true);
asset->add(prefix + "/data/config/demo.bin", t_data); asset->add(prefix + "/data/demo/demo.bin", t_data);
asset->add(prefix + "/data/config/gamecontrollerdb.txt", t_data);
// Musicas // Musicas
asset->add(prefix + "/data/music/intro.ogg", t_music); asset->add(prefix + "/data/music/intro.ogg", t_music);
@@ -383,6 +435,13 @@ void Director::initOptions() {
options->difficulty = DIFFICULTY_NORMAL; options->difficulty = DIFFICULTY_NORMAL;
options->language = ba_BA; options->language = ba_BA;
options->console = false; options->console = false;
#ifdef __EMSCRIPTEN__
// En Emscripten la ventana la gestiona el navegador
options->windowSize = 4;
options->videoMode = 0;
options->integerScale = true;
#endif
} }
// Comprueba los parametros del programa // Comprueba los parametros del programa
@@ -400,7 +459,10 @@ void Director::checkProgramArguments(int argc, const char *argv[]) {
// Crea la carpeta del sistema donde guardar datos // Crea la carpeta del sistema donde guardar datos
void Director::createSystemFolder(const std::string &folder) { void Director::createSystemFolder(const std::string &folder) {
#ifdef _WIN32 #ifdef __EMSCRIPTEN__
// En Emscripten usamos una carpeta en MEMFS (no persistente)
systemFolder = "/config/" + folder;
#elif _WIN32
systemFolder = std::string(getenv("APPDATA")) + "/" + folder; systemFolder = std::string(getenv("APPDATA")) + "/" + folder;
#elif __APPLE__ #elif __APPLE__
struct passwd *pw = getpwuid(getuid()); struct passwd *pw = getpwuid(getuid());
@@ -422,6 +484,10 @@ void Director::createSystemFolder(const std::string &folder) {
} }
#endif #endif
#ifdef __EMSCRIPTEN__
// En Emscripten no necesitamos crear carpetas (MEMFS las crea automáticamente)
(void)folder;
#else
struct stat st = {0}; struct stat st = {0};
if (stat(systemFolder.c_str(), &st) == -1) { if (stat(systemFolder.c_str(), &st) == -1) {
errno = 0; errno = 0;
@@ -451,6 +517,7 @@ void Director::createSystemFolder(const std::string &folder) {
} }
} }
} }
#endif
} }
// Carga el fichero de configuración // Carga el fichero de configuración
@@ -568,50 +635,147 @@ bool Director::saveConfigFile() {
return success; return success;
} }
void Director::runLogo() { // Gestiona las transiciones entre secciones
auto logo = std::make_unique<Logo>(renderer, screen, asset, input, section); void Director::handleSectionTransition() {
logo->run(); // Determina qué sección debería estar activa
} ActiveSection targetSection = ActiveSection::None;
void Director::runIntro() {
auto intro = std::make_unique<Intro>(renderer, screen, asset, input, lang, section);
intro->run();
}
void Director::runTitle() {
auto title = std::make_unique<Title>(renderer, screen, input, asset, options, lang, section);
title->run();
}
void Director::runGame() {
const int numPlayers = section->subsection == SUBSECTION_GAME_PLAY_1P ? 1 : 2;
auto game = std::make_unique<Game>(numPlayers, 0, renderer, screen, asset, lang, input, false, options, section);
game->run();
}
int Director::run() {
// Bucle principal
while (section->name != SECTION_PROG_QUIT) {
switch (section->name) { switch (section->name) {
case SECTION_PROG_LOGO: case SECTION_PROG_LOGO:
runLogo(); targetSection = ActiveSection::Logo;
break; break;
case SECTION_PROG_INTRO: case SECTION_PROG_INTRO:
runIntro(); targetSection = ActiveSection::Intro;
break; break;
case SECTION_PROG_TITLE: case SECTION_PROG_TITLE:
runTitle(); targetSection = ActiveSection::Title;
break; break;
case SECTION_PROG_GAME: case SECTION_PROG_GAME:
runGame(); targetSection = ActiveSection::Game;
break;
}
// Si no ha cambiado, no hay nada que hacer
if (targetSection == activeSection) return;
// Destruye la sección anterior
logo.reset();
intro.reset();
title.reset();
game.reset();
// Crea la nueva sección
activeSection = targetSection;
switch (activeSection) {
case ActiveSection::Logo:
logo = std::make_unique<Logo>(renderer, screen, asset, input, section);
break;
case ActiveSection::Intro:
intro = std::make_unique<Intro>(renderer, screen, asset, input, lang, section);
break;
case ActiveSection::Title:
title = std::make_unique<Title>(renderer, screen, input, asset, options, lang, section);
break;
case ActiveSection::Game: {
const int numPlayers = section->subsection == SUBSECTION_GAME_PLAY_1P ? 1 : 2;
game = std::make_unique<Game>(numPlayers, 0, renderer, screen, asset, lang, input, false, options, section);
break;
}
case ActiveSection::None:
break; break;
} }
} }
return 0; // Ejecuta un frame del juego
SDL_AppResult Director::iterate() {
#ifdef __EMSCRIPTEN__
// En WASM no se puede salir: reinicia al logo
if (section->name == SECTION_PROG_QUIT) {
section->name = SECTION_PROG_LOGO;
}
#else
if (section->name == SECTION_PROG_QUIT) {
return SDL_APP_SUCCESS;
}
#endif
// Actualiza la visibilidad del cursor del ratón
Mouse::updateCursorVisibility(options->videoMode != 0);
// Gestiona las transiciones entre secciones
handleSectionTransition();
// Ejecuta un frame de la sección activa
switch (activeSection) {
case ActiveSection::Logo:
logo->iterate();
break;
case ActiveSection::Intro:
intro->iterate();
break;
case ActiveSection::Title:
title->iterate();
break;
case ActiveSection::Game:
game->iterate();
break;
case ActiveSection::None:
break;
}
return SDL_APP_CONTINUE;
}
// Procesa un evento
SDL_AppResult Director::handleEvent(SDL_Event *event) {
#ifndef __EMSCRIPTEN__
// Evento de salida de la aplicación
if (event->type == SDL_EVENT_QUIT) {
section->name = SECTION_PROG_QUIT;
return SDL_APP_SUCCESS;
}
#endif
// Hot-plug de mandos
if (event->type == SDL_EVENT_GAMEPAD_ADDED) {
std::string name;
if (input->handleGamepadAdded(event->gdevice.which, name)) {
screen->notify(name + " " + lang->getText(94),
color_t{0x40, 0xFF, 0x40},
color_t{0, 0, 0},
2500);
}
} else if (event->type == SDL_EVENT_GAMEPAD_REMOVED) {
std::string name;
if (input->handleGamepadRemoved(event->gdevice.which, name)) {
screen->notify(name + " " + lang->getText(95),
color_t{0xFF, 0x50, 0x50},
color_t{0, 0, 0},
2500);
}
}
// Gestiona la visibilidad del cursor según el movimiento del ratón
Mouse::handleEvent(*event, options->videoMode != 0);
// Reenvía el evento a la sección activa
switch (activeSection) {
case ActiveSection::Logo:
logo->handleEvent(event);
break;
case ActiveSection::Intro:
intro->handleEvent(event);
break;
case ActiveSection::Title:
title->handleEvent(event);
break;
case ActiveSection::Game:
game->handleEvent(event);
break;
case ActiveSection::None:
break;
}
return SDL_APP_CONTINUE;
} }
// Asigna variables a partir de dos cadenas // Asigna variables a partir de dos cadenas

View File

@@ -2,6 +2,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <memory>
#include <string> // for string, basic_string #include <string> // for string, basic_string
class Asset; class Asset;
class Game; class Game;
@@ -17,6 +18,13 @@ struct section_t;
// Textos // Textos
constexpr const char *WINDOW_CAPTION = "© 2020 Coffee Crisis — JailDesigner"; constexpr const char *WINDOW_CAPTION = "© 2020 Coffee Crisis — JailDesigner";
// Secciones activas del Director
enum class ActiveSection { None,
Logo,
Intro,
Title,
Game };
class Director { class Director {
private: private:
// Objetos y punteros // Objetos y punteros
@@ -28,6 +36,13 @@ class Director {
Asset *asset; // Objeto que gestiona todos los ficheros de recursos Asset *asset; // Objeto que gestiona todos los ficheros de recursos
section_t *section; // Sección y subsección actual del programa; section_t *section; // Sección y subsección actual del programa;
// Secciones del juego
ActiveSection activeSection;
std::unique_ptr<Logo> logo;
std::unique_ptr<Intro> intro;
std::unique_ptr<Title> title;
std::unique_ptr<Game> game;
// Variables // Variables
struct options_t *options; // Variable con todas las opciones del programa struct options_t *options; // Variable con todas las opciones del programa
std::string executablePath; // Path del ejecutable std::string executablePath; // Path del ejecutable
@@ -63,17 +78,8 @@ class Director {
// Crea la carpeta del sistema donde guardar datos // Crea la carpeta del sistema donde guardar datos
void createSystemFolder(const std::string &folder); void createSystemFolder(const std::string &folder);
// Ejecuta la seccion de juego con el logo // Gestiona las transiciones entre secciones
void runLogo(); void handleSectionTransition();
// Ejecuta la seccion de juego de la introducción
void runIntro();
// Ejecuta la seccion de juego con el titulo y los menus
void runTitle();
// Ejecuta la seccion de juego donde se juega
void runGame();
public: public:
// Constructor // Constructor
@@ -82,6 +88,9 @@ class Director {
// Destructor // Destructor
~Director(); ~Director();
// Bucle principal // Ejecuta un frame del juego
int run(); SDL_AppResult iterate();
// Procesa un evento
SDL_AppResult handleEvent(SDL_Event *event);
}; };

View File

@@ -35,6 +35,12 @@ void Fade::init(Uint8 r, Uint8 g, Uint8 b) {
mR = r; mR = r;
mG = g; mG = g;
mB = b; mB = b;
mROriginal = r;
mGOriginal = g;
mBOriginal = b;
mLastSquareTicks = 0;
mSquaresDrawn = 0;
mFullscreenDone = false;
} }
// Pinta una transición en pantalla // Pinta una transición en pantalla
@@ -42,21 +48,13 @@ void Fade::render() {
if (mEnabled && !mFinished) { if (mEnabled && !mFinished) {
switch (mFadeType) { switch (mFadeType) {
case FADE_FULLSCREEN: { case FADE_FULLSCREEN: {
if (!mFullscreenDone) {
SDL_FRect fRect1 = {0, 0, (float)GAMECANVAS_WIDTH, (float)GAMECANVAS_HEIGHT}; SDL_FRect fRect1 = {0, 0, (float)GAMECANVAS_WIDTH, (float)GAMECANVAS_HEIGHT};
for (int i = 0; i < 256; i += 4) { int alpha = mCounter * 4;
// Dibujamos sobre el renderizador if (alpha >= 255) {
SDL_SetRenderTarget(mRenderer, nullptr); alpha = 255;
mFullscreenDone = true;
// Copia el backbuffer con la imagen que había al renderizador
SDL_RenderTexture(mRenderer, mBackbuffer, nullptr, nullptr);
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, i);
SDL_RenderFillRect(mRenderer, &fRect1);
// Vuelca el renderizador en pantalla
SDL_RenderPresent(mRenderer);
}
// Deja todos los buffers del mismo color // Deja todos los buffers del mismo color
SDL_SetRenderTarget(mRenderer, mBackbuffer); SDL_SetRenderTarget(mRenderer, mBackbuffer);
@@ -66,6 +64,19 @@ void Fade::render() {
SDL_SetRenderTarget(mRenderer, nullptr); SDL_SetRenderTarget(mRenderer, nullptr);
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 255); SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 255);
SDL_RenderClear(mRenderer); SDL_RenderClear(mRenderer);
mFinished = true;
} else {
// Dibujamos sobre el renderizador
SDL_SetRenderTarget(mRenderer, nullptr);
// Copia el backbuffer con la imagen que había al renderizador
SDL_RenderTexture(mRenderer, mBackbuffer, nullptr, nullptr);
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, alpha);
SDL_RenderFillRect(mRenderer, &fRect1);
}
}
break; break;
} }
@@ -89,14 +100,17 @@ void Fade::render() {
} }
case FADE_RANDOM_SQUARE: { case FADE_RANDOM_SQUARE: {
Uint32 now = SDL_GetTicks();
if (mSquaresDrawn < 50 && now - mLastSquareTicks >= 100) {
mLastSquareTicks = now;
SDL_FRect fRs = {0, 0, 32, 32}; SDL_FRect fRs = {0, 0, 32, 32};
for (Uint16 i = 0; i < 50; i++) {
// Crea un color al azar // Crea un color al azar
mR = 255 * (rand() % 2); Uint8 r = 255 * (rand() % 2);
mG = 255 * (rand() % 2); Uint8 g = 255 * (rand() % 2);
mB = 255 * (rand() % 2); Uint8 b = 255 * (rand() % 2);
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 64); SDL_SetRenderDrawColor(mRenderer, r, g, b, 64);
// Dibujamos sobre el backbuffer // Dibujamos sobre el backbuffer
SDL_SetRenderTarget(mRenderer, mBackbuffer); SDL_SetRenderTarget(mRenderer, mBackbuffer);
@@ -108,12 +122,14 @@ void Fade::render() {
// Volvemos a usar el renderizador de forma normal // Volvemos a usar el renderizador de forma normal
SDL_SetRenderTarget(mRenderer, nullptr); SDL_SetRenderTarget(mRenderer, nullptr);
mSquaresDrawn++;
}
// Copiamos el backbuffer al renderizador // Copiamos el backbuffer al renderizador
SDL_RenderTexture(mRenderer, mBackbuffer, nullptr, nullptr); SDL_RenderTexture(mRenderer, mBackbuffer, nullptr, nullptr);
// Volcamos el renderizador en pantalla if (mSquaresDrawn >= 50) {
SDL_RenderPresent(mRenderer); mFinished = true;
SDL_Delay(100);
} }
break; break;
} }
@@ -140,6 +156,12 @@ void Fade::activateFade() {
mEnabled = true; mEnabled = true;
mFinished = false; mFinished = false;
mCounter = 0; mCounter = 0;
mSquaresDrawn = 0;
mLastSquareTicks = 0;
mFullscreenDone = false;
mR = mROriginal;
mG = mGOriginal;
mB = mBOriginal;
} }
// Comprueba si está activo // Comprueba si está activo

View File

@@ -17,6 +17,10 @@ class Fade {
bool mEnabled; // Indica si el fade está activo bool mEnabled; // Indica si el fade está activo
bool mFinished; // Indica si ha terminado la transición bool mFinished; // Indica si ha terminado la transición
Uint8 mR, mG, mB; // Colores para el fade Uint8 mR, mG, mB; // Colores para el fade
Uint8 mROriginal, mGOriginal, mBOriginal; // Colores originales para FADE_RANDOM_SQUARE
Uint32 mLastSquareTicks; // Ticks del último cuadrado dibujado (FADE_RANDOM_SQUARE)
Uint16 mSquaresDrawn; // Número de cuadrados dibujados (FADE_RANDOM_SQUARE)
bool mFullscreenDone; // Indica si el fade fullscreen ha terminado la fase de fundido
SDL_Rect mRect1; // Rectangulo usado para crear los efectos de transición SDL_Rect mRect1; // Rectangulo usado para crear los efectos de transición
SDL_Rect mRect2; // Rectangulo usado para crear los efectos de transición SDL_Rect mRect2; // Rectangulo usado para crear los efectos de transición

View File

@@ -15,6 +15,7 @@
#include "input.h" // for inputs_e, Input, REPEAT_TRUE, REPEAT_FALSE #include "input.h" // for inputs_e, Input, REPEAT_TRUE, REPEAT_FALSE
#include "item.h" // for Item, ITEM_COFFEE_MACHINE, ITEM_CLOCK #include "item.h" // for Item, ITEM_COFFEE_MACHINE, ITEM_CLOCK
#include "jail_audio.hpp" // for JA_PlaySound, JA_DeleteSound, JA_LoadSound #include "jail_audio.hpp" // for JA_PlaySound, JA_DeleteSound, JA_LoadSound
#include "resource.h"
#include "lang.h" // for Lang #include "lang.h" // for Lang
#include "menu.h" // for Menu #include "menu.h" // for Menu
#include "movingsprite.h" // for MovingSprite #include "movingsprite.h" // for MovingSprite
@@ -98,84 +99,19 @@ Game::~Game() {
options->input[0].deviceType = onePlayerControl; options->input[0].deviceType = onePlayerControl;
} }
// Elimina todos los objetos contenidos en vectores // Elimina todos los objetos contenidos en vectores (jugadores, balas, etc.)
deleteAllVectorObjects(); deleteAllVectorObjects();
bulletTexture->unload(); // Las texturas, animaciones, Text, Menu, sonidos y música son propiedad
delete bulletTexture; // de Resource — no se liberan aquí. Solo limpiamos nuestras vistas.
gameBuildingsTexture->unload();
delete gameBuildingsTexture;
gameCloudsTexture->unload();
delete gameCloudsTexture;
gameGrassTexture->unload();
delete gameGrassTexture;
gamePowerMeterTexture->unload();
delete gamePowerMeterTexture;
gameSkyColorsTexture->unload();
delete gameSkyColorsTexture;
gameTextTexture->unload();
delete gameTextTexture;
gameOverTexture->unload();
delete gameOverTexture;
gameOverEndTexture->unload();
delete gameOverEndTexture;
// Animaciones
for (auto animation : playerAnimations) {
delete animation;
}
playerAnimations.clear(); playerAnimations.clear();
for (auto animation : balloonAnimations) {
delete animation;
}
balloonAnimations.clear(); balloonAnimations.clear();
for (auto animation : itemAnimations) {
delete animation;
}
itemAnimations.clear(); itemAnimations.clear();
// Texturas
for (auto texture : player1Textures) {
texture->unload();
delete texture;
}
player1Textures.clear(); player1Textures.clear();
for (auto texture : player2Textures) {
texture->unload();
delete texture;
}
player2Textures.clear(); player2Textures.clear();
for (auto texture : itemTextures) {
texture->unload();
delete texture;
}
itemTextures.clear(); itemTextures.clear();
for (auto texture : balloonTextures) {
texture->unload();
delete texture;
}
balloonTextures.clear(); balloonTextures.clear();
delete text;
delete textBig;
delete textScoreBoard;
delete textNokia2;
delete textNokiaBig2;
delete gameOverMenu;
delete pauseMenu;
delete fade; delete fade;
delete eventHandler; delete eventHandler;
delete clouds1A; delete clouds1A;
@@ -192,24 +128,6 @@ Game::~Game() {
delete powerMeterSprite; delete powerMeterSprite;
delete gameOverSprite; delete gameOverSprite;
delete gameOverEndSprite; delete gameOverEndSprite;
JA_DeleteSound(balloonSound);
JA_DeleteSound(bulletSound);
JA_DeleteSound(playerCollisionSound);
JA_DeleteSound(hiScoreSound);
JA_DeleteSound(itemDropSound);
JA_DeleteSound(itemPickUpSound);
JA_DeleteSound(coffeeOutSound);
JA_DeleteSound(stageChangeSound);
JA_DeleteSound(bubble1Sound);
JA_DeleteSound(bubble2Sound);
JA_DeleteSound(bubble3Sound);
JA_DeleteSound(bubble4Sound);
JA_DeleteSound(clockSound);
JA_DeleteSound(powerBallSound);
JA_DeleteSound(coffeeMachineSound);
JA_DeleteMusic(gameMusic);
} }
// Inicializa las variables necesarias para la sección 'Game' // Inicializa las variables necesarias para la sección 'Game'
@@ -284,6 +202,12 @@ void Game::init() {
effect.flash = false; effect.flash = false;
effect.shake = false; effect.shake = false;
effect.shakeCounter = SHAKE_COUNTER; effect.shakeCounter = SHAKE_COUNTER;
deathShake.active = false;
deathShake.step = 0;
deathShake.lastStepTicks = 0;
deathSequence.phase = DeathPhase::None;
deathSequence.phaseStartTicks = 0;
deathSequence.player = nullptr;
helper.needCoffee = false; helper.needCoffee = false;
helper.needCoffeeMachine = false; helper.needCoffeeMachine = false;
helper.needPowerBall = false; helper.needPowerBall = false;
@@ -299,6 +223,9 @@ void Game::init() {
coffeeMachineEnabled = false; coffeeMachineEnabled = false;
pauseCounter = 0; pauseCounter = 0;
leavingPauseMenu = false; leavingPauseMenu = false;
pauseInitialized = false;
gameOverInitialized = false;
gameOverPostFade = 0;
if (demo.enabled) { if (demo.enabled) {
const int num = rand() % 2; const int num = rand() % 2;
@@ -405,186 +332,106 @@ void Game::loadMedia() {
<< "** LOADING RESOURCES FOR GAME SECTION" << std::endl; << "** LOADING RESOURCES FOR GAME SECTION" << std::endl;
} }
// Texturas Resource *R = Resource::get();
bulletTexture = new Texture(renderer, asset->get("bullet.png"));
gameBuildingsTexture = new Texture(renderer, asset->get("game_buildings.png")); // Texturas (handles compartidos — no se liberan aquí)
gameCloudsTexture = new Texture(renderer, asset->get("game_clouds.png")); bulletTexture = R->getTexture("bullet.png");
gameGrassTexture = new Texture(renderer, asset->get("game_grass.png")); gameBuildingsTexture = R->getTexture("game_buildings.png");
gamePowerMeterTexture = new Texture(renderer, asset->get("game_power_meter.png")); gameCloudsTexture = R->getTexture("game_clouds.png");
gameSkyColorsTexture = new Texture(renderer, asset->get("game_sky_colors.png")); gameGrassTexture = R->getTexture("game_grass.png");
gameTextTexture = new Texture(renderer, asset->get("game_text.png")); gamePowerMeterTexture = R->getTexture("game_power_meter.png");
gameOverTexture = new Texture(renderer, asset->get("menu_game_over.png")); gameSkyColorsTexture = R->getTexture("game_sky_colors.png");
gameOverEndTexture = new Texture(renderer, asset->get("menu_game_over_end.png")); gameTextTexture = R->getTexture("game_text.png");
gameOverTexture = R->getTexture("menu_game_over.png");
gameOverEndTexture = R->getTexture("menu_game_over_end.png");
// Texturas - Globos // Texturas - Globos
Texture *balloon1Texture = new Texture(renderer, asset->get("balloon1.png")); balloonTextures.push_back(R->getTexture("balloon1.png"));
balloonTextures.push_back(balloon1Texture); balloonTextures.push_back(R->getTexture("balloon2.png"));
balloonTextures.push_back(R->getTexture("balloon3.png"));
Texture *balloon2Texture = new Texture(renderer, asset->get("balloon2.png")); balloonTextures.push_back(R->getTexture("balloon4.png"));
balloonTextures.push_back(balloon2Texture);
Texture *balloon3Texture = new Texture(renderer, asset->get("balloon3.png"));
balloonTextures.push_back(balloon3Texture);
Texture *balloon4Texture = new Texture(renderer, asset->get("balloon4.png"));
balloonTextures.push_back(balloon4Texture);
// Texturas - Items // Texturas - Items
Texture *item1 = new Texture(renderer, asset->get("item_points1_disk.png")); itemTextures.push_back(R->getTexture("item_points1_disk.png"));
itemTextures.push_back(item1); itemTextures.push_back(R->getTexture("item_points2_gavina.png"));
itemTextures.push_back(R->getTexture("item_points3_pacmar.png"));
Texture *item2 = new Texture(renderer, asset->get("item_points2_gavina.png")); itemTextures.push_back(R->getTexture("item_clock.png"));
itemTextures.push_back(item2); itemTextures.push_back(R->getTexture("item_coffee.png"));
itemTextures.push_back(R->getTexture("item_coffee_machine.png"));
Texture *item3 = new Texture(renderer, asset->get("item_points3_pacmar.png"));
itemTextures.push_back(item3);
Texture *item4 = new Texture(renderer, asset->get("item_clock.png"));
itemTextures.push_back(item4);
Texture *item5 = new Texture(renderer, asset->get("item_coffee.png"));
itemTextures.push_back(item5);
Texture *item6 = new Texture(renderer, asset->get("item_coffee_machine.png"));
itemTextures.push_back(item6);
// Texturas - Player1 // Texturas - Player1
Texture *player1Head = new Texture(renderer, asset->get("player_bal1_head.png")); player1Textures.push_back(R->getTexture("player_bal1_head.png"));
player1Textures.push_back(player1Head); player1Textures.push_back(R->getTexture("player_bal1_body.png"));
player1Textures.push_back(R->getTexture("player_bal1_legs.png"));
Texture *player1Body = new Texture(renderer, asset->get("player_bal1_body.png")); player1Textures.push_back(R->getTexture("player_bal1_death.png"));
player1Textures.push_back(player1Body); player1Textures.push_back(R->getTexture("player_bal1_fire.png"));
Texture *player1Legs = new Texture(renderer, asset->get("player_bal1_legs.png"));
player1Textures.push_back(player1Legs);
Texture *player1Death = new Texture(renderer, asset->get("player_bal1_death.png"));
player1Textures.push_back(player1Death);
Texture *player1Fire = new Texture(renderer, asset->get("player_bal1_fire.png"));
player1Textures.push_back(player1Fire);
playerTextures.push_back(player1Textures); playerTextures.push_back(player1Textures);
// Texturas - Player2 // Texturas - Player2
Texture *player2Head = new Texture(renderer, asset->get("player_arounder_head.png")); player2Textures.push_back(R->getTexture("player_arounder_head.png"));
player2Textures.push_back(player2Head); player2Textures.push_back(R->getTexture("player_arounder_body.png"));
player2Textures.push_back(R->getTexture("player_arounder_legs.png"));
Texture *player2Body = new Texture(renderer, asset->get("player_arounder_body.png")); player2Textures.push_back(R->getTexture("player_arounder_death.png"));
player2Textures.push_back(player2Body); player2Textures.push_back(R->getTexture("player_arounder_fire.png"));
Texture *player2Legs = new Texture(renderer, asset->get("player_arounder_legs.png"));
player2Textures.push_back(player2Legs);
Texture *player2Death = new Texture(renderer, asset->get("player_arounder_death.png"));
player2Textures.push_back(player2Death);
Texture *player2Fire = new Texture(renderer, asset->get("player_arounder_fire.png"));
player2Textures.push_back(player2Fire);
playerTextures.push_back(player2Textures); playerTextures.push_back(player2Textures);
// Animaciones -- Jugador // Animaciones (handles a los vectores raw de Resource — no se liberan)
std::vector<std::string> *playerHeadAnimation = new std::vector<std::string>; playerAnimations.push_back(&R->getAnimationLines("player_head.ani"));
loadAnimations(asset->get("player_head.ani"), playerHeadAnimation); playerAnimations.push_back(&R->getAnimationLines("player_body.ani"));
playerAnimations.push_back(playerHeadAnimation); playerAnimations.push_back(&R->getAnimationLines("player_legs.ani"));
playerAnimations.push_back(&R->getAnimationLines("player_death.ani"));
playerAnimations.push_back(&R->getAnimationLines("player_fire.ani"));
std::vector<std::string> *playerBodyAnimation = new std::vector<std::string>; balloonAnimations.push_back(&R->getAnimationLines("balloon1.ani"));
loadAnimations(asset->get("player_body.ani"), playerBodyAnimation); balloonAnimations.push_back(&R->getAnimationLines("balloon2.ani"));
playerAnimations.push_back(playerBodyAnimation); balloonAnimations.push_back(&R->getAnimationLines("balloon3.ani"));
balloonAnimations.push_back(&R->getAnimationLines("balloon4.ani"));
std::vector<std::string> *playerLegsAnimation = new std::vector<std::string>; itemAnimations.push_back(&R->getAnimationLines("item_points1_disk.ani"));
loadAnimations(asset->get("player_legs.ani"), playerLegsAnimation); itemAnimations.push_back(&R->getAnimationLines("item_points2_gavina.ani"));
playerAnimations.push_back(playerLegsAnimation); itemAnimations.push_back(&R->getAnimationLines("item_points3_pacmar.ani"));
itemAnimations.push_back(&R->getAnimationLines("item_clock.ani"));
std::vector<std::string> *playerDeathAnimation = new std::vector<std::string>; itemAnimations.push_back(&R->getAnimationLines("item_coffee.ani"));
loadAnimations(asset->get("player_death.ani"), playerDeathAnimation); itemAnimations.push_back(&R->getAnimationLines("item_coffee_machine.ani"));
playerAnimations.push_back(playerDeathAnimation);
std::vector<std::string> *playerFireAnimation = new std::vector<std::string>;
loadAnimations(asset->get("player_fire.ani"), playerFireAnimation);
playerAnimations.push_back(playerFireAnimation);
// Animaciones -- Globos
std::vector<std::string> *balloon1Animation = new std::vector<std::string>;
loadAnimations(asset->get("balloon1.ani"), balloon1Animation);
balloonAnimations.push_back(balloon1Animation);
std::vector<std::string> *balloon2Animation = new std::vector<std::string>;
loadAnimations(asset->get("balloon2.ani"), balloon2Animation);
balloonAnimations.push_back(balloon2Animation);
std::vector<std::string> *balloon3Animation = new std::vector<std::string>;
loadAnimations(asset->get("balloon3.ani"), balloon3Animation);
balloonAnimations.push_back(balloon3Animation);
std::vector<std::string> *balloon4Animation = new std::vector<std::string>;
loadAnimations(asset->get("balloon4.ani"), balloon4Animation);
balloonAnimations.push_back(balloon4Animation);
// Animaciones -- Items
std::vector<std::string> *item1Animation = new std::vector<std::string>;
loadAnimations(asset->get("item_points1_disk.ani"), item1Animation);
itemAnimations.push_back(item1Animation);
std::vector<std::string> *item2Animation = new std::vector<std::string>;
loadAnimations(asset->get("item_points2_gavina.ani"), item2Animation);
itemAnimations.push_back(item2Animation);
std::vector<std::string> *item3Animation = new std::vector<std::string>;
loadAnimations(asset->get("item_points3_pacmar.ani"), item3Animation);
itemAnimations.push_back(item3Animation);
std::vector<std::string> *item4Animation = new std::vector<std::string>;
loadAnimations(asset->get("item_clock.ani"), item4Animation);
itemAnimations.push_back(item4Animation);
std::vector<std::string> *item5Animation = new std::vector<std::string>;
loadAnimations(asset->get("item_coffee.ani"), item5Animation);
itemAnimations.push_back(item5Animation);
std::vector<std::string> *item6Animation = new std::vector<std::string>;
loadAnimations(asset->get("item_coffee_machine.ani"), item6Animation);
itemAnimations.push_back(item6Animation);
// Texto // Texto
text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); text = R->getText("smb2");
textScoreBoard = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer); textScoreBoard = R->getText("8bithud");
textBig = new Text(asset->get("smb2_big.png"), asset->get("smb2_big.txt"), renderer); textBig = R->getText("smb2_big");
textNokia2 = new Text(asset->get("nokia2.png"), asset->get("nokia2.txt"), renderer); textNokia2 = R->getText("nokia2");
textNokiaBig2 = new Text(asset->get("nokia_big2.png"), asset->get("nokia_big2.txt"), renderer); textNokiaBig2 = R->getText("nokia_big2");
// Menus // Menus
gameOverMenu = new Menu(renderer, asset, input, asset->get("gameover.men")); gameOverMenu = R->getMenu("gameover");
gameOverMenu->setItemCaption(0, lang->getText(48)); gameOverMenu->setItemCaption(0, lang->getText(48));
gameOverMenu->setItemCaption(1, lang->getText(49)); gameOverMenu->setItemCaption(1, lang->getText(49));
const int w = text->getCharacterSize() * lang->getText(45).length(); const int w = text->getCharacterSize() * lang->getText(45).length();
gameOverMenu->setRectSize(w, 0); gameOverMenu->setRectSize(w, 0);
gameOverMenu->centerMenuOnX(199); gameOverMenu->centerMenuOnX(199);
pauseMenu = new Menu(renderer, asset, input, asset->get("pause.men")); pauseMenu = R->getMenu("pause");
pauseMenu->setItemCaption(0, lang->getText(41)); pauseMenu->setItemCaption(0, lang->getText(41));
pauseMenu->setItemCaption(1, lang->getText(46)); pauseMenu->setItemCaption(1, lang->getText(46));
pauseMenu->setItemCaption(2, lang->getText(47)); pauseMenu->setItemCaption(2, lang->getText(47));
// Sonidos // Sonidos
balloonSound = JA_LoadSound(asset->get("balloon.wav").c_str()); balloonSound = R->getSound("balloon.wav");
bubble1Sound = JA_LoadSound(asset->get("bubble1.wav").c_str()); bubble1Sound = R->getSound("bubble1.wav");
bubble2Sound = JA_LoadSound(asset->get("bubble2.wav").c_str()); bubble2Sound = R->getSound("bubble2.wav");
bubble3Sound = JA_LoadSound(asset->get("bubble3.wav").c_str()); bubble3Sound = R->getSound("bubble3.wav");
bubble4Sound = JA_LoadSound(asset->get("bubble4.wav").c_str()); bubble4Sound = R->getSound("bubble4.wav");
bulletSound = JA_LoadSound(asset->get("bullet.wav").c_str()); bulletSound = R->getSound("bullet.wav");
clockSound = JA_LoadSound(asset->get("clock.wav").c_str()); clockSound = R->getSound("clock.wav");
coffeeOutSound = JA_LoadSound(asset->get("coffeeout.wav").c_str()); coffeeOutSound = R->getSound("coffeeout.wav");
hiScoreSound = JA_LoadSound(asset->get("hiscore.wav").c_str()); hiScoreSound = R->getSound("hiscore.wav");
itemDropSound = JA_LoadSound(asset->get("itemdrop.wav").c_str()); itemDropSound = R->getSound("itemdrop.wav");
itemPickUpSound = JA_LoadSound(asset->get("itempickup.wav").c_str()); itemPickUpSound = R->getSound("itempickup.wav");
playerCollisionSound = JA_LoadSound(asset->get("player_collision.wav").c_str()); playerCollisionSound = R->getSound("player_collision.wav");
powerBallSound = JA_LoadSound(asset->get("powerball.wav").c_str()); powerBallSound = R->getSound("powerball.wav");
stageChangeSound = JA_LoadSound(asset->get("stage_change.wav").c_str()); stageChangeSound = R->getSound("stage_change.wav");
coffeeMachineSound = JA_LoadSound(asset->get("title.wav").c_str()); coffeeMachineSound = R->getSound("title.wav");
// Musicas // Musicas
gameMusic = JA_LoadMusic(asset->get("playing.ogg").c_str()); gameMusic = R->getMusic("playing.ogg");
if (options->console) { if (options->console) {
std::cout << "** RESOURCES FOR GAME SECTION LOADED" << std::endl std::cout << "** RESOURCES FOR GAME SECTION LOADED" << std::endl
@@ -657,26 +504,21 @@ bool Game::loadScoreFile() {
// Carga el fichero de datos para la demo // Carga el fichero de datos para la demo
bool Game::loadDemoFile() { bool Game::loadDemoFile() {
// Indicador de éxito en la carga // Lee los datos de la demo desde Resource (precargados al arrancar).
bool success = true; const auto &bytes = Resource::get()->getDemoBytes();
const std::string p = asset->get("demo.bin"); const size_t expected = sizeof(demoKeys_t) * TOTAL_DEMO_DATA;
const std::string filename = p.substr(p.find_last_of("\\/") + 1); if (bytes.size() >= expected) {
SDL_IOStream *file = SDL_IOFromFile(p.c_str(), "r+b"); for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
memcpy(&demo.dataFile[i], bytes.data() + i * sizeof(demoKeys_t), sizeof(demoKeys_t));
// El fichero no existe
if (file == nullptr) {
if (options->console) {
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
} }
// Creamos el fichero para escritura
file = SDL_IOFromFile(p.c_str(), "w+b");
if (file != nullptr) {
if (options->console) { if (options->console) {
std::cout << "New file (" << filename.c_str() << ") created!" << std::endl; std::cout << "Demo data loaded (" << bytes.size() << " bytes)" << std::endl;
}
} else {
// Si no hay datos (bytes vacíos o tamaño inválido), inicializamos a cero.
if (options->console) {
std::cout << "Warning: demo data missing or too small, initializing to zero" << std::endl;
} }
// Inicializamos los datos
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) { for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
demo.keys.left = 0; demo.keys.left = 0;
demo.keys.right = 0; demo.keys.right = 0;
@@ -685,32 +527,9 @@ bool Game::loadDemoFile() {
demo.keys.fireLeft = 0; demo.keys.fireLeft = 0;
demo.keys.fireRight = 0; demo.keys.fireRight = 0;
demo.dataFile[i] = demo.keys; demo.dataFile[i] = demo.keys;
SDL_WriteIO(file, &demo.dataFile[i], sizeof(demoKeys_t));
}
// Cerramos el fichero
SDL_CloseIO(file);
} else {
if (options->console) {
std::cout << "Error: Unable to create file " << filename.c_str() << std::endl;
}
success = false;
} }
} }
// El fichero existe return true;
else {
// Cargamos los datos
if (options->console) {
std::cout << "Reading file " << filename.c_str() << std::endl;
}
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
SDL_ReadIO(file, &demo.dataFile[i], sizeof(demoKeys_t));
// Cierra el fichero
SDL_CloseIO(file);
}
return success;
} }
// Guarda el fichero de puntos // Guarda el fichero de puntos
@@ -1681,10 +1500,12 @@ void Game::updateDeath() {
if ((deathCounter == 250) || (deathCounter == 200) || (deathCounter == 180) || (deathCounter == 120) || (deathCounter == 60)) { if ((deathCounter == 250) || (deathCounter == 200) || (deathCounter == 180) || (deathCounter == 120) || (deathCounter == 60)) {
// Hace sonar aleatoriamente uno de los 4 sonidos de burbujas // Hace sonar aleatoriamente uno de los 4 sonidos de burbujas
if (!demo.enabled) {
const Uint8 index = rand() % 4; const Uint8 index = rand() % 4;
JA_Sound_t *sound[4] = {bubble1Sound, bubble2Sound, bubble3Sound, bubble4Sound}; JA_Sound_t *sound[4] = {bubble1Sound, bubble2Sound, bubble3Sound, bubble4Sound};
JA_PlaySound(sound[index], 0); JA_PlaySound(sound[index], 0);
} }
}
} else { } else {
section->subsection = SUBSECTION_GAME_GAMEOVER; section->subsection = SUBSECTION_GAME_GAMEOVER;
} }
@@ -1963,7 +1784,9 @@ void Game::destroyAllBalloons() {
} }
enemyDeployCounter = 255; enemyDeployCounter = 255;
if (!demo.enabled) {
JA_PlaySound(powerBallSound); JA_PlaySound(powerBallSound);
}
effect.flash = true; effect.flash = true;
effect.shake = true; effect.shake = true;
} }
@@ -2362,21 +2185,54 @@ void Game::killPlayer(Player *player) {
if (player->hasExtraHit()) { if (player->hasExtraHit()) {
player->removeExtraHit(); player->removeExtraHit();
throwCoffee(player->getPosX() + (player->getWidth() / 2), player->getPosY() + (player->getHeight() / 2)); throwCoffee(player->getPosX() + (player->getWidth() / 2), player->getPosY() + (player->getHeight() / 2));
if (!demo.enabled) {
JA_PlaySound(coffeeOutSound); JA_PlaySound(coffeeOutSound);
} else { }
} else if (deathSequence.phase == DeathPhase::None) {
if (!demo.enabled) {
JA_PauseMusic(); JA_PauseMusic();
stopAllBalloons(10);
JA_PlaySound(playerCollisionSound); JA_PlaySound(playerCollisionSound);
}
stopAllBalloons(10);
shakeScreen(); shakeScreen();
SDL_Delay(500); deathSequence.phase = DeathPhase::Shaking;
deathSequence.phaseStartTicks = SDL_GetTicks();
deathSequence.player = player;
}
}
}
// Actualiza la secuencia de muerte del jugador
void Game::updateDeathSequence() {
switch (deathSequence.phase) {
case DeathPhase::None:
case DeathPhase::Done:
break;
case DeathPhase::Shaking:
// Espera a que termine el efecto de agitación
if (!isDeathShaking()) {
deathSequence.phase = DeathPhase::Waiting;
deathSequence.phaseStartTicks = SDL_GetTicks();
}
break;
case DeathPhase::Waiting:
// Espera 500ms antes de completar la muerte
if (SDL_GetTicks() - deathSequence.phaseStartTicks >= 500) {
if (!demo.enabled) {
JA_PlaySound(coffeeOutSound); JA_PlaySound(coffeeOutSound);
player->setAlive(false);
if (allPlayersAreDead()) { if (allPlayersAreDead()) {
JA_StopMusic(); JA_StopMusic();
} else { } else {
JA_ResumeMusic(); JA_ResumeMusic();
} }
} }
deathSequence.player->setAlive(false);
deathSequence.phase = DeathPhase::Done;
deathSequence.player = nullptr;
}
break;
} }
} }
@@ -2439,6 +2295,15 @@ void Game::update() {
// Actualiza el audio // Actualiza el audio
JA_Update(); JA_Update();
// Actualiza los efectos basados en tiempo real (no en el throttle del juego)
updateDeathShake();
updateDeathSequence();
// Durante la secuencia de muerte, congela el resto del juego
if (deathSequence.phase == DeathPhase::Shaking || deathSequence.phase == DeathPhase::Waiting) {
return;
}
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
if (SDL_GetTicks() - ticks > ticksSpeed) { if (SDL_GetTicks() - ticks > ticksSpeed) {
// Actualiza el contador de ticks // Actualiza el contador de ticks
@@ -2550,7 +2415,10 @@ void Game::updateBackground() {
grassSprite->setSpriteClip(0, (6 * (counter / 20 % 2)), 256, 6); grassSprite->setSpriteClip(0, (6 * (counter / 20 % 2)), 256, 6);
// Mueve los edificios en funcion de si está activo el efecto de agitarlos // Mueve los edificios en funcion de si está activo el efecto de agitarlos
if (effect.shake) { if (deathShake.active) {
const int v[] = {-1, 1, -1, 1, -1, 1, -1, 0};
buildingsSprite->setPosX(v[deathShake.step]);
} else if (effect.shake) {
buildingsSprite->setPosX(((effect.shakeCounter % 2) * 2) - 1); buildingsSprite->setPosX(((effect.shakeCounter % 2) * 2) - 1);
} else { } else {
buildingsSprite->setPosX(0); buildingsSprite->setPosX(0);
@@ -2651,15 +2519,15 @@ void Game::checkGameInput() {
// Comprueba las teclas de cambiar el tamaño de la centana y el modo de video // Comprueba las teclas de cambiar el tamaño de la centana y el modo de video
if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) { if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
screen->switchVideoMode(); screen->toggleVideoMode();
} }
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) { else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) {
screen->decWindowSize(); screen->decWindowZoom();
} }
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) { else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) {
screen->incWindowSize(); screen->incWindowZoom();
} }
// Modo Demo activo // Modo Demo activo
@@ -2777,7 +2645,7 @@ void Game::checkGameInput() {
} }
// Comprueba el input de pausa // Comprueba el input de pausa
if (input->checkInput(input_cancel, REPEAT_FALSE, options->input[i].deviceType, options->input[i].id)) { if (input->checkInput(input_pause, REPEAT_FALSE, options->input[i].deviceType, options->input[i].id)) {
section->subsection = SUBSECTION_GAME_PAUSE; section->subsection = SUBSECTION_GAME_PAUSE;
} }
@@ -2868,63 +2736,71 @@ void Game::disableTimeStopItem() {
} }
} }
// Agita la pantalla // Inicia el efecto de agitación intensa de la pantalla
void Game::shakeScreen() { void Game::shakeScreen() {
const int v[] = {-1, 1, -1, 1, -1, 1, -1, 0}; deathShake.active = true;
for (int n = 0; n < 8; ++n) { deathShake.step = 0;
// Prepara para empezar a dibujar en la textura de juego deathShake.lastStepTicks = SDL_GetTicks();
screen->start(); }
// Limpia la pantalla // Actualiza el efecto de agitación intensa
screen->clean(bgColor); void Game::updateDeathShake() {
if (!deathShake.active) return;
// Dibuja los objetos Uint32 now = SDL_GetTicks();
buildingsSprite->setPosX(0); if (now - deathShake.lastStepTicks >= 50) {
buildingsSprite->setWidth(1); deathShake.lastStepTicks = now;
buildingsSprite->setSpriteClip(0, 0, 1, 160); deathShake.step++;
renderBackground(); if (deathShake.step >= 8) {
deathShake.active = false;
buildingsSprite->setPosX(255); }
buildingsSprite->setSpriteClip(255, 0, 1, 160); }
buildingsSprite->render(); }
buildingsSprite->setPosX(v[n]); // Indica si el efecto de agitación intensa está activo
buildingsSprite->setWidth(256); bool Game::isDeathShaking() {
buildingsSprite->setSpriteClip(0, 0, 256, 160); return deathShake.active;
buildingsSprite->render(); }
grassSprite->render(); // Ejecuta un frame del juego
renderBalloons(); void Game::iterate() {
renderBullets(); // En modo demo, no hay pausa ni game over
renderItems(); if (demo.enabled) {
renderPlayers(); if (section->subsection == SUBSECTION_GAME_PAUSE || section->subsection == SUBSECTION_GAME_GAMEOVER) {
renderScoreBoard(); section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_INSTRUCTIONS;
// Vuelca el contenido del renderizador en pantalla return;
screen->blit();
SDL_Delay(50);
} }
} }
// Bucle para el juego
void Game::run() {
while (section->name == SECTION_PROG_GAME) {
// Sección juego en pausa // Sección juego en pausa
if (section->subsection == SUBSECTION_GAME_PAUSE) { if (section->subsection == SUBSECTION_GAME_PAUSE) {
runPausedGame(); if (!pauseInitialized) {
enterPausedGame();
}
updatePausedGame();
renderPausedGame();
} }
// Sección Game Over // Sección Game Over
if (section->subsection == SUBSECTION_GAME_GAMEOVER) { else if (section->subsection == SUBSECTION_GAME_GAMEOVER) {
runGameOverScreen(); if (!gameOverInitialized) {
enterGameOverScreen();
}
updateGameOverScreen();
renderGameOverScreen();
} }
// Sección juego jugando // Sección juego jugando
if ((section->subsection == SUBSECTION_GAME_PLAY_1P) || (section->subsection == SUBSECTION_GAME_PLAY_2P)) { else if ((section->subsection == SUBSECTION_GAME_PLAY_1P) || (section->subsection == SUBSECTION_GAME_PLAY_2P)) {
// Resetea los flags de inicialización de sub-estados
pauseInitialized = false;
gameOverInitialized = false;
// Si la música no está sonando // Si la música no está sonando
if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) { if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) {
// Reproduce la música // Reproduce la música (nunca en modo demo: deja sonar la del título)
if (!gameCompleted) { if (!gameCompleted && !demo.enabled) {
if (players[0]->isAlive()) { if (players[0]->isAlive()) {
JA_PlayMusic(gameMusic); JA_PlayMusic(gameMusic);
} }
@@ -2939,13 +2815,52 @@ void Game::run() {
update(); update();
#endif #endif
// Comprueba los eventos que hay en cola
checkEvents();
// Dibuja los objetos // Dibuja los objetos
render(); render();
} }
} }
// Indica si el juego ha terminado
bool Game::hasFinished() const {
return section->name != SECTION_PROG_GAME;
}
// Procesa un evento individual
void Game::handleEvent(SDL_Event *event) {
// SDL_EVENT_QUIT ya lo maneja Director
if (event->type == SDL_EVENT_WINDOW_FOCUS_LOST) {
// Solo pausar durante el juego activo (no en demo, game over, ni ya en pausa)
if (!demo.enabled && (section->subsection == SUBSECTION_GAME_PLAY_1P || section->subsection == SUBSECTION_GAME_PLAY_2P)) {
section->subsection = SUBSECTION_GAME_PAUSE;
}
}
#ifdef PAUSE
if (event->type == SDL_EVENT_KEY_DOWN) {
if (event->key.scancode == SDL_SCANCODE_P) {
pause = !pause;
}
}
#endif
// Eventos específicos de la pantalla de game over
if (section->subsection == SUBSECTION_GAME_GAMEOVER) {
if (event->type == SDL_EVENT_KEY_DOWN && event->key.repeat == 0) {
if (gameCompleted) {
gameOverPostFade = 1;
fade->activateFade();
JA_PlaySound(itemPickUpSound);
}
}
}
}
// Bucle para el juego
void Game::run() {
while (!hasFinished()) {
iterate();
}
} }
// Actualiza las variables del menu de pausa del juego // Actualiza las variables del menu de pausa del juego
@@ -3045,8 +2960,8 @@ void Game::renderPausedGame() {
screen->blit(); screen->blit();
} }
// Bucle para el menu de pausa del juego // Inicializa el estado de pausa del juego
void Game::runPausedGame() { void Game::enterPausedGame() {
// Pone en pausa la música // Pone en pausa la música
if (JA_GetMusicState() == JA_MUSIC_PLAYING) { if (JA_GetMusicState() == JA_MUSIC_PLAYING) {
JA_PauseMusic(); JA_PauseMusic();
@@ -3058,19 +2973,11 @@ void Game::runPausedGame() {
// Inicializa variables // Inicializa variables
pauseCounter = 90; pauseCounter = 90;
pauseInitialized = true;
while ((section->subsection == SUBSECTION_GAME_PAUSE) && (section->name == SECTION_PROG_GAME)) {
updatePausedGame();
checkEvents();
renderPausedGame();
}
} }
// Actualiza los elementos de la pantalla de game over // Actualiza los elementos de la pantalla de game over
void Game::updateGameOverScreen() { void Game::updateGameOverScreen() {
// Variables
static int postFade = 0;
// Calcula la lógica de los objetos // Calcula la lógica de los objetos
if (SDL_GetTicks() - ticks > ticksSpeed) { if (SDL_GetTicks() - ticks > ticksSpeed) {
// Actualiza el contador de ticks // Actualiza el contador de ticks
@@ -3084,7 +2991,7 @@ void Game::updateGameOverScreen() {
// Si ha terminado el fade, actua segun se haya operado // Si ha terminado el fade, actua segun se haya operado
if (fade->hasEnded()) { if (fade->hasEnded()) {
switch (postFade) { switch (gameOverPostFade) {
case 0: // YES case 0: // YES
section->name = SECTION_PROG_GAME; section->name = SECTION_PROG_GAME;
deleteAllVectorObjects(); deleteAllVectorObjects();
@@ -3109,12 +3016,12 @@ void Game::updateGameOverScreen() {
// Comprueba si se ha seleccionado algún item del menú // Comprueba si se ha seleccionado algún item del menú
switch (gameOverMenu->getItemSelected()) { switch (gameOverMenu->getItemSelected()) {
case 0: // YES case 0: // YES
postFade = 0; gameOverPostFade = 0;
fade->activateFade(); fade->activateFade();
break; break;
case 1: // NO case 1: // NO
postFade = 1; gameOverPostFade = 1;
fade->activateFade(); fade->activateFade();
break; break;
@@ -3123,8 +3030,10 @@ void Game::updateGameOverScreen() {
} }
} }
} }
}
// Comprueba los eventos que hay en la cola // Comprueba los eventos de la pantalla de game over
void Game::checkGameOverEvents() {
while (SDL_PollEvent(eventHandler) != 0) { while (SDL_PollEvent(eventHandler) != 0) {
// Evento de salida de la aplicación // Evento de salida de la aplicación
if (eventHandler->type == SDL_EVENT_QUIT) { if (eventHandler->type == SDL_EVENT_QUIT) {
@@ -3132,7 +3041,7 @@ void Game::updateGameOverScreen() {
break; break;
} else if (eventHandler->type == SDL_EVENT_KEY_DOWN && eventHandler->key.repeat == 0) { } else if (eventHandler->type == SDL_EVENT_KEY_DOWN && eventHandler->key.repeat == 0) {
if (gameCompleted) { if (gameCompleted) {
postFade = 1; gameOverPostFade = 1;
fade->activateFade(); fade->activateFade();
JA_PlaySound(itemPickUpSound); JA_PlaySound(itemPickUpSound);
} }
@@ -3196,18 +3105,15 @@ void Game::renderGameOverScreen() {
screen->blit(); screen->blit();
} }
// Bucle para la pantalla de game over // Inicializa el estado de game over
void Game::runGameOverScreen() { void Game::enterGameOverScreen() {
// Guarda los puntos // Guarda los puntos
saveScoreFile(); saveScoreFile();
// Reinicia el menu // Reinicia el menu
gameOverMenu->reset(); gameOverMenu->reset();
gameOverPostFade = 0;
while ((section->subsection == SUBSECTION_GAME_GAMEOVER) && (section->name == SECTION_PROG_GAME)) { gameOverInitialized = true;
updateGameOverScreen();
renderGameOverScreen();
}
} }
// Indica si se puede crear una powerball // Indica si se puede crear una powerball
@@ -3357,22 +3263,6 @@ void Game::checkEvents() {
} }
} }
// Carga las animaciones
void Game::loadAnimations(std::string filePath, std::vector<std::string> *buffer) {
std::ifstream file(filePath);
std::string line;
if (file) {
if (options->console) {
std::cout << "Animation loaded: " << filePath.substr(filePath.find_last_of("\\/") + 1).c_str() << std::endl;
}
while (std::getline(file, line)) {
buffer->push_back(line);
}
file.close();
}
}
// Elimina todos los objetos contenidos en vectores // Elimina todos los objetos contenidos en vectores
void Game::deleteAllVectorObjects() { void Game::deleteAllVectorObjects() {
for (auto player : players) { for (auto player : players) {

View File

@@ -88,6 +88,26 @@ class Game {
Uint8 shakeCounter; // Contador para medir el tiempo que dura el efecto Uint8 shakeCounter; // Contador para medir el tiempo que dura el efecto
}; };
// Estado para el efecto de agitación intensa (muerte del jugador)
struct deathShake_t {
bool active; // Indica si el efecto está activo
Uint8 step; // Paso actual del efecto (0-7)
Uint32 lastStepTicks; // Ticks del último paso
};
// Fases de la secuencia de muerte del jugador
enum class DeathPhase { None,
Shaking,
Waiting,
Done };
// Estado de la secuencia de muerte del jugador
struct deathSequence_t {
DeathPhase phase; // Fase actual
Uint32 phaseStartTicks; // Ticks del inicio de la fase actual
Player *player; // Jugador que está muriendo
};
struct helper_t { struct helper_t {
bool needCoffee; // Indica si se necesitan cafes bool needCoffee; // Indica si se necesitan cafes
bool needCoffeeMachine; // Indica si se necesita PowerUp bool needCoffeeMachine; // Indica si se necesita PowerUp
@@ -214,6 +234,8 @@ class Game {
float enemySpeed; // Velocidad a la que se mueven los enemigos float enemySpeed; // Velocidad a la que se mueven los enemigos
float defaultEnemySpeed; // Velocidad base de los enemigos, sin incrementar float defaultEnemySpeed; // Velocidad base de los enemigos, sin incrementar
effect_t effect; // Variable para gestionar los efectos visuales effect_t effect; // Variable para gestionar los efectos visuales
deathShake_t deathShake; // Variable para gestionar el efecto de agitación intensa
deathSequence_t deathSequence; // Variable para gestionar la secuencia de muerte
helper_t helper; // Variable para gestionar las ayudas helper_t helper; // Variable para gestionar las ayudas
bool powerBallEnabled; // Indica si hay una powerball ya activa bool powerBallEnabled; // Indica si hay una powerball ya activa
Uint8 powerBallCounter; // Contador de formaciones enemigas entre la aparicion de una PowerBall y otra Uint8 powerBallCounter; // Contador de formaciones enemigas entre la aparicion de una PowerBall y otra
@@ -233,6 +255,9 @@ class Game {
int cloudsSpeed; // Velocidad a la que se desplazan las nubes int cloudsSpeed; // Velocidad a la que se desplazan las nubes
int pauseCounter; // Contador para salir del menu de pausa y volver al juego int pauseCounter; // Contador para salir del menu de pausa y volver al juego
bool leavingPauseMenu; // Indica si esta saliendo del menu de pausa para volver al juego bool leavingPauseMenu; // Indica si esta saliendo del menu de pausa para volver al juego
bool pauseInitialized; // Indica si la pausa ha sido inicializada
bool gameOverInitialized; // Indica si el game over ha sido inicializado
int gameOverPostFade; // Opción a realizar cuando termina el fundido del game over
#ifdef PAUSE #ifdef PAUSE
bool pause; bool pause;
#endif #endif
@@ -459,17 +484,26 @@ class Game {
// Deshabilita el efecto del item de detener el tiempo // Deshabilita el efecto del item de detener el tiempo
void disableTimeStopItem(); void disableTimeStopItem();
// Agita la pantalla // Inicia el efecto de agitación intensa de la pantalla
void shakeScreen(); void shakeScreen();
// Actualiza el efecto de agitación intensa
void updateDeathShake();
// Indica si el efecto de agitación intensa está activo
bool isDeathShaking();
// Actualiza la secuencia de muerte del jugador
void updateDeathSequence();
// Actualiza las variables del menu de pausa del juego // Actualiza las variables del menu de pausa del juego
void updatePausedGame(); void updatePausedGame();
// Dibuja el menu de pausa del juego // Dibuja el menu de pausa del juego
void renderPausedGame(); void renderPausedGame();
// Bucle para el menu de pausa del juego // Inicializa el estado de pausa del juego
void runPausedGame(); void enterPausedGame();
// Actualiza los elementos de la pantalla de game over // Actualiza los elementos de la pantalla de game over
void updateGameOverScreen(); void updateGameOverScreen();
@@ -477,8 +511,11 @@ class Game {
// Dibuja los elementos de la pantalla de game over // Dibuja los elementos de la pantalla de game over
void renderGameOverScreen(); void renderGameOverScreen();
// Bucle para la pantalla de game over // Inicializa el estado de game over
void runGameOverScreen(); void enterGameOverScreen();
// Comprueba los eventos de la pantalla de game over
void checkGameOverEvents();
// Indica si se puede crear una powerball // Indica si se puede crear una powerball
bool canPowerBallBeCreated(); bool canPowerBallBeCreated();
@@ -498,9 +535,6 @@ class Game {
// Comprueba si todos los jugadores han muerto // Comprueba si todos los jugadores han muerto
bool allPlayersAreDead(); bool allPlayersAreDead();
// Carga las animaciones
void loadAnimations(std::string filePath, std::vector<std::string> *buffer);
// Elimina todos los objetos contenidos en vectores // Elimina todos los objetos contenidos en vectores
void deleteAllVectorObjects(); void deleteAllVectorObjects();
@@ -519,4 +553,13 @@ class Game {
// Bucle para el juego // Bucle para el juego
void run(); void run();
// Ejecuta un frame del juego
void iterate();
// Indica si el juego ha terminado
bool hasFinished() const;
// Procesa un evento
void handleEvent(SDL_Event *event);
}; };

View File

@@ -4,6 +4,40 @@
#include <iostream> // for basic_ostream, operator<<, cout, basi... #include <iostream> // for basic_ostream, operator<<, cout, basi...
// Emscripten-only: SDL 3.4+ ja no casa el GUID dels mandos de Chrome Android
// amb gamecontrollerdb (el gamepad.id d'Android no porta Vendor/Product, el
// parser extreu valors escombraries, el GUID resultant no està a la db i el
// gamepad queda obert amb un mapping incorrecte). Com el W3C Gamepad API
// garanteix el layout estàndard quan el navegador reporta mapping=="standard",
// injectem un mapping SDL amb eixe layout per al GUID del joystick abans
// d'obrir-lo com gamepad. Fora d'Emscripten és un no-op.
static void installWebStandardMapping(SDL_JoystickID jid) {
#ifdef __EMSCRIPTEN__
SDL_GUID guid = SDL_GetJoystickGUIDForID(jid);
char guidStr[33];
SDL_GUIDToString(guid, guidStr, sizeof(guidStr));
const char *name = SDL_GetJoystickNameForID(jid);
if (!name || !*name) name = "Standard Gamepad";
char mapping[512];
SDL_snprintf(mapping, sizeof(mapping),
"%s,%s,"
"a:b0,b:b1,x:b2,y:b3,"
"leftshoulder:b4,rightshoulder:b5,"
"lefttrigger:b6,righttrigger:b7,"
"back:b8,start:b9,"
"leftstick:b10,rightstick:b11,"
"dpup:b12,dpdown:b13,dpleft:b14,dpright:b15,"
"guide:b16,"
"leftx:a0,lefty:a1,rightx:a2,righty:a3,"
"platform:Emscripten",
guidStr, name);
SDL_AddGamepadMapping(mapping);
#else
(void)jid;
#endif
}
// Constructor // Constructor
Input::Input(std::string file) { Input::Input(std::string file) {
// Fichero gamecontrollerdb.txt // Fichero gamecontrollerdb.txt
@@ -20,10 +54,24 @@ Input::Input(std::string file) {
gcb.active = false; gcb.active = false;
gameControllerBindings.resize(input_number_of_inputs, gcb); gameControllerBindings.resize(input_number_of_inputs, gcb);
numGamepads = 0;
verbose = true; verbose = true;
enabled = true; enabled = true;
} }
// Destructor
Input::~Input() {
for (auto *pad : connectedControllers) {
if (pad != nullptr) {
SDL_CloseGamepad(pad);
}
}
connectedControllers.clear();
connectedControllerIds.clear();
controllerNames.clear();
numGamepads = 0;
}
// Actualiza el estado del objeto // Actualiza el estado del objeto
void Input::update() { void Input::update() {
if (disabledUntil == d_keyPressed && !checkAnyInput()) { if (disabledUntil == d_keyPressed && !checkAnyInput()) {
@@ -82,7 +130,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) {
} }
} }
if (gameControllerFound()) if (gameControllerFound() && index >= 0 && index < (int)connectedControllers.size())
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) { if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) {
if (repeat) { if (repeat) {
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button)) { if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button)) {
@@ -128,7 +176,7 @@ bool Input::checkAnyInput(int device, int index) {
} }
} }
if (gameControllerFound()) { if (gameControllerFound() && index >= 0 && index < (int)connectedControllers.size()) {
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) { if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) {
for (int i = 0; i < (int)gameControllerBindings.size(); ++i) { for (int i = 0; i < (int)gameControllerBindings.size(); ++i) {
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[i].button)) { if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[i].button)) {
@@ -141,8 +189,40 @@ bool Input::checkAnyInput(int device, int index) {
return false; return false;
} }
// Busca si hay un mando conectado // Construye el nombre visible de un mando.
// Recorta des del primer '(' o '[' (per a evitar coses tipus
// "Retroid Controller (vendor: 1001) ...") i talla a 25 caràcters.
std::string Input::buildControllerName(SDL_Gamepad *pad, int padIndex) {
(void)padIndex;
const char *padName = SDL_GetGamepadName(pad);
std::string name = padName ? padName : "Unknown";
const auto pos = name.find_first_of("([");
if (pos != std::string::npos) {
name.erase(pos);
}
while (!name.empty() && name.back() == ' ') {
name.pop_back();
}
if (name.size() > 25) {
name.resize(25);
}
return name;
}
// Busca si hay un mando conectado. Cierra y limpia el estado previo para
// que la función sea idempotente si se invoca más de una vez.
bool Input::discoverGameController() { bool Input::discoverGameController() {
// Cierra los mandos ya abiertos y limpia los vectores paralelos
for (auto *pad : connectedControllers) {
if (pad != nullptr) {
SDL_CloseGamepad(pad);
}
}
connectedControllers.clear();
connectedControllerIds.clear();
controllerNames.clear();
numGamepads = 0;
bool found = false; bool found = false;
if (SDL_WasInit(SDL_INIT_GAMEPAD) != SDL_INIT_GAMEPAD) { if (SDL_WasInit(SDL_INIT_GAMEPAD) != SDL_INIT_GAMEPAD) {
@@ -157,42 +237,39 @@ bool Input::discoverGameController() {
int nJoysticks = 0; int nJoysticks = 0;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&nJoysticks); SDL_JoystickID *joysticks = SDL_GetJoysticks(&nJoysticks);
numGamepads = 0;
if (joysticks) { if (joysticks) {
// Cuenta el numero de mandos int gamepadCount = 0;
for (int i = 0; i < nJoysticks; ++i) { for (int i = 0; i < nJoysticks; ++i) {
if (SDL_IsGamepad(joysticks[i])) { if (SDL_IsGamepad(joysticks[i])) {
numGamepads++; gamepadCount++;
} }
} }
if (verbose) { if (verbose) {
std::cout << "\nChecking for game controllers...\n"; std::cout << "\nChecking for game controllers...\n";
std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n"; std::cout << nJoysticks << " joysticks found, " << gamepadCount << " are gamepads\n";
} }
if (numGamepads > 0) { if (gamepadCount > 0) {
found = true; found = true;
int padIndex = 0; int padIndex = 0;
for (int i = 0; i < nJoysticks; i++) { for (int i = 0; i < nJoysticks; i++) {
if (!SDL_IsGamepad(joysticks[i])) continue; if (!SDL_IsGamepad(joysticks[i])) continue;
// Abre el mando y lo añade a la lista installWebStandardMapping(joysticks[i]);
SDL_Gamepad *pad = SDL_OpenGamepad(joysticks[i]); SDL_Gamepad *pad = SDL_OpenGamepad(joysticks[i]);
if (pad != nullptr) { if (pad != nullptr) {
const std::string name = buildControllerName(pad, padIndex);
connectedControllers.push_back(pad); connectedControllers.push_back(pad);
const std::string separator(" #"); connectedControllerIds.push_back(joysticks[i]);
const char *padName = SDL_GetGamepadName(pad); controllerNames.push_back(name);
std::string name = padName ? padName : "Unknown"; numGamepads++;
name.resize(25); padIndex++;
name = name + separator + std::to_string(padIndex);
if (verbose) { if (verbose) {
std::cout << name << std::endl; std::cout << name << std::endl;
} }
controllerNames.push_back(name);
padIndex++;
} else { } else {
if (verbose) { if (verbose) {
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl; std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
@@ -209,6 +286,67 @@ bool Input::discoverGameController() {
return found; return found;
} }
// Procesa un evento SDL_EVENT_GAMEPAD_ADDED
bool Input::handleGamepadAdded(SDL_JoystickID jid, std::string &outName) {
if (!SDL_IsGamepad(jid)) {
return false;
}
// Si el mando ya está registrado no hace nada (ej. evento retroactivo tras el scan inicial)
for (SDL_JoystickID existing : connectedControllerIds) {
if (existing == jid) {
return false;
}
}
installWebStandardMapping(jid);
SDL_Gamepad *pad = SDL_OpenGamepad(jid);
if (pad == nullptr) {
if (verbose) {
std::cout << "Failed to open gamepad " << jid << ": " << SDL_GetError() << std::endl;
}
return false;
}
const int padIndex = (int)connectedControllers.size();
const std::string name = buildControllerName(pad, padIndex);
connectedControllers.push_back(pad);
connectedControllerIds.push_back(jid);
controllerNames.push_back(name);
numGamepads++;
if (verbose) {
std::cout << "Gamepad connected: " << name << std::endl;
}
outName = name;
return true;
}
// Procesa un evento SDL_EVENT_GAMEPAD_REMOVED
bool Input::handleGamepadRemoved(SDL_JoystickID jid, std::string &outName) {
for (size_t i = 0; i < connectedControllerIds.size(); ++i) {
if (connectedControllerIds[i] != jid) continue;
outName = controllerNames[i];
if (connectedControllers[i] != nullptr) {
SDL_CloseGamepad(connectedControllers[i]);
}
connectedControllers.erase(connectedControllers.begin() + i);
connectedControllerIds.erase(connectedControllerIds.begin() + i);
controllerNames.erase(controllerNames.begin() + i);
numGamepads--;
if (numGamepads < 0) numGamepads = 0;
if (verbose) {
std::cout << "Gamepad disconnected: " << outName << std::endl;
}
return true;
}
return false;
}
// Comprueba si hay algun mando conectado // Comprueba si hay algun mando conectado
bool Input::gameControllerFound() { bool Input::gameControllerFound() {
if (numGamepads > 0) { if (numGamepads > 0) {

View File

@@ -58,6 +58,7 @@ class Input {
// Objetos y punteros // Objetos y punteros
std::vector<SDL_Gamepad *> connectedControllers; // Vector con todos los mandos conectados std::vector<SDL_Gamepad *> connectedControllers; // Vector con todos los mandos conectados
std::vector<SDL_JoystickID> connectedControllerIds; // Instance IDs paralelos para mapear eventos
// Variables // Variables
std::vector<keyBindings_t> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos std::vector<keyBindings_t> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos
@@ -69,10 +70,16 @@ class Input {
i_disable_e disabledUntil; // Tiempo que esta deshabilitado i_disable_e disabledUntil; // Tiempo que esta deshabilitado
bool enabled; // Indica si está habilitado bool enabled; // Indica si está habilitado
// Construye el nombre visible de un mando (name truncado + sufijo #N)
std::string buildControllerName(SDL_Gamepad *pad, int padIndex);
public: public:
// Constructor // Constructor
Input(std::string file); Input(std::string file);
// Destructor
~Input();
// Actualiza el estado del objeto // Actualiza el estado del objeto
void update(); void update();
@@ -91,6 +98,14 @@ class Input {
// Busca si hay un mando conectado // Busca si hay un mando conectado
bool discoverGameController(); bool discoverGameController();
// Procesa un evento SDL_EVENT_GAMEPAD_ADDED. Devuelve true si el mando se ha añadido
// (no estaba ya registrado) y escribe el nombre visible en outName.
bool handleGamepadAdded(SDL_JoystickID jid, std::string &outName);
// Procesa un evento SDL_EVENT_GAMEPAD_REMOVED. Devuelve true si se ha encontrado y
// eliminado, y escribe el nombre visible en outName.
bool handleGamepadRemoved(SDL_JoystickID jid, std::string &outName);
// Comprueba si hay algun mando conectado // Comprueba si hay algun mando conectado
bool gameControllerFound(); bool gameControllerFound();

View File

@@ -11,14 +11,13 @@
#include "input.h" // for Input, REPEAT_FALSE, inputs_e #include "input.h" // for Input, REPEAT_FALSE, inputs_e
#include "jail_audio.hpp" // for JA_StopMusic #include "jail_audio.hpp" // for JA_StopMusic
#include "lang.h" // for Lang #include "lang.h" // for Lang
#include "resource.h"
#include "screen.h" // for Screen #include "screen.h" // for Screen
#include "sprite.h" // for Sprite #include "sprite.h" // for Sprite
#include "text.h" // for Text, TXT_CENTER, TXT_COLOR, TXT_SHADOW #include "text.h" // for Text, TXT_CENTER, TXT_COLOR, TXT_SHADOW
#include "texture.h" // for Texture #include "texture.h" // for Texture
#include "utils.h" // for color_t, section_t #include "utils.h" // for color_t, section_t
const Uint8 SELF = 0;
// Constructor // Constructor
Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, section_t *section) { Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, section_t *section) {
// Copia los punteros // Copia los punteros
@@ -29,29 +28,19 @@ Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *asset,
this->lang = lang; this->lang = lang;
this->section = section; this->section = section;
// Reserva memoria para los punteros // Texturas (handles compartidos de Resource)
Texture *item1 = new Texture(renderer, asset->get("item_points1_disk.png")); Resource *R = Resource::get();
itemTextures.push_back(item1); itemTextures.push_back(R->getTexture("item_points1_disk.png"));
itemTextures.push_back(R->getTexture("item_points2_gavina.png"));
Texture *item2 = new Texture(renderer, asset->get("item_points2_gavina.png")); itemTextures.push_back(R->getTexture("item_points3_pacmar.png"));
itemTextures.push_back(item2); itemTextures.push_back(R->getTexture("item_clock.png"));
itemTextures.push_back(R->getTexture("item_coffee.png"));
Texture *item3 = new Texture(renderer, asset->get("item_points3_pacmar.png")); itemTextures.push_back(R->getTexture("item_coffee_machine.png"));
itemTextures.push_back(item3);
Texture *item4 = new Texture(renderer, asset->get("item_clock.png"));
itemTextures.push_back(item4);
Texture *item5 = new Texture(renderer, asset->get("item_coffee.png"));
itemTextures.push_back(item5);
Texture *item6 = new Texture(renderer, asset->get("item_coffee_machine.png"));
itemTextures.push_back(item6);
eventHandler = new SDL_Event(); eventHandler = new SDL_Event();
sprite = new Sprite(0, 0, 16, 16, itemTextures[0], renderer); sprite = new Sprite(0, 0, 16, 16, itemTextures[0], renderer);
text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); text = R->getText("smb2");
// Crea un backbuffer para el renderizador // Crea un backbuffer para el renderizador
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
@@ -62,31 +51,32 @@ Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *asset,
} }
// Inicializa variables // Inicializa variables
section->name = SELF;
ticks = 0; ticks = 0;
ticksSpeed = 15; ticksSpeed = 15;
manualQuit = false; manualQuit = false;
counter = 0; counter = 0;
counterEnd = 600; counterEnd = 600;
finished = false;
quitRequested = false;
} }
// Destructor // Destructor
Instructions::~Instructions() { Instructions::~Instructions() {
for (auto texture : itemTextures) { // itemTextures y text son propiedad de Resource — no liberar.
texture->unload();
delete texture;
}
itemTextures.clear(); itemTextures.clear();
delete sprite; delete sprite;
delete eventHandler; delete eventHandler;
delete text;
SDL_DestroyTexture(backbuffer); SDL_DestroyTexture(backbuffer);
} }
// Actualiza las variables // Actualiza las variables
void Instructions::update() { void Instructions::update() {
// Bombea el stream de música: si no se llama, el buffer se vacía y la
// música se corta hasta que volvamos a una escena que sí lo haga.
JA_Update();
// Comprueba las entradas // Comprueba las entradas
checkInput(); checkInput();
@@ -99,15 +89,13 @@ void Instructions::update() {
counter++; counter++;
if (counter == counterEnd) { if (counter == counterEnd) {
section->name = SECTION_PROG_TITLE; finished = true;
section->subsection = SUBSECTION_TITLE_1;
} }
} else { // Modo manual } else { // Modo manual
++counter %= 60000; ++counter %= 60000;
if (manualQuit) { if (manualQuit) {
section->name = SECTION_PROG_TITLE; finished = true;
section->subsection = SUBSECTION_TITLE_3;
} }
} }
} }
@@ -211,39 +199,42 @@ void Instructions::render() {
// Comprueba los eventos // Comprueba los eventos
void Instructions::checkEvents() { void Instructions::checkEvents() {
#ifndef __EMSCRIPTEN__
// Comprueba los eventos que hay en la cola // Comprueba los eventos que hay en la cola
while (SDL_PollEvent(eventHandler) != 0) { while (SDL_PollEvent(eventHandler) != 0) {
// Evento de salida de la aplicación // Evento de salida de la aplicación
if (eventHandler->type == SDL_EVENT_QUIT) { if (eventHandler->type == SDL_EVENT_QUIT) {
section->name = SECTION_PROG_QUIT; quitRequested = true;
finished = true;
break; break;
} }
} }
#endif
} }
// Comprueba las entradas // Comprueba las entradas
void Instructions::checkInput() { void Instructions::checkInput() {
#ifndef __EMSCRIPTEN__
if (input->checkInput(input_exit, REPEAT_FALSE)) { if (input->checkInput(input_exit, REPEAT_FALSE)) {
section->name = SECTION_PROG_QUIT; quitRequested = true;
} finished = true;
} else
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) { #endif
screen->switchVideoMode(); if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
screen->toggleVideoMode();
} }
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) { else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) {
screen->decWindowSize(); screen->decWindowZoom();
} }
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) { else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) {
screen->incWindowSize(); screen->incWindowZoom();
} }
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE)) { else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE)) {
if (mode == m_auto) { if (mode == m_auto) {
JA_StopMusic(); finished = true;
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_1;
} else { } else {
if (counter > 30) { if (counter > 30) {
manualQuit = true; manualQuit = true;
@@ -252,13 +243,41 @@ void Instructions::checkInput() {
} }
} }
// Bucle para la pantalla de instrucciones // Bucle para la pantalla de instrucciones (compatibilidad)
void Instructions::run(mode_e mode) { void Instructions::run(mode_e mode) {
this->mode = mode; start(mode);
while (section->name == SELF) { while (!finished) {
update(); update();
checkEvents(); checkEvents();
render(); render();
} }
// Aplica los cambios de sección según el resultado
if (quitRequested) {
section->name = SECTION_PROG_QUIT;
} else {
section->name = SECTION_PROG_TITLE;
section->subsection = (mode == m_auto) ? SUBSECTION_TITLE_1 : SUBSECTION_TITLE_3;
}
}
// Inicia las instrucciones (sin bucle)
void Instructions::start(mode_e mode) {
this->mode = mode;
finished = false;
quitRequested = false;
manualQuit = false;
counter = 0;
ticks = 0;
}
// Indica si las instrucciones han terminado
bool Instructions::hasFinished() const {
return finished;
}
// Indica si se ha solicitado salir de la aplicación
bool Instructions::isQuitRequested() const {
return quitRequested;
} }

View File

@@ -40,15 +40,8 @@ class Instructions {
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
bool manualQuit; // Indica si se quiere salir del modo manual bool manualQuit; // Indica si se quiere salir del modo manual
mode_e mode; // Modo en el que se van a ejecutar las instrucciones mode_e mode; // Modo en el que se van a ejecutar las instrucciones
bool finished; // Indica si las instrucciones han terminado
// Actualiza las variables bool quitRequested; // Indica si se ha solicitado salir de la aplicación
void update();
// Pinta en pantalla
void render();
// Comprueba los eventos
void checkEvents();
// Comprueba las entradas // Comprueba las entradas
void checkInput(); void checkInput();
@@ -62,4 +55,22 @@ class Instructions {
// Bucle principal // Bucle principal
void run(mode_e mode); void run(mode_e mode);
// Inicia las instrucciones (sin bucle)
void start(mode_e mode);
// Actualiza las variables
void update();
// Pinta en pantalla
void render();
// Comprueba los eventos
void checkEvents();
// Indica si las instrucciones han terminado
bool hasFinished() const;
// Indica si se ha solicitado salir de la aplicación
bool isQuitRequested() const;
}; };

View File

@@ -9,6 +9,7 @@
#include "input.h" // for Input, REPEAT_FALSE, inputs_e #include "input.h" // for Input, REPEAT_FALSE, inputs_e
#include "jail_audio.hpp" // for JA_StopMusic, JA_DeleteMusic, JA_LoadMusic #include "jail_audio.hpp" // for JA_StopMusic, JA_DeleteMusic, JA_LoadMusic
#include "lang.h" // for Lang #include "lang.h" // for Lang
#include "resource.h"
#include "screen.h" // for Screen #include "screen.h" // for Screen
#include "smartsprite.h" // for SmartSprite #include "smartsprite.h" // for SmartSprite
#include "text.h" // for Text #include "text.h" // for Text
@@ -28,11 +29,9 @@ Intro::Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input,
// Reserva memoria para los objetos // Reserva memoria para los objetos
eventHandler = new SDL_Event(); eventHandler = new SDL_Event();
texture = new Texture(renderer, asset->get("intro.png")); texture = Resource::get()->getTexture("intro.png");
text = new Text(asset->get("nokia.png"), asset->get("nokia.txt"), renderer); text = Resource::get()->getText("nokia");
music = Resource::get()->getMusic("intro.ogg");
// Carga los recursos
loadMedia();
// Inicializa variables // Inicializa variables
section->name = SECTION_PROG_INTRO; section->name = SECTION_PROG_INTRO;
@@ -153,36 +152,32 @@ Intro::Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input,
for (auto text : texts) { for (auto text : texts) {
text->center(GAMECANVAS_CENTER_X); text->center(GAMECANVAS_CENTER_X);
} }
JA_PlayMusic(music, 0);
} }
// Destructor // Destructor
Intro::~Intro() { Intro::~Intro() {
delete eventHandler; delete eventHandler;
texture->unload(); // texture, text, music son propiedad de Resource — no liberar aquí.
delete texture;
for (auto bitmap : bitmaps) { for (auto bitmap : bitmaps) {
delete bitmap; delete bitmap;
} }
for (auto text : texts) { for (auto t : texts) {
delete text; delete t;
}
} }
JA_DeleteMusic(music); // Carga los recursos (ya no carga nada, se mantiene por si hay callers)
}
// Carga los recursos
bool Intro::loadMedia() { bool Intro::loadMedia() {
// Musicas
music = JA_LoadMusic(asset->get("intro.ogg").c_str());
return true; return true;
} }
// Comprueba los eventos // Comprueba los eventos
void Intro::checkEvents() { void Intro::checkEvents() {
#ifndef __EMSCRIPTEN__
// Comprueba los eventos que hay en la cola // Comprueba los eventos que hay en la cola
while (SDL_PollEvent(eventHandler) != 0) { while (SDL_PollEvent(eventHandler) != 0) {
// Evento de salida de la aplicación // Evento de salida de la aplicación
@@ -191,24 +186,26 @@ void Intro::checkEvents() {
break; break;
} }
} }
#endif
} }
// Comprueba las entradas // Comprueba las entradas
void Intro::checkInput() { void Intro::checkInput() {
#ifndef __EMSCRIPTEN__
if (input->checkInput(input_exit, REPEAT_FALSE)) { if (input->checkInput(input_exit, REPEAT_FALSE)) {
section->name = SECTION_PROG_QUIT; section->name = SECTION_PROG_QUIT;
} } else
#endif
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) { if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
screen->switchVideoMode(); screen->toggleVideoMode();
} }
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) { else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) {
screen->decWindowSize(); screen->decWindowZoom();
} }
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) { else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) {
screen->incWindowSize(); screen->incWindowZoom();
} }
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE)) { else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE)) {
@@ -403,8 +400,17 @@ void Intro::run() {
JA_PlayMusic(music, 0); JA_PlayMusic(music, 0);
while (section->name == SECTION_PROG_INTRO) { while (section->name == SECTION_PROG_INTRO) {
iterate();
}
}
// Ejecuta un frame
void Intro::iterate() {
update(); update();
checkEvents();
render(); render();
} }
// Procesa un evento individual
void Intro::handleEvent(SDL_Event *event) {
// SDL_EVENT_QUIT ya lo maneja Director
} }

View File

@@ -63,4 +63,10 @@ class Intro {
// Bucle principal // Bucle principal
void run(); void run();
// Ejecuta un frame
void iterate();
// Procesa un evento
void handleEvent(SDL_Event *event);
}; };

View File

@@ -43,18 +43,22 @@ struct JA_Channel_t {
struct JA_Music_t { struct JA_Music_t {
SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000}; SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000};
Uint32 length{0};
Uint8* buffer{nullptr}; // OGG comprimit en memòria. Propietat nostra; es copia des del fitxer una
// sola vegada en JA_LoadMusic i es descomprimix en chunks per streaming.
Uint8* ogg_data{nullptr};
Uint32 ogg_length{0};
stb_vorbis* vorbis{nullptr}; // Handle del decoder, viu tot el cicle del JA_Music_t
char* filename{nullptr}; char* filename{nullptr};
int pos{0}; int times{0}; // Loops restants (-1 = infinit, 0 = un sol play)
int times{0};
SDL_AudioStream* stream{nullptr}; SDL_AudioStream* stream{nullptr};
JA_Music_state state{JA_MUSIC_INVALID}; JA_Music_state state{JA_MUSIC_INVALID};
}; };
// --- Internal Global State --- // --- Internal Global State ---
// Marcado 'inline' (C++17) para asegurar una unica instancia. // Marcado 'inline' (C++17) para asegurar una única instancia.
inline JA_Music_t* current_music{nullptr}; inline JA_Music_t* current_music{nullptr};
inline JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS]; inline JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
@@ -76,6 +80,57 @@ inline void JA_StopMusic();
inline void JA_StopChannel(const int channel); inline void JA_StopChannel(const int channel);
inline int JA_PlaySoundOnChannel(JA_Sound_t* sound, const int channel, const int loop = 0, const int group = 0); inline int JA_PlaySoundOnChannel(JA_Sound_t* sound, const int channel, const int loop = 0, const int group = 0);
// --- Music streaming internals ---
// Bytes-per-sample per canal (sempre s16)
static constexpr int JA_MUSIC_BYTES_PER_SAMPLE = 2;
// Quants shorts decodifiquem per crida a get_samples_short_interleaved.
// 8192 shorts = 4096 samples/channel en estèreo ≈ 85ms de so a 48kHz.
static constexpr int JA_MUSIC_CHUNK_SHORTS = 8192;
// Umbral d'audio per davant del cursor de reproducció. Mantenim ≥ 0.5 s a
// l'SDL_AudioStream per absorbir jitter de frame i evitar underruns.
static constexpr float JA_MUSIC_LOW_WATER_SECONDS = 0.5f;
// Decodifica un chunk del vorbis i el volca a l'stream. Retorna samples
// decodificats per canal (0 = EOF de l'stream vorbis).
inline int JA_FeedMusicChunk(JA_Music_t* music) {
if (!music || !music->vorbis || !music->stream) return 0;
short chunk[JA_MUSIC_CHUNK_SHORTS];
const int channels = music->spec.channels;
const int samples_per_channel = stb_vorbis_get_samples_short_interleaved(
music->vorbis,
channels,
chunk,
JA_MUSIC_CHUNK_SHORTS);
if (samples_per_channel <= 0) return 0;
const int bytes = samples_per_channel * channels * JA_MUSIC_BYTES_PER_SAMPLE;
SDL_PutAudioStreamData(music->stream, chunk, bytes);
return samples_per_channel;
}
// Reompli l'stream fins que tinga ≥ JA_MUSIC_LOW_WATER_SECONDS bufferats.
// En arribar a EOF del vorbis, aplica el loop (times) o deixa drenar.
inline void JA_PumpMusic(JA_Music_t* music) {
if (!music || !music->vorbis || !music->stream) return;
const int bytes_per_second = music->spec.freq * music->spec.channels * JA_MUSIC_BYTES_PER_SAMPLE;
const int low_water_bytes = static_cast<int>(JA_MUSIC_LOW_WATER_SECONDS * static_cast<float>(bytes_per_second));
while (SDL_GetAudioStreamAvailable(music->stream) < low_water_bytes) {
const int decoded = JA_FeedMusicChunk(music);
if (decoded > 0) continue;
// EOF: si queden loops, rebobinar; si no, tallar i deixar drenar.
if (music->times != 0) {
stb_vorbis_seek_start(music->vorbis);
if (music->times > 0) music->times--;
} else {
break;
}
}
}
// --- Core Functions --- // --- Core Functions ---
inline void JA_Update() { inline void JA_Update() {
@@ -93,13 +148,11 @@ inline void JA_Update() {
} }
} }
if (current_music->times != 0) { // Streaming: rellenem l'stream fins al low-water-mark i parem si el
if ((Uint32)SDL_GetAudioStreamAvailable(current_music->stream) < (current_music->length / 2)) { // vorbis s'ha esgotat i no queden loops.
SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length); JA_PumpMusic(current_music);
} if (current_music->times == 0 && SDL_GetAudioStreamAvailable(current_music->stream) == 0) {
if (current_music->times > 0) current_music->times--; JA_StopMusic();
} else {
if (SDL_GetAudioStreamAvailable(current_music->stream) == 0) JA_StopMusic();
} }
} }
@@ -139,19 +192,31 @@ inline void JA_Quit() {
// --- Music Functions --- // --- Music Functions ---
inline JA_Music_t* JA_LoadMusic(const Uint8* buffer, Uint32 length) { inline JA_Music_t* JA_LoadMusic(const Uint8* buffer, Uint32 length) {
JA_Music_t* music = new JA_Music_t(); if (!buffer || length == 0) return nullptr;
int chan, samplerate; // Còpia del OGG comprimit: stb_vorbis llig de forma persistent aquesta
short* output; // memòria mentre el handle estiga viu, així que hem de posseir-la nosaltres.
music->length = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &output) * chan * 2; Uint8* ogg_copy = static_cast<Uint8*>(SDL_malloc(length));
if (!ogg_copy) return nullptr;
SDL_memcpy(ogg_copy, buffer, length);
music->spec.channels = chan; int error = 0;
music->spec.freq = samplerate; stb_vorbis* vorbis = stb_vorbis_open_memory(ogg_copy, static_cast<int>(length), &error, nullptr);
if (!vorbis) {
SDL_free(ogg_copy);
SDL_Log("JA_LoadMusic: stb_vorbis_open_memory failed (error %d)", error);
return nullptr;
}
auto* music = new JA_Music_t();
music->ogg_data = ogg_copy;
music->ogg_length = length;
music->vorbis = vorbis;
const stb_vorbis_info info = stb_vorbis_get_info(vorbis);
music->spec.channels = info.channels;
music->spec.freq = static_cast<int>(info.sample_rate);
music->spec.format = SDL_AUDIO_S16; music->spec.format = SDL_AUDIO_S16;
music->buffer = static_cast<Uint8*>(SDL_malloc(music->length));
SDL_memcpy(music->buffer, output, music->length);
free(output);
music->pos = 0;
music->state = JA_MUSIC_STOPPED; music->state = JA_MUSIC_STOPPED;
return music; return music;
@@ -189,26 +254,38 @@ inline JA_Music_t* JA_LoadMusic(const char* filename) {
} }
inline void JA_PlayMusic(JA_Music_t* music, const int loop = -1) { inline void JA_PlayMusic(JA_Music_t* music, const int loop = -1) {
if (!JA_musicEnabled || !music) return; if (!JA_musicEnabled || !music || !music->vorbis) return;
JA_StopMusic(); JA_StopMusic();
current_music = music; current_music = music;
current_music->pos = 0;
current_music->state = JA_MUSIC_PLAYING; current_music->state = JA_MUSIC_PLAYING;
current_music->times = loop; current_music->times = loop;
// Rebobinem l'stream de vorbis al principi. Cobreix tant play-per-primera-
// vegada com replays/canvis de track que tornen a la mateixa pista.
stb_vorbis_seek_start(current_music->vorbis);
current_music->stream = SDL_CreateAudioStream(&current_music->spec, &JA_audioSpec); current_music->stream = SDL_CreateAudioStream(&current_music->spec, &JA_audioSpec);
if (!current_music->stream) { if (!current_music->stream) {
SDL_Log("Failed to create audio stream!"); SDL_Log("Failed to create audio stream!");
current_music->state = JA_MUSIC_STOPPED; current_music->state = JA_MUSIC_STOPPED;
return; return;
} }
if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) printf("[ERROR] SDL_PutAudioStreamData failed!\n");
SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume);
// Pre-cargem el buffer abans de bindejar per evitar un underrun inicial.
JA_PumpMusic(current_music);
if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n"); if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n");
} }
inline char* JA_GetMusicFilename(const JA_Music_t* music = nullptr) {
if (!music) music = current_music;
if (!music) return nullptr;
return music->filename;
}
inline void JA_PauseMusic() { inline void JA_PauseMusic() {
if (!JA_musicEnabled) return; if (!JA_musicEnabled) return;
if (!current_music || current_music->state != JA_MUSIC_PLAYING) return; if (!current_music || current_music->state != JA_MUSIC_PLAYING) return;
@@ -228,12 +305,16 @@ inline void JA_ResumeMusic() {
inline void JA_StopMusic() { inline void JA_StopMusic() {
if (!current_music || current_music->state == JA_MUSIC_INVALID || current_music->state == JA_MUSIC_STOPPED) return; if (!current_music || current_music->state == JA_MUSIC_INVALID || current_music->state == JA_MUSIC_STOPPED) return;
current_music->pos = 0;
current_music->state = JA_MUSIC_STOPPED; current_music->state = JA_MUSIC_STOPPED;
if (current_music->stream) { if (current_music->stream) {
SDL_DestroyAudioStream(current_music->stream); SDL_DestroyAudioStream(current_music->stream);
current_music->stream = nullptr; current_music->stream = nullptr;
} }
// Deixem el handle de vorbis viu — es tanca en JA_DeleteMusic.
// Rebobinem perquè un futur JA_PlayMusic comence des del principi.
if (current_music->vorbis) {
stb_vorbis_seek_start(current_music->vorbis);
}
} }
inline void JA_FadeOutMusic(const int milliseconds) { inline void JA_FadeOutMusic(const int milliseconds) {
@@ -259,8 +340,9 @@ inline void JA_DeleteMusic(JA_Music_t* music) {
JA_StopMusic(); JA_StopMusic();
current_music = nullptr; current_music = nullptr;
} }
SDL_free(music->buffer);
if (music->stream) SDL_DestroyAudioStream(music->stream); if (music->stream) SDL_DestroyAudioStream(music->stream);
if (music->vorbis) stb_vorbis_close(music->vorbis);
SDL_free(music->ogg_data);
free(music->filename); free(music->filename);
delete music; delete music;
} }
@@ -273,14 +355,14 @@ inline float JA_SetMusicVolume(float volume) {
return JA_musicVolume; return JA_musicVolume;
} }
inline void JA_SetMusicPosition(float value) { inline void JA_SetMusicPosition(float /*value*/) {
if (!current_music) return; // No implementat amb el backend de streaming. Mai va arribar a usar-se
current_music->pos = value * current_music->spec.freq; // en el codi existent, així que es manté com a stub.
} }
inline float JA_GetMusicPosition() { inline float JA_GetMusicPosition() {
if (!current_music) return 0; // Veure nota a JA_SetMusicPosition.
return float(current_music->pos) / float(current_music->spec.freq); return 0.0f;
} }
inline void JA_EnableMusic(const bool value) { inline void JA_EnableMusic(const bool value) {

View File

@@ -1,8 +1,10 @@
#include "lang.h" #include "lang.h"
#include <fstream> // for basic_ifstream, basic_istream, ifstream #include <fstream> // for basic_ifstream, basic_istream, ifstream
#include <sstream>
#include "asset.h" // for Asset #include "asset.h" // for Asset
#include "resource_helper.h"
// Constructor // Constructor
Lang::Lang(Asset *mAsset) { Lang::Lang(Asset *mAsset) {
@@ -38,16 +40,17 @@ bool Lang::setLang(Uint8 lang) {
for (int i = 0; i < MAX_TEXT_STRINGS; i++) for (int i = 0; i < MAX_TEXT_STRINGS; i++)
mTextStrings[i] = ""; mTextStrings[i] = "";
bool success = false; // Lee el fichero via ResourceHelper (pack o filesystem)
auto bytes = ResourceHelper::loadFile(file);
if (bytes.empty()) {
return false;
}
std::ifstream rfile(file); std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
if (rfile.is_open() && rfile.good()) { std::stringstream ss(content);
success = true;
std::string line; std::string line;
// lee el resto de datos del fichero
int index = 0; int index = 0;
while (std::getline(rfile, line)) { while (std::getline(ss, line)) {
// Almacena solo las lineas que no empiezan por # o no esten vacias // Almacena solo las lineas que no empiezan por # o no esten vacias
const bool test1 = line.substr(0, 1) != "#"; const bool test1 = line.substr(0, 1) != "#";
const bool test2 = !line.empty(); const bool test2 = !line.empty();
@@ -55,10 +58,9 @@ bool Lang::setLang(Uint8 lang) {
mTextStrings[index] = line; mTextStrings[index] = line;
index++; index++;
} }
};
} }
return success; return true;
} }
// Obtiene la cadena de texto del indice // Obtiene la cadena de texto del indice

View File

@@ -9,6 +9,7 @@
#include "const.h" // for bgColor, SECTION_PROG_LOGO, SECTION_PROG... #include "const.h" // for bgColor, SECTION_PROG_LOGO, SECTION_PROG...
#include "input.h" // for Input, REPEAT_FALSE, inputs_e #include "input.h" // for Input, REPEAT_FALSE, inputs_e
#include "jail_audio.hpp" // for JA_StopMusic #include "jail_audio.hpp" // for JA_StopMusic
#include "resource.h"
#include "screen.h" // for Screen #include "screen.h" // for Screen
#include "sprite.h" // for Sprite #include "sprite.h" // for Sprite
#include "texture.h" // for Texture #include "texture.h" // for Texture
@@ -29,7 +30,7 @@ Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, s
// Reserva memoria para los punteros // Reserva memoria para los punteros
eventHandler = new SDL_Event(); eventHandler = new SDL_Event();
texture = new Texture(renderer, asset->get("logo.png")); texture = Resource::get()->getTexture("logo.png");
sprite = new Sprite(14, 75, 226, 44, texture, renderer); sprite = new Sprite(14, 75, 226, 44, texture, renderer);
// Inicializa variables // Inicializa variables
@@ -38,13 +39,13 @@ Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, s
section->subsection = 0; section->subsection = 0;
ticks = 0; ticks = 0;
ticksSpeed = 15; ticksSpeed = 15;
JA_StopMusic();
} }
// Destructor // Destructor
Logo::~Logo() { Logo::~Logo() {
texture->unload(); // texture es propiedad de Resource — no liberar aquí.
delete texture;
delete sprite; delete sprite;
delete eventHandler; delete eventHandler;
} }
@@ -59,6 +60,7 @@ void Logo::checkLogoEnd() {
// Comprueba los eventos // Comprueba los eventos
void Logo::checkEvents() { void Logo::checkEvents() {
#ifndef __EMSCRIPTEN__
// Comprueba los eventos que hay en la cola // Comprueba los eventos que hay en la cola
while (SDL_PollEvent(eventHandler) != 0) { while (SDL_PollEvent(eventHandler) != 0) {
// Evento de salida de la aplicación // Evento de salida de la aplicación
@@ -67,24 +69,26 @@ void Logo::checkEvents() {
break; break;
} }
} }
#endif
} }
// Comprueba las entradas // Comprueba las entradas
void Logo::checkInput() { void Logo::checkInput() {
#ifndef __EMSCRIPTEN__
if (input->checkInput(input_exit, REPEAT_FALSE)) { if (input->checkInput(input_exit, REPEAT_FALSE)) {
section->name = SECTION_PROG_QUIT; section->name = SECTION_PROG_QUIT;
} } else
#endif
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) { if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
screen->switchVideoMode(); screen->toggleVideoMode();
} }
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) { else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) {
screen->decWindowSize(); screen->decWindowZoom();
} }
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) { else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) {
screen->incWindowSize(); screen->incWindowZoom();
} }
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE)) { else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE)) {
@@ -144,8 +148,17 @@ void Logo::run() {
JA_StopMusic(); JA_StopMusic();
while (section->name == SECTION_PROG_LOGO) { while (section->name == SECTION_PROG_LOGO) {
iterate();
}
}
// Ejecuta un frame
void Logo::iterate() {
update(); update();
checkEvents();
render(); render();
} }
// Procesa un evento individual
void Logo::handleEvent(SDL_Event *event) {
// SDL_EVENT_QUIT ya lo maneja Director
} }

View File

@@ -53,4 +53,10 @@ class Logo {
// Bucle principal // Bucle principal
void run(); void run();
// Ejecuta un frame
void iterate();
// Procesa un evento
void handleEvent(SDL_Event *event);
}; };

View File

@@ -39,15 +39,26 @@ Reescribiendo el código el 27/09/2022
*/ */
#include <memory> #define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL_main.h>
#include "director.h" #include "director.h"
#include "stb_vorbis.c" #include "stb_vorbis.c"
int main(int argc, char *argv[]) { SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
// Crea el objeto Director auto *director = new Director(argc, const_cast<const char **>(argv));
auto director = std::make_unique<Director>(argc, const_cast<const char **>(argv)); *appstate = director;
return SDL_APP_CONTINUE;
// Bucle principal }
return director->run();
SDL_AppResult SDL_AppIterate(void *appstate) {
return static_cast<Director *>(appstate)->iterate();
}
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
return static_cast<Director *>(appstate)->handleEvent(event);
}
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
delete static_cast<Director *>(appstate);
} }

View File

@@ -7,6 +7,7 @@
#include "asset.h" // for Asset #include "asset.h" // for Asset
#include "input.h" // for Input, REPEAT_FALSE, inputs_e #include "input.h" // for Input, REPEAT_FALSE, inputs_e
#include "jail_audio.hpp" // for JA_LoadSound, JA_PlaySound, JA_DeleteSound #include "jail_audio.hpp" // for JA_LoadSound, JA_PlaySound, JA_DeleteSound
#include "resource_helper.h"
#include "text.h" // for Text #include "text.h" // for Text
// Constructor // Constructor
@@ -61,17 +62,15 @@ Menu::Menu(SDL_Renderer *renderer, Asset *asset, Input *input, std::string file)
selector.itemColor = {0, 0, 0}; selector.itemColor = {0, 0, 0};
selector.a = 255; selector.a = 255;
// Inicializa las variables desde un fichero // Inicializa las variables desde un fichero. Si no se pasa fichero, el
// llamante (p.ej. Resource::preloadAll) usará loadFromBytes después —
// y ese método ya llama a setSelectorItemColors() y reset() al final.
if (file != "") { if (file != "") {
load(file); load(file);
}
// Calcula los colores del selector para el degradado
setSelectorItemColors(); setSelectorItemColors();
// Deja el cursor en el primer elemento
reset(); reset();
} }
}
Menu::~Menu() { Menu::~Menu() {
renderer = nullptr; renderer = nullptr;
@@ -95,22 +94,13 @@ Menu::~Menu() {
} }
} }
// Carga la configuración del menu desde un archivo de texto // Parser compartido (recibe cualquier istream)
bool Menu::load(std::string file_path) { bool Menu::parseFromStream(std::istream &file, const std::string &filename) {
// Indicador de éxito en la carga
bool success = true; bool success = true;
// Indica si se ha creado ya el objeto de texto
bool textAllocated = false; bool textAllocated = false;
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
std::string line; std::string line;
std::ifstream file(file_path); (void)filename;
// El fichero se puede abrir
if (file.good()) {
// Procesa el fichero linea a linea
// std::cout << "Reading file " << filename.c_str() << std::endl;
while (std::getline(file, line)) { while (std::getline(file, line)) {
if (line == "[item]") { if (line == "[item]") {
item_t item; item_t item;
@@ -123,54 +113,54 @@ bool Menu::load(std::string file_path) {
item.line = false; item.line = false;
do { do {
// Lee la siguiente linea
std::getline(file, line); std::getline(file, line);
// Encuentra la posición del caracter '='
int pos = line.find("="); int pos = line.find("=");
// Procesa las dos subcadenas
if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length()))) { if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length()))) {
// std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
success = false; success = false;
} }
} while (line != "[/item]"); } while (line != "[/item]");
addItem(item); addItem(item);
} } else {
// En caso contrario se parsea el fichero para buscar las variables y los valores
else {
// Encuentra la posición del caracter '='
int pos = line.find("="); int pos = line.find("=");
// Procesa las dos subcadenas
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length()))) { if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length()))) {
// std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
success = false; success = false;
} }
// Crea el objeto text tan pronto como se pueda. Necesario para añadir items // Crea el objeto text tan pronto como se pueda. Necesario para añadir items.
// Carga via ResourceHelper para que funcione tanto con pack como con filesystem.
if (font_png != "" && font_txt != "" && !textAllocated) { if (font_png != "" && font_txt != "" && !textAllocated) {
text = new Text(asset->get(font_png), asset->get(font_txt), renderer); auto pngBytes = ResourceHelper::loadFile(asset->get(font_png));
auto txtBytes = ResourceHelper::loadFile(asset->get(font_txt));
text = new Text(pngBytes, txtBytes, renderer);
textAllocated = true; textAllocated = true;
} }
} }
} }
// Cierra el fichero
// std::cout << "Closing file " << filename.c_str() << std::endl;
file.close();
}
// El fichero no se puede abrir
else {
// std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
success = false;
}
return success; return success;
} }
// Carga la configuración del menu desde un archivo de texto
bool Menu::load(std::string file_path) {
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
std::ifstream file(file_path);
if (!file.good()) {
return false;
}
return parseFromStream(file, filename);
}
// Carga el menu desde bytes en memoria
bool Menu::loadFromBytes(const std::vector<uint8_t> &bytes, const std::string &nameForLogs) {
if (bytes.empty()) return false;
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
std::stringstream ss(content);
bool ok = parseFromStream(ss, nameForLogs);
setSelectorItemColors();
reset();
return ok;
}
// Asigna variables a partir de dos cadenas // Asigna variables a partir de dos cadenas
bool Menu::setItem(item_t *item, std::string var, std::string value) { bool Menu::setItem(item_t *item, std::string var, std::string value) {
// Indicador de éxito en la asignación // Indicador de éxito en la asignación
@@ -228,15 +218,18 @@ bool Menu::setVars(std::string var, std::string value) {
} }
else if (var == "sound_cancel") { else if (var == "sound_cancel") {
soundCancel = JA_LoadSound(asset->get(value).c_str()); auto bytes = ResourceHelper::loadFile(asset->get(value));
if (!bytes.empty()) soundCancel = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
} }
else if (var == "sound_accept") { else if (var == "sound_accept") {
soundAccept = JA_LoadSound(asset->get(value).c_str()); auto bytes = ResourceHelper::loadFile(asset->get(value));
if (!bytes.empty()) soundAccept = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
} }
else if (var == "sound_move") { else if (var == "sound_move") {
soundMove = JA_LoadSound(asset->get(value).c_str()); auto bytes = ResourceHelper::loadFile(asset->get(value));
if (!bytes.empty()) soundMove = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
} }
else if (var == "name") { else if (var == "name") {

View File

@@ -2,6 +2,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <cstdint>
#include <string> // for string, basic_string #include <string> // for string, basic_string
#include <vector> // for vector #include <vector> // for vector
@@ -101,6 +102,9 @@ class Menu {
// Carga la configuración del menu desde un archivo de texto // Carga la configuración del menu desde un archivo de texto
bool load(std::string file_path); bool load(std::string file_path);
// Parser compartido (recibe cualquier istream)
bool parseFromStream(std::istream &file, const std::string &filename);
// Asigna variables a partir de dos cadenas // Asigna variables a partir de dos cadenas
bool setVars(std::string var, std::string value); bool setVars(std::string var, std::string value);
@@ -147,6 +151,9 @@ class Menu {
// Destructor // Destructor
~Menu(); ~Menu();
// Carga el menu desde bytes en memoria
bool loadFromBytes(const std::vector<uint8_t> &bytes, const std::string &nameForLogs = "");
// Carga los ficheros de audio // Carga los ficheros de audio
void loadAudioFile(std::string file, int sound); void loadAudioFile(std::string file, int sound);

35
source/mouse.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include "mouse.hpp"
namespace Mouse {
Uint32 cursorHideTime = 3000; // Tiempo en milisegundos para ocultar el cursor por inactividad
Uint32 lastMouseMoveTime = 0; // Última vez que el ratón se movió
bool cursorVisible = true; // Estado del cursor
void handleEvent(const SDL_Event &event, bool fullscreen) {
if (event.type == SDL_EVENT_MOUSE_MOTION) {
lastMouseMoveTime = SDL_GetTicks();
if (!cursorVisible && !fullscreen) {
SDL_ShowCursor();
cursorVisible = true;
}
}
}
void updateCursorVisibility(bool fullscreen) {
// En pantalla completa el cursor siempre está oculto
if (fullscreen) {
if (cursorVisible) {
SDL_HideCursor();
cursorVisible = false;
}
return;
}
// En modo ventana, lo oculta tras el periodo de inactividad
const Uint32 currentTime = SDL_GetTicks();
if (cursorVisible && (currentTime - lastMouseMoveTime > cursorHideTime)) {
SDL_HideCursor();
cursorVisible = false;
}
}
} // namespace Mouse

18
source/mouse.hpp Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#include <SDL3/SDL.h>
namespace Mouse {
extern Uint32 cursorHideTime; // Tiempo en milisegundos para ocultar el cursor por inactividad
extern Uint32 lastMouseMoveTime; // Última vez que el ratón se movió
extern bool cursorVisible; // Estado del cursor
// Procesa un evento de ratón. En pantalla completa ignora el movimiento
// para no volver a mostrar el cursor.
void handleEvent(const SDL_Event &event, bool fullscreen);
// Actualiza la visibilidad del cursor. En modo ventana lo oculta
// después de cursorHideTime ms sin movimiento. En pantalla completa
// lo mantiene siempre oculto.
void updateCursorVisibility(bool fullscreen);
} // namespace Mouse

218
source/resource.cpp Normal file
View File

@@ -0,0 +1,218 @@
#include "resource.h"
#include <algorithm>
#include <filesystem>
#include <iostream>
#include <sstream>
#include "asset.h"
#include "jail_audio.hpp"
#include "menu.h"
#include "resource_helper.h"
#include "text.h"
#include "texture.h"
Resource *Resource::instance_ = nullptr;
static std::string basename(const std::string &path) {
return path.substr(path.find_last_of("\\/") + 1);
}
static std::string stem(const std::string &path) {
std::string b = basename(path);
size_t dot = b.find_last_of('.');
if (dot == std::string::npos) return b;
return b.substr(0, dot);
}
void Resource::init(SDL_Renderer *renderer, Asset *asset, Input *input) {
if (instance_ == nullptr) {
instance_ = new Resource(renderer, asset, input);
instance_->preloadAll();
}
}
void Resource::destroy() {
delete instance_;
instance_ = nullptr;
}
Resource *Resource::get() {
return instance_;
}
Resource::Resource(SDL_Renderer *renderer, Asset *asset, Input *input)
: renderer_(renderer),
asset_(asset),
input_(input) {}
Resource::~Resource() {
for (auto &[name, m] : menus_) delete m;
menus_.clear();
for (auto &[name, t] : texts_) delete t;
texts_.clear();
for (auto &[name, t] : textures_) delete t;
textures_.clear();
for (auto &[name, s] : sounds_) JA_DeleteSound(s);
sounds_.clear();
for (auto &[name, m] : musics_) JA_DeleteMusic(m);
musics_.clear();
}
void Resource::preloadAll() {
const auto &items = asset_->getAll();
// Pass 1: texturas, sonidos, músicas, animaciones (raw lines), demo, lenguajes
for (const auto &it : items) {
if (!ResourceHelper::shouldUseResourcePack(it.file) && it.type != t_lang) {
// Ficheros absolutos (config.txt, score.bin, systemFolder) — no se precargan
continue;
}
auto bytes = ResourceHelper::loadFile(it.file);
if (bytes.empty()) continue;
const std::string bname = basename(it.file);
switch (it.type) {
case t_bitmap: {
auto *tex = new Texture(renderer_, bytes);
textures_[bname] = tex;
break;
}
case t_sound: {
JA_Sound_t *s = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
if (s) sounds_[bname] = s;
break;
}
case t_music: {
JA_Music_t *m = JA_LoadMusic(bytes.data(), (Uint32)bytes.size());
if (m) musics_[bname] = m;
break;
}
case t_data: {
if (bname.size() >= 4 && bname.substr(bname.size() - 4) == ".ani") {
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
std::stringstream ss(content);
std::vector<std::string> lines;
std::string line;
while (std::getline(ss, line)) {
lines.push_back(line);
}
animationLines_[bname] = std::move(lines);
} else if (bname == "demo.bin") {
demoBytes_ = bytes;
} else if (bname.size() >= 4 && bname.substr(bname.size() - 4) == ".men") {
// Menús: se construyen en pass 2 porque dependen de textos y sonidos
}
break;
}
case t_font:
// Fonts: se emparejan en pass 2
break;
case t_lang:
// Lenguaje: lo sigue leyendo la clase Lang a través de ResourceHelper
break;
default:
break;
}
}
// Pass 2: Text (fuentes emparejadas png+txt) y Menus (dependen de Text+sonidos)
// Fuentes: construimos un Text por cada par basename.png + basename.txt
// Acumulamos los bytes encontrados por stem (basename sin ext.)
std::unordered_map<std::string, std::vector<uint8_t>> fontPngs;
std::unordered_map<std::string, std::vector<uint8_t>> fontTxts;
for (const auto &it : items) {
if (it.type != t_font) continue;
auto bytes = ResourceHelper::loadFile(it.file);
if (bytes.empty()) continue;
const std::string s = stem(it.file);
const std::string bname = basename(it.file);
if (bname.size() >= 4 && bname.substr(bname.size() - 4) == ".png") {
fontPngs[s] = std::move(bytes);
} else if (bname.size() >= 4 && bname.substr(bname.size() - 4) == ".txt") {
fontTxts[s] = std::move(bytes);
}
}
for (const auto &[s, png] : fontPngs) {
auto itTxt = fontTxts.find(s);
if (itTxt == fontTxts.end()) continue;
Text *t = new Text(png, itTxt->second, renderer_);
texts_[s] = t;
}
// Menus: usan aún Menu::loadFromBytes que internamente llama a asset->get() y
// Text/JA_LoadSound por path. Funciona en modo fallback; en pack estricto
// requiere que Menu se adapte a cargar desde ResourceHelper. Por ahora
// lo dejamos así y será una migración del paso 7.
for (const auto &it : items) {
if (it.type != t_data) continue;
const std::string bname = basename(it.file);
if (bname.size() < 4 || bname.substr(bname.size() - 4) != ".men") continue;
auto bytes = ResourceHelper::loadFile(it.file);
if (bytes.empty()) continue;
Menu *m = new Menu(renderer_, asset_, input_, "");
m->loadFromBytes(bytes, bname);
const std::string s = stem(it.file);
menus_[s] = m;
}
}
Texture *Resource::getTexture(const std::string &name) {
auto it = textures_.find(name);
if (it == textures_.end()) {
std::cerr << "Resource::getTexture: missing " << name << '\n';
return nullptr;
}
return it->second;
}
JA_Sound_t *Resource::getSound(const std::string &name) {
auto it = sounds_.find(name);
if (it == sounds_.end()) {
std::cerr << "Resource::getSound: missing " << name << '\n';
return nullptr;
}
return it->second;
}
JA_Music_t *Resource::getMusic(const std::string &name) {
auto it = musics_.find(name);
if (it == musics_.end()) {
std::cerr << "Resource::getMusic: missing " << name << '\n';
return nullptr;
}
return it->second;
}
std::vector<std::string> &Resource::getAnimationLines(const std::string &name) {
auto it = animationLines_.find(name);
if (it == animationLines_.end()) {
static std::vector<std::string> empty;
std::cerr << "Resource::getAnimationLines: missing " << name << '\n';
return empty;
}
return it->second;
}
Text *Resource::getText(const std::string &name) {
auto it = texts_.find(name);
if (it == texts_.end()) {
std::cerr << "Resource::getText: missing " << name << '\n';
return nullptr;
}
return it->second;
}
Menu *Resource::getMenu(const std::string &name) {
auto it = menus_.find(name);
if (it == menus_.end()) {
std::cerr << "Resource::getMenu: missing " << name << '\n';
return nullptr;
}
return it->second;
}

53
source/resource.h Normal file
View File

@@ -0,0 +1,53 @@
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
class Asset;
class Input;
class Menu;
class Text;
class Texture;
struct JA_Music_t;
struct JA_Sound_t;
// Precarga y posee todos los recursos del juego durante toda la vida de la app.
// Singleton inicializado desde Director; las escenas consultan handles via get*().
class Resource {
public:
static void init(SDL_Renderer *renderer, Asset *asset, Input *input);
static void destroy();
static Resource *get();
Texture *getTexture(const std::string &name);
JA_Sound_t *getSound(const std::string &name);
JA_Music_t *getMusic(const std::string &name);
std::vector<std::string> &getAnimationLines(const std::string &name);
Text *getText(const std::string &name); // name sin extensión: "smb2", "nokia2", ...
Menu *getMenu(const std::string &name); // name sin extensión: "title", "options", ...
const std::vector<uint8_t> &getDemoBytes() const { return demoBytes_; }
private:
Resource(SDL_Renderer *renderer, Asset *asset, Input *input);
~Resource();
void preloadAll();
SDL_Renderer *renderer_;
Asset *asset_;
Input *input_;
std::unordered_map<std::string, Texture *> textures_;
std::unordered_map<std::string, JA_Sound_t *> sounds_;
std::unordered_map<std::string, JA_Music_t *> musics_;
std::unordered_map<std::string, std::vector<std::string>> animationLines_;
std::unordered_map<std::string, Text *> texts_;
std::unordered_map<std::string, Menu *> menus_;
std::vector<uint8_t> demoBytes_;
static Resource *instance_;
};

View File

@@ -0,0 +1,77 @@
#include "resource_helper.h"
#include <algorithm>
#include <cstddef>
#include <fstream>
#include <iostream>
#include "resource_loader.h"
namespace ResourceHelper {
static bool resource_system_initialized = false;
bool initializeResourceSystem(const std::string& pack_file, bool enable_fallback) {
auto& loader = ResourceLoader::getInstance();
bool ok = loader.initialize(pack_file, enable_fallback);
resource_system_initialized = ok;
if (ok && loader.getLoadedResourceCount() > 0) {
std::cout << "Resource system initialized with pack: " << pack_file << '\n';
} else if (ok) {
std::cout << "Resource system using fallback mode (filesystem only)" << '\n';
}
return ok;
}
void shutdownResourceSystem() {
if (resource_system_initialized) {
ResourceLoader::getInstance().shutdown();
resource_system_initialized = false;
}
}
std::vector<uint8_t> loadFile(const std::string& filepath) {
if (resource_system_initialized && shouldUseResourcePack(filepath)) {
auto& loader = ResourceLoader::getInstance();
std::string pack_path = getPackPath(filepath);
auto data = loader.loadResource(pack_path);
if (!data.empty()) {
return data;
}
}
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file) {
return {};
}
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> data(file_size);
if (!file.read(reinterpret_cast<char*>(data.data()), file_size)) {
return {};
}
return data;
}
bool shouldUseResourcePack(const std::string& filepath) {
// Solo entran al pack los ficheros dentro de data/
return filepath.find("data/") != std::string::npos;
}
std::string getPackPath(const std::string& asset_path) {
std::string pack_path = asset_path;
std::replace(pack_path.begin(), pack_path.end(), '\\', '/');
// Toma la última aparición de "data/" como prefijo a quitar
size_t last_data = pack_path.rfind("data/");
if (last_data != std::string::npos) {
pack_path = pack_path.substr(last_data + 5);
}
return pack_path;
}
}

15
source/resource_helper.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
namespace ResourceHelper {
bool initializeResourceSystem(const std::string& pack_file = "resources.pack", bool enable_fallback = true);
void shutdownResourceSystem();
std::vector<uint8_t> loadFile(const std::string& filepath);
bool shouldUseResourcePack(const std::string& filepath);
std::string getPackPath(const std::string& asset_path);
}

133
source/resource_loader.cpp Normal file
View File

@@ -0,0 +1,133 @@
#include "resource_loader.h"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include "resource_pack.h"
std::unique_ptr<ResourceLoader> ResourceLoader::instance = nullptr;
ResourceLoader::ResourceLoader()
: resource_pack_(nullptr),
fallback_to_files_(true) {}
ResourceLoader& ResourceLoader::getInstance() {
if (!instance) {
instance = std::unique_ptr<ResourceLoader>(new ResourceLoader());
}
return *instance;
}
ResourceLoader::~ResourceLoader() {
shutdown();
}
bool ResourceLoader::initialize(const std::string& pack_file, bool enable_fallback) {
shutdown();
fallback_to_files_ = enable_fallback;
pack_path_ = pack_file;
if (std::filesystem::exists(pack_file)) {
resource_pack_ = new ResourcePack();
if (resource_pack_->loadPack(pack_file)) {
return true;
}
delete resource_pack_;
resource_pack_ = nullptr;
std::cerr << "Failed to load resource pack: " << pack_file << '\n';
}
if (fallback_to_files_) {
return true;
}
std::cerr << "Resource pack not found and fallback disabled: " << pack_file << '\n';
return false;
}
void ResourceLoader::shutdown() {
if (resource_pack_ != nullptr) {
delete resource_pack_;
resource_pack_ = nullptr;
}
}
std::vector<uint8_t> ResourceLoader::loadResource(const std::string& filename) {
if ((resource_pack_ != nullptr) && resource_pack_->hasResource(filename)) {
return resource_pack_->getResource(filename);
}
if (fallback_to_files_) {
return loadFromFile(filename);
}
std::cerr << "Resource not found: " << filename << '\n';
return {};
}
bool ResourceLoader::resourceExists(const std::string& filename) {
if ((resource_pack_ != nullptr) && resource_pack_->hasResource(filename)) {
return true;
}
if (fallback_to_files_) {
std::string full_path = getDataPath(filename);
return std::filesystem::exists(full_path);
}
return false;
}
std::vector<uint8_t> ResourceLoader::loadFromFile(const std::string& filename) {
std::string full_path = getDataPath(filename);
std::ifstream file(full_path, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "Error: Could not open file: " << full_path << '\n';
return {};
}
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> data(file_size);
if (!file.read(reinterpret_cast<char*>(data.data()), file_size)) {
std::cerr << "Error: Could not read file: " << full_path << '\n';
return {};
}
return data;
}
std::string ResourceLoader::getDataPath(const std::string& filename) {
return "data/" + filename;
}
size_t ResourceLoader::getLoadedResourceCount() const {
if (resource_pack_ != nullptr) {
return resource_pack_->getResourceCount();
}
return 0;
}
std::vector<std::string> ResourceLoader::getAvailableResources() const {
if (resource_pack_ != nullptr) {
return resource_pack_->getResourceList();
}
std::vector<std::string> result;
if (fallback_to_files_ && std::filesystem::exists("data")) {
for (const auto& entry : std::filesystem::recursive_directory_iterator("data")) {
if (entry.is_regular_file()) {
std::string filename = std::filesystem::relative(entry.path(), "data").string();
std::replace(filename.begin(), filename.end(), '\\', '/');
result.push_back(filename);
}
}
}
return result;
}

39
source/resource_loader.h Normal file
View File

@@ -0,0 +1,39 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
class ResourcePack;
class ResourceLoader {
private:
static std::unique_ptr<ResourceLoader> instance;
ResourcePack* resource_pack_;
std::string pack_path_;
bool fallback_to_files_;
ResourceLoader();
public:
static ResourceLoader& getInstance();
~ResourceLoader();
bool initialize(const std::string& pack_file, bool enable_fallback = true);
void shutdown();
std::vector<uint8_t> loadResource(const std::string& filename);
bool resourceExists(const std::string& filename);
void setFallbackToFiles(bool enable) { fallback_to_files_ = enable; }
bool getFallbackToFiles() const { return fallback_to_files_; }
size_t getLoadedResourceCount() const;
std::vector<std::string> getAvailableResources() const;
private:
static std::vector<uint8_t> loadFromFile(const std::string& filename);
static std::string getDataPath(const std::string& filename);
};

223
source/resource_pack.cpp Normal file
View File

@@ -0,0 +1,223 @@
#include "resource_pack.h"
#include <algorithm>
#include <array>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <utility>
const std::string ResourcePack::DEFAULT_ENCRYPT_KEY = "CCRS_RESOURCES__2026";
ResourcePack::ResourcePack()
: loaded_(false) {}
ResourcePack::~ResourcePack() {
clear();
}
uint32_t ResourcePack::calculateChecksum(const std::vector<uint8_t>& data) {
uint32_t checksum = 0x12345678;
for (unsigned char i : data) {
checksum = ((checksum << 5) + checksum) + i;
}
return checksum;
}
void ResourcePack::encryptData(std::vector<uint8_t>& data, const std::string& key) {
if (key.empty()) return;
for (size_t i = 0; i < data.size(); ++i) {
data[i] ^= key[i % key.length()];
}
}
void ResourcePack::decryptData(std::vector<uint8_t>& data, const std::string& key) {
encryptData(data, key);
}
bool ResourcePack::loadPack(const std::string& pack_file) {
std::ifstream file(pack_file, std::ios::binary);
if (!file) {
std::cerr << "Error: Could not open pack file: " << pack_file << '\n';
return false;
}
std::array<char, 4> header;
file.read(header.data(), 4);
if (std::string(header.data(), 4) != "CCRS") {
std::cerr << "Error: Invalid pack file format" << '\n';
return false;
}
uint32_t version;
file.read(reinterpret_cast<char*>(&version), sizeof(version));
if (version != 1) {
std::cerr << "Error: Unsupported pack version: " << version << '\n';
return false;
}
uint32_t resource_count;
file.read(reinterpret_cast<char*>(&resource_count), sizeof(resource_count));
resources_.clear();
resources_.reserve(resource_count);
for (uint32_t i = 0; i < resource_count; ++i) {
uint32_t filename_length;
file.read(reinterpret_cast<char*>(&filename_length), sizeof(filename_length));
std::string filename(filename_length, '\0');
file.read(filename.data(), filename_length);
ResourceEntry entry;
entry.filename = filename;
file.read(reinterpret_cast<char*>(&entry.offset), sizeof(entry.offset));
file.read(reinterpret_cast<char*>(&entry.size), sizeof(entry.size));
file.read(reinterpret_cast<char*>(&entry.checksum), sizeof(entry.checksum));
resources_[filename] = entry;
}
uint64_t data_size;
file.read(reinterpret_cast<char*>(&data_size), sizeof(data_size));
data_.resize(data_size);
file.read(reinterpret_cast<char*>(data_.data()), data_size);
decryptData(data_, DEFAULT_ENCRYPT_KEY);
loaded_ = true;
return true;
}
bool ResourcePack::savePack(const std::string& pack_file) {
std::ofstream file(pack_file, std::ios::binary);
if (!file) {
std::cerr << "Error: Could not create pack file: " << pack_file << '\n';
return false;
}
file.write("CCRS", 4);
uint32_t version = 1;
file.write(reinterpret_cast<const char*>(&version), sizeof(version));
uint32_t resource_count = static_cast<uint32_t>(resources_.size());
file.write(reinterpret_cast<const char*>(&resource_count), sizeof(resource_count));
for (const auto& [filename, entry] : resources_) {
uint32_t filename_length = static_cast<uint32_t>(filename.length());
file.write(reinterpret_cast<const char*>(&filename_length), sizeof(filename_length));
file.write(filename.c_str(), filename_length);
file.write(reinterpret_cast<const char*>(&entry.offset), sizeof(entry.offset));
file.write(reinterpret_cast<const char*>(&entry.size), sizeof(entry.size));
file.write(reinterpret_cast<const char*>(&entry.checksum), sizeof(entry.checksum));
}
std::vector<uint8_t> encrypted_data = data_;
encryptData(encrypted_data, DEFAULT_ENCRYPT_KEY);
uint64_t data_size = encrypted_data.size();
file.write(reinterpret_cast<const char*>(&data_size), sizeof(data_size));
file.write(reinterpret_cast<const char*>(encrypted_data.data()), data_size);
return true;
}
bool ResourcePack::addFile(const std::string& filename, const std::string& filepath) {
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "Error: Could not open file: " << filepath << '\n';
return false;
}
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> file_data(file_size);
if (!file.read(reinterpret_cast<char*>(file_data.data()), file_size)) {
std::cerr << "Error: Could not read file: " << filepath << '\n';
return false;
}
ResourceEntry entry;
entry.filename = filename;
entry.offset = data_.size();
entry.size = file_data.size();
entry.checksum = calculateChecksum(file_data);
data_.insert(data_.end(), file_data.begin(), file_data.end());
resources_[filename] = entry;
return true;
}
bool ResourcePack::addDirectory(const std::string& directory) {
if (!std::filesystem::exists(directory)) {
std::cerr << "Error: Directory does not exist: " << directory << '\n';
return false;
}
for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) {
if (entry.is_regular_file()) {
std::string filepath = entry.path().string();
std::string filename = std::filesystem::relative(entry.path(), directory).string();
std::replace(filename.begin(), filename.end(), '\\', '/');
if (!addFile(filename, filepath)) {
return false;
}
}
}
return true;
}
std::vector<uint8_t> ResourcePack::getResource(const std::string& filename) {
auto it = resources_.find(filename);
if (it == resources_.end()) {
std::cerr << "Error: Resource not found: " << filename << '\n';
return {};
}
const ResourceEntry& entry = it->second;
if (entry.offset + entry.size > data_.size()) {
std::cerr << "Error: Invalid resource data: " << filename << '\n';
return {};
}
std::vector<uint8_t> result(data_.begin() + entry.offset,
data_.begin() + entry.offset + entry.size);
uint32_t checksum = calculateChecksum(result);
if (checksum != entry.checksum) {
std::cerr << "Warning: Checksum mismatch for resource: " << filename << '\n';
}
return result;
}
bool ResourcePack::hasResource(const std::string& filename) const {
return resources_.find(filename) != resources_.end();
}
void ResourcePack::clear() {
resources_.clear();
data_.clear();
loaded_ = false;
}
size_t ResourcePack::getResourceCount() const {
return resources_.size();
}
std::vector<std::string> ResourcePack::getResourceList() const {
std::vector<std::string> result;
result.reserve(resources_.size());
for (const auto& [filename, entry] : resources_) {
result.push_back(filename);
}
return result;
}

44
source/resource_pack.h Normal file
View File

@@ -0,0 +1,44 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
struct ResourceEntry {
std::string filename;
uint64_t offset;
uint64_t size;
uint32_t checksum;
};
class ResourcePack {
private:
std::unordered_map<std::string, ResourceEntry> resources_;
std::vector<uint8_t> data_;
bool loaded_;
static uint32_t calculateChecksum(const std::vector<uint8_t>& data);
static void encryptData(std::vector<uint8_t>& data, const std::string& key);
static void decryptData(std::vector<uint8_t>& data, const std::string& key);
public:
ResourcePack();
~ResourcePack();
bool loadPack(const std::string& pack_file);
bool savePack(const std::string& pack_file);
bool addFile(const std::string& filename, const std::string& filepath);
bool addDirectory(const std::string& directory);
std::vector<uint8_t> getResource(const std::string& filename);
bool hasResource(const std::string& filename) const;
void clear();
size_t getResourceCount() const;
std::vector<std::string> getResourceList() const;
static const std::string DEFAULT_ENCRYPT_KEY;
};

View File

@@ -5,7 +5,53 @@
#include <algorithm> // for max, min #include <algorithm> // for max, min
#include <iostream> // for basic_ostream, operator<<, cout, endl #include <iostream> // for basic_ostream, operator<<, cout, endl
#include <string> // for basic_string, char_traits, string #include <string> // for basic_string, char_traits, string
class Asset;
#include "asset.h" // for Asset
#include "mouse.hpp" // for Mouse::cursorVisible, Mouse::lastMouseMoveTime
#include "resource.h"
#include "text.h" // for Text, TXT_CENTER, TXT_COLOR, TXT_STROKE
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
// --- Fix per a fullscreen/resize en Emscripten ---
//
// SDL3 + Emscripten no emet de forma fiable SDL_EVENT_WINDOW_LEAVE_FULLSCREEN
// (libsdl-org/SDL#13300) ni SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED /
// SDL_EVENT_DISPLAY_ORIENTATION (libsdl-org/SDL#11389). Quan l'usuari ix de
// fullscreen amb Esc o rota el mòbil, el canvas HTML torna al tamany correcte
// però l'estat intern de SDL creu que segueix en fullscreen amb la resolució
// anterior i el viewport queda desencuadrat.
//
// Solució: registrar callbacks natius d'Emscripten, diferir la feina un tick
// del event loop (el canvas encara no està estable en el moment del callback)
// i cridar setVideoMode() amb el flag de fullscreen actualitzat. La crida
// interna a SDL_SetWindowFullscreen(false) és la peça que realment fa eixir
// SDL del seu estat intern de fullscreen — sense això res més funciona.
namespace {
Screen *g_screen_instance = nullptr;
void deferredCanvasResize(void * /*userData*/) {
if (g_screen_instance) {
g_screen_instance->handleCanvasResized();
}
}
EM_BOOL onEmFullscreenChange(int /*eventType*/, const EmscriptenFullscreenChangeEvent *event, void * /*userData*/) {
if (g_screen_instance && event) {
g_screen_instance->syncFullscreenFlagFromBrowser(event->isFullscreen != 0);
}
emscripten_async_call(deferredCanvasResize, nullptr, 0);
return EM_FALSE;
}
EM_BOOL onEmOrientationChange(int /*eventType*/, const EmscriptenOrientationChangeEvent * /*event*/, void * /*userData*/) {
emscripten_async_call(deferredCanvasResize, nullptr, 0);
return EM_FALSE;
}
} // namespace
#endif // __EMSCRIPTEN__
// Constructor // Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options) { Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options) {
@@ -19,11 +65,6 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
gameCanvasHeight = options->gameHeight; gameCanvasHeight = options->gameHeight;
borderWidth = options->borderWidth * 2; borderWidth = options->borderWidth * 2;
borderHeight = options->borderHeight * 2; borderHeight = options->borderHeight * 2;
notificationLogicalWidth = gameCanvasWidth;
notificationLogicalHeight = gameCanvasHeight;
iniFade();
iniSpectrumFade();
// Define el color del borde para el modo de pantalla completa // Define el color del borde para el modo de pantalla completa
borderColor = {0x00, 0x00, 0x00}; borderColor = {0x00, 0x00, 0x00};
@@ -35,19 +76,28 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
} }
if (gameCanvas == nullptr) { if (gameCanvas == nullptr) {
if (options->console) { if (options->console) {
std::cout << "TitleSurface could not be created!\nSDL Error: " << SDL_GetError() << std::endl; std::cout << "gameCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
} }
} }
// Establece el modo de video // Establece el modo de video
setVideoMode(options->videoMode); setVideoMode(options->videoMode != 0);
// Inicializa variables // Inicializa el sistema de notificaciones (Text compartido de Resource)
notifyActive = false; notificationText = Resource::get()->getText("8bithud");
notificationMessage = "";
notificationTextColor = {0xFF, 0xFF, 0xFF};
notificationOutlineColor = {0x00, 0x00, 0x00};
notificationEndTime = 0;
notificationY = 2;
// Registra callbacks natius d'Emscripten per a fullscreen/orientation
registerEmscriptenEventCallbacks();
} }
// Destructor // Destructor
Screen::~Screen() { Screen::~Screen() {
// notificationText es propiedad de Resource — no liberar.
SDL_DestroyTexture(gameCanvas); SDL_DestroyTexture(gameCanvas);
} }
@@ -64,6 +114,10 @@ void Screen::start() {
// Vuelca el contenido del renderizador en pantalla // Vuelca el contenido del renderizador en pantalla
void Screen::blit() { void Screen::blit() {
// Dibuja la notificación activa sobre el gameCanvas antes de presentar
SDL_SetRenderTarget(renderer, gameCanvas);
renderNotification();
// Vuelve a dejar el renderizador en modo normal // Vuelve a dejar el renderizador en modo normal
SDL_SetRenderTarget(renderer, nullptr); SDL_SetRenderTarget(renderer, nullptr);
@@ -79,48 +133,133 @@ void Screen::blit() {
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
// ============================================================================
// Video y ventana
// ============================================================================
// Establece el modo de video // Establece el modo de video
void Screen::setVideoMode(int videoMode) { void Screen::setVideoMode(bool fullscreen) {
// Aplica el modo de video applyFullscreen(fullscreen);
SDL_SetWindowFullscreen(window, videoMode != 0); if (fullscreen) {
applyFullscreenLayout();
} else {
applyWindowedLayout();
}
applyLogicalPresentation(fullscreen);
}
// Si está activo el modo ventana quita el borde // Cambia entre pantalla completa y ventana
if (videoMode == 0) { void Screen::toggleVideoMode() {
// Muestra el puntero setVideoMode(options->videoMode == 0);
}
// Reduce el zoom de la ventana
auto Screen::decWindowZoom() -> bool {
if (options->videoMode != 0) { return false; }
const int PREV = options->windowSize;
options->windowSize = std::max(options->windowSize - 1, WINDOW_ZOOM_MIN);
if (options->windowSize == PREV) { return false; }
setVideoMode(false);
return true;
}
// Aumenta el zoom de la ventana
auto Screen::incWindowZoom() -> bool {
if (options->videoMode != 0) { return false; }
const int PREV = options->windowSize;
options->windowSize = std::min(options->windowSize + 1, WINDOW_ZOOM_MAX);
if (options->windowSize == PREV) { return false; }
setVideoMode(false);
return true;
}
// Establece el zoom de la ventana directamente
auto Screen::setWindowZoom(int zoom) -> bool {
if (options->videoMode != 0) { return false; }
if (zoom < WINDOW_ZOOM_MIN || zoom > WINDOW_ZOOM_MAX) { return false; }
if (zoom == options->windowSize) { return false; }
options->windowSize = zoom;
setVideoMode(false);
return true;
}
// Establece el escalado entero
void Screen::setIntegerScale(bool enabled) {
if (options->integerScale == enabled) { return; }
options->integerScale = enabled;
setVideoMode(options->videoMode != 0);
}
// Alterna el escalado entero
void Screen::toggleIntegerScale() {
setIntegerScale(!options->integerScale);
}
// Establece el V-Sync
void Screen::setVSync(bool enabled) {
options->vSync = enabled;
SDL_SetRenderVSync(renderer, enabled ? 1 : SDL_RENDERER_VSYNC_DISABLED);
}
// Alterna el V-Sync
void Screen::toggleVSync() {
setVSync(!options->vSync);
}
// Cambia el color del borde
void Screen::setBorderColor(color_t color) {
borderColor = color;
}
// ============================================================================
// Helpers privados de setVideoMode
// ============================================================================
// SDL_SetWindowFullscreen + visibilidad del cursor
void Screen::applyFullscreen(bool fullscreen) {
SDL_SetWindowFullscreen(window, fullscreen);
if (fullscreen) {
SDL_HideCursor();
Mouse::cursorVisible = false;
} else {
SDL_ShowCursor(); SDL_ShowCursor();
Mouse::cursorVisible = true;
Mouse::lastMouseMoveTime = SDL_GetTicks();
}
}
// Esconde la ventana // Calcula windowWidth/Height/dest para el modo ventana y aplica SDL_SetWindowSize
// SDL_HideWindow(window); void Screen::applyWindowedLayout() {
if (options->borderEnabled) { if (options->borderEnabled) {
windowWidth = gameCanvasWidth + borderWidth; windowWidth = gameCanvasWidth + borderWidth;
windowHeight = gameCanvasHeight + borderHeight; windowHeight = gameCanvasHeight + borderHeight;
dest = {0 + (borderWidth / 2), 0 + (borderHeight / 2), gameCanvasWidth, gameCanvasHeight}; dest = {0 + (borderWidth / 2), 0 + (borderHeight / 2), gameCanvasWidth, gameCanvasHeight};
} } else {
else {
windowWidth = gameCanvasWidth; windowWidth = gameCanvasWidth;
windowHeight = gameCanvasHeight; windowHeight = gameCanvasHeight;
dest = {0, 0, gameCanvasWidth, gameCanvasHeight}; dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
} }
#ifdef __EMSCRIPTEN__
windowWidth *= WASM_RENDER_SCALE;
windowHeight *= WASM_RENDER_SCALE;
dest.w *= WASM_RENDER_SCALE;
dest.h *= WASM_RENDER_SCALE;
#endif
// Modifica el tamaño de la ventana // Modifica el tamaño de la ventana
SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize); SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize);
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
// Muestra la ventana
// SDL_ShowWindow(window);
} }
// Si está activo el modo de pantalla completa añade el borde // Obtiene el tamaño de la ventana en fullscreen y calcula el rect del juego
else if (videoMode == SDL_WINDOW_FULLSCREEN) { void Screen::applyFullscreenLayout() {
// Oculta el puntero
SDL_HideCursor();
// Obten el alto y el ancho de la ventana
SDL_GetWindowSize(window, &windowWidth, &windowHeight); SDL_GetWindowSize(window, &windowWidth, &windowHeight);
computeFullscreenGameRect();
}
// Aplica el escalado al rectangulo donde se pinta la textura del juego // Calcula el rectángulo dest para fullscreen: integerScale / keepAspect / stretched
void Screen::computeFullscreenGameRect() {
if (options->integerScale) { if (options->integerScale) {
// Calcula el tamaño de la escala máxima // Calcula el tamaño de la escala máxima
int scale = 0; int scale = 0;
@@ -152,182 +291,79 @@ void Screen::setVideoMode(int videoMode) {
} }
} }
// Modifica el tamaño del renderizador // Aplica la logical presentation y persiste el estado en options
void Screen::applyLogicalPresentation(bool fullscreen) {
SDL_SetRenderLogicalPresentation(renderer, windowWidth, windowHeight, SDL_LOGICAL_PRESENTATION_LETTERBOX); SDL_SetRenderLogicalPresentation(renderer, windowWidth, windowHeight, SDL_LOGICAL_PRESENTATION_LETTERBOX);
// Actualiza las opciones // Actualiza las opciones
options->videoMode = videoMode; options->videoMode = fullscreen ? SDL_WINDOW_FULLSCREEN : 0;
options->screen.windowWidth = windowWidth; options->screen.windowWidth = windowWidth;
options->screen.windowHeight = windowHeight; options->screen.windowHeight = windowHeight;
} }
// Camibia entre pantalla completa y ventana // ============================================================================
void Screen::switchVideoMode() { // Notificaciones
options->videoMode = (options->videoMode == 0) ? SDL_WINDOW_FULLSCREEN : 0; // ============================================================================
setVideoMode(options->videoMode);
// Muestra una notificación en la línea superior durante durationMs
void Screen::notify(const std::string &text, color_t textColor, color_t outlineColor, Uint32 durationMs) {
notificationMessage = text;
notificationTextColor = textColor;
notificationOutlineColor = outlineColor;
notificationEndTime = SDL_GetTicks() + durationMs;
} }
// Cambia el tamaño de la ventana // Limpia la notificación actual
void Screen::setWindowSize(int size) { void Screen::clearNotification() {
options->windowSize = size; notificationEndTime = 0;
setVideoMode(0); notificationMessage.clear();
} }
// Reduce el tamaño de la ventana // Dibuja la notificación activa (si la hay) sobre el gameCanvas
void Screen::decWindowSize() { void Screen::renderNotification() {
--options->windowSize; if (SDL_GetTicks() >= notificationEndTime) {
options->windowSize = std::max(options->windowSize, 1);
setVideoMode(0);
}
// Aumenta el tamaño de la ventana
void Screen::incWindowSize() {
++options->windowSize;
options->windowSize = std::min(options->windowSize, 4);
setVideoMode(0);
}
// Cambia el color del borde
void Screen::setBorderColor(color_t color) {
borderColor = color;
}
// Cambia el tipo de mezcla
void Screen::setBlendMode(SDL_BlendMode blendMode) {
SDL_SetRenderDrawBlendMode(renderer, blendMode);
}
// Establece el tamaño del borde
void Screen::setBorderWidth(int s) {
options->borderWidth = s;
}
// Establece el tamaño del borde
void Screen::setBorderHeight(int s) {
options->borderHeight = s;
}
// Establece si se ha de ver el borde en el modo ventana
void Screen::setBorderEnabled(bool value) {
options->borderEnabled = value;
}
// Cambia entre borde visible y no visible
void Screen::switchBorder() {
options->borderEnabled = !options->borderEnabled;
setVideoMode(0);
}
// Activa el fade
void Screen::setFade() {
fade = true;
}
// Comprueba si ha terminado el fade
bool Screen::fadeEnded() {
if (fade || fadeCounter > 0) {
return false;
}
return true;
}
// Activa el spectrum fade
void Screen::setspectrumFade() {
spectrumFade = true;
}
// Comprueba si ha terminado el spectrum fade
bool Screen::spectrumFadeEnded() {
if (spectrumFade || spectrumFadeCounter > 0) {
return false;
}
return true;
}
// Inicializa las variables para el fade
void Screen::iniFade() {
fade = false;
fadeCounter = 0;
fadeLenght = 200;
}
// Actualiza el fade
void Screen::updateFade() {
if (!fade) {
return; return;
} }
notificationText->writeDX(TXT_CENTER | TXT_COLOR | TXT_STROKE,
fadeCounter++; gameCanvasWidth / 2,
if (fadeCounter > fadeLenght) { notificationY,
iniFade(); notificationMessage,
} 1,
notificationTextColor,
1,
notificationOutlineColor);
} }
// Dibuja el fade // ============================================================================
void Screen::renderFade() { // Emscripten — fix per a fullscreen/resize (veure el bloc de comentaris al
if (!fade) { // principi del fitxer i l'anonymous namespace amb els callbacks natius).
return; // ============================================================================
void Screen::handleCanvasResized() {
#ifdef __EMSCRIPTEN__
// La crida a SDL_SetWindowFullscreen + SDL_SetRenderLogicalPresentation
// que fa setVideoMode és l'única manera de resincronitzar l'estat intern
// de SDL amb el canvas HTML real.
setVideoMode(options->videoMode != 0);
#endif
} }
const SDL_FRect rect = {0, 0, (float)gameCanvasWidth, (float)gameCanvasHeight}; void Screen::syncFullscreenFlagFromBrowser(bool isFullscreen) {
color_t color = {0, 0, 0}; #ifdef __EMSCRIPTEN__
const float step = (float)fadeCounter / (float)fadeLenght; options->videoMode = isFullscreen ? SDL_WINDOW_FULLSCREEN : 0;
const int alpha = 0 + (255 - 0) * step; #else
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha); (void)isFullscreen;
SDL_RenderFillRect(renderer, &rect); #endif
} }
// Inicializa las variables para el fade spectrum void Screen::registerEmscriptenEventCallbacks() {
void Screen::iniSpectrumFade() { #ifdef __EMSCRIPTEN__
spectrumFade = false; // IMPORTANT: NO registrem resize callback. En mòbil, fer scroll fa que el
spectrumFadeCounter = 0; // navegador oculti/mostri la barra d'URL, disparant un resize del DOM per
spectrumFadeLenght = 50; // cada scroll. Això portava a cridar setVideoMode per cada scroll, que
// re-aplicava la logical presentation i corrompia el viewport intern de SDL.
spectrumColor.clear(); g_screen_instance = this;
emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, EM_TRUE, onEmFullscreenChange);
// Inicializa el vector de colores emscripten_set_orientationchange_callback(nullptr, EM_TRUE, onEmOrientationChange);
const std::vector<std::string> vColors = {"black", "blue", "red", "magenta", "green", "cyan", "yellow", "bright_white"}; #endif
for (auto v : vColors) {
spectrumColor.push_back(stringToColor(options->palette, v));
}
}
// Actualiza el spectrum fade
void Screen::updateSpectrumFade() {
if (!spectrumFade) {
return;
}
spectrumFadeCounter++;
if (spectrumFadeCounter > spectrumFadeLenght) {
iniSpectrumFade();
SDL_SetTextureColorMod(gameCanvas, 255, 255, 255);
}
}
// Dibuja el spectrum fade
void Screen::renderSpectrumFade() {
if (!spectrumFade) {
return;
}
const float step = (float)spectrumFadeCounter / (float)spectrumFadeLenght;
const int max = spectrumColor.size() - 1;
const int index = max + (0 - max) * step;
const color_t c = spectrumColor[index];
SDL_SetTextureColorMod(gameCanvas, c.r, c.g, c.b);
}
// Actualiza los efectos
void Screen::updateFX() {
updateFade();
updateSpectrumFade();
}
// Dibuja los efectos
void Screen::renderFX() {
renderFade();
renderSpectrumFade();
} }

View File

@@ -2,17 +2,71 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <vector> // for vector #include <string> // for string
#include "utils.h" // for color_t #include "utils.h" // for color_t
class Asset; class Asset;
class Text;
// Tipos de filtro // Tipos de filtro
constexpr int FILTER_NEAREST = 0; constexpr int FILTER_NEAREST = 0;
constexpr int FILTER_LINEAL = 1; constexpr int FILTER_LINEAL = 1;
class Screen { class Screen {
public:
// Constantes
static constexpr int WINDOW_ZOOM_MIN = 1;
static constexpr int WINDOW_ZOOM_MAX = 4;
#ifdef __EMSCRIPTEN__
// En WASM el tamaño de ventana está fijado a 1x, así que escalamos el
// renderizado por 3 aprovechando el modo NEAREST de la textura del juego
// para que los píxeles salgan nítidos.
static constexpr int WASM_RENDER_SCALE = 3;
#endif
// Constructor y destructor
Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options);
~Screen();
// Render loop
void clean(color_t color = {0x00, 0x00, 0x00}); // Limpia la pantalla
void start(); // Prepara para empezar a dibujar en la textura de juego
void blit(); // Vuelca el contenido del renderizador en pantalla
// Video y ventana
void setVideoMode(bool fullscreen); // Establece el modo de video
void toggleVideoMode(); // Cambia entre pantalla completa y ventana
void handleCanvasResized(); // En Emscripten, reaplica setVideoMode tras un cambio del navegador (salida de fullscreen con Esc, rotación). No-op fuera de Emscripten
void syncFullscreenFlagFromBrowser(bool isFullscreen); // Sincroniza el flag interno de fullscreen con el estado real del navegador. Debe llamarse antes de diferir handleCanvasResized. No-op fuera de Emscripten
void toggleIntegerScale(); // Alterna el escalado entero
void setIntegerScale(bool enabled); // Establece el escalado entero
void toggleVSync(); // Alterna el V-Sync
void setVSync(bool enabled); // Establece el V-Sync
auto decWindowZoom() -> bool; // Reduce el zoom de la ventana (devuelve true si cambió)
auto incWindowZoom() -> bool; // Aumenta el zoom de la ventana (devuelve true si cambió)
auto setWindowZoom(int zoom) -> bool; // Establece el zoom de la ventana (devuelve true si cambió)
// Borde
void setBorderColor(color_t color); // Cambia el color del borde
// Notificaciones
void notify(const std::string &text, color_t textColor, color_t outlineColor, Uint32 durationMs); // Muestra una notificación en la línea superior del canvas durante durationMs. Sobrescribe cualquier notificación activa (sin apilación).
void clearNotification(); // Limpia la notificación actual
private: private:
// Helpers internos de setVideoMode
void applyFullscreen(bool fullscreen); // SDL_SetWindowFullscreen + visibilidad del cursor
void applyWindowedLayout(); // Calcula windowWidth/Height/dest + SDL_SetWindowSize + SDL_SetWindowPosition
void applyFullscreenLayout(); // SDL_GetWindowSize + delegación a computeFullscreenGameRect
void computeFullscreenGameRect(); // Calcula dest en fullscreen (integerScale / keepAspect / stretched)
void applyLogicalPresentation(bool fullscreen); // SDL_SetRenderLogicalPresentation + persistencia a options
// Emscripten
void registerEmscriptenEventCallbacks(); // Registra los callbacks nativos de Emscripten para fullscreenchange y orientationchange. No-op fuera de Emscripten
// Notificaciones
void renderNotification(); // Dibuja la notificación activa (si la hay) sobre el gameCanvas
// Objetos y punteros // Objetos y punteros
SDL_Window *window; // Ventana de la aplicación SDL_Window *window; // Ventana de la aplicación
SDL_Renderer *renderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
@@ -26,102 +80,15 @@ class Screen {
int gameCanvasWidth; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego int gameCanvasWidth; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego
int gameCanvasHeight; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego int gameCanvasHeight; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego
int borderWidth; // Anchura del borde int borderWidth; // Anchura del borde
int borderHeight; // Anltura del borde int borderHeight; // Altura del borde
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana
color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
bool notifyActive; // Indica si hay notificaciones activas
int notificationLogicalWidth; // Ancho lógico de las notificaciones en relación al tamaño de pantalla
int notificationLogicalHeight; // Alto lógico de las notificaciones en relación al tamaño de pantalla
// Variables - Efectos // Notificaciones - una sola activa, sin apilación ni animaciones
bool fade; // Indica si esta activo el efecto de fade Text *notificationText; // Fuente 8bithud dedicada a las notificaciones
int fadeCounter; // Temporizador para el efecto de fade std::string notificationMessage; // Texto a mostrar
int fadeLenght; // Duración del fade color_t notificationTextColor; // Color del texto
bool spectrumFade; // Indica si esta activo el efecto de fade spectrum color_t notificationOutlineColor; // Color del outline
int spectrumFadeCounter; // Temporizador para el efecto de fade spectrum Uint32 notificationEndTime; // SDL_GetTicks() hasta el cual se muestra
int spectrumFadeLenght; // Duración del fade spectrum int notificationY; // Fila vertical en el canvas virtual
std::vector<color_t> spectrumColor; // Colores para el fade spectrum
// Inicializa las variables para el fade
void iniFade();
// Actualiza el fade
void updateFade();
// Dibuja el fade
void renderFade();
// Inicializa las variables para el fade spectrum
void iniSpectrumFade();
// Actualiza el spectrum fade
void updateSpectrumFade();
// Dibuja el spectrum fade
void renderSpectrumFade();
public:
// Constructor
Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options);
// Destructor
~Screen();
// Limpia la pantalla
void clean(color_t color = {0x00, 0x00, 0x00});
// Prepara para empezar a dibujar en la textura de juego
void start();
// Vuelca el contenido del renderizador en pantalla
void blit();
// Establece el modo de video
void setVideoMode(int videoMode);
// Camibia entre pantalla completa y ventana
void switchVideoMode();
// Cambia el tamaño de la ventana
void setWindowSize(int size);
// Reduce el tamaño de la ventana
void decWindowSize();
// Aumenta el tamaño de la ventana
void incWindowSize();
// Cambia el color del borde
void setBorderColor(color_t color);
// Cambia el tipo de mezcla
void setBlendMode(SDL_BlendMode blendMode);
// Establece el tamaño del borde
void setBorderWidth(int s);
void setBorderHeight(int s);
// Establece si se ha de ver el borde en el modo ventana
void setBorderEnabled(bool value);
// Cambia entre borde visible y no visible
void switchBorder();
// Activa el fade
void setFade();
// Comprueba si ha terminado el fade
bool fadeEnded();
// Activa el spectrum fade
void setspectrumFade();
// Comprueba si ha terminado el spectrum fade
bool spectrumFadeEnded();
// Actualiza los efectos
void updateFX();
// Dibuja los efectos
void renderFX();
}; };

View File

@@ -3,30 +3,15 @@
#include <fstream> // for char_traits, basic_ostream, basic_ifstream, ope... #include <fstream> // for char_traits, basic_ostream, basic_ifstream, ope...
#include <iostream> // for cout #include <iostream> // for cout
#include <sstream>
#include "sprite.h" // for Sprite #include "sprite.h" // for Sprite
#include "texture.h" // for Texture #include "texture.h" // for Texture
#include "utils.h" // for color_t #include "utils.h" // for color_t
// Llena una estructuta textFile_t desde un fichero // Parser compartido: rellena un textFile_t desde cualquier istream
textFile_t LoadTextFile(std::string file, bool verbose) { static void parseTextFileStream(std::istream &rfile, textFile_t &tf) {
textFile_t tf;
// Inicializa a cero el vector con las coordenadas
for (int i = 0; i < 128; ++i) {
tf.offset[i].x = 0;
tf.offset[i].y = 0;
tf.offset[i].w = 0;
}
// Abre el fichero para leer los valores
const std::string filename = file.substr(file.find_last_of("\\/") + 1).c_str();
std::ifstream rfile(file);
if (rfile.is_open() && rfile.good()) {
std::string buffer; std::string buffer;
// Lee los dos primeros valores del fichero
std::getline(rfile, buffer); std::getline(rfile, buffer);
std::getline(rfile, buffer); std::getline(rfile, buffer);
tf.boxWidth = std::stoi(buffer); tf.boxWidth = std::stoi(buffer);
@@ -35,40 +20,65 @@ textFile_t LoadTextFile(std::string file, bool verbose) {
std::getline(rfile, buffer); std::getline(rfile, buffer);
tf.boxHeight = std::stoi(buffer); tf.boxHeight = std::stoi(buffer);
// lee el resto de datos del fichero
int index = 32; int index = 32;
int line_read = 0; int line_read = 0;
while (std::getline(rfile, buffer)) { while (std::getline(rfile, buffer)) {
// Almacena solo las lineas impares
if (line_read % 2 == 1) { if (line_read % 2 == 1) {
tf.offset[index++].w = std::stoi(buffer); tf.offset[index++].w = std::stoi(buffer);
} }
// Limpia el buffer
buffer.clear(); buffer.clear();
line_read++; line_read++;
};
// Cierra el fichero
if (verbose) {
std::cout << "Text loaded: " << filename.c_str() << std::endl;
}
rfile.close();
}
// El fichero no se puede abrir
else {
if (verbose) {
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
} }
} }
// Establece las coordenadas para cada caracter ascii de la cadena y su ancho static void computeTextFileOffsets(textFile_t &tf) {
for (int i = 32; i < 128; ++i) { for (int i = 32; i < 128; ++i) {
tf.offset[i].x = ((i - 32) % 15) * tf.boxWidth; tf.offset[i].x = ((i - 32) % 15) * tf.boxWidth;
tf.offset[i].y = ((i - 32) / 15) * tf.boxHeight; tf.offset[i].y = ((i - 32) / 15) * tf.boxHeight;
} }
}
// Llena una estructuta textFile_t desde un fichero
textFile_t LoadTextFile(std::string file, bool verbose) {
textFile_t tf;
for (int i = 0; i < 128; ++i) {
tf.offset[i].x = 0;
tf.offset[i].y = 0;
tf.offset[i].w = 0;
}
const std::string filename = file.substr(file.find_last_of("\\/") + 1).c_str();
std::ifstream rfile(file);
if (rfile.is_open() && rfile.good()) {
parseTextFileStream(rfile, tf);
if (verbose) {
std::cout << "Text loaded: " << filename.c_str() << std::endl;
}
} else if (verbose) {
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
}
computeTextFileOffsets(tf);
return tf;
}
// Llena una estructura textFile_t desde bytes en memoria
textFile_t LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose) {
textFile_t tf;
for (int i = 0; i < 128; ++i) {
tf.offset[i].x = 0;
tf.offset[i].y = 0;
tf.offset[i].w = 0;
}
if (!bytes.empty()) {
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
std::stringstream ss(content);
parseTextFileStream(ss, tf);
if (verbose) {
std::cout << "Text loaded from memory" << std::endl;
}
}
computeTextFileOffsets(tf);
return tf; return tf;
} }
@@ -135,6 +145,24 @@ Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer) {
fixedWidth = false; fixedWidth = false;
} }
// Constructor desde bytes
Text::Text(const std::vector<uint8_t> &pngBytes, const std::vector<uint8_t> &txtBytes, SDL_Renderer *renderer) {
textFile_t tf = LoadTextFileFromMemory(txtBytes);
boxHeight = tf.boxHeight;
boxWidth = tf.boxWidth;
for (int i = 0; i < 128; ++i) {
offset[i].x = tf.offset[i].x;
offset[i].y = tf.offset[i].y;
offset[i].w = tf.offset[i].w;
}
// Crea la textura desde bytes (Text es dueño en este overload)
texture = new Texture(renderer, pngBytes);
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
fixedWidth = false;
}
// Destructor // Destructor
Text::~Text() { Text::~Text() {
delete sprite; delete sprite;

View File

@@ -2,7 +2,9 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <cstdint>
#include <string> // for string #include <string> // for string
#include <vector>
class Sprite; class Sprite;
class Texture; class Texture;
#include "utils.h" #include "utils.h"
@@ -28,6 +30,9 @@ struct textFile_t {
// Llena una estructuta textFile_t desde un fichero // Llena una estructuta textFile_t desde un fichero
textFile_t LoadTextFile(std::string file, bool verbose = false); textFile_t LoadTextFile(std::string file, bool verbose = false);
// Llena una estructura textFile_t desde bytes en memoria
textFile_t LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose = false);
// Clase texto. Pinta texto en pantalla a partir de un bitmap // Clase texto. Pinta texto en pantalla a partir de un bitmap
class Text { class Text {
private: private:
@@ -47,6 +52,9 @@ class Text {
Text(std::string textFile, Texture *texture, SDL_Renderer *renderer); Text(std::string textFile, Texture *texture, SDL_Renderer *renderer);
Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer); Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer);
// Constructor desde bytes en memoria: comparte ownership del texture (no lo libera)
Text(const std::vector<uint8_t> &pngBytes, const std::vector<uint8_t> &txtBytes, SDL_Renderer *renderer);
// Destructor // Destructor
~Text(); ~Text();

View File

@@ -31,75 +31,86 @@ Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) {
} }
} }
// Constructor desde bytes
Texture::Texture(SDL_Renderer *renderer, const std::vector<uint8_t> &bytes, bool verbose) {
this->renderer = renderer;
this->path = "";
texture = nullptr;
width = 0;
height = 0;
if (!bytes.empty()) {
loadFromMemory(bytes.data(), bytes.size(), renderer, verbose);
}
}
// Destructor // Destructor
Texture::~Texture() { Texture::~Texture() {
// Libera memoria // Libera memoria
unload(); unload();
} }
// Helper: convierte píxeles RGBA decodificados por stbi en SDL_Texture
static SDL_Texture *createTextureFromPixels(SDL_Renderer *renderer, unsigned char *data, int w, int h, int *out_w, int *out_h) {
const int pitch = 4 * w;
SDL_Surface *loadedSurface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA32, (void *)data, pitch);
if (loadedSurface == nullptr) {
return nullptr;
}
SDL_Texture *newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture != nullptr) {
*out_w = loadedSurface->w;
*out_h = loadedSurface->h;
SDL_SetTextureScaleMode(newTexture, Texture::currentScaleMode);
}
SDL_DestroySurface(loadedSurface);
return newTexture;
}
// Carga una imagen desde un fichero // Carga una imagen desde un fichero
bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose) { bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose) {
const std::string filename = path.substr(path.find_last_of("\\/") + 1); const std::string filename = path.substr(path.find_last_of("\\/") + 1);
int req_format = STBI_rgb_alpha; int req_format = STBI_rgb_alpha;
int width, height, orig_format; int w, h, orig_format;
unsigned char *data = stbi_load(path.c_str(), &width, &height, &orig_format, req_format); unsigned char *data = stbi_load(path.c_str(), &w, &h, &orig_format, req_format);
if (data == nullptr) { if (data == nullptr) {
SDL_Log("Loading image failed: %s", stbi_failure_reason()); SDL_Log("Loading image failed: %s", stbi_failure_reason());
exit(1); exit(1);
} else { } else if (verbose) {
if (verbose) {
std::cout << "Image loaded: " << filename.c_str() << std::endl; std::cout << "Image loaded: " << filename.c_str() << std::endl;
} }
}
int pitch;
SDL_PixelFormat pixel_format;
if (req_format == STBI_rgb) {
pitch = 3 * width; // 3 bytes por pixel * pixels per linea
pixel_format = SDL_PIXELFORMAT_RGB24;
} else { // STBI_rgb_alpha (RGBA)
pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
// Limpia
unload(); unload();
SDL_Texture *newTexture = createTextureFromPixels(renderer, data, w, h, &this->width, &this->height);
// La textura final if (newTexture == nullptr && verbose) {
SDL_Texture *newTexture = nullptr;
// Carga la imagen desde una ruta específica
SDL_Surface *loadedSurface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch);
if (loadedSurface == nullptr) {
if (verbose) {
std::cout << "Unable to load image " << path.c_str() << std::endl; std::cout << "Unable to load image " << path.c_str() << std::endl;
} }
} else {
// Crea la textura desde los pixels de la surface
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == nullptr) {
if (verbose) {
std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl;
}
} else {
// Obtiene las dimensiones de la imagen
this->width = loadedSurface->w;
this->height = loadedSurface->h;
// Aplica el modo de escalado
SDL_SetTextureScaleMode(newTexture, currentScaleMode);
}
// Elimina la textura cargada
SDL_DestroySurface(loadedSurface);
}
// Return success
stbi_image_free(data); stbi_image_free(data);
texture = newTexture; texture = newTexture;
return texture != nullptr; return texture != nullptr;
} }
// Carga una imagen desde bytes en memoria
bool Texture::loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *renderer, bool verbose) {
int w, h, orig_format;
unsigned char *pixels = stbi_load_from_memory(data, (int)size, &w, &h, &orig_format, STBI_rgb_alpha);
if (pixels == nullptr) {
SDL_Log("Loading image from memory failed: %s", stbi_failure_reason());
return false;
}
unload();
SDL_Texture *newTexture = createTextureFromPixels(renderer, pixels, w, h, &this->width, &this->height);
if (newTexture == nullptr && verbose) {
std::cout << "Unable to create texture from memory" << std::endl;
}
stbi_image_free(pixels);
texture = newTexture;
return texture != nullptr;
}
// Crea una textura en blanco // Crea una textura en blanco
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) { bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) {
// Crea una textura sin inicializar // Crea una textura sin inicializar

View File

@@ -2,7 +2,9 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <cstdint>
#include <string> // for basic_string, string #include <string> // for basic_string, string
#include <vector>
class Texture { class Texture {
private: private:
@@ -24,12 +26,18 @@ class Texture {
// Constructor // Constructor
Texture(SDL_Renderer *renderer, std::string path = "", bool verbose = false); Texture(SDL_Renderer *renderer, std::string path = "", bool verbose = false);
// Constructor desde bytes (PNG en memoria)
Texture(SDL_Renderer *renderer, const std::vector<uint8_t> &bytes, bool verbose = false);
// Destructor // Destructor
~Texture(); ~Texture();
// Carga una imagen desde un fichero // Carga una imagen desde un fichero
bool loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose = false); bool loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose = false);
// Carga una imagen desde bytes en memoria
bool loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *renderer, bool verbose = false);
// Crea una textura en blanco // Crea una textura en blanco
bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING); bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);

View File

@@ -15,6 +15,7 @@
#include "jail_audio.hpp" // for JA_StopMusic, JA_GetMusicState, JA_Play... #include "jail_audio.hpp" // for JA_StopMusic, JA_GetMusicState, JA_Play...
#include "lang.h" // for Lang, ba_BA, en_UK, es_ES #include "lang.h" // for Lang, ba_BA, en_UK, es_ES
#include "menu.h" // for Menu #include "menu.h" // for Menu
#include "resource.h"
#include "screen.h" // for Screen, FILTER_LINEAL, FILTER_NEAREST #include "screen.h" // for Screen, FILTER_LINEAL, FILTER_NEAREST
#include "smartsprite.h" // for SmartSprite #include "smartsprite.h" // for SmartSprite
#include "sprite.h" // for Sprite #include "sprite.h" // for Sprite
@@ -36,34 +37,39 @@ Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset,
eventHandler = new SDL_Event(); eventHandler = new SDL_Event();
fade = new Fade(renderer); fade = new Fade(renderer);
dustTexture = new Texture(renderer, asset->get("title_dust.png")); Resource *R = Resource::get();
coffeeTexture = new Texture(renderer, asset->get("title_coffee.png")); dustTexture = R->getTexture("title_dust.png");
crisisTexture = new Texture(renderer, asset->get("title_crisis.png")); coffeeTexture = R->getTexture("title_coffee.png");
gradientTexture = new Texture(renderer, asset->get("title_gradient.png")); crisisTexture = R->getTexture("title_crisis.png");
gradientTexture = R->getTexture("title_gradient.png");
coffeeBitmap = new SmartSprite(coffeeTexture, renderer); coffeeBitmap = new SmartSprite(coffeeTexture, renderer);
crisisBitmap = new SmartSprite(crisisTexture, renderer); crisisBitmap = new SmartSprite(crisisTexture, renderer);
dustBitmapL = new AnimatedSprite(dustTexture, renderer, asset->get("title_dust.ani")); dustBitmapL = new AnimatedSprite(dustTexture, renderer, "", &R->getAnimationLines("title_dust.ani"));
dustBitmapR = new AnimatedSprite(dustTexture, renderer, asset->get("title_dust.ani")); dustBitmapR = new AnimatedSprite(dustTexture, renderer, "", &R->getAnimationLines("title_dust.ani"));
gradient = new Sprite({0, 0, 256, 192}, gradientTexture, renderer); gradient = new Sprite({0, 0, 256, 192}, gradientTexture, renderer);
text1 = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); text1 = R->getText("smb2");
text2 = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer); text2 = R->getText("8bithud");
#ifdef GAME_CONSOLE #ifdef GAME_CONSOLE
menu.title = new Menu(renderer, asset, input, asset->get("title_gc.men")); menu.title = R->getMenu("title_gc");
menu.options = new Menu(renderer, asset, input, asset->get("options_gc.men")); menu.options = R->getMenu("options_gc");
#else #else
menu.title = new Menu(renderer, asset, input, asset->get("title.men")); menu.title = R->getMenu("title");
menu.options = new Menu(renderer, asset, input, asset->get("options.men")); menu.options = R->getMenu("options");
#endif #endif
menu.playerSelect = new Menu(renderer, asset, input, asset->get("player_select.men")); menu.playerSelect = R->getMenu("player_select");
// Sonidos #ifdef __EMSCRIPTEN__
crashSound = JA_LoadSound(asset->get("title.wav").c_str()); // En la versión web no se puede cerrar el programa: ocultamos la opción QUIT del menú de título
menu.title->setVisible(3, false);
menu.title->setSelectable(3, false);
#endif
// Musicas // Sonidos y música (handles compartidos)
titleMusic = JA_LoadMusic(asset->get("title.ogg").c_str()); crashSound = R->getSound("title.wav");
titleMusic = R->getMusic("title.ogg");
// Inicializa los valores // Inicializa los valores
init(); init();
@@ -74,34 +80,14 @@ Title::~Title() {
delete eventHandler; delete eventHandler;
delete fade; delete fade;
dustTexture->unload(); // Las texturas, Text, Menu, sonido y música son propiedad de Resource —
delete dustTexture; // no se liberan aquí. Solo los sprites que posee esta escena.
coffeeTexture->unload();
delete coffeeTexture;
crisisTexture->unload();
delete crisisTexture;
gradientTexture->unload();
delete gradientTexture;
delete coffeeBitmap; delete coffeeBitmap;
delete crisisBitmap; delete crisisBitmap;
delete dustBitmapL; delete dustBitmapL;
delete dustBitmapR; delete dustBitmapR;
delete gradient; delete gradient;
delete text1;
delete text2;
delete menu.title;
delete menu.options;
delete menu.playerSelect;
JA_DeleteSound(crashSound);
JA_DeleteMusic(titleMusic);
SDL_DestroyTexture(background); SDL_DestroyTexture(background);
} }
@@ -120,6 +106,11 @@ void Title::init() {
ticksSpeed = 15; ticksSpeed = 15;
fade->init(0x17, 0x17, 0x26); fade->init(0x17, 0x17, 0x26);
demo = true; demo = true;
vibrationStep = 0;
vibrationInitialized = false;
instructionsActive = false;
demoGameActive = false;
demoThenInstructions = false;
// Pone valores por defecto a las opciones de control // Pone valores por defecto a las opciones de control
options->input.clear(); options->input.clear();
@@ -257,21 +248,27 @@ void Title::update() {
// Sección 2 - Titulo vibrando // Sección 2 - Titulo vibrando
case SUBSECTION_TITLE_2: { case SUBSECTION_TITLE_2: {
// Agita la pantalla // Captura las posiciones base y reproduce el sonido la primera vez
static const int v[] = {-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 0}; if (!vibrationInitialized) {
static const int a = coffeeBitmap->getPosX(); vibrationCoffeeBaseX = coffeeBitmap->getPosX();
static const int b = crisisBitmap->getPosX(); vibrationCrisisBaseX = crisisBitmap->getPosX();
static int step = 0; vibrationInitialized = true;
}
coffeeBitmap->setPosX(a + v[step / 3]); // Agita la pantalla
crisisBitmap->setPosX(b + v[step / 3]); const int v[] = {-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 0};
coffeeBitmap->setPosX(vibrationCoffeeBaseX + v[vibrationStep / 3]);
crisisBitmap->setPosX(vibrationCrisisBaseX + v[vibrationStep / 3]);
dustBitmapR->update(); dustBitmapR->update();
dustBitmapL->update(); dustBitmapL->update();
step++; vibrationStep++;
if (step == 33) { if (vibrationStep >= 33) {
section->subsection = SUBSECTION_TITLE_3; section->subsection = SUBSECTION_TITLE_3;
vibrationStep = 0;
vibrationInitialized = false;
} }
} break; } break;
@@ -303,18 +300,18 @@ void Title::update() {
break; break;
case 2: // QUIT case 2: // QUIT
#ifndef __EMSCRIPTEN__
section->name = SECTION_PROG_QUIT; section->name = SECTION_PROG_QUIT;
JA_StopMusic(); JA_StopMusic();
#endif
break; break;
case 3: // TIME OUT case 3: // TIME OUT
counter = TITLE_COUNTER; counter = TITLE_COUNTER;
menu.active->reset(); menu.active->reset();
if (demo) { if (demo) {
demoThenInstructions = true;
runDemoGame(); runDemoGame();
if (section->name != SECTION_PROG_QUIT) {
runInstructions(m_auto);
}
} else } else
section->name = SECTION_PROG_LOGO; section->name = SECTION_PROG_LOGO;
break; break;
@@ -328,7 +325,7 @@ void Title::update() {
updateBG(); updateBG();
// Comprueba las entradas para el menu // Comprueba las entradas para el menu
if (menuVisible == true) { if (menuVisible == true && !fade->isEnabled()) {
menu.active->update(); menu.active->update();
} }
@@ -482,13 +479,8 @@ void Title::update() {
} }
} else if (counter == 0) { } else if (counter == 0) {
if (demo) { if (demo) {
demoThenInstructions = true;
runDemoGame(); runDemoGame();
if (section->name != SECTION_PROG_QUIT) {
runInstructions(m_auto);
}
init();
demo = false;
counter = TITLE_COUNTER;
} else { } else {
section->name = SECTION_PROG_LOGO; section->name = SECTION_PROG_LOGO;
} }
@@ -497,8 +489,6 @@ void Title::update() {
// Sección Instrucciones // Sección Instrucciones
if (section->subsection == SUBSECTION_TITLE_INSTRUCTIONS) { if (section->subsection == SUBSECTION_TITLE_INSTRUCTIONS) {
runInstructions(m_auto); runInstructions(m_auto);
counter = TITLE_COUNTER;
demo = true;
} }
} }
@@ -539,14 +529,7 @@ void Title::render() {
} break; } break;
// Sección 2 - Titulo vibrando // Sección 2 - Titulo vibrando
case SUBSECTION_TITLE_2: { // Reproduce el efecto sonoro case SUBSECTION_TITLE_2: {
JA_PlaySound(crashSound);
// Agita la pantalla
const int v[] = {-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 0};
const int a = coffeeBitmap->getPosX();
const int b = crisisBitmap->getPosX();
for (int n = 0; n < 11 * 3; ++n) {
// Prepara para empezar a dibujar en la textura de juego // Prepara para empezar a dibujar en la textura de juego
screen->start(); screen->start();
@@ -562,25 +545,16 @@ void Title::render() {
// Dibuja el degradado // Dibuja el degradado
gradient->render(); gradient->render();
// Dibuja los objetos // Dibuja los objetos (posiciones ya actualizadas por update)
coffeeBitmap->setPosX(a + v[n / 3]);
crisisBitmap->setPosX(b + v[n / 3]);
coffeeBitmap->render(); coffeeBitmap->render();
crisisBitmap->render(); crisisBitmap->render();
dustBitmapR->update();
dustBitmapL->update();
dustBitmapR->render(); dustBitmapR->render();
dustBitmapL->render(); dustBitmapL->render();
// Vuelca el contenido del renderizador en pantalla // Vuelca el contenido del renderizador en pantalla
screen->blit(); screen->blit();
} } break;
section->subsection = SUBSECTION_TITLE_3;
}
break;
// Sección 3 - La pantalla de titulo con el menú y la música // Sección 3 - La pantalla de titulo con el menú y la música
case SUBSECTION_TITLE_3: { // Prepara para empezar a dibujar en la textura de juego case SUBSECTION_TITLE_3: { // Prepara para empezar a dibujar en la textura de juego
@@ -660,20 +634,21 @@ void Title::checkEvents() {
// Comprueba las entradas // Comprueba las entradas
void Title::checkInput() { void Title::checkInput() {
#ifndef __EMSCRIPTEN__
if (input->checkInput(input_exit, REPEAT_FALSE)) { if (input->checkInput(input_exit, REPEAT_FALSE)) {
section->name = SECTION_PROG_QUIT; section->name = SECTION_PROG_QUIT;
} } else
#endif
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) { if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
screen->switchVideoMode(); screen->toggleVideoMode();
} }
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) { else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) {
screen->decWindowSize(); screen->decWindowZoom();
} }
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) { else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) {
screen->incWindowSize(); screen->incWindowZoom();
} }
} }
@@ -865,6 +840,10 @@ void Title::updateMenuLabels() {
menu.title->setItemCaption(1, lang->getText(52)); // 2 PLAYERS menu.title->setItemCaption(1, lang->getText(52)); // 2 PLAYERS
menu.title->setItemCaption(2, lang->getText(1)); // OPTIONS menu.title->setItemCaption(2, lang->getText(1)); // OPTIONS
menu.title->setItemCaption(3, lang->getText(3)); // QUIT menu.title->setItemCaption(3, lang->getText(3)); // QUIT
#ifdef __EMSCRIPTEN__
menu.title->setVisible(3, false);
menu.title->setSelectable(3, false);
#endif
// Recoloca el menu de titulo // Recoloca el menu de titulo
menu.title->centerMenuOnX(GAMECANVAS_CENTER_X); menu.title->centerMenuOnX(GAMECANVAS_CENTER_X);
@@ -892,7 +871,7 @@ void Title::updateMenuLabels() {
// Aplica las opciones de menu seleccionadas // Aplica las opciones de menu seleccionadas
void Title::applyOptions() { void Title::applyOptions() {
screen->setVideoMode(options->videoMode); screen->setVideoMode(options->videoMode != 0);
lang->setLang(options->language); lang->setLang(options->language);
@@ -900,27 +879,119 @@ void Title::applyOptions() {
createTiledBackground(); createTiledBackground();
} }
// Bucle para el titulo del juego // Ejecuta un frame
void Title::run() { void Title::iterate() {
while (section->name == SECTION_PROG_TITLE) { // Si las instrucciones están activas, delega el frame
if (instructionsActive) {
instructions->update();
instructions->render();
if (instructions->hasFinished()) {
bool wasQuit = instructions->isQuitRequested();
delete instructions;
instructions = nullptr;
instructionsActive = false;
if (wasQuit) {
section->name = SECTION_PROG_QUIT;
} else if (instructionsMode == m_auto) {
section->name = SECTION_PROG_TITLE;
init();
demo = true;
} else {
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_3;
}
}
return;
}
// Si el juego demo está activo, delega el frame
if (demoGameActive) {
// El demo Game necesita section->name == SECTION_PROG_GAME para funcionar
section->name = SECTION_PROG_GAME;
demoGame->iterate();
if (demoGame->hasFinished()) {
bool wasQuit = (section->name == SECTION_PROG_QUIT);
delete demoGame;
demoGame = nullptr;
demoGameActive = false;
if (wasQuit) {
section->name = SECTION_PROG_QUIT;
} else if (demoThenInstructions) {
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_3;
demoThenInstructions = false;
runInstructions(m_auto);
} else {
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_1;
}
} else {
// Restaura section para que Director no transicione fuera de Title
section->name = SECTION_PROG_TITLE;
}
return;
}
// Ejecución normal del título
update(); update();
checkEvents();
render(); render();
} }
// Procesa un evento individual
void Title::handleEvent(SDL_Event *event) {
// Si hay un sub-estado activo, delega el evento
if (instructionsActive && instructions) {
// SDL_EVENT_QUIT ya lo maneja Director
return;
} }
// Ejecuta la parte donde se muestran las instrucciones if (demoGameActive && demoGame) {
demoGame->handleEvent(event);
return;
}
// SDL_EVENT_QUIT ya lo maneja Director
if (event->type == SDL_EVENT_RENDER_DEVICE_RESET || event->type == SDL_EVENT_RENDER_TARGETS_RESET) {
reLoadTextures();
}
if (section->subsection == SUBSECTION_TITLE_3) {
if ((event->type == SDL_EVENT_KEY_UP) || (event->type == SDL_EVENT_JOYSTICK_BUTTON_UP)) {
menuVisible = true;
counter = TITLE_COUNTER;
}
}
}
// Bucle para el titulo del juego (compatibilidad)
void Title::run() {
while (section->name == SECTION_PROG_TITLE || instructionsActive || demoGameActive) {
iterate();
}
}
// Inicia la parte donde se muestran las instrucciones
void Title::runInstructions(mode_e mode) { void Title::runInstructions(mode_e mode) {
instructions = new Instructions(renderer, screen, asset, input, lang, section); instructions = new Instructions(renderer, screen, asset, input, lang, section);
instructions->run(mode); instructions->start(mode);
delete instructions; instructionsActive = true;
instructionsMode = mode;
} }
// Ejecuta el juego en modo demo // Inicia el juego en modo demo
void Title::runDemoGame() { void Title::runDemoGame() {
// Temporalmente ponemos section para que el constructor de Game funcione
section->name = SECTION_PROG_GAME;
section->subsection = SUBSECTION_GAME_PLAY_1P;
demoGame = new Game(1, 0, renderer, screen, asset, lang, input, true, options, section); demoGame = new Game(1, 0, renderer, screen, asset, lang, input, true, options, section);
demoGame->run(); demoGameActive = true;
delete demoGame; // Restauramos section para que Director no transicione fuera de Title
section->name = SECTION_PROG_TITLE;
} }
// Modifica las opciones para los controles de los jugadores // Modifica las opciones para los controles de los jugadores
@@ -1014,12 +1085,13 @@ void Title::createTiledBackground() {
delete tile; delete tile;
} }
// Comprueba cuantos mandos hay conectados para gestionar el menu de opciones // Comprueba cuantos mandos hay conectados para gestionar el menu de opciones.
// El estado de Input lo mantiene al día Director via eventos SDL_EVENT_GAMEPAD_ADDED/REMOVED,
// así que aquí solo leemos la lista actual sin reescanear.
void Title::checkInputDevices() { void Title::checkInputDevices() {
if (options->console) { if (options->console) {
std::cout << "Filling devices for options menu..." << std::endl; std::cout << "Filling devices for options menu..." << std::endl;
} }
input->discoverGameController();
const int numControllers = input->getNumControllers(); const int numControllers = input->getNumControllers();
availableInputDevices.clear(); availableInputDevices.clear();
input_t temp; input_t temp;

View File

@@ -22,7 +22,7 @@ struct JA_Music_t;
struct JA_Sound_t; struct JA_Sound_t;
// Textos // Textos
constexpr const char *TEXT_COPYRIGHT = "@2020 JailDesigner (v2.3.3)"; constexpr const char *TEXT_COPYRIGHT = "@2020 JailDesigner (v2.3.4)";
// Contadores // Contadores
constexpr int TITLE_COUNTER = 800; constexpr int TITLE_COUNTER = 800;
@@ -90,6 +90,18 @@ class Title {
std::vector<input_t> availableInputDevices; // Vector con todos los metodos de control disponibles std::vector<input_t> availableInputDevices; // Vector con todos los metodos de control disponibles
std::vector<int> deviceIndex; // Indice para el jugador [i] del vector de dispositivos de entrada disponibles std::vector<int> deviceIndex; // Indice para el jugador [i] del vector de dispositivos de entrada disponibles
// Variables para la vibración del título (SUBSECTION_TITLE_2)
int vibrationStep; // Paso actual de la vibración
int vibrationCoffeeBaseX; // Posición X base del bitmap Coffee
int vibrationCrisisBaseX; // Posición X base del bitmap Crisis
bool vibrationInitialized; // Indica si se han capturado las posiciones base
// Variables para sub-estados delegados (instrucciones y demo)
bool instructionsActive; // Indica si las instrucciones están activas
bool demoGameActive; // Indica si el juego demo está activo
mode_e instructionsMode; // Modo de las instrucciones activas
bool demoThenInstructions; // Indica si tras la demo hay que mostrar instrucciones
// Inicializa los valores // Inicializa los valores
void init(); void init();
@@ -144,4 +156,10 @@ class Title {
// Bucle para el titulo del juego // Bucle para el titulo del juego
void run(); void run();
// Ejecuta un frame
void iterate();
// Procesa un evento
void handleEvent(SDL_Event *event);
}; };

View File

@@ -0,0 +1,52 @@
# Makefile para la herramienta de empaquetado de Coffee Crisis
# =============================================================
CXX := g++
CXXFLAGS := -std=c++20 -Wall -Os -I../../source
SOURCES := pack_resources.cpp ../../source/resource_pack.cpp
TARGET := pack_resources
CLEAN_FILES := pack_resources *.pack *.o
ifeq ($(OS),Windows_NT)
DETECTED_OS := Windows
TARGET := $(TARGET).exe
CLEAN_CMD := del /Q
FixPath = $(subst /,\\,$1)
else
DETECTED_OS := $(shell uname -s)
CLEAN_CMD := rm -f
FixPath = $1
endif
.PHONY: all pack_tool pack clean help
all: pack_tool
pack_tool:
@echo "Compilando herramienta de empaquetado para $(DETECTED_OS)..."
$(CXX) $(CXXFLAGS) $(SOURCES) -o $(TARGET)
@echo "Herramienta compilada: $(TARGET)"
clean:
@echo "Limpiando archivos generados..."
$(CLEAN_CMD) $(call FixPath,$(CLEAN_FILES))
@echo "Archivos limpiados"
# Crear pack final (invocado desde el Makefile raíz)
pack: pack_tool
ifeq ($(OS),Windows_NT)
.\$(TARGET) ..\..\data ..\..\resources.pack
else
./$(TARGET) ../../data ../../resources.pack
endif
help:
@echo "Makefile para herramientas de Coffee Crisis"
@echo "==========================================="
@echo ""
@echo "Comandos disponibles:"
@echo " all - Compilar herramienta de empaquetado (por defecto)"
@echo " pack_tool - Compilar herramienta de empaquetado"
@echo " pack - Crear pack final en ../../resources.pack"
@echo " clean - Limpiar archivos generados"
@echo " help - Mostrar esta ayuda"

View File

@@ -0,0 +1,104 @@
#include <filesystem>
#include <iostream>
#include "resource_pack.h"
static constexpr const char* APP_NAME = "Coffee Crisis";
void showHelp() {
std::cout << APP_NAME << " - Resource Packer" << '\n';
std::cout << "===============================================" << '\n';
std::cout << "Usage: pack_resources [options] [input_dir] [output_file]" << '\n';
std::cout << '\n';
std::cout << "Options:" << '\n';
std::cout << " --help Show this help message" << '\n';
std::cout << " --list List contents of an existing pack file" << '\n';
std::cout << '\n';
std::cout << "Arguments:" << '\n';
std::cout << " input_dir Directory to pack (default: data)" << '\n';
std::cout << " output_file Pack file name (default: resources.pack)" << '\n';
}
void listPackContents(const std::string& packFile) {
ResourcePack pack;
if (!pack.loadPack(packFile)) {
std::cerr << "Error: Cannot open pack file: " << packFile << '\n';
return;
}
auto resources = pack.getResourceList();
std::cout << "Pack file: " << packFile << '\n';
std::cout << "Resources: " << resources.size() << '\n';
std::cout << "Contents:" << '\n';
for (const auto& resource : resources) {
std::cout << " " << resource << '\n';
}
}
int main(int argc, char* argv[]) {
std::string dataDir = "data";
std::string outputFile = "resources.pack";
bool listMode = false;
bool dataDirSet = false;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
showHelp();
return 0;
} else if (arg == "--list") {
listMode = true;
if (i + 1 < argc) {
outputFile = argv[++i];
}
} else if (!arg.empty() && arg[0] != '-') {
if (!dataDirSet) {
dataDir = arg;
dataDirSet = true;
} else {
outputFile = arg;
}
}
}
if (listMode) {
listPackContents(outputFile);
return 0;
}
std::cout << APP_NAME << " - Resource Packer" << '\n';
std::cout << "===============================================" << '\n';
std::cout << "Input directory: " << dataDir << '\n';
std::cout << "Output file: " << outputFile << '\n';
std::cout << '\n';
if (!std::filesystem::exists(dataDir)) {
std::cerr << "Error: Input directory does not exist: " << dataDir << '\n';
return 1;
}
ResourcePack pack;
std::cout << "Scanning and packing resources..." << '\n';
if (!pack.addDirectory(dataDir)) {
std::cerr << "Error: Failed to add directory to pack" << '\n';
return 1;
}
std::cout << "Found " << pack.getResourceCount() << " resources" << '\n';
std::cout << "Saving pack file..." << '\n';
if (!pack.savePack(outputFile)) {
std::cerr << "Error: Failed to save pack file" << '\n';
return 1;
}
std::filesystem::path packPath(outputFile);
auto fileSize = std::filesystem::file_size(packPath);
std::cout << "Pack file created successfully!" << '\n';
std::cout << "File size: " << (fileSize / 1024.0 / 1024.0) << " MB" << '\n';
return 0;
}