afegits metodes per a poder ciclar presets i altres opcions

esc en el joc + menu ja no tanca la app
This commit is contained in:
2026-04-04 23:42:19 +02:00
parent dbecd1ed4f
commit be4b62414e
13 changed files with 163 additions and 150 deletions

View File

@@ -11,30 +11,30 @@
struct JA_Sound_t {
SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000};
Uint32 length{0};
Uint8 *buffer{NULL};
Uint8* buffer{NULL};
};
struct JA_Channel_t {
JA_Sound_t *sound{nullptr};
JA_Sound_t* sound{nullptr};
int pos{0};
int times{0};
SDL_AudioStream *stream{nullptr};
SDL_AudioStream* stream{nullptr};
JA_Channel_state state{JA_CHANNEL_FREE};
};
struct JA_Music_t {
SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000};
Uint32 length{0};
Uint8 *buffer{nullptr};
char *filename{nullptr};
Uint8* buffer{nullptr};
char* filename{nullptr};
int pos{0};
int times{0};
SDL_AudioStream *stream{nullptr};
SDL_AudioStream* stream{nullptr};
JA_Music_state state{JA_MUSIC_INVALID};
};
JA_Music_t *current_music{nullptr};
JA_Music_t* current_music{nullptr};
JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
SDL_AudioSpec JA_audioSpec{SDL_AUDIO_S16, 2, 48000};
@@ -88,7 +88,7 @@ void audioCallback(void * userdata, uint8_t * stream, int len) {
}
*/
Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval) {
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();
@@ -152,47 +152,47 @@ void JA_Quit() {
sdlAudioDevice = 0;
}
JA_Music_t *JA_LoadMusic(Uint8 *buffer, Uint32 length, const char *filename) {
JA_Music_t *music = new JA_Music_t();
JA_Music_t* JA_LoadMusic(Uint8* buffer, Uint32 length, const char* filename) {
JA_Music_t* music = new JA_Music_t();
int chan, samplerate;
short *output;
short* output;
music->length = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &output) * chan * 2;
music->spec.channels = chan;
music->spec.freq = samplerate;
music->spec.format = SDL_AUDIO_S16;
music->buffer = (Uint8 *)SDL_malloc(music->length);
music->buffer = (Uint8*)SDL_malloc(music->length);
SDL_memcpy(music->buffer, output, music->length);
free(output);
music->pos = 0;
music->state = JA_MUSIC_STOPPED;
if (filename) {
music->filename = (char *)malloc(strlen(filename) + 1);
music->filename = (char*)malloc(strlen(filename) + 1);
strcpy(music->filename, filename);
}
return music;
}
JA_Music_t *JA_LoadMusic(const char *filename) {
JA_Music_t* JA_LoadMusic(const char* filename) {
// [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid.
FILE *f = fopen(filename, "rb");
FILE* f = fopen(filename, "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
Uint8 *buffer = (Uint8 *)malloc(fsize + 1);
Uint8* buffer = (Uint8*)malloc(fsize + 1);
if (fread(buffer, fsize, 1, f) != 1) return NULL;
fclose(f);
JA_Music_t *music = JA_LoadMusic(buffer, fsize, filename);
JA_Music_t* music = JA_LoadMusic(buffer, fsize, filename);
free(buffer);
return music;
}
void JA_PlayMusic(JA_Music_t *music, const int loop) {
void JA_PlayMusic(JA_Music_t* music, const int loop) {
if (!JA_musicEnabled) return;
JA_StopMusic();
@@ -209,7 +209,7 @@ void JA_PlayMusic(JA_Music_t *music, const int loop) {
// SDL_ResumeAudioStreamDevice(current_music->stream);
}
char *JA_GetMusicFilename(JA_Music_t *music) {
char* JA_GetMusicFilename(JA_Music_t* music) {
if (!music) music = current_music;
return music->filename;
}
@@ -262,7 +262,7 @@ JA_Music_state JA_GetMusicState() {
return current_music->state;
}
void JA_DeleteMusic(JA_Music_t *music) {
void JA_DeleteMusic(JA_Music_t* music) {
if (current_music == music) current_music = nullptr;
SDL_free(music->buffer);
if (music->stream) SDL_DestroyAudioStream(music->stream);
@@ -291,28 +291,28 @@ void JA_EnableMusic(const bool value) {
JA_musicEnabled = value;
}
JA_Sound_t *JA_NewSound(Uint8 *buffer, Uint32 length) {
JA_Sound_t *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_t *JA_LoadSound(uint8_t *buffer, uint32_t size) {
JA_Sound_t *sound = new JA_Sound_t();
JA_Sound_t* JA_LoadSound(uint8_t* buffer, uint32_t size) {
JA_Sound_t* sound = new JA_Sound_t();
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 *sound = new JA_Sound_t();
JA_Sound_t* JA_LoadSound(const char* filename) {
JA_Sound_t* sound = new JA_Sound_t();
SDL_LoadWAV(filename, &sound->spec, &sound->buffer, &sound->length);
return sound;
}
int JA_PlaySound(JA_Sound_t *sound, const int loop) {
int JA_PlaySound(JA_Sound_t* sound, const int loop) {
if (!JA_soundEnabled) return -1;
int channel = 0;
@@ -332,7 +332,7 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop) {
return channel;
}
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop) {
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;
@@ -350,7 +350,7 @@ int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop)
return channel;
}
void JA_DeleteSound(JA_Sound_t *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);
}