16 Commits

Author SHA1 Message Date
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
27 changed files with 1260 additions and 551 deletions

View File

@@ -30,11 +30,30 @@ if(NOT SOURCES)
endif() endif()
# 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()
# Configuración común de salida de ejecutables en el directorio raíz # Configuración de salida de ejecutables
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}) if(NOT EMSCRIPTEN)
# En desktop, el ejecutable va a la raíz del proyecto
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,14 @@ 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}/data@/data
-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

@@ -103,7 +103,7 @@ windows_release:
@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
@@ -236,6 +236,23 @@ 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/"
# ============================================================================== # ==============================================================================
# REGLAS ESPECIALES # REGLAS ESPECIALES
# ============================================================================== # ==============================================================================
@@ -255,9 +272,10 @@ 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)"
@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 show_version help

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

View File

@@ -1,11 +1,13 @@
#include "director.h" #include "director.h"
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#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 <fstream> // for basic_ostream, operator<<, basi... #include <fstream> // for basic_ostream, operator<<, basi...
@@ -22,12 +24,13 @@
#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 "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
@@ -81,11 +84,21 @@ Director::Director(int argc, const char *argv[]) {
initInput(); initInput();
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();
delete asset; delete asset;
delete input; delete input;
delete screen; delete screen;
@@ -137,8 +150,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);
@@ -383,6 +396,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 = 1;
options->videoMode = 0;
options->integerScale = true;
#endif
} }
// Comprueba los parametros del programa // Comprueba los parametros del programa
@@ -400,7 +420,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 +445,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 +478,7 @@ void Director::createSystemFolder(const std::string &folder) {
} }
} }
} }
#endif
} }
// Carga el fichero de configuración // Carga el fichero de configuración
@@ -568,50 +596,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;
switch (section->name) {
case SECTION_PROG_LOGO:
targetSection = ActiveSection::Logo;
break;
case SECTION_PROG_INTRO:
targetSection = ActiveSection::Intro;
break;
case SECTION_PROG_TITLE:
targetSection = ActiveSection::Title;
break;
case SECTION_PROG_GAME:
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;
}
} }
void Director::runIntro() { // Ejecuta un frame del juego
auto intro = std::make_unique<Intro>(renderer, screen, asset, input, lang, section); SDL_AppResult Director::iterate() {
intro->run(); #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;
} }
void Director::runTitle() { // Procesa un evento
auto title = std::make_unique<Title>(renderer, screen, input, asset, options, lang, section); SDL_AppResult Director::handleEvent(SDL_Event *event) {
title->run(); #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
void Director::runGame() { // Hot-plug de mandos
const int numPlayers = section->subsection == SUBSECTION_GAME_PLAY_1P ? 1 : 2; if (event->type == SDL_EVENT_GAMEPAD_ADDED) {
auto game = std::make_unique<Game>(numPlayers, 0, renderer, screen, asset, lang, input, false, options, section); std::string name;
game->run(); if (input->handleGamepadAdded(event->gdevice.which, name)) {
} screen->notify(name + " " + lang->getText(94),
color_t{0x40, 0xFF, 0x40},
int Director::run() { color_t{0, 0, 0},
// Bucle principal 2500);
while (section->name != SECTION_PROG_QUIT) { }
switch (section->name) { } else if (event->type == SDL_EVENT_GAMEPAD_REMOVED) {
case SECTION_PROG_LOGO: std::string name;
runLogo(); if (input->handleGamepadRemoved(event->gdevice.which, name)) {
break; screen->notify(name + " " + lang->getText(95),
color_t{0xFF, 0x50, 0x50},
case SECTION_PROG_INTRO: color_t{0, 0, 0},
runIntro(); 2500);
break;
case SECTION_PROG_TITLE:
runTitle();
break;
case SECTION_PROG_GAME:
runGame();
break;
} }
} }
return 0; // 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,30 +48,35 @@ void Fade::render() {
if (mEnabled && !mFinished) { if (mEnabled && !mFinished) {
switch (mFadeType) { switch (mFadeType) {
case FADE_FULLSCREEN: { case FADE_FULLSCREEN: {
SDL_FRect fRect1 = {0, 0, (float)GAMECANVAS_WIDTH, (float)GAMECANVAS_HEIGHT}; if (!mFullscreenDone) {
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 // Deja todos los buffers del mismo color
SDL_RenderTexture(mRenderer, mBackbuffer, nullptr, nullptr); SDL_SetRenderTarget(mRenderer, mBackbuffer);
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 255);
SDL_RenderClear(mRenderer);
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, i); SDL_SetRenderTarget(mRenderer, nullptr);
SDL_RenderFillRect(mRenderer, &fRect1); SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 255);
SDL_RenderClear(mRenderer);
// Vuelca el renderizador en pantalla mFinished = true;
SDL_RenderPresent(mRenderer); } 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);
}
} }
// Deja todos los buffers del mismo color
SDL_SetRenderTarget(mRenderer, mBackbuffer);
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 255);
SDL_RenderClear(mRenderer);
SDL_SetRenderTarget(mRenderer, nullptr);
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 255);
SDL_RenderClear(mRenderer);
break; break;
} }
@@ -89,14 +100,17 @@ void Fade::render() {
} }
case FADE_RANDOM_SQUARE: { case FADE_RANDOM_SQUARE: {
SDL_FRect fRs = {0, 0, 32, 32}; Uint32 now = SDL_GetTicks();
if (mSquaresDrawn < 50 && now - mLastSquareTicks >= 100) {
mLastSquareTicks = now;
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);
// Copiamos el backbuffer al renderizador mSquaresDrawn++;
SDL_RenderTexture(mRenderer, mBackbuffer, nullptr, nullptr); }
// Volcamos el renderizador en pantalla // Copiamos el backbuffer al renderizador
SDL_RenderPresent(mRenderer); SDL_RenderTexture(mRenderer, mBackbuffer, nullptr, nullptr);
SDL_Delay(100);
if (mSquaresDrawn >= 50) {
mFinished = true;
} }
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

@@ -10,15 +10,19 @@ constexpr int FADE_RANDOM_SQUARE = 2;
// Clase Fade // Clase Fade
class Fade { class Fade {
private: private:
SDL_Renderer *mRenderer; // El renderizador de la ventana SDL_Renderer *mRenderer; // El renderizador de la ventana
SDL_Texture *mBackbuffer; // Textura para usar como backbuffer SDL_Texture *mBackbuffer; // Textura para usar como backbuffer
Uint8 mFadeType; // Tipo de fade a realizar Uint8 mFadeType; // Tipo de fade a realizar
Uint16 mCounter; // Contador interno Uint16 mCounter; // Contador interno
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
SDL_Rect mRect1; // Rectangulo usado para crear los efectos de transición Uint8 mROriginal, mGOriginal, mBOriginal; // Colores originales para FADE_RANDOM_SQUARE
SDL_Rect mRect2; // Rectangulo usado para crear los efectos de transición 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 mRect2; // Rectangulo usado para crear los efectos de transición
public: public:
// Constructor // Constructor

View File

@@ -284,6 +284,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 +305,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;
@@ -1963,7 +1972,9 @@ void Game::destroyAllBalloons() {
} }
enemyDeployCounter = 255; enemyDeployCounter = 255;
JA_PlaySound(powerBallSound); if (!demo.enabled) {
JA_PlaySound(powerBallSound);
}
effect.flash = true; effect.flash = true;
effect.shake = true; effect.shake = true;
} }
@@ -2363,23 +2374,50 @@ void Game::killPlayer(Player *player) {
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));
JA_PlaySound(coffeeOutSound); JA_PlaySound(coffeeOutSound);
} else { } else if (deathSequence.phase == DeathPhase::None) {
JA_PauseMusic(); JA_PauseMusic();
stopAllBalloons(10); stopAllBalloons(10);
JA_PlaySound(playerCollisionSound); JA_PlaySound(playerCollisionSound);
shakeScreen(); shakeScreen();
SDL_Delay(500); deathSequence.phase = DeathPhase::Shaking;
JA_PlaySound(coffeeOutSound); deathSequence.phaseStartTicks = SDL_GetTicks();
player->setAlive(false); deathSequence.player = player;
if (allPlayersAreDead()) {
JA_StopMusic();
} else {
JA_ResumeMusic();
}
} }
} }
} }
// 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) {
JA_PlaySound(coffeeOutSound);
deathSequence.player->setAlive(false);
if (allPlayersAreDead()) {
JA_StopMusic();
} else {
JA_ResumeMusic();
}
deathSequence.phase = DeathPhase::Done;
deathSequence.player = nullptr;
}
break;
}
}
// Calcula y establece el valor de amenaza en funcion de los globos activos // Calcula y establece el valor de amenaza en funcion de los globos activos
void Game::evaluateAndSetMenace() { void Game::evaluateAndSetMenace() {
menaceCurrent = 0; menaceCurrent = 0;
@@ -2439,6 +2477,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 +2597,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);
@@ -2777,7 +2827,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,83 +2918,130 @@ 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;
Uint32 now = SDL_GetTicks();
if (now - deathShake.lastStepTicks >= 50) {
deathShake.lastStepTicks = now;
deathShake.step++;
if (deathShake.step >= 8) {
deathShake.active = false;
}
}
}
// Indica si el efecto de agitación intensa está activo
bool Game::isDeathShaking() {
return deathShake.active;
}
// Ejecuta un frame del juego
void Game::iterate() {
// En modo demo, no hay pausa ni game over
if (demo.enabled) {
if (section->subsection == SUBSECTION_GAME_PAUSE || section->subsection == SUBSECTION_GAME_GAMEOVER) {
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_INSTRUCTIONS;
return;
}
}
// Sección juego en pausa
if (section->subsection == SUBSECTION_GAME_PAUSE) {
if (!pauseInitialized) {
enterPausedGame();
}
updatePausedGame();
renderPausedGame();
}
// Sección Game Over
else if (section->subsection == SUBSECTION_GAME_GAMEOVER) {
if (!gameOverInitialized) {
enterGameOverScreen();
}
updateGameOverScreen();
renderGameOverScreen();
}
// Sección juego jugando
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
if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) {
// Reproduce la música
if (!gameCompleted) {
if (players[0]->isAlive()) {
JA_PlayMusic(gameMusic);
}
}
}
#ifdef PAUSE
if (!pause)
update();
#else
// Actualiza la lógica del juego
update();
#endif
// Dibuja los objetos // Dibuja los objetos
buildingsSprite->setPosX(0); render();
buildingsSprite->setWidth(1); }
buildingsSprite->setSpriteClip(0, 0, 1, 160); }
renderBackground();
buildingsSprite->setPosX(255); // Indica si el juego ha terminado
buildingsSprite->setSpriteClip(255, 0, 1, 160); bool Game::hasFinished() const {
buildingsSprite->render(); return section->name != SECTION_PROG_GAME;
}
buildingsSprite->setPosX(v[n]); // Procesa un evento individual
buildingsSprite->setWidth(256); void Game::handleEvent(SDL_Event *event) {
buildingsSprite->setSpriteClip(0, 0, 256, 160); // SDL_EVENT_QUIT ya lo maneja Director
buildingsSprite->render();
grassSprite->render(); if (event->type == SDL_EVENT_WINDOW_FOCUS_LOST) {
renderBalloons(); // Solo pausar durante el juego activo (no en demo, game over, ni ya en pausa)
renderBullets(); if (!demo.enabled && (section->subsection == SUBSECTION_GAME_PLAY_1P || section->subsection == SUBSECTION_GAME_PLAY_2P)) {
renderItems(); section->subsection = SUBSECTION_GAME_PAUSE;
renderPlayers(); }
renderScoreBoard(); }
// Vuelca el contenido del renderizador en pantalla #ifdef PAUSE
screen->blit(); if (event->type == SDL_EVENT_KEY_DOWN) {
SDL_Delay(50); 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 // Bucle para el juego
void Game::run() { void Game::run() {
while (section->name == SECTION_PROG_GAME) { while (!hasFinished()) {
// Sección juego en pausa iterate();
if (section->subsection == SUBSECTION_GAME_PAUSE) {
runPausedGame();
}
// Sección Game Over
if (section->subsection == SUBSECTION_GAME_GAMEOVER) {
runGameOverScreen();
}
// Sección juego jugando
if ((section->subsection == SUBSECTION_GAME_PLAY_1P) || (section->subsection == SUBSECTION_GAME_PLAY_2P)) {
// Si la música no está sonando
if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) {
// Reproduce la música
if (!gameCompleted) {
if (players[0]->isAlive()) {
JA_PlayMusic(gameMusic);
}
}
}
#ifdef PAUSE
if (!pause)
update();
#else
// Actualiza la lógica del juego
update();
#endif
// Comprueba los eventos que hay en cola
checkEvents();
// Dibuja los objetos
render();
}
} }
} }
@@ -3045,8 +3142,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 +3155,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 +3173,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 +3198,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 +3212,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 +3223,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 +3287,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

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();
@@ -519,4 +556,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

@@ -20,10 +20,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 +96,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 +142,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 +155,31 @@ bool Input::checkAnyInput(int device, int index) {
return false; return false;
} }
// Busca si hay un mando conectado // Construye el nombre visible de un mando
std::string Input::buildControllerName(SDL_Gamepad *pad, int padIndex) {
(void)padIndex;
const char *padName = SDL_GetGamepadName(pad);
std::string name = padName ? padName : "Unknown";
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 +194,38 @@ 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
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 +242,66 @@ 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;
}
}
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

@@ -57,7 +57,8 @@ 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

@@ -17,8 +17,6 @@
#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
@@ -62,12 +60,13 @@ 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
@@ -99,15 +98,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,23 +208,28 @@ 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
if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
screen->switchVideoMode(); screen->switchVideoMode();
} }
@@ -242,8 +244,7 @@ void Instructions::checkInput() {
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(); JA_StopMusic();
section->name = SECTION_PROG_TITLE; finished = true;
section->subsection = SUBSECTION_TITLE_1;
} else { } else {
if (counter > 30) { if (counter > 30) {
manualQuit = true; manualQuit = true;
@@ -252,13 +253,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

@@ -34,21 +34,14 @@ class Instructions {
section_t *section; // Estado del bucle principal para saber si continua o se sale section_t *section; // Estado del bucle principal para saber si continua o se sale
// Variables // Variables
Uint16 counter; // Contador Uint16 counter; // Contador
Uint16 counterEnd; // Valor final para el contador Uint16 counterEnd; // Valor final para el contador
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
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

@@ -153,6 +153,8 @@ 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
@@ -183,6 +185,7 @@ bool Intro::loadMedia() {
// 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,15 +194,17 @@ 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->switchVideoMode();
} }
@@ -403,8 +408,17 @@ void Intro::run() {
JA_PlayMusic(music, 0); JA_PlayMusic(music, 0);
while (section->name == SECTION_PROG_INTRO) { while (section->name == SECTION_PROG_INTRO) {
update(); iterate();
checkEvents();
render();
} }
} }
// Ejecuta un frame
void Intro::iterate() {
update();
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

@@ -38,6 +38,8 @@ 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
@@ -59,6 +61,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,15 +70,17 @@ 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->switchVideoMode();
} }
@@ -144,8 +149,17 @@ void Logo::run() {
JA_StopMusic(); JA_StopMusic();
while (section->name == SECTION_PROG_LOGO) { while (section->name == SECTION_PROG_LOGO) {
update(); iterate();
checkEvents();
render();
} }
} }
// Ejecuta un frame
void Logo::iterate() {
update();
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);
} }

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

View File

@@ -5,7 +5,10 @@
#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 "text.h" // for Text, TXT_CENTER, TXT_COLOR, TXT_STROKE
// 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 +22,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};
@@ -42,12 +40,18 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
// Establece el modo de video // Establece el modo de video
setVideoMode(options->videoMode); setVideoMode(options->videoMode);
// Inicializa variables // Inicializa el sistema de notificaciones
notifyActive = false; notificationText = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer);
notificationMessage = "";
notificationTextColor = {0xFF, 0xFF, 0xFF};
notificationOutlineColor = {0x00, 0x00, 0x00};
notificationEndTime = 0;
notificationY = 2;
} }
// Destructor // Destructor
Screen::~Screen() { Screen::~Screen() {
delete notificationText;
SDL_DestroyTexture(gameCanvas); SDL_DestroyTexture(gameCanvas);
} }
@@ -64,6 +68,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);
@@ -86,8 +94,10 @@ void Screen::setVideoMode(int videoMode) {
// Si está activo el modo ventana quita el borde // Si está activo el modo ventana quita el borde
if (videoMode == 0) { if (videoMode == 0) {
// Muestra el puntero // Muestra el puntero y reinicia el temporizador de inactividad
SDL_ShowCursor(); SDL_ShowCursor();
Mouse::cursorVisible = true;
Mouse::lastMouseMoveTime = SDL_GetTicks();
// Esconde la ventana // Esconde la ventana
// SDL_HideWindow(window); // SDL_HideWindow(window);
@@ -104,6 +114,17 @@ void Screen::setVideoMode(int videoMode) {
dest = {0, 0, gameCanvasWidth, gameCanvasHeight}; dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
} }
#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.
constexpr int WASM_RENDER_SCALE = 3;
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);
@@ -116,6 +137,7 @@ void Screen::setVideoMode(int videoMode) {
else if (videoMode == SDL_WINDOW_FULLSCREEN) { else if (videoMode == SDL_WINDOW_FULLSCREEN) {
// Oculta el puntero // Oculta el puntero
SDL_HideCursor(); SDL_HideCursor();
Mouse::cursorVisible = false;
// Obten el alto y el ancho de la ventana // Obten el alto y el ancho de la ventana
SDL_GetWindowSize(window, &windowWidth, &windowHeight); SDL_GetWindowSize(window, &windowWidth, &windowHeight);
@@ -218,116 +240,31 @@ void Screen::switchBorder() {
setVideoMode(0); setVideoMode(0);
} }
// Activa el fade // Muestra una notificación en la línea superior durante durationMs
void Screen::setFade() { void Screen::notify(const std::string &text, color_t textColor, color_t outlineColor, Uint32 durationMs) {
fade = true; notificationMessage = text;
notificationTextColor = textColor;
notificationOutlineColor = outlineColor;
notificationEndTime = SDL_GetTicks() + durationMs;
} }
// Comprueba si ha terminado el fade // Limpia la notificación actual
bool Screen::fadeEnded() { void Screen::clearNotification() {
if (fade || fadeCounter > 0) { notificationEndTime = 0;
return false; notificationMessage.clear();
}
return true;
} }
// Activa el spectrum fade // Dibuja la notificación activa (si la hay) sobre el gameCanvas
void Screen::setspectrumFade() { void Screen::renderNotification() {
spectrumFade = true; if (SDL_GetTicks() >= notificationEndTime) {
}
// 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,
// Dibuja el fade notificationOutlineColor);
void Screen::renderFade() {
if (!fade) {
return;
}
const SDL_FRect rect = {0, 0, (float)gameCanvasWidth, (float)gameCanvasHeight};
color_t color = {0, 0, 0};
const float step = (float)fadeCounter / (float)fadeLenght;
const int alpha = 0 + (255 - 0) * step;
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha);
SDL_RenderFillRect(renderer, &rect);
}
// Inicializa las variables para el fade spectrum
void Screen::iniSpectrumFade() {
spectrumFade = false;
spectrumFadeCounter = 0;
spectrumFadeLenght = 50;
spectrumColor.clear();
// Inicializa el vector de colores
const std::vector<std::string> vColors = {"black", "blue", "red", "magenta", "green", "cyan", "yellow", "bright_white"};
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,10 +2,11 @@
#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;
@@ -21,44 +22,25 @@ class Screen {
options_t *options; // Variable con todas las opciones del programa options_t *options; // Variable con todas las opciones del programa
// Variables // Variables
int windowWidth; // Ancho de la pantalla o ventana int windowWidth; // Ancho de la pantalla o ventana
int windowHeight; // Alto de la pantalla o ventana int windowHeight; // Alto de la pantalla o ventana
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; // Anltura 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 // Dibuja la notificación activa (si la hay) sobre el gameCanvas
void iniFade(); void renderNotification();
// 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: public:
// Constructor // Constructor
@@ -107,21 +89,10 @@ class Screen {
// Cambia entre borde visible y no visible // Cambia entre borde visible y no visible
void switchBorder(); void switchBorder();
// Activa el fade // Muestra una notificación en la línea superior del canvas durante durationMs.
void setFade(); // Sobrescribe cualquier notificación activa (sin apilación).
void notify(const std::string &text, color_t textColor, color_t outlineColor, Uint32 durationMs);
// Comprueba si ha terminado el fade // Limpia la notificación actual
bool fadeEnded(); void clearNotification();
// 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

@@ -59,6 +59,12 @@ Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset,
#endif #endif
menu.playerSelect = new Menu(renderer, asset, input, asset->get("player_select.men")); menu.playerSelect = new Menu(renderer, asset, input, asset->get("player_select.men"));
#ifdef __EMSCRIPTEN__
// 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
// Sonidos // Sonidos
crashSound = JA_LoadSound(asset->get("title.wav").c_str()); crashSound = JA_LoadSound(asset->get("title.wav").c_str());
@@ -120,6 +126,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 +268,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 +320,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;
@@ -482,13 +499,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 +509,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,48 +549,32 @@ 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); // Prepara para empezar a dibujar en la textura de juego
screen->start();
// Agita la pantalla // Limpia la pantalla
const int v[] = {-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 0}; screen->clean(bgColor);
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
screen->start();
// Limpia la pantalla // Dibuja el tileado de fondo
screen->clean(bgColor); {
SDL_FRect fSrc = {(float)backgroundWindow.x, (float)backgroundWindow.y, (float)backgroundWindow.w, (float)backgroundWindow.h};
SDL_RenderTexture(renderer, background, &fSrc, nullptr);
};
// Dibuja el tileado de fondo // Dibuja el degradado
{ gradient->render();
SDL_FRect fSrc = {(float)backgroundWindow.x, (float)backgroundWindow.y, (float)backgroundWindow.w, (float)backgroundWindow.h};
SDL_RenderTexture(renderer, background, &fSrc, nullptr);
};
// Dibuja el degradado // Dibuja los objetos (posiciones ya actualizadas por update)
gradient->render(); coffeeBitmap->render();
crisisBitmap->render();
// Dibuja los objetos dustBitmapR->render();
coffeeBitmap->setPosX(a + v[n / 3]); dustBitmapL->render();
crisisBitmap->setPosX(b + v[n / 3]);
coffeeBitmap->render();
crisisBitmap->render();
dustBitmapR->update(); // Vuelca el contenido del renderizador en pantalla
dustBitmapL->update(); screen->blit();
dustBitmapR->render(); } break;
dustBitmapL->render();
// Vuelca el contenido del renderizador en pantalla
screen->blit();
}
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,11 +654,12 @@ 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->switchVideoMode();
} }
@@ -865,6 +860,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);
@@ -900,27 +899,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
update(); if (instructionsActive) {
checkEvents(); instructions->update();
render(); 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();
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;
}
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;
}
} }
} }
// Ejecuta la parte donde se muestran las instrucciones // Bucle para el titulo del juego (compatibilidad)
void Title::runInstructions(mode_e mode) { void Title::run() {
instructions = new Instructions(renderer, screen, asset, input, lang, section); while (section->name == SECTION_PROG_TITLE || instructionsActive || demoGameActive) {
instructions->run(mode); iterate();
delete instructions; }
} }
// Ejecuta el juego en modo demo // Inicia la parte donde se muestran las instrucciones
void Title::runInstructions(mode_e mode) {
instructions = new Instructions(renderer, screen, asset, input, lang, section);
instructions->start(mode);
instructionsActive = true;
instructionsMode = mode;
}
// 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 +1105,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);
}; };