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);
}

View File

@@ -18,31 +18,31 @@ struct JA_Music_t;
void JA_Init(const int freq, const SDL_AudioFormat format, const int num_channels);
void JA_Quit();
JA_Music_t *JA_LoadMusic(const char *filename);
JA_Music_t *JA_LoadMusic(Uint8 *buffer, Uint32 length, const char *filename = nullptr);
void JA_PlayMusic(JA_Music_t *music, const int loop = -1);
char *JA_GetMusicFilename(JA_Music_t *music = nullptr);
JA_Music_t* JA_LoadMusic(const char* filename);
JA_Music_t* JA_LoadMusic(Uint8* buffer, Uint32 length, const char* filename = nullptr);
void JA_PlayMusic(JA_Music_t* music, const int loop = -1);
char* JA_GetMusicFilename(JA_Music_t* music = nullptr);
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);
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);
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);
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);
JA_Channel_state JA_GetChannelState(const int channel);
void JA_DeleteSound(JA_Sound_t *sound);
void JA_DeleteSound(JA_Sound_t* sound);
float JA_SetSoundVolume(float volume);
void JA_EnableSound(const bool value);

View File

@@ -8,12 +8,12 @@
JD8_Surface screen = NULL;
JD8_Palette main_palette = NULL;
Uint32 *pixel_data = NULL;
Uint32* pixel_data = NULL;
void JD8_Init() {
screen = (JD8_Surface)calloc(1, 64000);
main_palette = (JD8_Palette)calloc(1, 768);
pixel_data = (Uint32 *)calloc(1, 320 * 200 * 4);
pixel_data = (Uint32*)calloc(1, 320 * 200 * 4);
}
void JD8_Quit() {
@@ -32,12 +32,12 @@ JD8_Surface JD8_NewSurface() {
return surface;
}
JD8_Surface JD8_LoadSurface(const char *file) {
JD8_Surface JD8_LoadSurface(const char* file) {
int filesize = 0;
char *buffer = file_getfilebuffer(file, filesize);
char* buffer = file_getfilebuffer(file, filesize);
unsigned short w, h;
Uint8 *pixels = LoadGif((unsigned char *)buffer, &w, &h);
Uint8* pixels = LoadGif((unsigned char*)buffer, &w, &h);
free(buffer);
@@ -53,12 +53,12 @@ JD8_Surface JD8_LoadSurface(const char *file) {
return image;
}
JD8_Palette JD8_LoadPalette(const char *file) {
JD8_Palette JD8_LoadPalette(const char* file) {
int filesize = 0;
char *buffer = NULL;
char* buffer = NULL;
buffer = file_getfilebuffer(file, filesize);
JD8_Palette palette = (JD8_Palette)LoadPalette((unsigned char *)buffer);
JD8_Palette palette = (JD8_Palette)LoadPalette((unsigned char*)buffer);
return palette;
}

View File

@@ -7,8 +7,8 @@ struct Color {
Uint8 b;
};
typedef Uint8 *JD8_Surface;
typedef Color *JD8_Palette;
typedef Uint8* JD8_Surface;
typedef Color* JD8_Palette;
void JD8_Init();
@@ -18,9 +18,9 @@ void JD8_ClearScreen(Uint8 color);
JD8_Surface JD8_NewSurface();
JD8_Surface JD8_LoadSurface(const char *file);
JD8_Surface JD8_LoadSurface(const char* file);
JD8_Palette JD8_LoadPalette(const char *file);
JD8_Palette JD8_LoadPalette(const char* file);
void JD8_SetScreenPalette(JD8_Palette palette);

View File

@@ -34,22 +34,22 @@ struct keyvalue_t {
std::string key, value;
};
char *resource_filename = NULL;
char *resource_folder = NULL;
char* resource_filename = NULL;
char* resource_folder = NULL;
int file_source = SOURCE_FILE;
char scratch[255];
static std::string config_folder;
std::vector<keyvalue_t> config;
void file_setresourcefilename(const char *str) {
void file_setresourcefilename(const char* str) {
if (resource_filename != NULL) free(resource_filename);
resource_filename = (char *)malloc(strlen(str) + 1);
resource_filename = (char*)malloc(strlen(str) + 1);
strcpy(resource_filename, str);
}
void file_setresourcefolder(const char *str) {
void file_setresourcefolder(const char* str) {
if (resource_folder != NULL) free(resource_folder);
resource_folder = (char *)malloc(strlen(str) + 1);
resource_folder = (char*)malloc(strlen(str) + 1);
strcpy(resource_folder, str);
}
@@ -66,16 +66,16 @@ bool file_getdictionary() {
char header[4];
fi.read(header, 4);
uint32_t num_files, toc_offset;
fi.read((char *)&num_files, 4);
fi.read((char *)&toc_offset, 4);
fi.read((char*)&num_files, 4);
fi.read((char*)&toc_offset, 4);
fi.seekg(toc_offset);
for (uint32_t i = 0; i < num_files; ++i) {
uint32_t file_offset, file_size;
fi.read((char *)&file_offset, 4);
fi.read((char *)&file_size, 4);
fi.read((char*)&file_offset, 4);
fi.read((char*)&file_size, 4);
uint8_t path_size;
fi.read((char *)&path_size, 1);
fi.read((char*)&path_size, 1);
char file_name[path_size + 1];
fi.read(file_name, path_size);
file_name[path_size] = 0;
@@ -86,18 +86,18 @@ bool file_getdictionary() {
return true;
}
char *file_getfilenamewithfolder(const char *filename) {
char* file_getfilenamewithfolder(const char* filename) {
strcpy(scratch, resource_folder);
strcat(scratch, filename);
return scratch;
}
FILE *file_getfilepointer(const char *resourcename, int &filesize, const bool binary) {
FILE* file_getfilepointer(const char* resourcename, int& filesize, const bool binary) {
if (file_source == SOURCE_FILE and toc.size() == 0) {
if (not file_getdictionary()) file_setsource(SOURCE_FOLDER);
}
FILE *f;
FILE* f;
if (file_source == SOURCE_FILE) {
bool found = false;
@@ -129,9 +129,9 @@ FILE *file_getfilepointer(const char *resourcename, int &filesize, const bool bi
return f;
}
char *file_getfilebuffer(const char *resourcename, int &filesize, const bool zero_terminate) {
FILE *f = file_getfilepointer(resourcename, filesize, true);
char *buffer = (char *)malloc(zero_terminate ? filesize : filesize + 1);
char* file_getfilebuffer(const char* resourcename, int& filesize, const bool zero_terminate) {
FILE* f = file_getfilepointer(resourcename, filesize, true);
char* buffer = (char*)malloc(zero_terminate ? filesize : filesize + 1);
fread(buffer, filesize, 1, f);
if (zero_terminate) buffer[filesize] = 0;
fclose(f);
@@ -140,23 +140,23 @@ char *file_getfilebuffer(const char *resourcename, int &filesize, const bool zer
// Crea la carpeta del sistema donde guardar datos.
// Acepta rutas con subdirectorios (ej: "jailgames/aee") y crea toda la jerarquía.
void file_setconfigfolder(const char *foldername) {
void file_setconfigfolder(const char* foldername) {
#ifdef _WIN32
config_folder = std::string(getenv("APPDATA")) + "/" + foldername;
#elif __APPLE__
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
struct passwd* pw = getpwuid(getuid());
const char* homedir = pw->pw_dir;
config_folder = std::string(homedir) + "/Library/Application Support/" + foldername;
#elif __linux__
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
struct passwd* pw = getpwuid(getuid());
const char* homedir = pw->pw_dir;
config_folder = std::string(homedir) + "/.config/" + foldername;
#endif
std::filesystem::create_directories(config_folder);
}
const char *file_getconfigfolder() {
const char* file_getconfigfolder() {
static std::string folder;
folder = config_folder + "/";
return folder.c_str();
@@ -165,12 +165,12 @@ const char *file_getconfigfolder() {
void file_loadconfigvalues() {
config.clear();
std::string config_file = config_folder + "/config.txt";
FILE *f = fopen(config_file.c_str(), "r");
FILE* f = fopen(config_file.c_str(), "r");
if (!f) return;
char line[1024];
while (fgets(line, sizeof(line), f)) {
char *value = strchr(line, '=');
char* value = strchr(line, '=');
if (value) {
*value = '\0';
value++;
@@ -183,7 +183,7 @@ void file_loadconfigvalues() {
void file_saveconfigvalues() {
std::string config_file = config_folder + "/config.txt";
FILE *f = fopen(config_file.c_str(), "w");
FILE* f = fopen(config_file.c_str(), "w");
if (f) {
for (auto pair : config) {
fprintf(f, "%s=%s\n", pair.key.c_str(), pair.value.c_str());
@@ -192,7 +192,7 @@ void file_saveconfigvalues() {
}
}
const char *file_getconfigvalue(const char *key) {
const char* file_getconfigvalue(const char* key) {
if (config.empty()) file_loadconfigvalues();
for (auto pair : config) {
if (pair.key == std::string(key)) {
@@ -203,9 +203,9 @@ const char *file_getconfigvalue(const char *key) {
return NULL;
}
void file_setconfigvalue(const char *key, const char *value) {
void file_setconfigvalue(const char* key, const char* value) {
if (config.empty()) file_loadconfigvalues();
for (auto &pair : config) {
for (auto& pair : config) {
if (pair.key == std::string(key)) {
pair.value = value;
file_saveconfigvalues();

View File

@@ -4,15 +4,15 @@
#define SOURCE_FILE 0
#define SOURCE_FOLDER 1
void file_setconfigfolder(const char *foldername);
const char *file_getconfigfolder();
void file_setconfigfolder(const char* foldername);
const char* file_getconfigfolder();
void file_setresourcefilename(const char *str);
void file_setresourcefolder(const char *str);
void file_setresourcefilename(const char* str);
void file_setresourcefolder(const char* str);
void file_setsource(const int src);
FILE *file_getfilepointer(const char *resourcename, int &filesize, const bool binary = false);
char *file_getfilebuffer(const char *resourcename, int &filesize, const bool zero_terminate = false);
FILE* file_getfilepointer(const char* resourcename, int& filesize, const bool binary = false);
char* file_getfilebuffer(const char* resourcename, int& filesize, const bool zero_terminate = false);
const char *file_getconfigvalue(const char *key);
void file_setconfigvalue(const char *key, const char *value);
const char* file_getconfigvalue(const char* key);
void file_setconfigvalue(const char* key, const char* value);