From 22619cf20702866b45ed3c53736e14babd8c0fbd Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 6 Nov 2024 13:49:44 +0100 Subject: [PATCH 1/6] =?UTF-8?q?Treballant=20en=20la=20conversi=C3=B3=20a?= =?UTF-8?q?=20SDL3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/actor.h | 2 +- source/config.cpp | 2 +- source/console.cpp | 2 +- source/jail_audio.cpp | 34 +++++++++++++------------ source/jail_audio.h | 2 +- source/jaudio.cpp | 4 +-- source/jdraw.cpp | 41 ++++++++++++++----------------- source/jgame.cpp | 28 ++++++++++----------- source/jinput.cpp | 28 ++++++++++++++------- source/jui.cpp | 2 +- source/jutil.cpp | 2 +- source/m_catslife.cpp | 2 +- source/m_editor_bitmap.cpp | 2 +- source/m_editor_bitmap_file.cpp | 2 +- source/m_editor_colors.cpp | 2 +- source/m_editor_map.cpp | 2 +- source/m_editor_templates.cpp | 2 +- source/m_end_prologo_sequence.cpp | 2 +- source/m_end_sequence.cpp | 2 +- source/m_gameover.cpp | 2 +- source/m_ingame.cpp | 2 +- source/m_intro.cpp | 2 +- source/m_logo.cpp | 2 +- source/m_menu.cpp | 2 +- source/m_menu_audio.cpp | 2 +- source/m_menu_gamepad.cpp | 2 +- source/m_menu_tecles.cpp | 2 +- source/m_prologo_intro.cpp | 2 +- source/main.cpp | 4 +-- 29 files changed, 97 insertions(+), 88 deletions(-) diff --git a/source/actor.h b/source/actor.h index e43b4cc..8201625 100644 --- a/source/actor.h +++ b/source/actor.h @@ -1,5 +1,5 @@ #pragma once -#include +#include #include "misc.h" #include "jdraw.h" #include diff --git a/source/config.cpp b/source/config.cpp index 85f9b01..3a04a5d 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -1,6 +1,6 @@ #include "config.h" #include "jfile.h" -#include +#include namespace config { diff --git a/source/console.cpp b/source/console.cpp index b934b5d..0c1a121 100644 --- a/source/console.cpp +++ b/source/console.cpp @@ -1,5 +1,5 @@ #include "console.h" -#include +#include #include "jdraw.h" #include "jinput.h" #include "room.h" diff --git a/source/jail_audio.cpp b/source/jail_audio.cpp index e58f22e..269b621 100644 --- a/source/jail_audio.cpp +++ b/source/jail_audio.cpp @@ -1,7 +1,7 @@ #ifndef JA_USESDLMIXER #include "jail_audio.h" #include "stb_vorbis.c" -#include +#include #include #define JA_MAX_SIMULTANEOUS_CHANNELS 5 @@ -30,23 +30,25 @@ JA_Music_t *current_music{NULL}; JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS]; int JA_freq {48000}; -SDL_AudioFormat JA_format {AUDIO_S16}; +SDL_AudioFormat JA_format {SDL_AUDIO_S16LE}; Uint8 JA_channels {2}; int JA_musicVolume = 128; int JA_soundVolume = 64; bool JA_musicEnabled = true; bool JA_soundEnabled = true; -SDL_AudioDeviceID sdlAudioDevice = 0; +SDL_AudioStream *sdlAudioStream = nullptr; -void audioCallback(void * userdata, uint8_t * stream, int len) { +void audioCallback(void * userdata, SDL_AudioStream* stream, int additional_amount, int total_amount) { + int len = total_amount; SDL_memset(stream, 0, len); if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) { const int size = SDL_min(len, current_music->samples*2-current_music->pos); - SDL_MixAudioFormat(stream, (Uint8*)(current_music->output+current_music->pos), AUDIO_S16, size, JA_musicVolume); + SDL_PutAudioStreamData(sdlAudioStream, (Uint8*)(current_music->output+current_music->pos), size); + SDL_MixAudio(stream, (Uint8*)(current_music->output+current_music->pos), SDL_AUDIO_S16LE, size, JA_musicVolume); current_music->pos += size/2; if (size < len) { if (current_music->times != 0) { - SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, JA_musicVolume); + SDL_MixAudio(stream+size, (Uint8*)current_music->output, SDL_AUDIO_S16LE, len-size, JA_musicVolume); current_music->pos = (len-size)/2; if (current_music->times > 0) current_music->times--; } else { @@ -59,11 +61,11 @@ void audioCallback(void * userdata, uint8_t * stream, int len) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { if (channels[i].state == JA_CHANNEL_PLAYING) { const int size = SDL_min(len, channels[i].sound->length - channels[i].pos); - SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, JA_soundVolume); + SDL_MixAudio(stream, channels[i].sound->buffer + channels[i].pos, SDL_AUDIO_S16LE, size, JA_soundVolume); channels[i].pos += size; if (size < len) { if (channels[i].times != 0) { - SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, JA_soundVolume); + SDL_MixAudio(stream + size, channels[i].sound->buffer, SDL_AUDIO_S16LE, len-size, JA_soundVolume); channels[i].pos = len-size; if (channels[i].times > 0) channels[i].times--; } else { @@ -78,16 +80,16 @@ void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) { JA_freq = freq; JA_format = format; JA_channels = channels; - SDL_AudioSpec audioSpec{JA_freq, JA_format, JA_channels, 0, 1024, 0, 0, audioCallback, NULL}; - if (sdlAudioDevice != 0) SDL_CloseAudioDevice(sdlAudioDevice); - sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0); - SDL_PauseAudioDevice(sdlAudioDevice, 0); + SDL_AudioSpec audioSpec{JA_format, JA_channels, JA_freq}; //, 0, 1024, 0, 0, audioCallback, NULL}; + if (sdlAudioStream) SDL_DestroyAudioStream(sdlAudioStream); + sdlAudioStream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audioSpec, audioCallback, NULL); + SDL_PauseAudioStreamDevice(sdlAudioStream); } void JA_Quit() { - SDL_PauseAudioDevice(sdlAudioDevice, 1); - if (sdlAudioDevice != 0) SDL_CloseAudioDevice(sdlAudioDevice); - sdlAudioDevice = 0; + SDL_ResumeAudioStreamDevice(sdlAudioStream); + if (sdlAudioStream) SDL_DestroyAudioStream(sdlAudioStream); + sdlAudioStream = nullptr; } JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length) @@ -212,7 +214,7 @@ JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) { JA_Sound_t *JA_LoadSound(uint8_t* buffer, uint32_t size) { JA_Sound_t *sound = new JA_Sound_t(); SDL_AudioSpec wavSpec; - SDL_LoadWAV_RW(SDL_RWFromMem(buffer, size),1, &wavSpec, &sound->buffer, &sound->length); + SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size),1, &wavSpec, &sound->buffer, &sound->length); SDL_AudioCVT cvt; SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq); diff --git a/source/jail_audio.h b/source/jail_audio.h index eac8a82..ae2ef76 100644 --- a/source/jail_audio.h +++ b/source/jail_audio.h @@ -1,5 +1,5 @@ #pragma once -#include +#include enum JA_Channel_state { JA_CHANNEL_INVALID, JA_CHANNEL_FREE, JA_CHANNEL_PLAYING, JA_CHANNEL_PAUSED, JA_SOUND_DISABLED }; enum JA_Music_state { JA_MUSIC_INVALID, JA_MUSIC_PLAYING, JA_MUSIC_PAUSED, JA_MUSIC_STOPPED, JA_MUSIC_DISABLED }; diff --git a/source/jaudio.cpp b/source/jaudio.cpp index 6921d1b..8e92dfd 100644 --- a/source/jaudio.cpp +++ b/source/jaudio.cpp @@ -1,6 +1,6 @@ #include "jaudio.h" #include "jail_audio.h" -#include +#include #include #include "jfile.h" #include @@ -22,7 +22,7 @@ namespace audio // Inicialitza el sistema de só void init() { - JA_Init(48000, AUDIO_S16, 2); + JA_Init(48000, SDL_AUDIO_S16LE, 2); //Mix_Init(MIX_INIT_OGG);// | MIX_INIT_WAVPACK); //Mix_OpenAudio(48000, AUDIO_S16, 2, 512); } diff --git a/source/jdraw.cpp b/source/jdraw.cpp index ddd03d8..e9b10b2 100644 --- a/source/jdraw.cpp +++ b/source/jdraw.cpp @@ -1,5 +1,5 @@ #include "jdraw.h" -#include +#include #include "gif.c" #include "jfile.h" #include @@ -15,7 +15,7 @@ namespace draw SDL_Renderer *sdl_renderer = nullptr; // El renderer de SDL SDL_Texture *sdl_texture = nullptr; // La textura de SDL a la que pintarem la nostra superficie "screen" i que despres volcarem a pantalla - SDL_Rect dest_rect = {0, 0, 320, 240}; + SDL_FRect dest_rect = {0, 0, 320, 240}; static int fullscreen_scale = 1; static int screen_zoom = 1; @@ -89,23 +89,22 @@ namespace draw // [TODO] Incloure gestió de pantalla completa // Inicialització de les estructures de SDL - sdl_window = SDL_CreateWindow(screen_title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width * screen_zoom, screen_height * screen_zoom, screen_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_SHOWN); - sdl_renderer = SDL_CreateRenderer(sdl_window, -1, 0); + sdl_window = SDL_CreateWindow(screen_title.c_str(), screen_width * screen_zoom, screen_height * screen_zoom, screen_fullscreen ? SDL_WINDOW_FULLSCREEN : 0); + sdl_renderer = SDL_CreateRenderer(sdl_window, nullptr); sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen_width, screen_height); - SDL_RendererInfo info; + char *driver_name; const int num_render_drivers = SDL_GetNumRenderDrivers(); printf("Available renderers:\n"); for (int i=0; i dm.w) return; - if (screen_height*value > dm.h) return; + if (screen_width*value > dm->w) return; + if (screen_height*value > dm->h) return; screen_zoom = value; reinit(); @@ -707,7 +704,7 @@ namespace draw } // Pintem la textura a pantalla - SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, &dest_rect); + SDL_RenderTexture(sdl_renderer, sdl_texture, NULL, &dest_rect); if (screen_mode & SCREEN_MODE_SCANLINES) { @@ -718,14 +715,14 @@ namespace draw for (int y=0; y3) SDL_RenderDrawLine(sdl_renderer, dest_rect.x, 1+dest_rect.y + y*zoom, dest_rect.x + dest_rect.w, 1+dest_rect.y + y*zoom); + if (zoom>3) SDL_RenderLine(sdl_renderer, dest_rect.x, 1+dest_rect.y + y*zoom, dest_rect.x + dest_rect.w, 1+dest_rect.y + y*zoom); } SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 16); for (int x=0; x +#include namespace game { @@ -34,7 +34,7 @@ int main(int argc, char *argv[]) game::param_count = argc; game::params = argv; - SDL_Init(SDL_INIT_EVERYTHING); + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD); input::init(); audio::init(); @@ -47,26 +47,26 @@ int main(int argc, char *argv[]) { while(SDL_PollEvent(&e)) { - if (e.type==SDL_QUIT) { game::should_exit = true; break; } - if (e.type==SDL_KEYDOWN) + if (e.type==SDL_EVENT_QUIT) { game::should_exit = true; break; } + if (e.type==SDL_EVENT_KEY_DOWN) { - input::updateKey(e.key.keysym.scancode); + input::updateKey(e.key.scancode); } - if (e.type==SDL_KEYUP) + if (e.type==SDL_EVENT_KEY_UP) { - input::updateKeypressed(e.key.keysym.scancode); + input::updateKeypressed(e.key.scancode); } - if (e.type==SDL_MOUSEBUTTONUP) + if (e.type==SDL_EVENT_MOUSE_BUTTON_UP) { input::updateClk(e.button.button); } - if (e.type==SDL_MOUSEWHEEL) + if (e.type==SDL_EVENT_MOUSE_WHEEL) { input::updateWheel(e.wheel.y); } - if (e.type==SDL_CONTROLLERBUTTONDOWN) { - input::updatePadBtn(e.cbutton.button); - input::updatePadBtnPressed(e.cbutton.button); + if (e.type==SDL_EVENT_GAMEPAD_BUTTON_DOWN) { + input::updatePadBtn(e.gbutton.button); + input::updatePadBtnPressed(e.gbutton.button); } } @@ -78,8 +78,8 @@ int main(int argc, char *argv[]) input::updateKeypressed(SDL_SCANCODE_UNKNOWN); input::updateClk(0); input::updateWheel(0); - input::updatePadBtn(SDL_CONTROLLER_BUTTON_INVALID); - input::updatePadBtnPressed(SDL_CONTROLLER_BUTTON_INVALID); + input::updatePadBtn(SDL_GAMEPAD_BUTTON_INVALID); + input::updatePadBtnPressed(SDL_GAMEPAD_BUTTON_INVALID); } } diff --git a/source/jinput.cpp b/source/jinput.cpp index a361976..9ec3abc 100644 --- a/source/jinput.cpp +++ b/source/jinput.cpp @@ -1,16 +1,16 @@ #include "jinput.h" -#include +#include #include "jdraw.h" #include "jfile.h" namespace input { - static const uint8_t *keys = nullptr; + static const bool *keys = nullptr; static uint8_t keypressed = 0; static uint8_t keydown = 0; static uint8_t btnClicked = 0; static int wheel = 0; - static SDL_GameController *gamepad = NULL; + static SDL_Gamepad *gamepad = NULL; static int8_t pad_btn_pressed = SDL_CONTROLLER_BUTTON_INVALID; static int8_t pad_btn_down = SDL_CONTROLLER_BUTTON_INVALID; @@ -18,14 +18,23 @@ namespace input { int size; char *buffer = file::getFileBuffer("gamecontrollerdb.txt", size); - if (SDL_GameControllerAddMappingsFromRW(SDL_RWFromMem(buffer, size), 1) < 0) printf("No s'ha pogut carregar el gamecontrollersdb.txt\n"); + if (SDL_AddGamepadMappingsFromIO(SDL_IOFromMem(buffer, size), 1) < 0) printf("No s'ha pogut carregar el gamecontrollersdb.txt\n"); free(buffer); + int num_gamepads; + SDL_JoystickID *gamepads = SDL_GetGamepads(&num_gamepads); + if (num_gamepads>0) + { + gamepad = SDL_OpenGamepad(gamepads[0]); + SDL_SetGamepadEventsEnabled(true); + SDL_free(gamepads); + } +/* const int num_joysticks = SDL_NumJoysticks(); if (num_joysticks>=1) { for (int i=0; i -#include +#include namespace ui { diff --git a/source/jutil.cpp b/source/jutil.cpp index b9a19da..2f4b2e1 100644 --- a/source/jutil.cpp +++ b/source/jutil.cpp @@ -1,7 +1,7 @@ #include "jutil.h" #include #include -#include +#include namespace util { int stringToInt(const char *value, std::vector strings, std::vector values) diff --git a/source/m_catslife.cpp b/source/m_catslife.cpp index ce99c48..753be04 100644 --- a/source/m_catslife.cpp +++ b/source/m_catslife.cpp @@ -6,7 +6,7 @@ #include "actor.h" #include "room.h" #include "config.h" -#include +#include namespace modules { diff --git a/source/m_editor_bitmap.cpp b/source/m_editor_bitmap.cpp index 9f05126..f642006 100644 --- a/source/m_editor_bitmap.cpp +++ b/source/m_editor_bitmap.cpp @@ -1,7 +1,7 @@ #include "m_editor_bitmap.h" #include "jdraw.h" #include "jinput.h" -#include +#include #include "room.h" #include "actor.h" #include "m_game.h" diff --git a/source/m_editor_bitmap_file.cpp b/source/m_editor_bitmap_file.cpp index 22106d7..bb50ec2 100644 --- a/source/m_editor_bitmap_file.cpp +++ b/source/m_editor_bitmap_file.cpp @@ -2,7 +2,7 @@ #include "jdraw.h" #include "jinput.h" #include "jfile.h" -#include +#include #include "room.h" #include "actor.h" #include "m_game.h" diff --git a/source/m_editor_colors.cpp b/source/m_editor_colors.cpp index 1499a6a..90d5c3e 100644 --- a/source/m_editor_colors.cpp +++ b/source/m_editor_colors.cpp @@ -1,7 +1,7 @@ #include "m_editor_colors.h" #include "jdraw.h" #include "jinput.h" -#include +#include #include "room.h" namespace modules diff --git a/source/m_editor_map.cpp b/source/m_editor_map.cpp index 7d278cf..1e4423a 100644 --- a/source/m_editor_map.cpp +++ b/source/m_editor_map.cpp @@ -2,7 +2,7 @@ #include "jdraw.h" #include "jinput.h" #include "misc.h" -#include +#include #include "room.h" #include "actor.h" diff --git a/source/m_editor_templates.cpp b/source/m_editor_templates.cpp index 3db8565..9f945d0 100644 --- a/source/m_editor_templates.cpp +++ b/source/m_editor_templates.cpp @@ -2,7 +2,7 @@ #include "jdraw.h" #include "jinput.h" #include "misc.h" -#include +#include #include "actor.h" #include #include "room.h" diff --git a/source/m_end_prologo_sequence.cpp b/source/m_end_prologo_sequence.cpp index b0732bd..48fa8ef 100644 --- a/source/m_end_prologo_sequence.cpp +++ b/source/m_end_prologo_sequence.cpp @@ -5,7 +5,7 @@ #include "jaudio.h" #include "controller.h" #include "config.h" -#include +#include #include "actor.h" namespace modules diff --git a/source/m_end_sequence.cpp b/source/m_end_sequence.cpp index 92b22d5..4b2c40c 100644 --- a/source/m_end_sequence.cpp +++ b/source/m_end_sequence.cpp @@ -5,7 +5,7 @@ #include "jaudio.h" #include "controller.h" #include "config.h" -#include +#include #include "actor.h" namespace modules diff --git a/source/m_gameover.cpp b/source/m_gameover.cpp index 0424dda..29c9b6f 100644 --- a/source/m_gameover.cpp +++ b/source/m_gameover.cpp @@ -6,7 +6,7 @@ #include "actor.h" #include "room.h" #include "config.h" -#include +#include namespace modules { diff --git a/source/m_ingame.cpp b/source/m_ingame.cpp index cf416d4..05cef36 100644 --- a/source/m_ingame.cpp +++ b/source/m_ingame.cpp @@ -7,7 +7,7 @@ #include "actor.h" #include "room.h" #include "config.h" -#include +#include namespace modules { diff --git a/source/m_intro.cpp b/source/m_intro.cpp index 8d4be78..b209235 100644 --- a/source/m_intro.cpp +++ b/source/m_intro.cpp @@ -4,7 +4,7 @@ #include "jaudio.h" #include "controller.h" #include "config.h" -#include +#include #include "actor.h" namespace modules diff --git a/source/m_logo.cpp b/source/m_logo.cpp index 2d1328f..5bf912a 100644 --- a/source/m_logo.cpp +++ b/source/m_logo.cpp @@ -4,7 +4,7 @@ #include "controller.h" #include "jaudio.h" #include "config.h" -#include +#include namespace modules { diff --git a/source/m_menu.cpp b/source/m_menu.cpp index 708166b..968891e 100644 --- a/source/m_menu.cpp +++ b/source/m_menu.cpp @@ -4,7 +4,7 @@ #include "controller.h" #include "jdraw.h" #include "config.h" -#include +#include #include "jaudio.h" #include "actor.h" namespace modules diff --git a/source/m_menu_audio.cpp b/source/m_menu_audio.cpp index af1751f..312b64c 100644 --- a/source/m_menu_audio.cpp +++ b/source/m_menu_audio.cpp @@ -4,7 +4,7 @@ #include "controller.h" #include "jdraw.h" #include "jaudio.h" -#include +#include #include "config.h" namespace modules diff --git a/source/m_menu_gamepad.cpp b/source/m_menu_gamepad.cpp index 143d93c..5c539de 100644 --- a/source/m_menu_gamepad.cpp +++ b/source/m_menu_gamepad.cpp @@ -4,7 +4,7 @@ #include "jaudio.h" #include "controller.h" #include "config.h" -#include +#include namespace modules { diff --git a/source/m_menu_tecles.cpp b/source/m_menu_tecles.cpp index 663bdee..87df708 100644 --- a/source/m_menu_tecles.cpp +++ b/source/m_menu_tecles.cpp @@ -4,7 +4,7 @@ #include "jaudio.h" #include "controller.h" #include "config.h" -#include +#include namespace modules { diff --git a/source/m_prologo_intro.cpp b/source/m_prologo_intro.cpp index dd7d930..696771a 100644 --- a/source/m_prologo_intro.cpp +++ b/source/m_prologo_intro.cpp @@ -4,7 +4,7 @@ #include "jaudio.h" #include "controller.h" #include "config.h" -#include +#include #include "actor.h" namespace modules diff --git a/source/main.cpp b/source/main.cpp index e491e92..ffd0821 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -7,7 +7,7 @@ #include "console.h" #include #include "config.h" -#include +#include #include "actor.h" #include "room.h" @@ -106,7 +106,7 @@ void game::init() if (editor::isDevMode()) { draw::init("The Pool", 520, 240, zoom); - SDL_ShowCursor(true); + SDL_ShowCursor(); } else { loadConfig(); draw::init("The Pool", 320, 240, zoom, fullscreen); From 53784e6fb01d754400a38180f706edd1971a690d Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 6 Nov 2024 21:35:30 +0100 Subject: [PATCH 2/6] Pas a SDL3 complet --- Makefile | 14 +- source/config.cpp | 2 +- source/jail_audio.cpp | 296 +++++++++++++++++++++++--------------- source/jail_audio.h | 6 +- source/jinput.cpp | 6 +- source/m_logo.cpp | 1 + source/m_menu_gamepad.cpp | 2 +- 7 files changed, 200 insertions(+), 127 deletions(-) diff --git a/Makefile b/Makefile index f87cac0..e8326bb 100644 --- a/Makefile +++ b/Makefile @@ -3,25 +3,25 @@ source = source/*.cpp windows: @echo off - g++ $(source) bin/icon.res -lmingw32 -lSDL2main -lSDL2 -mwindows -o "$(executable).exe" + g++ $(source) bin/icon.res -lmingw32 -lSDL3 -mwindows -o "$(executable).exe" strip -s -R .comment -R .gnu.version --strip-unneeded "$(executable).exe" windows_debug: @echo off - g++ $(source) -D DEBUG -g -Wall -fvar-tracking -lmingw32 -lSDL2main -lSDL2 -o "$(executable)_debug.exe" + g++ $(source) -D DEBUG -g -Wall -fvar-tracking -lmingw32 -lSDL3 -o "$(executable)_debug.exe" macos: - clang++ $(source) -Wall -Os -std=c++11 -ffunction-sections -fdata-sections -lSDL2 -o "$(executable)" + clang++ $(source) -Wall -Os -std=c++11 -ffunction-sections -fdata-sections -lSDL3 -o "$(executable)" macos_debug: - clang++ $(source) -D DEBUG -g -Wall -std=c++11 -lSDL2 -o "$(executable)_debug" + clang++ $(source) -D DEBUG -g -Wall -std=c++11 -lSDL3 -o "$(executable)_debug" macos_bundle: - clang++ $(source) -D MACOS_BUNDLE -Wall -Os -std=c++11 -framework SDL2 -F /Library/Frameworks -ffunction-sections -fdata-sections -o mini_bundle -rpath @executable_path/../Frameworks/ -target x86_64-apple-macos10.12 + clang++ $(source) -D MACOS_BUNDLE -Wall -Os -std=c++11 -framework SDL3 -F /Library/Frameworks -ffunction-sections -fdata-sections -o mini_bundle -rpath @executable_path/../Frameworks/ -target x86_64-apple-macos10.12 linux: - g++ $(source) -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -o "$(executable)" + g++ $(source) -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL3 -o "$(executable)" strip -s -R .comment -R .gnu.version --strip-unneeded "$(executable)" linux_debug: - g++ $(source) -D DEBUG -g -lSDL2 -o "$(executable)_debug" + g++ $(source) -D DEBUG -g -lSDL3 -o "$(executable)_debug" diff --git a/source/config.cpp b/source/config.cpp index 3a04a5d..9908021 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -7,7 +7,7 @@ namespace config bool musicEnabled = true; int soundMode = SOUND_ALL; uint8_t keys[7] = {SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_SPACE, SDL_SCANCODE_RETURN, SDL_SCANCODE_ESCAPE}; - int8_t pad_btns[7] = {SDL_CONTROLLER_BUTTON_DPAD_UP, SDL_CONTROLLER_BUTTON_DPAD_DOWN, SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, SDL_CONTROLLER_BUTTON_A, SDL_CONTROLLER_BUTTON_B, SDL_CONTROLLER_BUTTON_START}; + int8_t pad_btns[7] = {SDL_GAMEPAD_BUTTON_DPAD_UP, SDL_GAMEPAD_BUTTON_DPAD_DOWN, SDL_GAMEPAD_BUTTON_DPAD_LEFT, SDL_GAMEPAD_BUTTON_DPAD_RIGHT, SDL_GAMEPAD_BUTTON_SOUTH, SDL_GAMEPAD_BUTTON_EAST, SDL_GAMEPAD_BUTTON_START}; bool prologo_desbloquejat = false; void setMusic(const bool value) diff --git a/source/jail_audio.cpp b/source/jail_audio.cpp index 269b621..0944eca 100644 --- a/source/jail_audio.cpp +++ b/source/jail_audio.cpp @@ -6,49 +6,55 @@ #define JA_MAX_SIMULTANEOUS_CHANNELS 5 -struct JA_Sound_t { - Uint32 length {0}; - Uint8* buffer {NULL}; +struct JA_Sound_t +{ + SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 }; + Uint32 length { 0 }; + Uint8 *buffer { NULL }; }; -struct JA_Channel_t { - JA_Sound_t *sound; - int pos {0}; - int times {0}; - JA_Channel_state state { JA_CHANNEL_FREE }; +struct JA_Channel_t +{ + JA_Sound_t *sound { nullptr }; + int pos { 0 }; + int times { 0 }; + SDL_AudioStream *stream { nullptr }; + JA_Channel_state state { JA_CHANNEL_FREE }; }; -struct JA_Music_t { - int samples {0}; - int pos {0}; - int times {0}; - short* output {NULL}; - JA_Music_state state {JA_MUSIC_INVALID}; +struct JA_Music_t +{ + SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 }; + Uint32 length { 0 }; + Uint8 *buffer { nullptr }; + + int pos { 0 }; + int times { 0 }; + SDL_AudioStream *stream { nullptr }; + JA_Music_state state { JA_MUSIC_INVALID }; }; -JA_Music_t *current_music{NULL}; -JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS]; +JA_Music_t *current_music { nullptr }; +JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS]; -int JA_freq {48000}; -SDL_AudioFormat JA_format {SDL_AUDIO_S16LE}; -Uint8 JA_channels {2}; -int JA_musicVolume = 128; -int JA_soundVolume = 64; -bool JA_musicEnabled = true; -bool JA_soundEnabled = true; -SDL_AudioStream *sdlAudioStream = nullptr; +SDL_AudioSpec JA_audioSpec { SDL_AUDIO_S16, 2, 48000 }; +float JA_musicVolume { 1.0f }; +float JA_soundVolume { 0.5f }; +bool JA_musicEnabled { true }; +bool JA_soundEnabled { true }; +SDL_AudioDeviceID sdlAudioDevice { 0 }; +SDL_TimerID JA_timerID { 0 }; -void audioCallback(void * userdata, SDL_AudioStream* stream, int additional_amount, int total_amount) { - int len = total_amount; +/* +void audioCallback(void * userdata, uint8_t * stream, int len) { SDL_memset(stream, 0, len); if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) { const int size = SDL_min(len, current_music->samples*2-current_music->pos); - SDL_PutAudioStreamData(sdlAudioStream, (Uint8*)(current_music->output+current_music->pos), size); - SDL_MixAudio(stream, (Uint8*)(current_music->output+current_music->pos), SDL_AUDIO_S16LE, size, JA_musicVolume); + SDL_MixAudioFormat(stream, (Uint8*)(current_music->output+current_music->pos), AUDIO_S16, size, JA_musicVolume); current_music->pos += size/2; if (size < len) { if (current_music->times != 0) { - SDL_MixAudio(stream+size, (Uint8*)current_music->output, SDL_AUDIO_S16LE, len-size, JA_musicVolume); + SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, JA_musicVolume); current_music->pos = (len-size)/2; if (current_music->times > 0) current_music->times--; } else { @@ -61,11 +67,11 @@ void audioCallback(void * userdata, SDL_AudioStream* stream, int additional_amou for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { if (channels[i].state == JA_CHANNEL_PLAYING) { const int size = SDL_min(len, channels[i].sound->length - channels[i].pos); - SDL_MixAudio(stream, channels[i].sound->buffer + channels[i].pos, SDL_AUDIO_S16LE, size, JA_soundVolume); + SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, JA_soundVolume); channels[i].pos += size; if (size < len) { if (channels[i].times != 0) { - SDL_MixAudio(stream + size, channels[i].sound->buffer, SDL_AUDIO_S16LE, len-size, JA_soundVolume); + SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, JA_soundVolume); channels[i].pos = len-size; if (channels[i].times > 0) channels[i].times--; } else { @@ -75,42 +81,77 @@ void audioCallback(void * userdata, SDL_AudioStream* stream, int additional_amou } } } +*/ -void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) { - JA_freq = freq; - JA_format = format; - JA_channels = channels; - SDL_AudioSpec audioSpec{JA_format, JA_channels, JA_freq}; //, 0, 1024, 0, 0, audioCallback, NULL}; - if (sdlAudioStream) SDL_DestroyAudioStream(sdlAudioStream); - sdlAudioStream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audioSpec, audioCallback, NULL); - SDL_PauseAudioStreamDevice(sdlAudioStream); +Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval) +{ + if (JA_musicEnabled && current_music && current_music->state == JA_MUSIC_PLAYING) + { + if (current_music->times != 0) + { + if (SDL_GetAudioStreamAvailable(current_music->stream) < (current_music->length/2)) + SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length); + if (current_music->times>0) current_music->times--; + } + else + { + if (SDL_GetAudioStreamAvailable(current_music->stream) == 0) JA_StopMusic(); + } + } + + if (JA_soundEnabled) + { + for (int i=0; i < JA_MAX_SIMULTANEOUS_CHANNELS; ++i) + if (channels[i].state == JA_CHANNEL_PLAYING) + { + if (channels[i].times != 0) + { + if (SDL_GetAudioStreamAvailable(channels[i].stream) < (channels[i].sound->length/2)) + SDL_PutAudioStreamData(channels[i].stream, channels[i].sound->buffer, channels[i].sound->length); + if (channels[i].times>0) channels[i].times--; + } + } + else + { + if (SDL_GetAudioStreamAvailable(channels[i].stream) == 0) JA_StopChannel(i); + } + + } + + return 30; } -void JA_Quit() { - SDL_ResumeAudioStreamDevice(sdlAudioStream); - if (sdlAudioStream) SDL_DestroyAudioStream(sdlAudioStream); - sdlAudioStream = nullptr; +void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) +{ + JA_audioSpec = {format, channels, freq }; + if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); + sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec); + SDL_PauseAudioDevice(sdlAudioDevice); + JA_timerID = SDL_AddTimer(30, JA_UpdateCallback, nullptr); +} + +void JA_Quit() +{ + if (JA_timerID) SDL_RemoveTimer(JA_timerID); + + if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); + sdlAudioDevice = 0; } JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length) { - int chan, samplerate; JA_Music_t *music = new JA_Music_t(); - music->samples = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &music->output); - // [RZC 28/08/22] Abans el descomprimiem mentre el teniem obert -// music->samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music->output); + int chan, samplerate; + short *output; + music->length = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &output) * chan * 2; - SDL_AudioCVT cvt; - SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, JA_format, JA_channels, JA_freq); - if (cvt.needed) { - cvt.len = music->samples * chan * 2; - cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult); - SDL_memcpy(cvt.buf, music->output, cvt.len); - SDL_ConvertAudio(&cvt); - free(music->output); - music->output = (short*)cvt.buf; - } + music->spec.channels = chan; + music->spec.freq = samplerate; + music->spec.format = SDL_AUDIO_S16; + music->buffer = (Uint8*)SDL_malloc(music->length); + SDL_memcpy(music->buffer, output, music->length); + SDL_free(output); music->pos = 0; music->state = JA_MUSIC_STOPPED; @@ -147,55 +188,69 @@ void JA_PlayMusic(JA_Music_t *music, const int loop) current_music->pos = 0; current_music->state = JA_MUSIC_PLAYING; current_music->times = loop; + + current_music->stream = SDL_CreateAudioStream(¤t_music->spec, &JA_audioSpec); + 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); + if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n"); } void JA_PauseMusic() { if (!JA_musicEnabled) return; + if (!current_music || current_music->state == JA_MUSIC_INVALID) return; - if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return; current_music->state = JA_MUSIC_PAUSED; + SDL_PauseAudioStreamDevice(current_music->stream); } void JA_ResumeMusic() { if (!JA_musicEnabled) return; + if (!current_music || current_music->state == JA_MUSIC_INVALID) return; - if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return; current_music->state = JA_MUSIC_PLAYING; + SDL_ResumeAudioStreamDevice(current_music->stream); } void JA_StopMusic() { if (!JA_musicEnabled) return; + if (!current_music || current_music->state == JA_MUSIC_INVALID) return; - if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return; current_music->pos = 0; current_music->state = JA_MUSIC_STOPPED; + SDL_PauseAudioStreamDevice(current_music->stream); + SDL_DestroyAudioStream(current_music->stream); + current_music->stream = nullptr; } -JA_Music_state JA_GetMusicState() { +JA_Music_state JA_GetMusicState() +{ if (!JA_musicEnabled) return JA_MUSIC_DISABLED; + if (!current_music) return JA_MUSIC_INVALID; - if (current_music == NULL) return JA_MUSIC_INVALID; return current_music->state; } -void JA_DeleteMusic(JA_Music_t *music) { - if (current_music == music) current_music = NULL; - free(music->output); +void JA_DeleteMusic(JA_Music_t *music) +{ + if (current_music == music) current_music = nullptr; + SDL_free(music->buffer); + SDL_DestroyAudioStream(music->stream); delete music; } -int JA_SetMusicVolume(int volume) +float JA_SetMusicVolume(float volume) { - JA_musicVolume = volume > 128 ? 128 : volume < 0 ? 0 : volume; + JA_musicVolume = SDL_clamp( volume, 0.0f, 1.0f ); + if (current_music) SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); return JA_musicVolume; } void JA_EnableMusic(const bool value) { - if (!value && current_music != NULL && current_music->state==JA_MUSIC_PLAYING) JA_StopMusic(); + if ( !value && current_music && (current_music->state==JA_MUSIC_PLAYING) ) JA_StopMusic(); JA_musicEnabled = value; } @@ -204,45 +259,26 @@ void JA_EnableMusic(const bool value) -JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) { +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_t *JA_LoadSound(uint8_t* buffer, uint32_t size) { +JA_Sound_t *JA_LoadSound(uint8_t* buffer, uint32_t size) +{ JA_Sound_t *sound = new JA_Sound_t(); - SDL_AudioSpec wavSpec; - SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size),1, &wavSpec, &sound->buffer, &sound->length); - - SDL_AudioCVT cvt; - SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq); - cvt.len = sound->length; - cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult); - SDL_memcpy(cvt.buf, sound->buffer, sound->length); - SDL_ConvertAudio(&cvt); - SDL_FreeWAV(sound->buffer); - sound->buffer = cvt.buf; - sound->length = cvt.len_cvt; + SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size),1, &sound->spec, &sound->buffer, &sound->length); return sound; } -JA_Sound_t *JA_LoadSound(const char* filename) { +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); - - SDL_AudioCVT cvt; - SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq); - cvt.len = sound->length; - cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult); - SDL_memcpy(cvt.buf, sound->buffer, sound->length); - SDL_ConvertAudio(&cvt); - SDL_FreeWAV(sound->buffer); - sound->buffer = cvt.buf; - sound->length = cvt.len_cvt; + SDL_LoadWAV(filename, &sound->spec, &sound->buffer, &sound->length); return sound; } @@ -259,6 +295,11 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop) channels[channel].times = loop; channels[channel].pos = 0; channels[channel].state = JA_CHANNEL_PLAYING; + channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec); + SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length); + SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume); + SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream); + return channel; } @@ -275,12 +316,22 @@ void JA_PauseChannel(const int channel) { if (!JA_soundEnabled) return; - if (channel == -1) { - for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { - if (channels[i].state == JA_CHANNEL_PLAYING) channels[i].state = JA_CHANNEL_PAUSED; + if (channel == -1) + { + for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) + if (channels[i].state == JA_CHANNEL_PLAYING) + { + channels[i].state = JA_CHANNEL_PAUSED; + SDL_PauseAudioStreamDevice(channels[i].stream); + } + } + else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) + { + if (channels[channel].state == JA_CHANNEL_PLAYING) + { + channels[channel].state = JA_CHANNEL_PAUSED; + SDL_PauseAudioStreamDevice(channels[channel].stream); } - } else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) { - if (channels[channel].state == JA_CHANNEL_PLAYING) channels[channel].state = JA_CHANNEL_PAUSED; } } @@ -288,12 +339,22 @@ void JA_ResumeChannel(const int channel) { if (!JA_soundEnabled) return; - if (channel == -1) { - for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { - if (channels[i].state == JA_CHANNEL_PAUSED) channels[i].state = JA_CHANNEL_PLAYING; + if (channel == -1) + { + for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) + if (channels[i].state == JA_CHANNEL_PAUSED) + { + channels[i].state = JA_CHANNEL_PLAYING; + SDL_ResumeAudioStreamDevice(channels[i].stream); + } + } + else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) + { + if (channels[channel].state == JA_CHANNEL_PAUSED) + { + channels[channel].state = JA_CHANNEL_PLAYING; + SDL_ResumeAudioStreamDevice(channels[channel].stream); } - } else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) { - if (channels[channel].state == JA_CHANNEL_PAUSED) channels[channel].state = JA_CHANNEL_PLAYING; } } @@ -301,16 +362,21 @@ void JA_StopChannel(const int channel) { if (!JA_soundEnabled) return; - if (channel == -1) { + if (channel == -1) + { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { channels[i].state = JA_CHANNEL_FREE; channels[i].pos = 0; channels[i].sound = NULL; + SDL_DestroyAudioStream(channels[i].stream); } - } else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) { + } + else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) + { channels[channel].state = JA_CHANNEL_FREE; channels[channel].pos = 0; channels[channel].sound = NULL; + SDL_DestroyAudioStream(channels[channel].stream); } } @@ -319,12 +385,18 @@ JA_Channel_state JA_GetChannelState(const int channel) if (!JA_soundEnabled) return JA_SOUND_DISABLED; if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID; + return channels[channel].state; } -int JA_SetSoundVolume(int volume) +float JA_SetSoundVolume(float volume) { - JA_soundVolume = volume > 128 ? 128 : volume < 0 ? 0 : volume; + JA_soundVolume = SDL_clamp( volume, 0.0f, 1.0f ); + + for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) + if ( (channels[i].state == JA_CHANNEL_PLAYING) || (channels[i].state == JA_CHANNEL_PAUSED) ) + SDL_SetAudioStreamGain(channels[i].stream, JA_soundVolume); + return JA_soundVolume; } @@ -337,10 +409,10 @@ void JA_EnableSound(const bool value) JA_soundEnabled = value; } -int JA_SetVolume(int volume) +float JA_SetVolume(float volume) { - JA_musicVolume = volume > 128 ? 128 : volume < 0 ? 0 : volume; - JA_soundVolume = JA_musicVolume/2; + JA_SetSoundVolume(JA_SetMusicVolume(volume) / 2.0f); + return JA_musicVolume; } diff --git a/source/jail_audio.h b/source/jail_audio.h index ae2ef76..f4cde72 100644 --- a/source/jail_audio.h +++ b/source/jail_audio.h @@ -18,7 +18,7 @@ void JA_ResumeMusic(); void JA_StopMusic(); JA_Music_state JA_GetMusicState(); void JA_DeleteMusic(JA_Music_t *music); -int JA_SetMusicVolume(int volume); +float JA_SetMusicVolume(float volume); void JA_EnableMusic(const bool value); JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length); @@ -30,7 +30,7 @@ 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_t *sound); -int JA_SetSoundVolume(int volume); +float JA_SetSoundVolume(float volume); void JA_EnableSound(const bool value); -int JA_SetVolume(int volume); +float JA_SetVolume(float volume); diff --git a/source/jinput.cpp b/source/jinput.cpp index 9ec3abc..e02f3ea 100644 --- a/source/jinput.cpp +++ b/source/jinput.cpp @@ -11,8 +11,8 @@ namespace input static uint8_t btnClicked = 0; static int wheel = 0; static SDL_Gamepad *gamepad = NULL; - static int8_t pad_btn_pressed = SDL_CONTROLLER_BUTTON_INVALID; - static int8_t pad_btn_down = SDL_CONTROLLER_BUTTON_INVALID; + static int8_t pad_btn_pressed = SDL_GAMEPAD_BUTTON_INVALID; + static int8_t pad_btn_down = SDL_GAMEPAD_BUTTON_INVALID; void initGamePad() { @@ -98,7 +98,7 @@ namespace input // Determina si hi ha algun botó del pad polsat ara mateix bool anyPadBtn() { - return pad_btn_down != SDL_CONTROLLER_BUTTON_INVALID; + return pad_btn_down != SDL_GAMEPAD_BUTTON_INVALID; } // Torna el codi del botó del pad que està sent polsat ara mateix diff --git a/source/m_logo.cpp b/source/m_logo.cpp index 5bf912a..af9c1b1 100644 --- a/source/m_logo.cpp +++ b/source/m_logo.cpp @@ -5,6 +5,7 @@ #include "jaudio.h" #include "config.h" #include +#include namespace modules { diff --git a/source/m_menu_gamepad.cpp b/source/m_menu_gamepad.cpp index 5c539de..e3b5ee2 100644 --- a/source/m_menu_gamepad.cpp +++ b/source/m_menu_gamepad.cpp @@ -57,7 +57,7 @@ namespace modules } } else { const int8_t btn = input::getPadBtnPressed(); - if (btn != SDL_CONTROLLER_BUTTON_INVALID) { + if (btn != SDL_GAMEPAD_BUTTON_INVALID) { config::definePadBtn(selected_option, btn); selected_option++; if (selected_option==6) state = STATE_SELECTING; From ad4fa14ab1c1b5094ae74b24d35fcb965209224e Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Mon, 21 Apr 2025 10:17:12 +0200 Subject: [PATCH 3/6] - [NEW] Afegida paleta monocroma (no accesible encara) --- data/grayscale.gif | Bin 0 -> 148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 data/grayscale.gif diff --git a/data/grayscale.gif b/data/grayscale.gif new file mode 100644 index 0000000000000000000000000000000000000000..cee8b18d0f169e58e4f156d0d47607aa20fa360b GIT binary patch literal 148 zcmZ?wbh9u|WMN=sIKlt|2M!!iP*5;4GXs*EnwrMO#wjT&ot>T2r%&IsX%j>iC=OIG zZ{ECZ+qRuJapKLJH~;_t2dXCs6o0aCase&T0oeqylYv!GL4bofP=bAu2KQwPei6@L H4hCxgC#5GB literal 0 HcmV?d00001 From 7feabb30f764ce48ed717f3704d49f933bc41fac Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Fri, 25 Apr 2025 11:46:51 +0200 Subject: [PATCH 4/6] - [FIX] Retocada paleta grayscale --- data/grayscale.gif | Bin 148 -> 148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/data/grayscale.gif b/data/grayscale.gif index cee8b18d0f169e58e4f156d0d47607aa20fa360b..3d12fd8cf8978fd19bd19b95490d08335f095a5c 100644 GIT binary patch literal 148 zcmZ?wbh9u|WMN=sIKlt|2M!znl4fRRKvGjv)7aQJB_*Y^vvd0N>6T2r%&IsX%j>iC=OIG zZ{ECZ+qRuJapKLJH~;_t2dXCs6o0aCase&T0oeqylYv!GL4bofP=bAu2KQwPei6@L H4hCxgC#5GB From 186775d58551ca361c97ab0ec4fd9154042bdfc8 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Fri, 6 Feb 2026 12:10:28 +0100 Subject: [PATCH 5/6] No se, alguns canvis --- source/jail_audio.cpp | 4 ++-- source/jdraw.cpp | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/source/jail_audio.cpp b/source/jail_audio.cpp index 0944eca..6a6c627 100644 --- a/source/jail_audio.cpp +++ b/source/jail_audio.cpp @@ -89,7 +89,7 @@ Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval) { if (current_music->times != 0) { - if (SDL_GetAudioStreamAvailable(current_music->stream) < (current_music->length/2)) + if (SDL_GetAudioStreamAvailable(current_music->stream) < int(current_music->length/2)) SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length); if (current_music->times>0) current_music->times--; } @@ -106,7 +106,7 @@ Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval) { if (channels[i].times != 0) { - if (SDL_GetAudioStreamAvailable(channels[i].stream) < (channels[i].sound->length/2)) + if (SDL_GetAudioStreamAvailable(channels[i].stream) < int(channels[i].sound->length/2)) SDL_PutAudioStreamData(channels[i].stream, channels[i].sound->buffer, channels[i].sound->length); if (channels[i].times>0) channels[i].times--; } diff --git a/source/jdraw.cpp b/source/jdraw.cpp index e9b10b2..89aba2a 100644 --- a/source/jdraw.cpp +++ b/source/jdraw.cpp @@ -93,13 +93,11 @@ namespace draw sdl_renderer = SDL_CreateRenderer(sdl_window, nullptr); sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen_width, screen_height); - char *driver_name; const int num_render_drivers = SDL_GetNumRenderDrivers(); printf("Available renderers:\n"); for (int i=0; i Date: Tue, 3 Mar 2026 12:56:11 +0100 Subject: [PATCH 6/6] =?UTF-8?q?-=20[FIX]=20No=20es=20mostrava=20res=20en?= =?UTF-8?q?=20pantalla=20perque=20faltava=20el=200xff000000=20al=20ficar?= =?UTF-8?q?=20pixels=20en=20la=20textura=20-=20[FIX]=20No=20s'escoltava=20?= =?UTF-8?q?so,=20no=20estic=20segur=20de=20perqu=C3=A9.=20He=20agafat=20el?= =?UTF-8?q?=20JailAudio=20de=20mini=20i=20ja=20va=20tot.=20-=20[NEW]=20Afe?= =?UTF-8?q?gida=20opci=C3=B3=20"SET=20INFINITELIVES"=20desde=20la=20consol?= =?UTF-8?q?a=20-=20[NEW]=20Afegida=20opci=C3=B3=20"SET=20KIOSK"=20desde=20?= =?UTF-8?q?la=20consola=20-=20[FIX]=20Les=20SDL=5Ftexture=20es=20veien=20b?= =?UTF-8?q?orroses=20-=20[FIX]=20En=20windows=20no=20es=20carregaven=20be?= =?UTF-8?q?=20els=20GIFs=20del=20txt=20per=20culpa=20del=20salt=20de=20lin?= =?UTF-8?q?ea=20doble=20que=20t=C3=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lagueirtofile | 2 +- source/actor.cpp | 2 +- source/config.cpp | 25 +++++ source/config.h | 6 ++ source/console.cpp | 29 +++++- source/{gif.c => gif.h} | 1 + source/jail_audio.cpp | 128 ++++++++++++++++++++++---- source/jail_audio.h | 6 ++ source/jdraw.cpp | 38 +++++++- source/log.h | 27 ++++++ source/m_menu.cpp | 18 ++-- source/main.cpp | 4 + source/{stb_vorbis.c => stb_vorbis.h} | 0 13 files changed, 247 insertions(+), 39 deletions(-) rename source/{gif.c => gif.h} (99%) create mode 100644 source/log.h rename source/{stb_vorbis.c => stb_vorbis.h} (100%) diff --git a/lagueirtofile b/lagueirtofile index 082fa4d..90425fd 100644 --- a/lagueirtofile +++ b/lagueirtofile @@ -1,4 +1,4 @@ -libs = -lSDL2 +libs = -lSDL3 cppflags = -D DEBUG -g executable = thepool_debug sourcepath = source diff --git a/source/actor.cpp b/source/actor.cpp index f06cbf1..543435f 100644 --- a/source/actor.cpp +++ b/source/actor.cpp @@ -2118,7 +2118,7 @@ namespace actor void setLives(int value) { - lives = value; + lives = config::getInfiniteLives() ? 8 : value; } void die() diff --git a/source/config.cpp b/source/config.cpp index 9908021..d5e324d 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -9,6 +9,8 @@ namespace config uint8_t keys[7] = {SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_SPACE, SDL_SCANCODE_RETURN, SDL_SCANCODE_ESCAPE}; int8_t pad_btns[7] = {SDL_GAMEPAD_BUTTON_DPAD_UP, SDL_GAMEPAD_BUTTON_DPAD_DOWN, SDL_GAMEPAD_BUTTON_DPAD_LEFT, SDL_GAMEPAD_BUTTON_DPAD_RIGHT, SDL_GAMEPAD_BUTTON_SOUTH, SDL_GAMEPAD_BUTTON_EAST, SDL_GAMEPAD_BUTTON_START}; bool prologo_desbloquejat = false; + bool kiosk_mode = false; + bool infinite_lives = false; void setMusic(const bool value) { @@ -82,4 +84,27 @@ namespace config return prologo_desbloquejat; } + void setKioskMode(const bool value) + { + kiosk_mode = value; + file::setConfigValue("kiosk", kiosk_mode ? "yes" : "no"); + } + + const bool getKioskMode() + { + return kiosk_mode; + } + + void setInifiniteLives(const bool value) + { + infinite_lives= value; + file::setConfigValue("infinitelives", infinite_lives? "yes" : "no"); + } + + const bool getInfiniteLives() + { + return infinite_lives; + } + + } diff --git a/source/config.h b/source/config.h index 32b4f4d..00dfa80 100644 --- a/source/config.h +++ b/source/config.h @@ -30,4 +30,10 @@ namespace config void setProgoloDesbloquejat(); const bool isProgoloDesbloquejat(); + + void setKioskMode(const bool value); + const bool getKioskMode(); + + void setInifiniteLives(const bool value); + const bool getInfiniteLives(); } \ No newline at end of file diff --git a/source/console.cpp b/source/console.cpp index 0c1a121..50a3953 100644 --- a/source/console.cpp +++ b/source/console.cpp @@ -6,6 +6,7 @@ #include "actor.h" #include "jgame.h" #include "debug.h" +#include "config.h" namespace console { @@ -134,10 +135,11 @@ namespace console #define CMD_EXIT 4 #define CMD_SHOW 5 #define CMD_HIDE 6 - #define CMD_HELP 7 - #define NUM_CMDS 8 + #define CMD_SET 7 + #define CMD_HELP 8 + #define NUM_CMDS 9 - const char *command_text[NUM_CMDS] = { "GIVE", "DROP", "GOTO", "MOVE", "EXIT", "SHOW", "HIDE", "HELP" }; + const char *command_text[NUM_CMDS] = { "GIVE", "DROP", "GOTO", "MOVE", "EXIT", "SHOW", "HIDE", "SET", "HELP" }; #define PARAM_UNKNOWN -1 #define PARAM_RUN 0 @@ -283,9 +285,28 @@ namespace console debug::disable(value); } break; + case CMD_SET: + if (!tokens[1]) { + strcpy(msg, "ERROR: Nothing to set."); + } else { + strcpy(msg, "Ok."); + const int value = getIndexFromString(tokens[1], {"KIOSK", "INFINITELIVES"}); + if (value==-1) + strcpy(msg, "ERROR: Cannot set that."); + else + switch (value) { + case 0: + config::setKioskMode(!config::getKioskMode()); + break; + case 1: + config::setInifiniteLives(!config::getInfiniteLives()); + break; + } + + } case CMD_HELP: if (!tokens[1]) { - strcpy(msg, "GIVE DROP GOTO MOVE EXIT SHOW HIDE HELP"); + strcpy(msg, "GIVE DROP GOTO MOVE EXIT SHOW HIDE SET HELP"); } else { int command = getCommand(); switch (command) diff --git a/source/gif.c b/source/gif.h similarity index 99% rename from source/gif.c rename to source/gif.h index 8c19b14..9685352 100644 --- a/source/gif.c +++ b/source/gif.h @@ -2,6 +2,7 @@ #include #include #include +#include #define EXTENSION_INTRODUCER 0x21 #define IMAGE_DESCRIPTOR 0x2C diff --git a/source/jail_audio.cpp b/source/jail_audio.cpp index 6a6c627..42f3a56 100644 --- a/source/jail_audio.cpp +++ b/source/jail_audio.cpp @@ -1,8 +1,9 @@ #ifndef JA_USESDLMIXER #include "jail_audio.h" -#include "stb_vorbis.c" +#include "stb_vorbis.h" #include #include +#include "log.h" #define JA_MAX_SIMULTANEOUS_CHANNELS 5 @@ -45,6 +46,11 @@ bool JA_soundEnabled { true }; SDL_AudioDeviceID sdlAudioDevice { 0 }; SDL_TimerID JA_timerID { 0 }; +bool fading = false; +int fade_start_time; +int fade_duration; +int fade_initial_volume; + /* void audioCallback(void * userdata, uint8_t * stream, int len) { SDL_memset(stream, 0, len); @@ -87,10 +93,24 @@ Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval) { if (JA_musicEnabled && current_music && current_music->state == JA_MUSIC_PLAYING) { + if (fading) { + int time = SDL_GetTicks(); + if (time > (fade_start_time+fade_duration)) { + fading = false; + JA_StopMusic(); + return 30; + } else { + const int time_passed = time - fade_start_time; + const float percent = (float)time_passed / (float)fade_duration; + SDL_SetAudioStreamGain(current_music->stream, 1.0 - percent); + } + } + if (current_music->times != 0) { - if (SDL_GetAudioStreamAvailable(current_music->stream) < int(current_music->length/2)) + if (SDL_GetAudioStreamAvailable(current_music->stream) < int(current_music->length/2)) { SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length); + } if (current_music->times>0) current_music->times--; } else @@ -123,10 +143,19 @@ Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval) void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) { + #ifdef DEBUG + SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG); + #endif + JA_audioSpec = {format, channels, freq }; if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec); - SDL_PauseAudioDevice(sdlAudioDevice); + if (!sdlAudioDevice) { + log_msg(LOG_FAIL, "Failed to initialize SDL audio: %s\n", SDL_GetError()); + } else { + log_msg(LOG_OK, "Audio subsytem initialized\n"); + } + //SDL_PauseAudioDevice(sdlAudioDevice); JA_timerID = SDL_AddTimer(30, JA_UpdateCallback, nullptr); } @@ -151,7 +180,7 @@ JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length) music->spec.format = SDL_AUDIO_S16; music->buffer = (Uint8*)SDL_malloc(music->length); SDL_memcpy(music->buffer, output, music->length); - SDL_free(output); + free(output); music->pos = 0; music->state = JA_MUSIC_STOPPED; @@ -180,19 +209,18 @@ void JA_PlayMusic(JA_Music_t *music, const int loop) { if (!JA_musicEnabled) return; - if (current_music != NULL) { - current_music->pos = 0; - current_music->state = JA_MUSIC_STOPPED; - } + JA_StopMusic(); + current_music = music; current_music->pos = 0; current_music->state = JA_MUSIC_PLAYING; current_music->times = loop; current_music->stream = SDL_CreateAudioStream(¤t_music->spec, &JA_audioSpec); - if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) printf("[ERROR] SDL_PutAudioStreamData failed!\n"); + if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) log_msg(LOG_FAIL, "SDL_PutAudioStreamData failed!\n"); SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); - if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n"); + if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) log_msg(LOG_FAIL, "SDL_BindAudioStream failed!\n"); + //SDL_ResumeAudioStreamDevice(current_music->stream); } void JA_PauseMusic() @@ -201,7 +229,8 @@ void JA_PauseMusic() if (!current_music || current_music->state == JA_MUSIC_INVALID) return; current_music->state = JA_MUSIC_PAUSED; - SDL_PauseAudioStreamDevice(current_music->stream); + //SDL_PauseAudioStreamDevice(current_music->stream); + SDL_UnbindAudioStream(current_music->stream); } void JA_ResumeMusic() @@ -210,7 +239,8 @@ void JA_ResumeMusic() if (!current_music || current_music->state == JA_MUSIC_INVALID) return; current_music->state = JA_MUSIC_PLAYING; - SDL_ResumeAudioStreamDevice(current_music->stream); + //SDL_ResumeAudioStreamDevice(current_music->stream); + SDL_BindAudioStream(sdlAudioDevice, current_music->stream); } void JA_StopMusic() @@ -220,11 +250,22 @@ void JA_StopMusic() current_music->pos = 0; current_music->state = JA_MUSIC_STOPPED; - SDL_PauseAudioStreamDevice(current_music->stream); + //SDL_PauseAudioStreamDevice(current_music->stream); SDL_DestroyAudioStream(current_music->stream); current_music->stream = nullptr; } +void JA_FadeOutMusic(const int milliseconds) +{ + if (!JA_musicEnabled) return; + if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return; + + fading = true; + fade_start_time = SDL_GetTicks(); + fade_duration = milliseconds; + fade_initial_volume = JA_musicVolume; +} + JA_Music_state JA_GetMusicState() { if (!JA_musicEnabled) return JA_MUSIC_DISABLED; @@ -237,7 +278,7 @@ void JA_DeleteMusic(JA_Music_t *music) { if (current_music == music) current_music = nullptr; SDL_free(music->buffer); - SDL_DestroyAudioStream(music->stream); + if (music->stream) SDL_DestroyAudioStream(music->stream); delete music; } @@ -248,6 +289,18 @@ float JA_SetMusicVolume(float volume) return JA_musicVolume; } +void JA_SetMusicPosition(float value) +{ + if (!current_music) return; + current_music->pos = value * current_music->spec.freq; +} + +float JA_GetMusicPosition() +{ + if (!current_music) return 0; + return float(current_music->pos)/float(current_music->spec.freq); +} + void JA_EnableMusic(const bool value) { if ( !value && current_music && (current_music->state==JA_MUSIC_PLAYING) ) JA_StopMusic(); @@ -255,6 +308,10 @@ void JA_EnableMusic(const bool value) JA_musicEnabled = value; } +const bool JA_IsMusicEnabled() +{ + return JA_musicEnabled; +} @@ -290,6 +347,26 @@ 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; + JA_StopChannel(channel); + + channels[channel].sound = sound; + channels[channel].times = loop; + channels[channel].pos = 0; + channels[channel].state = JA_CHANNEL_PLAYING; + channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec); + SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length); + SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume); + SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream); + + return channel; +} + +int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop) +{ + if (!JA_soundEnabled) return -1; + + if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1; + JA_StopChannel(channel); channels[channel].sound = sound; channels[channel].times = loop; @@ -322,7 +399,8 @@ void JA_PauseChannel(const int channel) if (channels[i].state == JA_CHANNEL_PLAYING) { channels[i].state = JA_CHANNEL_PAUSED; - SDL_PauseAudioStreamDevice(channels[i].stream); + //SDL_PauseAudioStreamDevice(channels[i].stream); + SDL_UnbindAudioStream(channels[i].stream); } } else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) @@ -330,7 +408,8 @@ void JA_PauseChannel(const int channel) if (channels[channel].state == JA_CHANNEL_PLAYING) { channels[channel].state = JA_CHANNEL_PAUSED; - SDL_PauseAudioStreamDevice(channels[channel].stream); + //SDL_PauseAudioStreamDevice(channels[channel].stream); + SDL_UnbindAudioStream(channels[channel].stream); } } } @@ -345,7 +424,8 @@ void JA_ResumeChannel(const int channel) if (channels[i].state == JA_CHANNEL_PAUSED) { channels[i].state = JA_CHANNEL_PLAYING; - SDL_ResumeAudioStreamDevice(channels[i].stream); + //SDL_ResumeAudioStreamDevice(channels[i].stream); + SDL_BindAudioStream(sdlAudioDevice, channels[i].stream); } } else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) @@ -353,7 +433,8 @@ void JA_ResumeChannel(const int channel) if (channels[channel].state == JA_CHANNEL_PAUSED) { channels[channel].state = JA_CHANNEL_PLAYING; - SDL_ResumeAudioStreamDevice(channels[channel].stream); + //SDL_ResumeAudioStreamDevice(channels[channel].stream); + SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream); } } } @@ -365,18 +446,20 @@ void JA_StopChannel(const int channel) if (channel == -1) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { + if (channels[i].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[i].stream); + channels[channel].stream = nullptr; channels[i].state = JA_CHANNEL_FREE; channels[i].pos = 0; channels[i].sound = NULL; - SDL_DestroyAudioStream(channels[i].stream); } } else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) { + if (channels[channel].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[channel].stream); + channels[channel].stream = nullptr; channels[channel].state = JA_CHANNEL_FREE; channels[channel].pos = 0; channels[channel].sound = NULL; - SDL_DestroyAudioStream(channels[channel].stream); } } @@ -409,6 +492,11 @@ void JA_EnableSound(const bool value) JA_soundEnabled = value; } +const bool JA_IsSoundEnabled() +{ + return JA_soundEnabled; +} + float JA_SetVolume(float volume) { JA_SetSoundVolume(JA_SetMusicVolume(volume) / 2.0f); diff --git a/source/jail_audio.h b/source/jail_audio.h index f4cde72..e145161 100644 --- a/source/jail_audio.h +++ b/source/jail_audio.h @@ -16,15 +16,20 @@ void JA_PlayMusic(JA_Music_t *music, const int loop = -1); void JA_PauseMusic(); void JA_ResumeMusic(); void JA_StopMusic(); +void JA_FadeOutMusic(const int milliseconds); JA_Music_state JA_GetMusicState(); void JA_DeleteMusic(JA_Music_t *music); float JA_SetMusicVolume(float volume); +void JA_SetMusicPosition(float value); +float JA_GetMusicPosition(); void JA_EnableMusic(const bool value); +const bool JA_IsMusicEnabled(); JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length); JA_Sound_t *JA_LoadSound(Uint8* buffer, Uint32 length); JA_Sound_t *JA_LoadSound(const char* filename); int JA_PlaySound(JA_Sound_t *sound, const int loop = 0); +int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop = 0); void JA_PauseChannel(const int channel); void JA_ResumeChannel(const int channel); void JA_StopChannel(const int channel); @@ -32,5 +37,6 @@ JA_Channel_state JA_GetChannelState(const int channel); void JA_DeleteSound(JA_Sound_t *sound); float JA_SetSoundVolume(float volume); void JA_EnableSound(const bool value); +const bool JA_IsSoundEnabled(); float JA_SetVolume(float volume); diff --git a/source/jdraw.cpp b/source/jdraw.cpp index 89aba2a..16bbb32 100644 --- a/source/jdraw.cpp +++ b/source/jdraw.cpp @@ -1,6 +1,6 @@ #include "jdraw.h" #include -#include "gif.c" +#include "gif.h" #include "jfile.h" #include @@ -92,6 +92,7 @@ namespace draw sdl_window = SDL_CreateWindow(screen_title.c_str(), screen_width * screen_zoom, screen_height * screen_zoom, screen_fullscreen ? SDL_WINDOW_FULLSCREEN : 0); sdl_renderer = SDL_CreateRenderer(sdl_window, nullptr); sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen_width, screen_height); + SDL_SetTextureScaleMode(sdl_texture, SDL_SCALEMODE_NEAREST); const int num_render_drivers = SDL_GetNumRenderDrivers(); printf("Available renderers:\n"); @@ -135,10 +136,12 @@ namespace draw char *p = buffer; char *n = buffer; while (*n!=0) { - while (*n!='\n') n++; + while (*n!='\n' && *n!='\r') n++; *n=0; loadSurface(p); - p=++n; + n++; + while (*n=='\n' || *n=='\r') n++; + p=n; } free(buffer); @@ -183,6 +186,7 @@ namespace draw sdl_window = SDL_CreateWindow(screen_title.c_str(), screen_width * zoom, screen_height * zoom, screen_fullscreen?SDL_WINDOW_FULLSCREEN:0); sdl_renderer = SDL_CreateRenderer(sdl_window, nullptr); sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen_width, screen_height); + SDL_SetTextureScaleMode(sdl_texture, SDL_SCALEMODE_NEAREST); SDL_HideCursor(); @@ -672,10 +676,34 @@ namespace draw print2(buffer, x, y, color, zoom); } +bool SaveIndexedAsBMP(const char *filename, + const uint8_t *pixels, + const uint32_t *palette, + int w, int h) +{ + SDL_Surface *surface = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_ARGB8888); + if (!surface) return false; + + uint32_t *dst = (uint32_t *)surface->pixels; + int pitch32 = surface->pitch / 4; + + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + uint8_t idx = pixels[y * w + x]; + dst[y * pitch32 + x] = palette[idx]; // assumes ARGB8888 + } + } + + bool ok = SDL_SaveBMP(surface, filename); + SDL_DestroySurface(surface); + return ok; +} + // Refresca la pantalla void render() { - + //SaveIndexedAsBMP("screen.bmp", screen->pixels, palette, 320, 240); + Uint32 *sdl_pixels; // Punter al array de pixels que enstornarà SDL_LockTexture int sdl_pitch; // Ací estarà guardat el pitch de la textura, com es de 32 bits, no m'afecta const uint32_t size = screen->w * screen->h; // tamany de la superficie @@ -689,7 +717,7 @@ namespace draw // i el enviem a la textura SDL for (uint32_t i = 0; i < size; ++i) { - sdl_pixels[i] = palette[screen->pixels[i]]; + sdl_pixels[i] = 0xff000000 | palette[screen->pixels[i]]; } // Desbloquejem la textura diff --git a/source/log.h b/source/log.h new file mode 100644 index 0000000..1daa14d --- /dev/null +++ b/source/log.h @@ -0,0 +1,27 @@ +#pragma once +#include +#include + +#ifdef DEBUG +enum LogLevel { LOG_OK, LOG_FAIL, LOG_WARN, LOG_INFO, LOG_LUART, LOG_LUALD, LOG_VERBOSE, LOG_UNSALTED }; + +static inline void log_msg(enum LogLevel level, const char *fmt, ...) { + va_list args; + va_start(args, fmt); + + switch (level) { + case LOG_OK: printf("[\033[1;32m OK \033[0m] "); break; + case LOG_FAIL: printf("[\033[1;31mFAIL\033[0m] "); break; + case LOG_WARN: printf("[\033[1;33mWARN\033[0m] "); break; + case LOG_INFO: printf("[\033[1;34mINFO\033[0m] "); break; + case LOG_LUART: printf("[\033[1;35mLUA RUNTIME ERROR\033[0m] "); break; + case LOG_LUALD: printf("[\033[1;35mLUA LOADING ERROR\033[0m] "); break; + case LOG_VERBOSE: printf(" - "); break; + case LOG_UNSALTED: break; + } + vprintf(fmt, args); + va_end(args); +} +#else +#define log_msg(...) ((void)0) +#endif \ No newline at end of file diff --git a/source/m_menu.cpp b/source/m_menu.cpp index 968891e..a91578c 100644 --- a/source/m_menu.cpp +++ b/source/m_menu.cpp @@ -32,19 +32,21 @@ namespace modules int loop() { - if (controller::pressed(KEY_MENU)) { + int max_option = config::getKioskMode() ? 4 : 5; + + if (controller::pressed(KEY_MENU) && !config::getKioskMode()) { return OPTION_EIXIR; } if (controller::pressed(KEY_DOWN) || input::keyPressed(SDL_SCANCODE_DOWN)) { audio::playSound("snd_push.wav", SOUND_BASIC); - selected_option++; if (selected_option==6) selected_option=min_option; + selected_option++; if (selected_option==max_option+1) selected_option=min_option; } if (controller::pressed(KEY_UP) || input::keyPressed(SDL_SCANCODE_UP)) { audio::playSound("snd_push.wav", SOUND_BASIC); - selected_option--; if (selected_option