diff --git a/source/common/jail_audio.cpp b/source/common/jail_audio.cpp index 7ce9aec..42907fe 100644 --- a/source/common/jail_audio.cpp +++ b/source/common/jail_audio.cpp @@ -1,3 +1,4 @@ +#ifndef JA_USESDLMIXER #include "jail_audio.h" #include "stb_vorbis.c" #include @@ -11,7 +12,7 @@ struct JA_Sound_t { }; struct JA_Channel_t { - JA_Sound sound; + JA_Sound_t *sound; int pos {0}; int times {0}; JA_Channel_state state { JA_CHANNEL_FREE }; @@ -25,7 +26,7 @@ struct JA_Music_t { JA_Music_state state {JA_MUSIC_INVALID}; }; -JA_Music current_music{NULL}; +JA_Music_t *current_music{NULL}; JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS]; int JA_freq {48000}; @@ -86,7 +87,7 @@ void JA_Quit() { sdlAudioDevice = 0; } -JA_Music JA_LoadMusic(const char* filename) { +JA_Music_t *JA_LoadMusic(const char* filename) { int chan, samplerate; // [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid. @@ -98,7 +99,7 @@ JA_Music JA_LoadMusic(const char* filename) { if (fread(buffer, fsize, 1, f)!=1) return NULL; fclose(f); - JA_Music music = new JA_Music_t(); + JA_Music_t *music = new JA_Music_t(); music->samples = stb_vorbis_decode_memory(buffer, fsize, &chan, &samplerate, &music->output); free(buffer); @@ -121,7 +122,7 @@ JA_Music JA_LoadMusic(const char* filename) { return music; } -void JA_PlayMusic(JA_Music music, const int loop) { +void JA_PlayMusic(JA_Music_t *music, const int loop) { if (current_music != NULL) { current_music->pos = 0; current_music->state = JA_MUSIC_STOPPED; @@ -153,21 +154,21 @@ JA_Music_state JA_GetMusicState() { return current_music->state; } -void JA_DeleteMusic(JA_Music music) { +void JA_DeleteMusic(JA_Music_t *music) { if (current_music == music) current_music = NULL; free(music->output); delete music; } -JA_Sound JA_NewSound(Uint8* buffer, Uint32 length) { - JA_Sound sound = new JA_Sound_t(); +JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) { + JA_Sound_t *sound = new JA_Sound_t(); sound->buffer = buffer; sound->length = length; return sound; } -JA_Sound JA_LoadSound(const char* filename) { - JA_Sound sound = new JA_Sound_t(); +JA_Sound_t *JA_LoadSound(const char* filename) { + JA_Sound_t *sound = new JA_Sound_t(); SDL_AudioSpec wavSpec; SDL_LoadWAV(filename, &wavSpec, &sound->buffer, &sound->length); @@ -184,7 +185,7 @@ JA_Sound JA_LoadSound(const char* filename) { return sound; } -int JA_PlaySound(JA_Sound sound, const int loop) { +int JA_PlaySound(JA_Sound_t *sound, const int loop) { int channel = 0; while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; } if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0; @@ -196,7 +197,7 @@ int JA_PlaySound(JA_Sound sound, const int loop) { return channel; } -void JA_DeleteSound(JA_Sound sound) { +void JA_DeleteSound(JA_Sound_t *sound) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { if (channels[i].sound == sound) JA_StopChannel(i); } @@ -246,4 +247,5 @@ JA_Channel_state JA_GetChannelState(const int channel) { int JA_SetVolume(int volume) { JA_volume = volume > 128 ? 128 : volume < 0 ? 0 : volume; return JA_volume; -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/source/common/jail_audio.h b/source/common/jail_audio.h index 7d03400..4c4d7f9 100644 --- a/source/common/jail_audio.h +++ b/source/common/jail_audio.h @@ -4,27 +4,27 @@ enum JA_Channel_state { JA_CHANNEL_INVALID, JA_CHANNEL_FREE, JA_CHANNEL_PLAYING, JA_CHANNEL_PAUSED }; enum JA_Music_state { JA_MUSIC_INVALID, JA_MUSIC_PLAYING, JA_MUSIC_PAUSED, JA_MUSIC_STOPPED }; -typedef struct JA_Sound_t *JA_Sound; -typedef struct JA_Music_t *JA_Music; +struct JA_Sound_t; +struct JA_Music_t; void JA_Init(const int freq, const SDL_AudioFormat format, const int channels); void JA_Quit(); -JA_Music JA_LoadMusic(const char* filename); -void JA_PlayMusic(JA_Music music, const int loop = -1); +JA_Music_t *JA_LoadMusic(const char* filename); +void JA_PlayMusic(JA_Music_t *music, const int loop = -1); void JA_PauseMusic(); void JA_ResumeMusic(); void JA_StopMusic(); JA_Music_state JA_GetMusicState(); -void JA_DeleteMusic(JA_Music music); +void JA_DeleteMusic(JA_Music_t *music); -JA_Sound JA_NewSound(Uint8* buffer, Uint32 length); -JA_Sound JA_LoadSound(const char* filename); -int JA_PlaySound(JA_Sound sound, const int loop = 0); +JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length); +JA_Sound_t *JA_LoadSound(const char* filename); +int JA_PlaySound(JA_Sound_t *sound, const int loop = 0); void JA_PauseChannel(const int channel); void JA_ResumeChannel(const int channel); void JA_StopChannel(const int channel); JA_Channel_state JA_GetChannelState(const int channel); -void JA_DeleteSound(JA_Sound sound); +void JA_DeleteSound(JA_Sound_t *sound); int JA_SetVolume(int volume); \ No newline at end of file diff --git a/source/common/jail_audio_sdlmixer.cpp b/source/common/jail_audio_sdlmixer.cpp new file mode 100644 index 0000000..ddb6b61 --- /dev/null +++ b/source/common/jail_audio_sdlmixer.cpp @@ -0,0 +1,101 @@ +#ifdef JA_USESDLMIXER +#include "jail_audio.h" +#include +#include +#include + +struct JA_Sound_t {}; // Dummy structs +struct JA_Music_t {}; + +int JA_freq {48000}; +SDL_AudioFormat JA_format {AUDIO_S16}; +Uint8 JA_channels {2}; + +void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) { + JA_freq = freq; + JA_format = format; + JA_channels = channels; + Mix_OpenAudio(JA_freq, JA_format, JA_channels, 1024); +} + +void JA_Quit() { + Mix_CloseAudio(); +} + +JA_Music_t *JA_LoadMusic(const char* filename) { + return (JA_Music_t*)Mix_LoadMUS(filename); +} + +void JA_PlayMusic(JA_Music_t *music, const int loop) { + Mix_PlayMusic((Mix_Music*)music, loop); +} + +void JA_PauseMusic() { + Mix_PauseMusic(); +} + +void JA_ResumeMusic() { + Mix_ResumeMusic(); +} + +void JA_StopMusic() { + Mix_HaltMusic(); +} + +JA_Music_state JA_GetMusicState() { + if (Mix_PausedMusic()) { + return JA_MUSIC_PAUSED; + } else if (Mix_PlayingMusic()) { + return JA_MUSIC_PLAYING; + } else { + return JA_MUSIC_STOPPED; + } +} + +void JA_DeleteMusic(JA_Music_t *music) { + Mix_FreeMusic((Mix_Music*)music); +} + +JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) { + return NULL; +} + +JA_Sound_t *JA_LoadSound(const char* filename) { + JA_Sound_t *sound = (JA_Sound_t*)Mix_LoadWAV(filename); + return sound; +} + +int JA_PlaySound(JA_Sound_t *sound, const int loop) { + return Mix_PlayChannel(-1, (Mix_Chunk*)sound, loop); +} + +void JA_DeleteSound(JA_Sound_t *sound) { + Mix_FreeChunk((Mix_Chunk*)sound); +} + +void JA_PauseChannel(const int channel) { + Mix_Pause(channel); +} + +void JA_ResumeChannel(const int channel) { + Mix_Resume(channel); +} + +void JA_StopChannel(const int channel) { + Mix_HaltChannel(channel); +} + +JA_Channel_state JA_GetChannelState(const int channel) { + if (Mix_Paused(channel)) { + return JA_CHANNEL_PAUSED; + } else if (Mix_Playing(channel)) { + return JA_CHANNEL_PLAYING; + } else { + return JA_CHANNEL_FREE; + } +} + +int JA_SetVolume(int volume) { + return Mix_Volume(-1, volume); +} +#endif diff --git a/source/common/menu.h b/source/common/menu.h index 5157187..5534b65 100644 --- a/source/common/menu.h +++ b/source/common/menu.h @@ -89,9 +89,9 @@ private: bool isCenteredOnY; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje Y bool areElementsCenteredOnX; // Variable para saber si los elementos van centrados en el eje X int widestItem; // Anchura del elemento más ancho - JA_Sound soundAccept; // Sonido al aceptar o elegir una opción del menu - JA_Sound soundCancel; // Sonido al cancelar el menu - JA_Sound soundMove; // Sonido al mover el selector + JA_Sound_t* soundAccept; // Sonido al aceptar o elegir una opción del menu + JA_Sound_t* soundCancel; // Sonido al cancelar el menu + JA_Sound_t* soundMove; // Sonido al mover el selector color_t colorGreyed; // Color para los elementos agrisados rectangle_t rectBG; // Rectangulo de fondo del menu std::vector item; // Estructura para cada elemento del menu diff --git a/source/game.cpp b/source/game.cpp index 6420bd9..de26da7 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -1806,7 +1806,7 @@ void Game::updateDeath() { // Hace sonar aleatoriamente uno de los 4 sonidos de burbujas const Uint8 index = rand() % 4; - const JA_Sound sound[4] = {bubble1Sound, bubble2Sound, bubble3Sound, bubble4Sound}; + const JA_Sound_t* sound[4] = {bubble1Sound, bubble2Sound, bubble3Sound, bubble4Sound}; JA_PlaySound(sound[index], 0); } } diff --git a/source/game.h b/source/game.h index ed064d1..897be19 100644 --- a/source/game.h +++ b/source/game.h @@ -174,23 +174,23 @@ private: Sprite *gameOverSprite; // Sprite para dibujar los graficos del game over Sprite *gameOverEndSprite; // Sprite para dibujar los graficos del game over de acabar el juego - JA_Sound balloonSound; // Sonido para la explosión del globo - JA_Sound bulletSound; // Sonido para los disparos - JA_Sound playerCollisionSound; // Sonido para la colisión del jugador con un enemigo - JA_Sound hiScoreSound; // Sonido para cuando se alcanza la máxima puntuación - JA_Sound itemDropSound; // Sonido para cuando se genera un item - JA_Sound itemPickUpSound; // Sonido para cuando se recoge un item - JA_Sound coffeeOutSound; // Sonido para cuando el jugador pierde el café al recibir un impacto - JA_Sound stageChangeSound; // Sonido para cuando se cambia de fase - JA_Sound bubble1Sound; // Sonido para cuando el jugador muere - JA_Sound bubble2Sound; // Sonido para cuando el jugador muere - JA_Sound bubble3Sound; // Sonido para cuando el jugador muere - JA_Sound bubble4Sound; // Sonido para cuando el jugador muere - JA_Sound clockSound; // Sonido para cuando se detiene el tiempo con el item reloj - JA_Sound powerBallSound; // Sonido para cuando se explota una Power Ball - JA_Sound coffeeMachineSound; // Sonido para cuando la máquina de café toca el suelo + JA_Sound_t* balloonSound; // Sonido para la explosión del globo + JA_Sound_t* bulletSound; // Sonido para los disparos + JA_Sound_t* playerCollisionSound; // Sonido para la colisión del jugador con un enemigo + JA_Sound_t* hiScoreSound; // Sonido para cuando se alcanza la máxima puntuación + JA_Sound_t* itemDropSound; // Sonido para cuando se genera un item + JA_Sound_t* itemPickUpSound; // Sonido para cuando se recoge un item + JA_Sound_t* coffeeOutSound; // Sonido para cuando el jugador pierde el café al recibir un impacto + JA_Sound_t* stageChangeSound; // Sonido para cuando se cambia de fase + JA_Sound_t* bubble1Sound; // Sonido para cuando el jugador muere + JA_Sound_t* bubble2Sound; // Sonido para cuando el jugador muere + JA_Sound_t* bubble3Sound; // Sonido para cuando el jugador muere + JA_Sound_t* bubble4Sound; // Sonido para cuando el jugador muere + JA_Sound_t* clockSound; // Sonido para cuando se detiene el tiempo con el item reloj + JA_Sound_t* powerBallSound; // Sonido para cuando se explota una Power Ball + JA_Sound_t* coffeeMachineSound; // Sonido para cuando la máquina de café toca el suelo - JA_Music gameMusic; // Musica de fondo + JA_Music_t* gameMusic; // Musica de fondo // Variables int numPlayers; // Numero de jugadores diff --git a/source/intro.h b/source/intro.h index fa3d105..4d08bd6 100644 --- a/source/intro.h +++ b/source/intro.h @@ -32,7 +32,7 @@ private: Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa Uint8 ticksSpeed; // Velocidad a la que se repiten los bucles del programa section_t section; // Estado del bucle principal para saber si continua o se sale - JA_Music music; // Musica para la intro + JA_Music_t* music; // Musica para la intro int scene; // Indica que escena está activa // Actualiza las variables del objeto diff --git a/source/title.h b/source/title.h index 87bc4c0..d194708 100644 --- a/source/title.h +++ b/source/title.h @@ -74,8 +74,8 @@ private: Fade *fade; // Objeto para realizar fundidos en pantalla // Variable - JA_Music titleMusic; // Musica para el titulo - JA_Sound crashSound; // Sonido con el impacto del título + JA_Music_t* titleMusic; // Musica para el titulo + JA_Sound_t* crashSound; // Sonido con el impacto del título int backgroundCounter; // Temporizador para el fondo de tiles de la pantalla de titulo int counter; // Temporizador para la pantalla de titulo Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa