Compare commits

...

6 Commits

Author SHA1 Message Date
7f470361cc soport de gamepad per a wasm 2026-04-13 13:20:50 +02:00
d9c41f420b fix: arrancar amb el borde desactivat feia crash al activarlo 2026-04-13 11:57:01 +02:00
023bbb224b arreglat el cuelgue de la precàrrega en wasm i afegit nom del recurs en curs
- CMakeLists.txt (Emscripten): afegit -fexceptions (compile + link) perquè
  fkyaml i altres throws ara es capturen pels try/catch enlloc de cridar
  abort(). També -sASSERTIONS=1 per veure missatges clars d'error en el
  runtime de Emscripten.
- resource_cache: abans de carregar cada recurs, desa el seu nom en
  current_loading_name_ i (en wasm/debug) el repinta immediatament sobre la
  barra de progrés. Ara, si la càrrega es penja en un fitxer concret, el
  nom queda visible en pantalla i ajuda a diagnosticar el problema.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 09:52:28 +02:00
70cfe5245d afegit suport Emscripten/WebAssembly al build system
- CMakeLists.txt: branca EMSCRIPTEN amb SDL3 via FetchContent, preload de data/
  config/ i gamecontrollerdb.txt, WebGL2, EMSCRIPTEN_BUILD define i sortida .html.
  Exclou sdl3gpu_shader (no soportat a WebGL2) i el pack_tool en wasm.
- Makefile: target wasm via Docker emscripten/emsdk, build a build/wasm i
  sortida a dist/wasm (.html .js .wasm .data).
- director.cpp: createSystemFolder utilitza MEMFS en wasm (sense pwd.h/unistd.h),
  executable_path buit, dev-mode forçat (filesystem preload, no pack), windowed.
- screen.cpp: initShaders és no-op en wasm (SDL3 GPU no suportat a WebGL2).
- global_inputs.cpp: handleQuit és no-op en wasm (no es pot eixir del joc).
- Director::handleEvent ignora SDL_EVENT_QUIT en wasm.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 09:02:00 +02:00
c32a880b6a refactor: migració a l'arquitectura SDL3 Callback API
Substitueix el bucle blocant main() → Director::run() → escena::run() per
SDL_AppInit/Iterate/Event/Quit. Cada escena implementa ara iterate() (un frame)
i handleEvent() (un event) sota una interfície base Scene.

- Director gestiona l'escena activa i les transicions via switchToActiveScene()
- Setup/cleanup que estava al voltant del while de run() mogut a ctor/dtor
  (música de Game/Ending/Ending2, volum de LoadingScreen)
- GlobalEvents ja no processa SDL_EVENT_QUIT (ho fa Director::handleEvent)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 08:49:19 +02:00
714de067c8 corregit makefile per a windows 2026-04-05 18:27:51 +02:00
33 changed files with 633 additions and 433 deletions

View File

@@ -127,11 +127,27 @@ set(DEBUG_SOURCES
) )
# Configuración de SDL3 # Configuración de SDL3
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) if(EMSCRIPTEN)
message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}") # 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.2.12
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)
message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}")
endif()
# --- SHADER COMPILATION (Linux/Windows only - macOS uses Metal) --- # --- SHADER COMPILATION (Linux/Windows only - macOS usa Metal, Emscripten no els necessita) ---
if(NOT APPLE) if(NOT APPLE AND NOT EMSCRIPTEN)
find_program(GLSLC_EXE NAMES glslc) find_program(GLSLC_EXE NAMES glslc)
set(SHADERS_DIR "${CMAKE_SOURCE_DIR}/data/shaders") set(SHADERS_DIR "${CMAKE_SOURCE_DIR}/data/shaders")
@@ -196,10 +212,15 @@ else()
endif() endif()
# --- 2. AÑADIR EJECUTABLE --- # --- 2. AÑADIR EJECUTABLE ---
add_executable(${PROJECT_NAME} ${APP_SOURCES} ${RENDERING_SOURCES}) if(EMSCRIPTEN)
# En Emscripten no compilem sdl3gpu_shader (SDL3 GPU no està suportat a WebGL2)
add_executable(${PROJECT_NAME} ${APP_SOURCES})
else()
add_executable(${PROJECT_NAME} ${APP_SOURCES} ${RENDERING_SOURCES})
endif()
# Shaders deben compilarse antes que el ejecutable (Linux/Windows con glslc) # Shaders deben compilarse antes que el ejecutable (Linux/Windows con glslc)
if(NOT APPLE AND GLSLC_EXE) if(NOT APPLE AND NOT EMSCRIPTEN AND GLSLC_EXE)
add_dependencies(${PROJECT_NAME} shaders) add_dependencies(${PROJECT_NAME} shaders)
endif() endif()
@@ -243,12 +264,32 @@ elseif(APPLE)
-rpath @executable_path/../Frameworks/ -rpath @executable_path/../Frameworks/
) )
endif() endif()
elseif(EMSCRIPTEN)
target_compile_definitions(${PROJECT_NAME} PRIVATE EMSCRIPTEN_BUILD)
# -fexceptions: habilita excepcions C++ (fkyaml, std::runtime_error...) — sense això qualsevol throw crida abort()
target_compile_options(${PROJECT_NAME} PRIVATE -fexceptions)
target_link_options(${PROJECT_NAME} PRIVATE
"SHELL:--preload-file ${CMAKE_SOURCE_DIR}/data@/data"
"SHELL:--preload-file ${CMAKE_SOURCE_DIR}/config@/config"
"SHELL:--preload-file ${CMAKE_SOURCE_DIR}/gamecontrollerdb.txt@/gamecontrollerdb.txt"
-fexceptions
-sALLOW_MEMORY_GROWTH=1
-sMAX_WEBGL_VERSION=2
-sINITIAL_MEMORY=67108864
-sASSERTIONS=1
# ASYNCIFY només per permetre emscripten_sleep(0) durant la precàrrega de recursos
# (el bucle principal del joc ja usa SDL3 Callback API, no depèn d'ASYNCIFY).
-sASYNCIFY=1
)
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)
endif() endif()
# Especificar la ubicación del ejecutable # Especificar la ubicación del ejecutable (en desktop; a wasm queda a build/wasm/)
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}) if(NOT EMSCRIPTEN)
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()
# --- 5. STATIC ANALYSIS TARGETS --- # --- 5. STATIC ANALYSIS TARGETS ---
@@ -315,29 +356,31 @@ else()
message(STATUS "clang-format no encontrado - targets 'format' y 'format-check' no disponibles") message(STATUS "clang-format no encontrado - targets 'format' y 'format-check' no disponibles")
endif() endif()
# --- 6. PACK RESOURCES TARGETS --- # --- 6. PACK RESOURCES TARGETS (no en Emscripten: s'utilitza --preload-file) ---
set(PACK_TOOL_SOURCES if(NOT EMSCRIPTEN)
${CMAKE_SOURCE_DIR}/tools/pack_resources/pack_resources.cpp set(PACK_TOOL_SOURCES
${CMAKE_SOURCE_DIR}/source/core/resources/resource_pack.cpp ${CMAKE_SOURCE_DIR}/tools/pack_resources/pack_resources.cpp
) ${CMAKE_SOURCE_DIR}/source/core/resources/resource_pack.cpp
)
add_executable(pack_tool ${PACK_TOOL_SOURCES}) add_executable(pack_tool ${PACK_TOOL_SOURCES})
target_include_directories(pack_tool PRIVATE ${CMAKE_SOURCE_DIR}/source) target_include_directories(pack_tool PRIVATE ${CMAKE_SOURCE_DIR}/source)
set_target_properties(pack_tool PROPERTIES set_target_properties(pack_tool PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/tools/pack_resources RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/tools/pack_resources
) )
file(GLOB_RECURSE DATA_FILES "${CMAKE_SOURCE_DIR}/data/*") file(GLOB_RECURSE DATA_FILES "${CMAKE_SOURCE_DIR}/data/*")
add_custom_command( add_custom_command(
OUTPUT "${CMAKE_SOURCE_DIR}/resources.pack" OUTPUT "${CMAKE_SOURCE_DIR}/resources.pack"
COMMAND $<TARGET_FILE:pack_tool> COMMAND $<TARGET_FILE:pack_tool>
"${CMAKE_SOURCE_DIR}/data" "${CMAKE_SOURCE_DIR}/data"
"${CMAKE_SOURCE_DIR}/resources.pack" "${CMAKE_SOURCE_DIR}/resources.pack"
DEPENDS pack_tool ${DATA_FILES} DEPENDS pack_tool ${DATA_FILES}
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
COMMENT "Generando resources.pack desde data/..." COMMENT "Generando resources.pack desde data/..."
) )
add_custom_target(pack DEPENDS "${CMAKE_SOURCE_DIR}/resources.pack") add_custom_target(pack DEPENDS "${CMAKE_SOURCE_DIR}/resources.pack")
add_dependencies(${PROJECT_NAME} pack) add_dependencies(${PROJECT_NAME} pack)
endif()

View File

@@ -54,9 +54,11 @@ endif
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
WIN_TARGET_FILE := $(DIR_BIN)$(APP_NAME) WIN_TARGET_FILE := $(DIR_BIN)$(APP_NAME)
WIN_RELEASE_FILE := $(RELEASE_FOLDER)/$(APP_NAME) WIN_RELEASE_FILE := $(RELEASE_FOLDER)/$(APP_NAME)
WIN_RELEASE_FILE_PS := $(subst ','',$(WIN_RELEASE_FILE))
else else
WIN_TARGET_FILE := $(TARGET_FILE) WIN_TARGET_FILE := $(TARGET_FILE)
WIN_RELEASE_FILE := $(RELEASE_FILE) WIN_RELEASE_FILE := $(RELEASE_FILE)
WIN_RELEASE_FILE_PS := $(WIN_RELEASE_FILE)
endif endif
# ============================================================================== # ==============================================================================
@@ -148,7 +150,7 @@ windows_release:
@powershell -Command "Copy-Item 'README.md' -Destination '$(RELEASE_FOLDER)'" @powershell -Command "Copy-Item 'README.md' -Destination '$(RELEASE_FOLDER)'"
@powershell -Command "Copy-Item 'gamecontrollerdb.txt' -Destination '$(RELEASE_FOLDER)'" @powershell -Command "Copy-Item 'gamecontrollerdb.txt' -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_PS).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
@@ -283,6 +285,39 @@ linux_release:
# Elimina la carpeta temporal # Elimina la carpeta temporal
$(RMDIR) "$(RELEASE_FOLDER)" $(RMDIR) "$(RELEASE_FOLDER)"
# ==============================================================================
# COMPILACIÓN PARA WEBASSEMBLY (requiere Docker)
# ==============================================================================
wasm:
@echo "Compilando para WebAssembly - Version: $(VERSION)"
docker run --rm \
-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/"
# Versió Debug del build wasm: arrenca directament a la GAME (sense logo/loading/title)
# i activa l'editor i la consola. Sortida a dist/wasm_debug/.
wasm_debug:
@echo "Compilando WebAssembly Debug - Version: $(VERSION)"
docker run --rm \
-v $(DIR_ROOT):/src \
-w /src \
emscripten/emsdk:latest \
bash -c "emcmake cmake -S . -B build/wasm_debug -DCMAKE_BUILD_TYPE=Debug && cmake --build build/wasm_debug"
$(MKDIR) "$(DIST_DIR)/wasm_debug"
cp build/wasm_debug/$(TARGET_NAME).html $(DIST_DIR)/wasm_debug/
cp build/wasm_debug/$(TARGET_NAME).js $(DIST_DIR)/wasm_debug/
cp build/wasm_debug/$(TARGET_NAME).wasm $(DIST_DIR)/wasm_debug/
cp build/wasm_debug/$(TARGET_NAME).data $(DIST_DIR)/wasm_debug/
@echo "Output: $(DIST_DIR)/wasm_debug/"
# ============================================================================== # ==============================================================================
# REGLAS ESPECIALES # REGLAS ESPECIALES
# ============================================================================== # ==============================================================================
@@ -304,6 +339,8 @@ 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 - Crear release per a WebAssembly (requereix Docker)"
@echo " make wasm_debug - Crear build Debug per a WebAssembly (entra directe a la GAME)"
@echo "" @echo ""
@echo " Herramientas:" @echo " Herramientas:"
@echo " make compile_shaders - Compilar shaders SPIR-V" @echo " make compile_shaders - Compilar shaders SPIR-V"
@@ -314,4 +351,4 @@ help:
@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 compile_shaders pack_tool resources.pack show_version help .PHONY: all debug release windows_release macos_release linux_release wasm wasm_debug compile_shaders pack_tool resources.pack show_version help

View File

@@ -8,6 +8,7 @@ title:
keyboard: "2. REDEFINIR TECLES" keyboard: "2. REDEFINIR TECLES"
joystick: "3. REDEFINIR MANDO" joystick: "3. REDEFINIR MANDO"
projects: "4. PROJECTES" projects: "4. PROJECTES"
press_to_play: "PREM PER JUGAR"
keys: keys:
prompt0: "PREM UNA TECLA PER A ESQUERRA" prompt0: "PREM UNA TECLA PER A ESQUERRA"
prompt1: "PREM UNA TECLA PER A DRETA" prompt1: "PREM UNA TECLA PER A DRETA"
@@ -103,6 +104,8 @@ achievements:
ui: ui:
press_again_menu: "PREM DE NOU PER TORNAR AL MENÚ" press_again_menu: "PREM DE NOU PER TORNAR AL MENÚ"
press_again_exit: "PREM DE NOU PER EIXIR" press_again_exit: "PREM DE NOU PER EIXIR"
gamepad_connected: "CONNECTAT"
gamepad_disconnected: "DESCONNECTAT"
border_enabled: "VORA ACTIVADA" border_enabled: "VORA ACTIVADA"
border_disabled: "VORA DESACTIVADA" border_disabled: "VORA DESACTIVADA"
fullscreen_enabled: "PANTALLA COMPLETA ACTIVADA" fullscreen_enabled: "PANTALLA COMPLETA ACTIVADA"

View File

@@ -8,6 +8,7 @@ title:
keyboard: "2. REDEFINE KEYBOARD" keyboard: "2. REDEFINE KEYBOARD"
joystick: "3. REDEFINE JOYSTICK" joystick: "3. REDEFINE JOYSTICK"
projects: "4. PROJECTS" projects: "4. PROJECTS"
press_to_play: "PRESS TO PLAY"
keys: keys:
prompt0: "PRESS KEY FOR LEFT" prompt0: "PRESS KEY FOR LEFT"
prompt1: "PRESS KEY FOR RIGHT" prompt1: "PRESS KEY FOR RIGHT"
@@ -103,6 +104,8 @@ achievements:
ui: ui:
press_again_menu: "PRESS AGAIN TO RETURN TO MENU" press_again_menu: "PRESS AGAIN TO RETURN TO MENU"
press_again_exit: "PRESS AGAIN TO EXIT" press_again_exit: "PRESS AGAIN TO EXIT"
gamepad_connected: "CONNECTED"
gamepad_disconnected: "DISCONNECTED"
border_enabled: "BORDER ENABLED" border_enabled: "BORDER ENABLED"
border_disabled: "BORDER DISABLED" border_disabled: "BORDER DISABLED"
fullscreen_enabled: "FULLSCREEN ENABLED" fullscreen_enabled: "FULLSCREEN ENABLED"

View File

@@ -9,6 +9,7 @@
#include "core/locale/locale.hpp" // Para Locale #include "core/locale/locale.hpp" // Para Locale
#include "core/rendering/render_info.hpp" // Para RenderInfo #include "core/rendering/render_info.hpp" // Para RenderInfo
#include "core/rendering/screen.hpp" // Para Screen #include "core/rendering/screen.hpp" // Para Screen
#include "core/system/global_events.hpp" // Para GlobalEvents::consumeGamepadButtonPressed
#include "game/options.hpp" // Para Options, options, OptionsVideo, Section #include "game/options.hpp" // Para Options, options, OptionsVideo, Section
#include "game/scene_manager.hpp" // Para SceneManager #include "game/scene_manager.hpp" // Para SceneManager
#include "game/ui/console.hpp" // Para Console #include "game/ui/console.hpp" // Para Console
@@ -20,7 +21,8 @@ namespace GlobalInputs {
// Funciones internas // Funciones internas
namespace { namespace {
void handleQuit() { void handleQuit() {
// En la escena GAME el comportamiento es siempre el mismo (con o sin modo kiosko) // En la escena GAME el comportamiento es siempre el mismo (con o sin modo kiosko):
// Escape torna al menu principal. Això també és vàlid en la versió web.
if (SceneManager::current == SceneManager::Scene::GAME) { if (SceneManager::current == SceneManager::Scene::GAME) {
const std::string CODE = "PRESS AGAIN TO RETURN TO MENU"; const std::string CODE = "PRESS AGAIN TO RETURN TO MENU";
if (stringInVector(Notifier::get()->getCodes(), CODE)) { if (stringInVector(Notifier::get()->getCodes(), CODE)) {
@@ -41,6 +43,11 @@ namespace GlobalInputs {
return; return;
} }
#ifdef __EMSCRIPTEN__
// A la versió web no es pot eixir del joc des de fora de l'escena GAME
// (el navegador gestiona la pestanya; Escape no tanca res).
return;
#else
// Comportamiento normal fuera del modo kiosko // Comportamiento normal fuera del modo kiosko
const std::string CODE = "PRESS AGAIN TO EXIT"; const std::string CODE = "PRESS AGAIN TO EXIT";
if (stringInVector(Notifier::get()->getCodes(), CODE)) { if (stringInVector(Notifier::get()->getCodes(), CODE)) {
@@ -48,6 +55,7 @@ namespace GlobalInputs {
} else { } else {
Notifier::get()->show({Locale::get()->get("ui.press_again_exit")}, Notifier::Style::DEFAULT, -1, true, CODE); // NOLINT(readability-static-accessed-through-instance) Notifier::get()->show({Locale::get()->get("ui.press_again_exit")}, Notifier::Style::DEFAULT, -1, true, CODE); // NOLINT(readability-static-accessed-through-instance)
} }
#endif // __EMSCRIPTEN__
} }
void handleSkipSection() { void handleSkipSection() {
@@ -145,6 +153,12 @@ namespace GlobalInputs {
// Detecta qué acción global ha sido presionada (si alguna) // Detecta qué acción global ha sido presionada (si alguna)
auto getPressedAction() -> InputAction { // NOLINT(readability-function-cognitive-complexity) auto getPressedAction() -> InputAction { // NOLINT(readability-function-cognitive-complexity)
// Qualsevol botó del comandament actua com a ACCEPT (saltar escenes
// d'attract mode: logo, loading, credits, demo, ending...). Es prioritza
// sobre EXIT perquè s'envia com a flag d'event, no com a check d'acció.
if (GlobalEvents::consumeGamepadButtonPressed()) {
return InputAction::ACCEPT;
}
if (Input::get()->checkAction(InputAction::EXIT, Input::DO_NOT_ALLOW_REPEAT)) { if (Input::get()->checkAction(InputAction::EXIT, Input::DO_NOT_ALLOW_REPEAT)) {
return InputAction::EXIT; return InputAction::EXIT;
} }

View File

@@ -421,7 +421,13 @@ auto Input::addGamepad(int device_index) -> std::string { // NOLINT(readability
auto name = gamepad->name; auto name = gamepad->name;
std::cout << "Gamepad connected (" << name << ")" << '\n'; std::cout << "Gamepad connected (" << name << ")" << '\n';
gamepads_.push_back(std::move(gamepad)); gamepads_.push_back(std::move(gamepad));
return name + " CONNECTED";
// Aplica els bindings d'Options al nou gamepad (en hot-plug/wasm el ctor
// ja ha cridat applyGamepadBindingsFromOptions però llavors gamepads_
// estava buit i no s'ha fet res).
applyGamepadBindingsFromOptions();
return name;
} }
auto Input::removeGamepad(SDL_JoystickID id) -> std::string { // NOLINT(readability-convert-member-functions-to-static) auto Input::removeGamepad(SDL_JoystickID id) -> std::string { // NOLINT(readability-convert-member-functions-to-static)
@@ -433,7 +439,7 @@ auto Input::removeGamepad(SDL_JoystickID id) -> std::string { // NOLINT(readabi
std::string name = (*it)->name; std::string name = (*it)->name;
std::cout << "Gamepad disconnected (" << name << ")" << '\n'; std::cout << "Gamepad disconnected (" << name << ")" << '\n';
gamepads_.erase(it); gamepads_.erase(it);
return name + " DISCONNECTED"; return name;
} }
std::cerr << "No se encontró el gamepad con ID " << id << '\n'; std::cerr << "No se encontró el gamepad con ID " << id << '\n';
return {}; return {};

View File

@@ -11,17 +11,19 @@
#include <iterator> // Para istreambuf_iterator, operator== #include <iterator> // Para istreambuf_iterator, operator==
#include <string> // Para char_traits, string, operator+, operator== #include <string> // Para char_traits, string, operator+, operator==
#include "core/input/mouse.hpp" // Para updateCursorVisibility #include "core/input/mouse.hpp" // Para updateCursorVisibility
#include "core/rendering/render_info.hpp" // Para RenderInfo #include "core/rendering/render_info.hpp" // Para RenderInfo
#include "core/rendering/sdl3gpu/sdl3gpu_shader.hpp" // Para SDL3GPUShader #ifndef __EMSCRIPTEN__
#include "core/rendering/surface.hpp" // Para Surface, readPalFile #include "core/rendering/sdl3gpu/sdl3gpu_shader.hpp" // Para SDL3GPUShader (no suportat a WebGL2)
#include "core/rendering/text.hpp" // Para Text #endif
#include "core/resources/resource_cache.hpp" // Para Resource #include "core/rendering/surface.hpp" // Para Surface, readPalFile
#include "core/resources/resource_helper.hpp" // Para ResourceHelper #include "core/rendering/text.hpp" // Para Text
#include "core/resources/resource_list.hpp" // Para Asset, AssetType #include "core/resources/resource_cache.hpp" // Para Resource
#include "game/options.hpp" // Para Options, options, OptionsVideo, Border #include "core/resources/resource_helper.hpp" // Para ResourceHelper
#include "game/ui/console.hpp" // Para Console #include "core/resources/resource_list.hpp" // Para Asset, AssetType
#include "game/ui/notifier.hpp" // Para Notifier #include "game/options.hpp" // Para Options, options, OptionsVideo, Border
#include "game/ui/console.hpp" // Para Console
#include "game/ui/notifier.hpp" // Para Notifier
// [SINGLETON] // [SINGLETON]
Screen* Screen::screen = nullptr; Screen* Screen::screen = nullptr;
@@ -293,16 +295,16 @@ void Screen::adjustWindowSize() {
window_width_ = Options::game.width + (Options::video.border.enabled ? Options::video.border.width * 2 : 0); window_width_ = Options::game.width + (Options::video.border.enabled ? Options::video.border.width * 2 : 0);
window_height_ = Options::game.height + (Options::video.border.enabled ? Options::video.border.height * 2 : 0); window_height_ = Options::game.height + (Options::video.border.enabled ? Options::video.border.height * 2 : 0);
// Reservamos memoria una sola vez. // border_surface_ sempre té el tamany complet del borde (game + 2*border_w/h),
// Si el buffer es más pequeño que la superficie, crash asegurado. // independentment de si el borde està visible o no. El buffer ARGB que l'ombra
border_pixel_buffer_.resize(static_cast<size_t>(window_width_ * window_height_)); // ha de ser ALMENYS tan gran com la surface; si no, toARGBBuffer() escriu fora
// de bounds i corromp el heap (bug latent a desktop fins a disparar-lo un toggleBorder).
const size_t FULL_BORDER_BUFFER_SIZE =
static_cast<size_t>(Options::game.width + (Options::video.border.width * 2)) *
static_cast<size_t>(Options::game.height + (Options::video.border.height * 2));
border_pixel_buffer_.resize(FULL_BORDER_BUFFER_SIZE);
game_pixel_buffer_.resize(static_cast<size_t>(Options::game.width * Options::game.height)); game_pixel_buffer_.resize(static_cast<size_t>(Options::game.width * Options::game.height));
// border_pixel_buffer_ es el buffer que se sube a la GPU (tamaño total ventana).
if (Options::video.border.enabled) {
border_pixel_buffer_.resize(static_cast<size_t>(window_width_ * window_height_));
}
// Lógica de centrado y redimensionado de ventana SDL // Lógica de centrado y redimensionado de ventana SDL
if (static_cast<int>(Options::video.fullscreen) == 0) { if (static_cast<int>(Options::video.fullscreen) == 0) {
int old_w; int old_w;
@@ -605,6 +607,10 @@ void Screen::nextShader() {
// El device GPU se crea siempre (independientemente de postfx) para evitar // El device GPU se crea siempre (independientemente de postfx) para evitar
// conflictos SDL_Renderer/SDL_GPU al hacer toggle F4 en Windows/Vulkan. // conflictos SDL_Renderer/SDL_GPU al hacer toggle F4 en Windows/Vulkan.
void Screen::initShaders() { void Screen::initShaders() {
#ifdef __EMSCRIPTEN__
// A WebGL2 no hi ha SDL3 GPU, el render va per SDL_Renderer sense shaders.
shader_backend_.reset();
#else
SDL_Texture* tex = Options::video.border.enabled ? border_texture_ : game_texture_; SDL_Texture* tex = Options::video.border.enabled ? border_texture_ : game_texture_;
if (!shader_backend_) { if (!shader_backend_) {
@@ -633,6 +639,7 @@ void Screen::initShaders() {
if (Options::video.shader.current_shader == Rendering::ShaderType::CRTPI) { if (Options::video.shader.current_shader == Rendering::ShaderType::CRTPI) {
applyCurrentCrtPiPreset(); applyCurrentCrtPiPreset();
} }
#endif
} }
// Obtiene información sobre la pantalla // Obtiene información sobre la pantalla

View File

@@ -2,6 +2,10 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h> // Para emscripten_sleep
#endif
#include <algorithm> // Para find_if #include <algorithm> // Para find_if
#include <cstdlib> // Para exit, size_t #include <cstdlib> // Para exit, size_t
#include <fstream> // Para ifstream, istreambuf_iterator #include <fstream> // Para ifstream, istreambuf_iterator
@@ -226,6 +230,7 @@ namespace Resource {
for (const auto& l : list) { for (const auto& l : list) {
try { try {
auto name = getFileName(l); auto name = getFileName(l);
setCurrentLoading(name);
JA_Sound_t* sound = nullptr; JA_Sound_t* sound = nullptr;
// Try loading from resource pack first // Try loading from resource pack first
@@ -261,6 +266,7 @@ namespace Resource {
for (const auto& l : list) { for (const auto& l : list) {
try { try {
auto name = getFileName(l); auto name = getFileName(l);
setCurrentLoading(name);
JA_Music_t* music = nullptr; JA_Music_t* music = nullptr;
// Try loading from resource pack first // Try loading from resource pack first
@@ -296,6 +302,7 @@ namespace Resource {
for (const auto& l : list) { for (const auto& l : list) {
try { try {
auto name = getFileName(l); auto name = getFileName(l);
setCurrentLoading(name);
surfaces_.emplace_back(SurfaceResource{.name = name, .surface = std::make_shared<Surface>(l)}); surfaces_.emplace_back(SurfaceResource{.name = name, .surface = std::make_shared<Surface>(l)});
surfaces_.back().surface->setTransparentColor(0); surfaces_.back().surface->setTransparentColor(0);
updateLoadingProgress(); updateLoadingProgress();
@@ -323,6 +330,7 @@ namespace Resource {
for (const auto& l : list) { for (const auto& l : list) {
try { try {
auto name = getFileName(l); auto name = getFileName(l);
setCurrentLoading(name);
palettes_.emplace_back(ResourcePalette{.name = name, .palette = readPalFile(l)}); palettes_.emplace_back(ResourcePalette{.name = name, .palette = readPalFile(l)});
updateLoadingProgress(); updateLoadingProgress();
} catch (const std::exception& e) { } catch (const std::exception& e) {
@@ -340,6 +348,7 @@ namespace Resource {
for (const auto& l : list) { for (const auto& l : list) {
try { try {
auto name = getFileName(l); auto name = getFileName(l);
setCurrentLoading(name);
text_files_.emplace_back(TextFileResource{.name = name, .text_file = Text::loadTextFile(l)}); text_files_.emplace_back(TextFileResource{.name = name, .text_file = Text::loadTextFile(l)});
updateLoadingProgress(); updateLoadingProgress();
} catch (const std::exception& e) { } catch (const std::exception& e) {
@@ -357,6 +366,7 @@ namespace Resource {
for (const auto& l : list) { for (const auto& l : list) {
try { try {
auto name = getFileName(l); auto name = getFileName(l);
setCurrentLoading(name);
// Cargar bytes del archivo YAML sin parsear (carga lazy) // Cargar bytes del archivo YAML sin parsear (carga lazy)
auto yaml_bytes = Helper::loadFile(l); auto yaml_bytes = Helper::loadFile(l);
@@ -383,6 +393,7 @@ namespace Resource {
for (const auto& l : list) { for (const auto& l : list) {
try { try {
auto name = getFileName(l); auto name = getFileName(l);
setCurrentLoading(name);
rooms_.emplace_back(RoomResource{.name = name, .room = std::make_shared<Room::Data>(Room::loadYAML(l))}); rooms_.emplace_back(RoomResource{.name = name, .room = std::make_shared<Room::Data>(Room::loadYAML(l))});
printWithDots("Room : ", name, "[ LOADED ]"); printWithDots("Room : ", name, "[ LOADED ]");
updateLoadingProgress(); updateLoadingProgress();
@@ -501,9 +512,37 @@ namespace Resource {
SDL_FRect rect_full = {.x = X_PADDING, .y = BAR_POSITION, .w = FULL_BAR_WIDTH, .h = BAR_HEIGHT}; SDL_FRect rect_full = {.x = X_PADDING, .y = BAR_POSITION, .w = FULL_BAR_WIDTH, .h = BAR_HEIGHT};
surface->fillRect(&rect_full, BAR_COLOR); surface->fillRect(&rect_full, BAR_COLOR);
#if defined(__EMSCRIPTEN__) || defined(_DEBUG)
// Mostra el nom del recurs que està a punt de carregar-se, centrat sobre la barra
if (!current_loading_name_.empty()) {
const float TEXT_Y = BAR_POSITION - static_cast<float>(TEXT_HEIGHT) - 2.0F;
loading_text_->writeColored(
CENTER_X - (loading_text_->length(current_loading_name_) / 2),
static_cast<int>(TEXT_Y),
current_loading_name_,
LOADING_TEXT_COLOR);
}
#endif
Screen::get()->render(); Screen::get()->render();
} }
// Desa el nom del recurs que s'està a punt de carregar i repinta immediatament.
// A wasm/debug serveix per veure exactament en quin fitxer es penja la càrrega.
void Cache::setCurrentLoading(const std::string& name) {
current_loading_name_ = name;
#if defined(__EMSCRIPTEN__) || defined(_DEBUG)
renderProgress();
checkEvents();
#endif
#ifdef __EMSCRIPTEN__
// Cedeix el control al navegador perquè pinte el canvas i processe
// events. Sense això, el thread principal queda bloquejat durant tota
// la precàrrega i el jugador només veu pantalla negra.
emscripten_sleep(0);
#endif
}
// Comprueba los eventos de la pantalla de carga // Comprueba los eventos de la pantalla de carga
void Cache::checkEvents() { void Cache::checkEvents() {
SDL_Event event; SDL_Event event;

View File

@@ -68,6 +68,7 @@ namespace Resource {
void renderProgress(); void renderProgress();
static void checkEvents(); static void checkEvents();
void updateLoadingProgress(int steps = 5); void updateLoadingProgress(int steps = 5);
void setCurrentLoading(const std::string& name); // Desa el nom del recurs en curs i repinta (wasm/debug)
// Helper para mensajes de error de carga // Helper para mensajes de error de carga
[[noreturn]] static void throwLoadError(const std::string& asset_type, const std::string& file_path, const std::exception& e); [[noreturn]] static void throwLoadError(const std::string& asset_type, const std::string& file_path, const std::exception& e);
@@ -91,6 +92,7 @@ namespace Resource {
ResourceCount count_{}; // Contador de recursos ResourceCount count_{}; // Contador de recursos
std::shared_ptr<Text> loading_text_; // Texto para la pantalla de carga std::shared_ptr<Text> loading_text_; // Texto para la pantalla de carga
std::string current_loading_name_; // Nom del recurs que s'està a punt de carregar (debug/wasm)
}; };
} // namespace Resource } // namespace Resource

View File

@@ -40,7 +40,7 @@
#include "game/editor/map_editor.hpp" // Para MapEditor #include "game/editor/map_editor.hpp" // Para MapEditor
#endif #endif
#ifndef _WIN32 #if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
#include <pwd.h> #include <pwd.h>
#endif #endif
@@ -48,12 +48,17 @@
Director::Director() { Director::Director() {
std::cout << "Game start" << '\n'; std::cout << "Game start" << '\n';
#ifdef __EMSCRIPTEN__
// En Emscripten els assets estan al root del filesystem virtual (/data, /config)
executable_path_ = "";
#else
// Obtiene la ruta del ejecutable // Obtiene la ruta del ejecutable
std::string base = SDL_GetBasePath(); std::string base = SDL_GetBasePath();
if (!base.empty() && base.back() == '/') { if (!base.empty() && base.back() == '/') {
base.pop_back(); base.pop_back();
} }
executable_path_ = base; executable_path_ = base;
#endif
// Crea la carpeta del sistema donde guardar datos // Crea la carpeta del sistema donde guardar datos
createSystemFolder("jailgames"); createSystemFolder("jailgames");
@@ -83,7 +88,7 @@ Director::Director() {
// Preparar ruta al pack (en macOS bundle está en Contents/Resources/) // Preparar ruta al pack (en macOS bundle está en Contents/Resources/)
std::string pack_path = executable_path_ + PREFIX + "/resources.pack"; std::string pack_path = executable_path_ + PREFIX + "/resources.pack";
#ifdef RELEASE_BUILD #if defined(RELEASE_BUILD) && !defined(__EMSCRIPTEN__)
// ============================================================ // ============================================================
// RELEASE BUILD: Pack-first architecture // RELEASE BUILD: Pack-first architecture
// ============================================================ // ============================================================
@@ -141,6 +146,17 @@ Director::Director() {
Options::setConfigFile(Resource::List::get()->get("config.yaml")); // NOLINT(readability-static-accessed-through-instance) Options::setConfigFile(Resource::List::get()->get("config.yaml")); // NOLINT(readability-static-accessed-through-instance)
Options::loadFromFile(); Options::loadFromFile();
#ifdef __EMSCRIPTEN__
// A la versió web el navegador gestiona la finestra: forcem zoom x3
// perquè la textura 256x192 no es vegi minúscula al canvas HTML,
// i desactivem el borde per aprofitar al màxim l'espai del canvas.
Options::video.fullscreen = false;
Options::window.zoom = 4;
Options::video.border.enabled = true;
Options::video.border.height = 8;
Options::video.border.width = 8;
#endif
// Configura la ruta y carga los presets de PostFX // Configura la ruta y carga los presets de PostFX
Options::setPostFXFile(Resource::List::get()->get("postfx.yaml")); // NOLINT(readability-static-accessed-through-instance) Options::setPostFXFile(Resource::List::get()->get("postfx.yaml")); // NOLINT(readability-static-accessed-through-instance)
Options::loadPostFXFromFile(); Options::loadPostFXFromFile();
@@ -168,7 +184,7 @@ Director::Director() {
Screen::get()->setNotificationsEnabled(true); Screen::get()->setNotificationsEnabled(true);
// Special handling for gamecontrollerdb.txt - SDL needs filesystem path // Special handling for gamecontrollerdb.txt - SDL needs filesystem path
#ifdef RELEASE_BUILD #if defined(RELEASE_BUILD) && !defined(__EMSCRIPTEN__)
// In release, construct the path manually (not from Asset which has empty executable_path) // In release, construct the path manually (not from Asset which has empty executable_path)
std::string gamecontroller_db = executable_path_ + PREFIX + "/gamecontrollerdb.txt"; std::string gamecontroller_db = executable_path_ + PREFIX + "/gamecontrollerdb.txt";
Input::init(gamecontroller_db); Input::init(gamecontroller_db);
@@ -183,16 +199,22 @@ Director::Director() {
#ifdef _DEBUG #ifdef _DEBUG
Debug::init(); Debug::init();
#ifdef __EMSCRIPTEN__
// A wasm el debug.yaml viu a SYSTEM_FOLDER (MEMFS no persistent) i no està
// disponible. Saltem el loadFromFile i entrem directament a la GAME.
SceneManager::current = SceneManager::Scene::GAME;
#else
Debug::get()->setDebugFile(Resource::List::get()->get("debug.yaml")); Debug::get()->setDebugFile(Resource::List::get()->get("debug.yaml"));
Debug::get()->loadFromFile(); Debug::get()->loadFromFile();
SceneManager::current = Debug::get()->getInitialScene(); SceneManager::current = Debug::get()->getInitialScene();
#endif
MapEditor::init(); MapEditor::init();
#endif #endif
std::cout << "\n"; // Fin de inicialización de sistemas std::cout << "\n"; // Fin de inicialización de sistemas
// Inicializa el sistema de localización (antes de Cheevos que usa textos traducidos) // Inicializa el sistema de localización (antes de Cheevos que usa textos traducidos)
#ifdef RELEASE_BUILD #if defined(RELEASE_BUILD) && !defined(__EMSCRIPTEN__)
{ {
// En release el locale está en el pack, no en el filesystem // En release el locale está en el pack, no en el filesystem
std::string locale_key = Resource::List::get()->get(Options::language + ".yaml"); // NOLINT(readability-static-accessed-through-instance) std::string locale_key = Resource::List::get()->get(Options::language + ".yaml"); // NOLINT(readability-static-accessed-through-instance)
@@ -205,12 +227,15 @@ Director::Director() {
#endif #endif
// Special handling for cheevos.bin - also needs filesystem path // Special handling for cheevos.bin - also needs filesystem path
#ifdef RELEASE_BUILD #if defined(RELEASE_BUILD) && !defined(__EMSCRIPTEN__)
std::string cheevos_path = system_folder_ + "/cheevos.bin"; std::string cheevos_path = system_folder_ + "/cheevos.bin";
Cheevos::init(cheevos_path); Cheevos::init(cheevos_path);
#else #else
Cheevos::init(Resource::List::get()->get("cheevos.bin")); Cheevos::init(Resource::List::get()->get("cheevos.bin"));
#endif #endif
// Construeix la primera escena (LOGO per defecte, o la que digui Debug)
switchToActiveScene();
} }
Director::~Director() { Director::~Director() {
@@ -241,6 +266,12 @@ Director::~Director() {
// Crea la carpeta del sistema donde guardar datos // Crea la carpeta del sistema donde guardar datos
void Director::createSystemFolder(const std::string& folder) { // NOLINT(readability-convert-member-functions-to-static) void Director::createSystemFolder(const std::string& folder) { // NOLINT(readability-convert-member-functions-to-static)
#ifdef __EMSCRIPTEN__
// En Emscripten utilitzem MEMFS (no persistent entre sessions).
// No cal crear directoris: MEMFS els crea automàticament en escriure-hi.
system_folder_ = "/config/" + folder;
return;
#else
#ifdef _WIN32 #ifdef _WIN32
system_folder_ = std::string(getenv("APPDATA")) + "/" + folder; system_folder_ = std::string(getenv("APPDATA")) + "/" + folder;
#elif __APPLE__ #elif __APPLE__
@@ -292,6 +323,7 @@ void Director::createSystemFolder(const std::string& folder) { // NOLINT(readab
} }
} }
} }
#endif // __EMSCRIPTEN__
} }
// Carga la configuración de assets desde assets.yaml // Carga la configuración de assets desde assets.yaml
@@ -311,118 +343,93 @@ void Director::setFileList() { // NOLINT(readability-convert-member-functions-t
Resource::List::get()->loadFromFile(config_path, PREFIX, system_folder_); Resource::List::get()->loadFromFile(config_path, PREFIX, system_folder_);
} }
// Ejecuta la seccion de juego con el logo // Construeix l'escena segons SceneManager::current i la deixa en active_scene_.
void Director::runLogo() { // Substitueix els vells runLogo(), runTitle(), runGame(), etc.
auto logo = std::make_unique<Logo>(); void Director::switchToActiveScene() {
logo->run(); // Si la escena anterior va demanar RESTART_CURRENT, restaurem la que estava activa
} if (SceneManager::current == SceneManager::Scene::RESTART_CURRENT) {
SceneManager::current = SceneManager::scene_before_restart;
// Ejecuta la seccion de juego de la pantalla de carga
void Director::runLoadingScreen() {
auto loading_screen = std::make_unique<LoadingScreen>();
loading_screen->run();
}
// Ejecuta la seccion de juego con el titulo y los menus
void Director::runTitle() {
auto title = std::make_unique<Title>();
title->run();
}
// Ejecuta la seccion de los creditos del juego
void Director::runCredits() {
auto credits = std::make_unique<Credits>();
credits->run();
}
// Ejecuta la seccion de la demo, donde se ven pantallas del juego
void Director::runDemo() {
auto game = std::make_unique<Game>(Game::Mode::DEMO);
game->run();
}
// Ejecuta la seccion del final del juego
void Director::runEnding() {
auto ending = std::make_unique<Ending>();
ending->run();
}
// Ejecuta la seccion del final del juego
void Director::runEnding2() {
auto ending2 = std::make_unique<Ending2>();
ending2->run();
}
// Ejecuta la seccion del final de la partida
void Director::runGameOver() {
auto game_over = std::make_unique<GameOver>();
game_over->run();
}
// Ejecuta la seccion de juego donde se juega
void Director::runGame() {
Audio::get()->stopMusic();
auto game = std::make_unique<Game>(Game::Mode::GAME);
game->run();
}
auto Director::run() -> int {
// Bucle principal
while (SceneManager::current != SceneManager::Scene::QUIT) {
const SceneManager::Scene ACTIVE = SceneManager::current;
switch (SceneManager::current) {
case SceneManager::Scene::LOGO:
runLogo();
break;
case SceneManager::Scene::LOADING_SCREEN:
runLoadingScreen();
break;
case SceneManager::Scene::TITLE:
runTitle();
break;
case SceneManager::Scene::CREDITS:
runCredits();
break;
case SceneManager::Scene::DEMO:
runDemo();
break;
case SceneManager::Scene::GAME:
runGame();
break;
case SceneManager::Scene::GAME_OVER:
runGameOver();
break;
case SceneManager::Scene::ENDING:
runEnding();
break;
case SceneManager::Scene::ENDING2:
runEnding2();
break;
case SceneManager::Scene::RESTART_CURRENT:
// La escena salió por RESTART_CURRENT → relanzar la escena guardada
SceneManager::current = SceneManager::scene_before_restart;
break;
default:
break;
}
// Si la escena que acaba de correr dejó RESTART_CURRENT pendiente,
// restaurar la escena que estaba activa para relanzarla en la próxima iteración
if (SceneManager::current == SceneManager::Scene::RESTART_CURRENT) {
SceneManager::current = ACTIVE;
}
} }
return 0; // Destrueix l'escena anterior (pot parar música, etc. al seu destructor)
active_scene_.reset();
switch (SceneManager::current) {
case SceneManager::Scene::LOGO:
active_scene_ = std::make_unique<Logo>();
break;
case SceneManager::Scene::LOADING_SCREEN:
active_scene_ = std::make_unique<LoadingScreen>();
break;
case SceneManager::Scene::TITLE:
active_scene_ = std::make_unique<Title>();
break;
case SceneManager::Scene::CREDITS:
active_scene_ = std::make_unique<Credits>();
break;
case SceneManager::Scene::DEMO:
active_scene_ = std::make_unique<Game>(Game::Mode::DEMO);
break;
case SceneManager::Scene::GAME:
Audio::get()->stopMusic();
active_scene_ = std::make_unique<Game>(Game::Mode::GAME);
break;
case SceneManager::Scene::GAME_OVER:
active_scene_ = std::make_unique<GameOver>();
break;
case SceneManager::Scene::ENDING:
active_scene_ = std::make_unique<Ending>();
break;
case SceneManager::Scene::ENDING2:
active_scene_ = std::make_unique<Ending2>();
break;
default:
break;
}
current_scene_ = SceneManager::current;
}
// SDL_AppIterate: executa un frame de l'escena activa
auto Director::iterate() -> SDL_AppResult {
if (SceneManager::current == SceneManager::Scene::QUIT) {
return SDL_APP_SUCCESS;
}
// Si l'escena ha canviat (o s'ha demanat RESTART_CURRENT), canviar-la abans del frame
if (SceneManager::current != current_scene_ || SceneManager::current == SceneManager::Scene::RESTART_CURRENT) {
switchToActiveScene();
}
if (active_scene_) {
active_scene_->iterate();
}
return SDL_APP_CONTINUE;
}
// SDL_AppEvent: despatxa un event a l'escena activa
auto Director::handleEvent(const SDL_Event& event) -> SDL_AppResult {
#ifndef __EMSCRIPTEN__
// A la versió web no tenim event de quit del navegador
if (event.type == SDL_EVENT_QUIT) {
SceneManager::current = SceneManager::Scene::QUIT;
return SDL_APP_SUCCESS;
}
#endif
if (active_scene_) {
active_scene_->handleEvent(event);
}
return SDL_APP_CONTINUE;
} }

View File

@@ -2,29 +2,31 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <memory> // Para unique_ptr
#include <string> // Para string #include <string> // Para string
#include "game/scene_manager.hpp" // Para SceneManager::Scene
#include "game/scenes/scene.hpp" // Para Scene base
class Director { class Director {
public: public:
Director(); // Constructor Director(); // Constructor: inicialitza sistemes i crea l'escena inicial
~Director(); // Destructor ~Director(); // Destructor
static auto run() -> int; // Bucle principal
// SDL3 Callback API: un frame i un event
auto iterate() -> SDL_AppResult;
auto handleEvent(const SDL_Event& event) -> SDL_AppResult;
private: private:
// --- Variables --- // --- Variables ---
std::string executable_path_; // Path del ejecutable std::string executable_path_; // Path del ejecutable
std::string system_folder_; // Carpeta del sistema donde guardar datos std::string system_folder_; // Carpeta del sistema donde guardar datos
std::unique_ptr<Scene> active_scene_; // Escena activa
SceneManager::Scene current_scene_{SceneManager::Scene::LOGO}; // Tipus d'escena activa
// --- Funciones --- // --- Funciones ---
void createSystemFolder(const std::string& folder); // Crea la carpeta del sistema donde guardar datos void createSystemFolder(const std::string& folder); // Crea la carpeta del sistema donde guardar datos
void setFileList(); // Carga la configuración de assets desde assets.yaml void setFileList(); // Carga la configuración de assets desde assets.yaml
static void runLogo(); // Ejecuta la seccion de juego con el logo void switchToActiveScene(); // Construeix l'escena segons SceneManager::current
static void runLoadingScreen(); // Ejecuta la seccion de juego de la pantalla de carga };
static void runTitle(); // Ejecuta la seccion de juego con el titulo y los menus
static void runCredits(); // Ejecuta la seccion de los creditos del juego
static void runDemo(); // Ejecuta la seccion de la demo, donde se ven pantallas del juego
static void runEnding(); // Ejecuta la seccion del final del juego
static void runEnding2(); // Ejecuta la seccion del final del juego
static void runGameOver(); // Ejecuta la seccion del final de la partida
static void runGame(); // Ejecuta la seccion de juego donde se juega
};

View File

@@ -1,23 +1,49 @@
#include "core/system/global_events.hpp" #include "core/system/global_events.hpp"
#include "core/input/input.hpp" // Para Input (gamepad add/remove)
#include "core/input/mouse.hpp" #include "core/input/mouse.hpp"
#include "core/locale/locale.hpp" // Para Locale
#include "game/options.hpp" // Para Options, options, OptionsGame, OptionsAudio #include "game/options.hpp" // Para Options, options, OptionsGame, OptionsAudio
#include "game/scene_manager.hpp" // Para SceneManager
#include "game/ui/console.hpp" // Para Console #include "game/ui/console.hpp" // Para Console
#include "game/ui/notifier.hpp" // Para Notifier
namespace GlobalEvents { namespace GlobalEvents {
// Comprueba los eventos que se pueden producir en cualquier sección del juego
void handle(const SDL_Event& event) {
// Evento de salida de la aplicación
if (event.type == SDL_EVENT_QUIT) {
SceneManager::current = SceneManager::Scene::QUIT;
return;
}
namespace {
// Flag per saber si en aquest frame s'ha rebut un button down del gamepad.
// El consumeix GlobalInputs perquè un botó del comandament salti escenes.
bool gamepad_button_pressed_ = false;
} // namespace
// Comprueba los eventos que se pueden producir en cualquier sección del juego.
// Nota: SDL_EVENT_QUIT el gestiona Director::handleEvent() directament.
void handle(const SDL_Event& event) {
if (event.type == SDL_EVENT_RENDER_DEVICE_RESET || event.type == SDL_EVENT_RENDER_TARGETS_RESET) { if (event.type == SDL_EVENT_RENDER_DEVICE_RESET || event.type == SDL_EVENT_RENDER_TARGETS_RESET) {
// reLoadTextures(); // reLoadTextures();
} }
// Connexió/desconnexió de gamepads: cal enrutar-los a Input perquè
// afegisca el dispositiu a gamepads_. Sense això, en wasm els gamepads
// mai es detecten (la Gamepad API del navegador només els exposa
// després que l'usuari els active, més tard que el discoverGamepads
// inicial). En desktop també arregla la connexió en calent.
if (event.type == SDL_EVENT_GAMEPAD_ADDED || event.type == SDL_EVENT_GAMEPAD_REMOVED) {
if (Input::get() != nullptr) {
std::string name = Input::get()->handleEvent(event);
if (!name.empty() && Notifier::get() != nullptr && Locale::get() != nullptr) {
const std::string KEY = (event.type == SDL_EVENT_GAMEPAD_ADDED)
? "ui.gamepad_connected"
: "ui.gamepad_disconnected";
Notifier::get()->show({name + " " + Locale::get()->get(KEY)});
}
}
}
// Marcar polsació de qualsevol botó del comandament (els consumirà GlobalInputs).
if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
gamepad_button_pressed_ = true;
}
// Enrutar eventos de texto a la consola cuando está activa // Enrutar eventos de texto a la consola cuando está activa
if (Console::get() != nullptr && Console::get()->isActive()) { if (Console::get() != nullptr && Console::get()->isActive()) {
if (event.type == SDL_EVENT_TEXT_INPUT || event.type == SDL_EVENT_KEY_DOWN) { if (event.type == SDL_EVENT_TEXT_INPUT || event.type == SDL_EVENT_KEY_DOWN) {
@@ -28,4 +54,10 @@ namespace GlobalEvents {
Mouse::handleEvent(event); Mouse::handleEvent(event);
} }
auto consumeGamepadButtonPressed() -> bool {
const bool RESULT = gamepad_button_pressed_;
gamepad_button_pressed_ = false;
return RESULT;
}
} // namespace GlobalEvents } // namespace GlobalEvents

View File

@@ -5,4 +5,9 @@
namespace GlobalEvents { namespace GlobalEvents {
// Comprueba los eventos que se pueden producir en cualquier sección del juego // Comprueba los eventos que se pueden producir en cualquier sección del juego
void handle(const SDL_Event& event); void handle(const SDL_Event& event);
// True si en aquest frame s'ha rebut un SDL_EVENT_GAMEPAD_BUTTON_DOWN.
// Es consumeix (i es reseteja) per GlobalInputs::getPressedAction perquè
// qualsevol botó del comandament actuï com a "ACCEPT" (saltar escena).
auto consumeGamepadButtonPressed() -> bool;
} // namespace GlobalEvents } // namespace GlobalEvents

View File

@@ -37,12 +37,9 @@ Credits::Credits()
Audio::get()->playMusic("title.ogg"); // Inicia la musica Audio::get()->playMusic("title.ogg"); // Inicia la musica
} }
// Comprueba el manejador de eventos // Despatx d'un event (SDL3 Callback API)
void Credits::handleEvents() { void Credits::handleEvent(const SDL_Event& event) {
SDL_Event event; GlobalEvents::handle(event);
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
}
} }
// Comprueba las entradas // Comprueba las entradas
@@ -124,8 +121,7 @@ void Credits::update() {
const float DELTA_TIME = delta_timer_->tick(); const float DELTA_TIME = delta_timer_->tick();
total_time_ += DELTA_TIME; // Actualiza el tiempo total total_time_ += DELTA_TIME; // Actualiza el tiempo total
handleEvents(); // Comprueba los eventos handleInput(); // Comprueba las entradas
handleInput(); // Comprueba las entradas
updateState(DELTA_TIME); // Actualiza la máquina de estados updateState(DELTA_TIME); // Actualiza la máquina de estados
@@ -238,10 +234,8 @@ void Credits::render() {
Screen::get()->render(); Screen::get()->render();
} }
// Bucle para el logo del juego // Un frame de l'escena (SDL3 Callback API)
void Credits::run() { void Credits::iterate() {
while (SceneManager::current == SceneManager::Scene::CREDITS) { update();
update(); render();
render();
}
} }

View File

@@ -2,22 +2,25 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <memory> // Para shared_ptr #include <memory> // Para shared_ptr
#include <string> // Para string #include <string> // Para string
#include <vector> // Para vector #include <vector> // Para vector
class AnimatedSprite; // lines 11-11
#include "game/scenes/scene.hpp" // Para Scene
class AnimatedSprite; // lines 11-11
class Surface; class Surface;
class PixelReveal; class PixelReveal;
class DeltaTimer; class DeltaTimer;
class Credits { class Credits : public Scene {
public: public:
// --- Constructor y Destructor --- // --- Constructor y Destructor ---
Credits(); Credits();
~Credits(); // NOLINT(modernize-use-equals-default, performance-trivially-destructible) -- defined in .cpp for unique_ptr with forward declarations ~Credits() override; // NOLINT(modernize-use-equals-default, performance-trivially-destructible) -- defined in .cpp for unique_ptr with forward declarations
// --- Bucle principal --- // --- Bucle principal (SDL3 Callback API) ---
void run(); void iterate() override;
void handleEvent(const SDL_Event& event) override;
private: private:
// --- Tipos anidados --- // --- Tipos anidados ---
@@ -55,7 +58,6 @@ class Credits {
// --- Métodos privados --- // --- Métodos privados ---
void update(); // Actualiza las variables void update(); // Actualiza las variables
void render(); // Dibuja en pantalla void render(); // Dibuja en pantalla
static void handleEvents(); // Comprueba el manejador de eventos
static void handleInput(); // Comprueba las entradas static void handleInput(); // Comprueba las entradas
void updateState(float delta_time); // Actualiza la máquina de estados void updateState(float delta_time); // Actualiza la máquina de estados
void transitionToState(State new_state); // Transición entre estados void transitionToState(State new_state); // Transición entre estados

View File

@@ -19,7 +19,9 @@
#include "utils/utils.hpp" // Para PaletteColor #include "utils/utils.hpp" // Para PaletteColor
// Destructor // Destructor
Ending::~Ending() = default; Ending::~Ending() {
Audio::get()->stopMusic();
}
// Constructor // Constructor
Ending::Ending() Ending::Ending()
@@ -32,6 +34,7 @@ Ending::Ending()
iniScenes(); // Inicializa las escenas iniScenes(); // Inicializa las escenas
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); // Cambia el color del borde Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); // Cambia el color del borde
Audio::get()->playMusic("ending1.ogg");
} }
// Actualiza el objeto // Actualiza el objeto
@@ -39,8 +42,7 @@ void Ending::update() {
const float DELTA_TIME = delta_timer_->tick(); const float DELTA_TIME = delta_timer_->tick();
total_time_ += DELTA_TIME; // Actualiza el tiempo total total_time_ += DELTA_TIME; // Actualiza el tiempo total
handleEvents(); // Comprueba los eventos handleInput(); // Comprueba las entradas
handleInput(); // Comprueba las entradas
updateState(DELTA_TIME); // Actualiza la máquina de estados updateState(DELTA_TIME); // Actualiza la máquina de estados
updateSpriteCovers(); // Actualiza las cortinillas de los elementos updateSpriteCovers(); // Actualiza las cortinillas de los elementos
@@ -86,12 +88,9 @@ void Ending::render() {
Screen::get()->render(); Screen::get()->render();
} }
// Comprueba el manejador de eventos // Despatx d'un event (SDL3 Callback API)
void Ending::handleEvents() { void Ending::handleEvent(const SDL_Event& event) {
SDL_Event event; GlobalEvents::handle(event);
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
}
} }
// Comprueba las entradas // Comprueba las entradas
@@ -355,16 +354,10 @@ void Ending::iniScenes() { // NOLINT(readability-convert-member-functions-to-st
scenes_.push_back(sc); scenes_.push_back(sc);
} }
// Bucle principal // Un frame de l'escena (SDL3 Callback API)
void Ending::run() { void Ending::iterate() {
Audio::get()->playMusic("ending1.ogg"); update();
render();
while (SceneManager::current == SceneManager::Scene::ENDING) {
update();
render();
}
Audio::get()->stopMusic();
} }
// Actualiza las cortinillas de los elementos // Actualiza las cortinillas de los elementos

View File

@@ -5,19 +5,22 @@
#include <memory> // Para shared_ptr #include <memory> // Para shared_ptr
#include <string> // Para string #include <string> // Para string
#include <vector> // Para vector #include <vector> // Para vector
class Sprite; // lines 8-8
class Surface; // lines 9-9 #include "game/scenes/scene.hpp" // Para Scene
class Sprite; // lines 8-8
class Surface; // lines 9-9
class PixelReveal; class PixelReveal;
class DeltaTimer; class DeltaTimer;
class Ending { class Ending : public Scene {
public: public:
// --- Constructor y Destructor --- // --- Constructor y Destructor ---
Ending(); Ending();
~Ending(); // NOLINT(modernize-use-equals-default, performance-trivially-destructible) -- defined in .cpp for unique_ptr with forward declarations ~Ending() override; // NOLINT(modernize-use-equals-default, performance-trivially-destructible) -- defined in .cpp for unique_ptr with forward declarations
// --- Bucle principal --- // --- Bucle principal (SDL3 Callback API) ---
void run(); void iterate() override;
void handleEvent(const SDL_Event& event) override;
private: private:
// --- Enumeraciones --- // --- Enumeraciones ---
@@ -77,7 +80,6 @@ class Ending {
// --- Métodos --- // --- Métodos ---
void update(); // Actualiza el objeto void update(); // Actualiza el objeto
void render(); // Dibuja el final en pantalla void render(); // Dibuja el final en pantalla
static void handleEvents(); // Comprueba el manejador de eventos
static void handleInput(); // Comprueba las entradas static void handleInput(); // Comprueba las entradas
void iniTexts(); // Inicializa los textos void iniTexts(); // Inicializa los textos
void iniPics(); // Inicializa las imágenes void iniPics(); // Inicializa las imágenes

View File

@@ -41,14 +41,20 @@ Ending2::Ending2()
placeSprites(); // Coloca los sprites en su sito placeSprites(); // Coloca los sprites en su sito
createSpriteTexts(); // Crea los sprites con las texturas con los textos createSpriteTexts(); // Crea los sprites con las texturas con los textos
createTexts(); // Crea los sprites con las texturas con los textos del final createTexts(); // Crea los sprites con las texturas con los textos del final
Audio::get()->playMusic("ending2.ogg");
}
// Destructor
Ending2::~Ending2() {
Audio::get()->stopMusic();
} }
// Actualiza el objeto // Actualiza el objeto
void Ending2::update() { void Ending2::update() {
const float DELTA_TIME = delta_timer_->tick(); const float DELTA_TIME = delta_timer_->tick();
handleEvents(); // Comprueba los eventos handleInput(); // Comprueba las entradas
handleInput(); // Comprueba las entradas
updateState(DELTA_TIME); // Actualiza el estado updateState(DELTA_TIME); // Actualiza el estado
@@ -95,12 +101,9 @@ void Ending2::render() {
Screen::get()->render(); Screen::get()->render();
} }
// Comprueba el manejador de eventos // Despatx d'un event (SDL3 Callback API)
void Ending2::handleEvents() { void Ending2::handleEvent(const SDL_Event& event) {
SDL_Event event; GlobalEvents::handle(event);
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
}
} }
// Comprueba las entradas // Comprueba las entradas
@@ -109,16 +112,10 @@ void Ending2::handleInput() {
GlobalInputs::handle(); GlobalInputs::handle();
} }
// Bucle principal // Un frame de l'escena (SDL3 Callback API)
void Ending2::run() { void Ending2::iterate() {
Audio::get()->playMusic("ending2.ogg"); update();
render();
while (SceneManager::current == SceneManager::Scene::ENDING2) {
update();
render();
}
Audio::get()->stopMusic();
} }
// Actualiza el estado // Actualiza el estado

View File

@@ -7,19 +7,21 @@
#include <vector> // Para vector #include <vector> // Para vector
#include "core/rendering/sprite/dissolve_sprite.hpp" // Para SurfaceDissolveSprite #include "core/rendering/sprite/dissolve_sprite.hpp" // Para SurfaceDissolveSprite
#include "game/scenes/scene.hpp" // Para Scene
#include "utils/defines.hpp" // Para GameCanvas::WIDTH, GameCanvas::FIRST_QUAR... #include "utils/defines.hpp" // Para GameCanvas::WIDTH, GameCanvas::FIRST_QUAR...
class MovingSprite; class MovingSprite;
class DeltaTimer; class DeltaTimer;
class Ending2 { class Ending2 : public Scene {
public: public:
// --- Constructor y Destructor --- // --- Constructor y Destructor ---
Ending2(); Ending2();
~Ending2() = default; ~Ending2() override;
// --- Bucle principal --- // --- Bucle principal (SDL3 Callback API) ---
void run(); void iterate() override;
void handleEvent(const SDL_Event& event) override;
private: private:
// --- Enumeraciones --- // --- Enumeraciones ---
@@ -58,7 +60,6 @@ class Ending2 {
// --- Métodos --- // --- Métodos ---
void update(); // Actualiza el objeto void update(); // Actualiza el objeto
void render(); // Dibuja el final en pantalla void render(); // Dibuja el final en pantalla
static void handleEvents(); // Comprueba el manejador de eventos
static void handleInput(); // Comprueba las entradas static void handleInput(); // Comprueba las entradas
void updateState(float delta_time); // Actualiza el estado void updateState(float delta_time); // Actualiza el estado
void transitionToState(EndingState new_state); // Transición entre estados void transitionToState(EndingState new_state); // Transición entre estados

View File

@@ -163,9 +163,19 @@ Game::Game(Mode mode)
SceneManager::current = (mode_ == Mode::GAME) ? SceneManager::Scene::GAME : SceneManager::Scene::DEMO; SceneManager::current = (mode_ == Mode::GAME) ? SceneManager::Scene::GAME : SceneManager::Scene::DEMO;
SceneManager::options = SceneManager::Options::NONE; SceneManager::options = SceneManager::Options::NONE;
// Arranca la música del juego (abans a run())
keepMusicPlaying();
if (!scoreboard_data_->music && mode_ == Mode::GAME) {
Audio::get()->pauseMusic();
}
} }
Game::~Game() { Game::~Game() {
if (mode_ == Mode::GAME) {
Audio::get()->stopMusic();
}
ItemTracker::destroy(); ItemTracker::destroy();
GameControl::change_player_skin = nullptr; GameControl::change_player_skin = nullptr;
@@ -188,38 +198,35 @@ Game::~Game() {
#endif #endif
} }
// Comprueba los eventos de la cola // Despatx d'un event (SDL3 Callback API)
void Game::handleEvents() { void Game::handleEvent(const SDL_Event& event) {
SDL_Event event; GlobalEvents::handle(event);
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
#ifdef _DEBUG #ifdef _DEBUG
// En modo editor: click del ratón cierra la consola // En modo editor: click del ratón cierra la consola
if (Console::get()->isActive() && MapEditor::get()->isActive() && if (Console::get()->isActive() && MapEditor::get()->isActive() &&
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
Console::get()->toggle(); Console::get()->toggle();
}
if (!Console::get()->isActive()) {
// Tecla 9: toggle editor (funciona tanto dentro como fuera del editor)
if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_9 && static_cast<int>(event.key.repeat) == 0) {
if (MapEditor::get()->isActive()) {
GameControl::exit_editor();
Notifier::get()->show({Locale::get()->get("game.editor_disabled")}); // NOLINT(readability-static-accessed-through-instance)
} else {
GameControl::enter_editor();
Notifier::get()->show({Locale::get()->get("game.editor_enabled")}); // NOLINT(readability-static-accessed-through-instance)
}
} else if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_8 && static_cast<int>(event.key.repeat) == 0 && MapEditor::get()->isActive()) {
MapEditor::get()->showGrid(!MapEditor::get()->isGridEnabled());
} else if (MapEditor::get()->isActive()) {
MapEditor::get()->handleEvent(event);
} else {
handleDebugEvents(event);
}
}
#endif
} }
if (!Console::get()->isActive()) {
// Tecla 9: toggle editor (funciona tanto dentro como fuera del editor)
if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_9 && static_cast<int>(event.key.repeat) == 0) {
if (MapEditor::get()->isActive()) {
GameControl::exit_editor();
Notifier::get()->show({Locale::get()->get("game.editor_disabled")}); // NOLINT(readability-static-accessed-through-instance)
} else {
GameControl::enter_editor();
Notifier::get()->show({Locale::get()->get("game.editor_enabled")}); // NOLINT(readability-static-accessed-through-instance)
}
} else if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_8 && static_cast<int>(event.key.repeat) == 0 && MapEditor::get()->isActive()) {
MapEditor::get()->showGrid(!MapEditor::get()->isGridEnabled());
} else if (MapEditor::get()->isActive()) {
MapEditor::get()->handleEvent(event);
} else {
handleDebugEvents(event);
}
}
#endif
} }
// Comprueba el teclado // Comprueba el teclado
@@ -262,29 +269,17 @@ void Game::handleInput() {
GlobalInputs::handle(); GlobalInputs::handle();
} }
// Bucle para el juego // Un frame de l'escena (SDL3 Callback API)
void Game::run() { void Game::iterate() {
keepMusicPlaying(); update();
if (!scoreboard_data_->music && mode_ == Mode::GAME) { render();
Audio::get()->pauseMusic();
}
while (SceneManager::current == SceneManager::Scene::GAME || SceneManager::current == SceneManager::Scene::DEMO) {
update();
render();
}
if (mode_ == Mode::GAME) {
Audio::get()->stopMusic();
}
} }
// Actualiza el juego, las variables, comprueba la entrada, etc. // Actualiza el juego, las variables, comprueba la entrada, etc.
void Game::update() { void Game::update() {
const float DELTA_TIME = delta_timer_.tick(); const float DELTA_TIME = delta_timer_.tick();
handleEvents(); // Comprueba los eventos handleInput(); // Comprueba las entradas
handleInput(); // Comprueba las entradas
#ifdef _DEBUG #ifdef _DEBUG
Debug::get()->clear(); Debug::get()->clear();

View File

@@ -8,6 +8,7 @@
#include <vector> // Para vector #include <vector> // Para vector
#include "game/entities/player.hpp" // Para PlayerSpawn #include "game/entities/player.hpp" // Para PlayerSpawn
#include "game/scenes/scene.hpp" // Para Scene
#include "utils/delta_timer.hpp" // Para DeltaTimer #include "utils/delta_timer.hpp" // Para DeltaTimer
class Room; // lines 12-12 class Room; // lines 12-12
class RoomTracker; // lines 13-13 class RoomTracker; // lines 13-13
@@ -15,7 +16,7 @@ class Scoreboard; // lines 14-14
class Stats; // lines 15-15 class Stats; // lines 15-15
class Surface; class Surface;
class Game { class Game : public Scene {
public: public:
// --- Estructuras --- // --- Estructuras ---
enum class Mode { enum class Mode {
@@ -33,10 +34,11 @@ class Game {
// --- Constructor y Destructor --- // --- Constructor y Destructor ---
explicit Game(Mode mode); explicit Game(Mode mode);
~Game(); ~Game() override;
// --- Bucle para el juego --- // --- Bucle para el juego (SDL3 Callback API) ---
void run(); void iterate() override;
void handleEvent(const SDL_Event& event) override;
private: private:
// --- Constantes de tiempo --- // --- Constantes de tiempo ---
@@ -57,7 +59,6 @@ class Game {
// --- Métodos --- // --- Métodos ---
void update(); // Actualiza el juego, las variables, comprueba la entrada, etc. void update(); // Actualiza el juego, las variables, comprueba la entrada, etc.
void render(); // Pinta los objetos en pantalla void render(); // Pinta los objetos en pantalla
void handleEvents(); // Comprueba los eventos de la cola
void renderRoomName(); // Escribe el nombre de la pantalla void renderRoomName(); // Escribe el nombre de la pantalla
void transitionToState(State new_state); // Cambia al estado especificado y resetea los timers void transitionToState(State new_state); // Cambia al estado especificado y resetea los timers
void updatePlaying(float delta_time); // Actualiza el juego en estado PLAYING void updatePlaying(float delta_time); // Actualiza el juego en estado PLAYING

View File

@@ -49,8 +49,7 @@ void GameOver::update() {
const float DELTA_TIME = delta_timer_->tick(); const float DELTA_TIME = delta_timer_->tick();
elapsed_time_ += DELTA_TIME; elapsed_time_ += DELTA_TIME;
handleEvents(); // Comprueba los eventos handleInput(); // Comprueba las entradas
handleInput(); // Comprueba las entradas
updateState(); // Actualiza el estado de la escena updateState(); // Actualiza el estado de la escena
updateColor(); // Actualiza el color usado para renderizar los textos e imagenes updateColor(); // Actualiza el color usado para renderizar los textos e imagenes
@@ -91,12 +90,9 @@ void GameOver::render() {
Screen::get()->render(); Screen::get()->render();
} }
// Comprueba el manejador de eventos // Despatx d'un event (SDL3 Callback API)
void GameOver::handleEvents() { void GameOver::handleEvent(const SDL_Event& event) {
SDL_Event event; GlobalEvents::handle(event);
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
}
} }
// Comprueba las entradas // Comprueba las entradas
@@ -105,12 +101,10 @@ void GameOver::handleInput() {
GlobalInputs::handle(); GlobalInputs::handle();
} }
// Bucle principal // Un frame de l'escena (SDL3 Callback API)
void GameOver::run() { void GameOver::iterate() {
while (SceneManager::current == SceneManager::Scene::GAME_OVER) { update();
update(); render();
render();
}
} }
// Actualiza el color usado para renderizar los textos e imagenes // Actualiza el color usado para renderizar los textos e imagenes

View File

@@ -2,19 +2,22 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <memory> // Para shared_ptr #include <memory> // Para shared_ptr
#include <vector> // Para vector #include <vector> // Para vector
class AnimatedSprite; // lines 7-7
class DeltaTimer; // Forward declaration
class GameOver { #include "game/scenes/scene.hpp" // Para Scene
class AnimatedSprite; // lines 7-7
class DeltaTimer; // Forward declaration
class GameOver : public Scene {
public: public:
// Constructor y Destructor // Constructor y Destructor
GameOver(); GameOver();
~GameOver() = default; ~GameOver() override = default;
// Bucle principal // Bucle principal (SDL3 Callback API)
void run(); void iterate() override;
void handleEvent(const SDL_Event& event) override;
private: private:
// --- Enumeraciones --- // --- Enumeraciones ---
@@ -45,13 +48,12 @@ class GameOver {
static constexpr int NIGHTMARE_TEXT_Y_OFFSET = 120; // Offset Y del texto nightmare desde TEXT_Y static constexpr int NIGHTMARE_TEXT_Y_OFFSET = 120; // Offset Y del texto nightmare desde TEXT_Y
// --- Métodos --- // --- Métodos ---
void update(); // Actualiza el objeto void update(); // Actualiza el objeto
void render(); // Dibuja el final en pantalla void render(); // Dibuja el final en pantalla
static void handleEvents(); // Comprueba el manejador de eventos static void handleInput(); // Comprueba las entradas
static void handleInput(); // Comprueba las entradas void updateState(); // Actualiza el estado y transiciones
void updateState(); // Actualiza el estado y transiciones void updateColor(); // Actualiza el color usado para renderizar
void updateColor(); // Actualiza el color usado para renderizar void renderSprites(); // Dibuja los sprites
void renderSprites(); // Dibuja los sprites
// --- Variables miembro --- // --- Variables miembro ---
// Objetos y punteros a recursos // Objetos y punteros a recursos

View File

@@ -41,19 +41,23 @@ LoadingScreen::LoadingScreen()
// Cambia el color del borde // Cambia el color del borde
Screen::get()->setBorderColor(stringToColor("white")); Screen::get()->setBorderColor(stringToColor("white"));
transitionToState(State::SILENT1); transitionToState(State::SILENT1);
// Ajusta el volumen i neteja la pantalla (abans a run())
Audio::get()->setMusicVolume(50);
Screen::get()->start();
Screen::get()->clearRenderer();
Screen::get()->render();
} }
// Destructor // Destructor
LoadingScreen::~LoadingScreen() { LoadingScreen::~LoadingScreen() {
Audio::get()->stopMusic(); Audio::get()->stopMusic();
Audio::get()->setMusicVolume(100);
} }
// Comprueba el manejador de eventos // Despatx d'un event (SDL3 Callback API)
void LoadingScreen::handleEvents() { void LoadingScreen::handleEvent(const SDL_Event& event) {
SDL_Event event; GlobalEvents::handle(event);
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
}
} }
// Comprueba las entradas // Comprueba las entradas
@@ -347,8 +351,7 @@ void LoadingScreen::renderColoredBorder(PaletteColor color) {
void LoadingScreen::update() { void LoadingScreen::update() {
const float DELTA_TIME = delta_timer_->tick(); const float DELTA_TIME = delta_timer_->tick();
handleEvents(); // Comprueba los eventos handleInput(); // Comprueba las entradas
handleInput(); // Comprueba las entradas
updateState(DELTA_TIME); // Actualiza el estado y gestiona transiciones updateState(DELTA_TIME); // Actualiza el estado y gestiona transiciones
@@ -400,22 +403,10 @@ void LoadingScreen::render() {
Screen::get()->render(); Screen::get()->render();
} }
// Bucle para el logo del juego // Un frame de l'escena (SDL3 Callback API)
void LoadingScreen::run() { void LoadingScreen::iterate() {
// Ajusta el volumen update();
Audio::get()->setMusicVolume(50); render();
// Limpia la pantalla
Screen::get()->start();
Screen::get()->clearRenderer();
Screen::get()->render();
while (SceneManager::current == SceneManager::Scene::LOADING_SCREEN) {
update();
render();
}
Audio::get()->setMusicVolume(100);
} }
// Pinta el borde // Pinta el borde

View File

@@ -5,19 +5,21 @@
#include <array> // Para std::array #include <array> // Para std::array
#include <memory> // Para shared_ptr #include <memory> // Para shared_ptr
#include "game/scenes/scene.hpp" // Para Scene
#include "utils/delta_timer.hpp" // Para DeltaTimer #include "utils/delta_timer.hpp" // Para DeltaTimer
#include "utils/utils.hpp" // Para PaletteColor #include "utils/utils.hpp" // Para PaletteColor
class Sprite; // Forward declaration class Sprite; // Forward declaration
class Surface; // Forward declaration class Surface; // Forward declaration
class LoadingScreen { class LoadingScreen : public Scene {
public: public:
// --- Constructor y Destructor --- // --- Constructor y Destructor ---
LoadingScreen(); LoadingScreen();
~LoadingScreen(); ~LoadingScreen() override;
// --- Bucle principal --- // --- Bucle principal (SDL3 Callback API) ---
void run(); void iterate() override;
void handleEvent(const SDL_Event& event) override;
private: private:
// --- Enumeraciones --- // --- Enumeraciones ---
@@ -81,7 +83,6 @@ class LoadingScreen {
// --- Métodos --- // --- Métodos ---
void update(); // Actualiza las variables void update(); // Actualiza las variables
void render(); // Dibuja en pantalla void render(); // Dibuja en pantalla
static void handleEvents(); // Comprueba el manejador de eventos
static void handleInput(); // Comprueba las entradas static void handleInput(); // Comprueba las entradas
void updateState(float delta_time); // Actualiza el estado actual void updateState(float delta_time); // Actualiza el estado actual
void transitionToState(State new_state); // Transiciona a un nuevo estado void transitionToState(State new_state); // Transiciona a un nuevo estado

View File

@@ -54,12 +54,9 @@ Logo::Logo()
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK));
} }
// Comprueba el manejador de eventos // Despatx d'un event (SDL3 Callback API)
void Logo::handleEvents() { void Logo::handleEvent(const SDL_Event& event) {
SDL_Event event; GlobalEvents::handle(event);
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
}
} }
// Comprueba las entradas // Comprueba las entradas
@@ -201,8 +198,7 @@ void Logo::updateState(float delta_time) {
void Logo::update() { void Logo::update() {
const float DELTA_TIME = delta_timer_->tick(); const float DELTA_TIME = delta_timer_->tick();
handleEvents(); // Comprueba los eventos handleInput(); // Comprueba las entradas
handleInput(); // Comprueba las entradas
updateState(DELTA_TIME); // Actualiza el estado y gestiona transiciones updateState(DELTA_TIME); // Actualiza el estado y gestiona transiciones
updateJAILGAMES(DELTA_TIME); // Gestiona el logo de JAILGAME updateJAILGAMES(DELTA_TIME); // Gestiona el logo de JAILGAME
@@ -228,12 +224,10 @@ void Logo::render() { // NOLINT(readability-convert-member-functions-to-static)
Screen::get()->render(); Screen::get()->render();
} }
// Bucle para el logo del juego // Un frame de l'escena (SDL3 Callback API)
void Logo::run() { void Logo::iterate() {
while (SceneManager::current == SceneManager::Scene::LOGO) { update();
update(); render();
render();
}
} }
// Termina la sección // Termina la sección

View File

@@ -6,11 +6,12 @@
#include <memory> // Para shared_ptr #include <memory> // Para shared_ptr
#include <vector> // Para vector #include <vector> // Para vector
#include "game/scenes/scene.hpp" // Para Scene
#include "utils/delta_timer.hpp" // Para DeltaTimer #include "utils/delta_timer.hpp" // Para DeltaTimer
class Sprite; // Forward declaration class Sprite; // Forward declaration
class Surface; // Forward declaration class Surface; // Forward declaration
class Logo { class Logo : public Scene {
public: public:
// --- Tipos --- // --- Tipos ---
using EasingFunction = std::function<float(float)>; // Función de easing (permite lambdas) using EasingFunction = std::function<float(float)>; // Función de easing (permite lambdas)
@@ -28,10 +29,11 @@ class Logo {
// --- Constructor y Destructor --- // --- Constructor y Destructor ---
Logo(); Logo();
~Logo() = default; ~Logo() override = default;
// --- Bucle principal --- // --- Bucle principal (SDL3 Callback API) ---
void run(); void iterate() override;
void handleEvent(const SDL_Event& event) override;
private: private:
// --- Constantes de tiempo (en segundos) --- // --- Constantes de tiempo (en segundos) ---
@@ -48,7 +50,6 @@ class Logo {
// --- Métodos --- // --- Métodos ---
void update(); // Actualiza las variables void update(); // Actualiza las variables
void render(); // Dibuja en pantalla void render(); // Dibuja en pantalla
static void handleEvents(); // Comprueba el manejador de eventos
static void handleInput(); // Comprueba las entradas static void handleInput(); // Comprueba las entradas
void updateJAILGAMES(float delta_time); // Gestiona el logo de JAILGAME (time-based) void updateJAILGAMES(float delta_time); // Gestiona el logo de JAILGAME (time-based)
void updateTextureColors(); // Gestiona el color de las texturas void updateTextureColors(); // Gestiona el color de las texturas

View File

@@ -0,0 +1,13 @@
#pragma once
#include <SDL3/SDL.h>
// Interfície base per a totes les escenes del joc.
// Cada escena concreta implementa iterate() (un frame) i handleEvent() (un event).
// Director crida aquests mètodes des de SDL_AppIterate / SDL_AppEvent.
class Scene {
public:
virtual ~Scene() = default;
virtual void iterate() = 0;
virtual void handleEvent(const SDL_Event& event) = 0;
};

View File

@@ -81,28 +81,34 @@ void Title::initMarquee() {
last_active_letter_ = 0; last_active_letter_ = 0;
} }
// Comprueba el manejador de eventos // Despatx d'un event (SDL3 Callback API)
void Title::handleEvents() { void Title::handleEvent(const SDL_Event& event) {
SDL_Event event; GlobalEvents::handle(event);
while (SDL_PollEvent(&event)) {
GlobalEvents::handle(event);
// Manejo especial para captura de botones de gamepad // Manejo especial para captura de botones de gamepad
if (is_remapping_joystick_ && !remap_completed_ && if (is_remapping_joystick_ && !remap_completed_ &&
(event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN || event.type == SDL_EVENT_GAMEPAD_AXIS_MOTION)) { (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN || event.type == SDL_EVENT_GAMEPAD_AXIS_MOTION)) {
handleJoystickRemap(event); handleJoystickRemap(event);
continue; // No procesar más este evento return; // No procesar más este evento
}
// Qualsevol botó del comandament al menú principal inicia partida directament
// (els bindings ja estan definits, no cal "pulsar 1" amb el teclat).
if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN &&
state_ == State::MAIN_MENU &&
!is_remapping_keyboard_ && !is_remapping_joystick_) {
handleMainMenuKeyPress(SDLK_1); // PLAY
return;
}
if (event.type == SDL_EVENT_KEY_DOWN && !Console::get()->isActive()) {
// Si estamos en modo remap de teclado, capturar tecla
if (is_remapping_keyboard_ && !remap_completed_) {
handleKeyboardRemap(event);
} }
// Si estamos en el menú principal normal
if (event.type == SDL_EVENT_KEY_DOWN && !Console::get()->isActive()) { else if (state_ == State::MAIN_MENU && !is_remapping_keyboard_ && !is_remapping_joystick_) {
// Si estamos en modo remap de teclado, capturar tecla handleMainMenuKeyPress(event.key.key);
if (is_remapping_keyboard_ && !remap_completed_) {
handleKeyboardRemap(event);
}
// Si estamos en el menú principal normal
else if (state_ == State::MAIN_MENU && !is_remapping_keyboard_ && !is_remapping_joystick_) {
handleMainMenuKeyPress(event.key.key);
}
} }
} }
} }
@@ -242,7 +248,6 @@ void Title::renderMarquee() const {
void Title::update() { void Title::update() {
const float DELTA_TIME = delta_timer_->tick(); const float DELTA_TIME = delta_timer_->tick();
handleEvents(); // Comprueba los eventos
handleInput(DELTA_TIME); // Comprueba las entradas handleInput(DELTA_TIME); // Comprueba las entradas
updateState(DELTA_TIME); // Actualiza el estado actual updateState(DELTA_TIME); // Actualiza el estado actual
@@ -434,12 +439,10 @@ void Title::render() {
Screen::get()->render(); Screen::get()->render();
} }
// Bucle para el logo del juego // Un frame de l'escena (SDL3 Callback API)
void Title::run() { void Title::iterate() {
while (SceneManager::current == SceneManager::Scene::TITLE) { update();
update(); render();
render();
}
} }
// Crea y rellena la textura para mostrar los logros // Crea y rellena la textura para mostrar los logros

View File

@@ -8,19 +8,21 @@
#include <vector> // Para vector #include <vector> // Para vector
#include "game/scene_manager.hpp" // Para SceneManager::Scene #include "game/scene_manager.hpp" // Para SceneManager::Scene
#include "game/scenes/scene.hpp" // Para Scene
#include "utils/delta_timer.hpp" // Para DeltaTimer #include "utils/delta_timer.hpp" // Para DeltaTimer
class Sprite; // Forward declaration class Sprite; // Forward declaration
class Surface; // Forward declaration class Surface; // Forward declaration
class Text; // Forward declaration class Text; // Forward declaration
class Title { class Title : public Scene {
public: public:
// --- Constructor y Destructor --- // --- Constructor y Destructor ---
Title(); Title();
~Title(); ~Title() override;
// --- Bucle principal --- // --- Bucle principal (SDL3 Callback API) ---
void run(); void iterate() override;
void handleEvent(const SDL_Event& event) override;
private: private:
// --- Estructuras y enumeraciones --- // --- Estructuras y enumeraciones ---
@@ -61,7 +63,6 @@ class Title {
// --- Métodos --- // --- Métodos ---
void update(); // Actualiza las variables void update(); // Actualiza las variables
void render(); // Dibuja en pantalla void render(); // Dibuja en pantalla
void handleEvents(); // Comprueba el manejador de eventos
void handleMainMenuKeyPress(SDL_Keycode key); // Maneja las teclas del menu principal void handleMainMenuKeyPress(SDL_Keycode key); // Maneja las teclas del menu principal
void handleInput(float delta_time); // Comprueba las entradas void handleInput(float delta_time); // Comprueba las entradas
void updateState(float delta_time); // Actualiza el estado actual void updateState(float delta_time); // Actualiza el estado actual

View File

@@ -948,11 +948,16 @@ static auto cmdKiosk(const std::vector<std::string>& args) -> std::string {
// EXIT / QUIT // EXIT / QUIT
static auto cmdExit(const std::vector<std::string>& args) -> std::string { static auto cmdExit(const std::vector<std::string>& args) -> std::string {
#ifdef __EMSCRIPTEN__
(void)args;
return "Not allowed in web version";
#else
if (Options::kiosk.enabled && (args.empty() || args[0] != "PLEASE")) { if (Options::kiosk.enabled && (args.empty() || args[0] != "PLEASE")) {
return "Not allowed in kiosk mode"; return "Not allowed in kiosk mode";
} }
SceneManager::current = SceneManager::Scene::QUIT; SceneManager::current = SceneManager::Scene::QUIT;
return "Quitting..."; return "Quitting...";
#endif
} }
// SIZE // SIZE

View File

@@ -5,14 +5,24 @@ Empezado en Castalla el 01/07/2022.
*/ */
#include <memory> #define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL_main.h>
#include "core/system/director.hpp" #include "core/system/director.hpp"
auto main() -> int { SDL_AppResult SDL_AppInit(void** appstate, int /*argc*/, char* /*argv*/[]) {
// Crea el objeto Director *appstate = new Director();
auto director = std::make_unique<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

@@ -6,7 +6,7 @@
namespace Texts { namespace Texts {
constexpr const char* WINDOW_CAPTION = "© 2022 JailDoctor's Dilemma — JailDesigner"; constexpr const char* WINDOW_CAPTION = "© 2022 JailDoctor's Dilemma — JailDesigner";
constexpr const char* COPYRIGHT = "@2022 JailDesigner"; constexpr const char* COPYRIGHT = "@2022 JailDesigner";
constexpr const char* VERSION = "1.13"; // Versión por defecto constexpr const char* VERSION = "1.14"; // Versión por defecto
} // namespace Texts } // namespace Texts
// Tamaño de bloque // Tamaño de bloque