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 { struct JA_Sound_t {
SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000}; SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000};
Uint32 length{0}; Uint32 length{0};
Uint8 *buffer{NULL}; Uint8* buffer{NULL};
}; };
struct JA_Channel_t { struct JA_Channel_t {
JA_Sound_t *sound{nullptr}; JA_Sound_t* sound{nullptr};
int pos{0}; int pos{0};
int times{0}; int times{0};
SDL_AudioStream *stream{nullptr}; SDL_AudioStream* stream{nullptr};
JA_Channel_state state{JA_CHANNEL_FREE}; JA_Channel_state state{JA_CHANNEL_FREE};
}; };
struct JA_Music_t { struct JA_Music_t {
SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000}; SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000};
Uint32 length{0}; Uint32 length{0};
Uint8 *buffer{nullptr}; Uint8* buffer{nullptr};
char *filename{nullptr}; char* filename{nullptr};
int pos{0}; int pos{0};
int times{0}; int times{0};
SDL_AudioStream *stream{nullptr}; SDL_AudioStream* stream{nullptr};
JA_Music_state state{JA_MUSIC_INVALID}; JA_Music_state state{JA_MUSIC_INVALID};
}; };
JA_Music_t *current_music{nullptr}; JA_Music_t* current_music{nullptr};
JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS]; JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
SDL_AudioSpec JA_audioSpec{SDL_AUDIO_S16, 2, 48000}; 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 (JA_musicEnabled && current_music && current_music->state == JA_MUSIC_PLAYING) {
if (fading) { if (fading) {
int time = SDL_GetTicks(); int time = SDL_GetTicks();
@@ -152,47 +152,47 @@ void JA_Quit() {
sdlAudioDevice = 0; sdlAudioDevice = 0;
} }
JA_Music_t *JA_LoadMusic(Uint8 *buffer, Uint32 length, const char *filename) { JA_Music_t* JA_LoadMusic(Uint8* buffer, Uint32 length, const char* filename) {
JA_Music_t *music = new JA_Music_t(); JA_Music_t* music = new JA_Music_t();
int chan, samplerate; int chan, samplerate;
short *output; short* output;
music->length = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &output) * chan * 2; music->length = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &output) * chan * 2;
music->spec.channels = chan; music->spec.channels = chan;
music->spec.freq = samplerate; music->spec.freq = samplerate;
music->spec.format = SDL_AUDIO_S16; 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); SDL_memcpy(music->buffer, output, music->length);
free(output); free(output);
music->pos = 0; music->pos = 0;
music->state = JA_MUSIC_STOPPED; music->state = JA_MUSIC_STOPPED;
if (filename) { if (filename) {
music->filename = (char *)malloc(strlen(filename) + 1); music->filename = (char*)malloc(strlen(filename) + 1);
strcpy(music->filename, filename); strcpy(music->filename, filename);
} }
return music; 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. // [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); fseek(f, 0, SEEK_END);
long fsize = ftell(f); long fsize = ftell(f);
fseek(f, 0, SEEK_SET); 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; if (fread(buffer, fsize, 1, f) != 1) return NULL;
fclose(f); fclose(f);
JA_Music_t *music = JA_LoadMusic(buffer, fsize, filename); JA_Music_t* music = JA_LoadMusic(buffer, fsize, filename);
free(buffer); free(buffer);
return music; 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; if (!JA_musicEnabled) return;
JA_StopMusic(); JA_StopMusic();
@@ -209,7 +209,7 @@ void JA_PlayMusic(JA_Music_t *music, const int loop) {
// SDL_ResumeAudioStreamDevice(current_music->stream); // SDL_ResumeAudioStreamDevice(current_music->stream);
} }
char *JA_GetMusicFilename(JA_Music_t *music) { char* JA_GetMusicFilename(JA_Music_t* music) {
if (!music) music = current_music; if (!music) music = current_music;
return music->filename; return music->filename;
} }
@@ -262,7 +262,7 @@ JA_Music_state JA_GetMusicState() {
return current_music->state; 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; if (current_music == music) current_music = nullptr;
SDL_free(music->buffer); SDL_free(music->buffer);
if (music->stream) SDL_DestroyAudioStream(music->stream); if (music->stream) SDL_DestroyAudioStream(music->stream);
@@ -291,28 +291,28 @@ void JA_EnableMusic(const bool value) {
JA_musicEnabled = value; JA_musicEnabled = 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(); JA_Sound_t* sound = new JA_Sound_t();
sound->buffer = buffer; sound->buffer = buffer;
sound->length = length; sound->length = length;
return sound; 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(); JA_Sound_t* sound = new JA_Sound_t();
SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size), 1, &sound->spec, &sound->buffer, &sound->length); SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size), 1, &sound->spec, &sound->buffer, &sound->length);
return sound; 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(); JA_Sound_t* sound = new JA_Sound_t();
SDL_LoadWAV(filename, &sound->spec, &sound->buffer, &sound->length); SDL_LoadWAV(filename, &sound->spec, &sound->buffer, &sound->length);
return sound; 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; if (!JA_soundEnabled) return -1;
int channel = 0; int channel = 0;
@@ -332,7 +332,7 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop) {
return channel; 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 (!JA_soundEnabled) return -1;
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) 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; 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++) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
if (channels[i].sound == sound) JA_StopChannel(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_Init(const int freq, const SDL_AudioFormat format, const int num_channels);
void JA_Quit(); void JA_Quit();
JA_Music_t *JA_LoadMusic(const char *filename); JA_Music_t* JA_LoadMusic(const char* filename);
JA_Music_t *JA_LoadMusic(Uint8 *buffer, Uint32 length, const char *filename = nullptr); JA_Music_t* JA_LoadMusic(Uint8* buffer, Uint32 length, const char* filename = nullptr);
void JA_PlayMusic(JA_Music_t *music, const int loop = -1); void JA_PlayMusic(JA_Music_t* music, const int loop = -1);
char *JA_GetMusicFilename(JA_Music_t *music = nullptr); char* JA_GetMusicFilename(JA_Music_t* music = nullptr);
void JA_PauseMusic(); void JA_PauseMusic();
void JA_ResumeMusic(); void JA_ResumeMusic();
void JA_StopMusic(); void JA_StopMusic();
void JA_FadeOutMusic(const int milliseconds); void JA_FadeOutMusic(const int milliseconds);
JA_Music_state JA_GetMusicState(); JA_Music_state JA_GetMusicState();
void JA_DeleteMusic(JA_Music_t *music); void JA_DeleteMusic(JA_Music_t* music);
float JA_SetMusicVolume(float volume); float JA_SetMusicVolume(float volume);
void JA_SetMusicPosition(float value); void JA_SetMusicPosition(float value);
float JA_GetMusicPosition(); float JA_GetMusicPosition();
void JA_EnableMusic(const bool value); 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 *JA_LoadSound(Uint8 *buffer, Uint32 length); JA_Sound_t* JA_LoadSound(Uint8* buffer, Uint32 length);
JA_Sound_t *JA_LoadSound(const char *filename); JA_Sound_t* JA_LoadSound(const char* filename);
int JA_PlaySound(JA_Sound_t *sound, const int loop = 0); 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); int JA_PlaySoundOnChannel(JA_Sound_t* sound, const int channel, const int loop = 0);
void JA_PauseChannel(const int channel); void JA_PauseChannel(const int channel);
void JA_ResumeChannel(const int channel); void JA_ResumeChannel(const int channel);
void JA_StopChannel(const int channel); void JA_StopChannel(const int channel);
JA_Channel_state JA_GetChannelState(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); float JA_SetSoundVolume(float volume);
void JA_EnableSound(const bool value); void JA_EnableSound(const bool value);

View File

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

View File

@@ -7,8 +7,8 @@ struct Color {
Uint8 b; Uint8 b;
}; };
typedef Uint8 *JD8_Surface; typedef Uint8* JD8_Surface;
typedef Color *JD8_Palette; typedef Color* JD8_Palette;
void JD8_Init(); void JD8_Init();
@@ -18,9 +18,9 @@ void JD8_ClearScreen(Uint8 color);
JD8_Surface JD8_NewSurface(); 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); void JD8_SetScreenPalette(JD8_Palette palette);

View File

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

View File

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

View File

@@ -19,17 +19,17 @@ namespace Menu {
static constexpr int BOX_W = 220; static constexpr int BOX_W = 220;
static constexpr int BOX_H = 150; static constexpr int BOX_H = 150;
static constexpr int BOX_X = (SCREEN_W - BOX_W) / 2; // 50 static constexpr int BOX_X = (SCREEN_W - BOX_W) / 2; // 50
static constexpr int BOX_Y = (SCREEN_H - BOX_H) / 2; // 25 static constexpr int BOX_Y = (SCREEN_H - BOX_H) / 2; // 25
static constexpr Uint32 BG_COLOR = 0xFF1A0E0E; // fons marró fosc (ABGR) static constexpr Uint32 BG_COLOR = 0xFF1A0E0E; // fons marró fosc (ABGR)
static constexpr Uint8 BG_ALPHA = 220; // semi-transparent static constexpr Uint8 BG_ALPHA = 220; // semi-transparent
static constexpr Uint32 BORDER_COLOR = 0xFFFFFF00; // cyan static constexpr Uint32 BORDER_COLOR = 0xFFFFFF00; // cyan
static constexpr Uint32 TITLE_COLOR = 0xFFFFFFFF; // blanc static constexpr Uint32 TITLE_COLOR = 0xFFFFFFFF; // blanc
static constexpr Uint32 LABEL_COLOR = 0xFFCCCCCC; // gris clar static constexpr Uint32 LABEL_COLOR = 0xFFCCCCCC; // gris clar
static constexpr Uint32 VALUE_COLOR = 0xFFFFFF00; // cyan static constexpr Uint32 VALUE_COLOR = 0xFFFFFF00; // cyan
static constexpr Uint32 CURSOR_COLOR = 0xFF00FFFF; // groc static constexpr Uint32 CURSOR_COLOR = 0xFF00FFFF; // groc
static constexpr Uint32 FOOTER_COLOR = 0xFF888888; // gris static constexpr Uint32 FOOTER_COLOR = 0xFF888888; // gris
static constexpr int TITLE_PAD_Y = 4; static constexpr int TITLE_PAD_Y = 4;
static constexpr int ITEM_PAD_X = 10; static constexpr int ITEM_PAD_X = 10;
@@ -64,63 +64,46 @@ namespace Menu {
items_.clear(); items_.clear();
// ZOOM // ZOOM
items_.push_back({"ZOOM", ItemKind::IntRange, items_.push_back({"ZOOM", ItemKind::IntRange, [] {
[] {
char buf[16]; char buf[16];
std::snprintf(buf, sizeof(buf), "%dX", Screen::get()->getZoom()); std::snprintf(buf, sizeof(buf), "%dX", Screen::get()->getZoom());
return std::string(buf); return std::string(buf); }, [](int dir) {
},
[](int dir) {
if (dir < 0) Screen::get()->decZoom(); if (dir < 0) Screen::get()->decZoom();
else if (dir > 0) Screen::get()->incZoom(); else if (dir > 0) Screen::get()->incZoom(); }});
}});
// PANTALLA (fullscreen) // PANTALLA (fullscreen)
items_.push_back({"PANTALLA", ItemKind::Toggle, items_.push_back({"PANTALLA", ItemKind::Toggle, [] { return std::string(Screen::get()->isFullscreen() ? "COMPLETA" : "FINESTRA"); }, [](int) { Screen::get()->toggleFullscreen(); }});
[] { return std::string(Screen::get()->isFullscreen() ? "COMPLETA" : "FINESTRA"); },
[](int) { Screen::get()->toggleFullscreen(); }});
// SHADER // SHADER
items_.push_back({"SHADER", ItemKind::Toggle, items_.push_back({"SHADER", ItemKind::Toggle, [] { return onOff(Options::video.shader_enabled); }, [](int) { Screen::get()->toggleShaders(); }});
[] { return onOff(Options::video.shader_enabled); },
[](int) { Screen::get()->toggleShaders(); }});
// ASPECTE 4:3 // ASPECTE 4:3
items_.push_back({"ASPECTE 4:3", ItemKind::Toggle, items_.push_back({"ASPECTE 4:3", ItemKind::Toggle, [] { return yesNo(Options::video.aspect_ratio_4_3); }, [](int) { Screen::get()->toggleAspectRatio(); }});
[] { return yesNo(Options::video.aspect_ratio_4_3); },
[](int) { Screen::get()->toggleAspectRatio(); }});
// SUPERSAMPLING // SUPERSAMPLING
items_.push_back({"SUPERSAMPLING", ItemKind::Toggle, items_.push_back({"SUPERSAMPLING", ItemKind::Toggle, [] { return onOff(Options::video.supersampling); }, [](int) { Screen::get()->toggleSupersampling(); }});
[] { return onOff(Options::video.supersampling); },
[](int) { Screen::get()->toggleSupersampling(); }});
// TIPUS SHADER // TIPUS SHADER
items_.push_back({"TIPUS SHADER", ItemKind::Cycle, items_.push_back({"TIPUS SHADER", ItemKind::Cycle, [] { return std::string(Screen::get()->getActiveShaderName()); }, [](int dir) {
[] { return std::string(Screen::get()->getActiveShaderName()); }, if (dir < 0) Screen::get()->prevShaderType();
[](int) { Screen::get()->nextShaderType(); }}); else Screen::get()->nextShaderType(); }});
// PRESET // PRESET
items_.push_back({"PRESET", ItemKind::Cycle, items_.push_back({"PRESET", ItemKind::Cycle, [] { return std::string(Screen::get()->getCurrentPresetName()); }, [](int dir) {
[] { return std::string(Screen::get()->getCurrentPresetName()); }, if (dir < 0) Screen::get()->prevPreset();
[](int) { Screen::get()->nextPreset(); }}); else Screen::get()->nextPreset(); }});
// FILTRE 4:3 // FILTRE 4:3
items_.push_back({"FILTRE 4:3", ItemKind::Toggle, items_.push_back({"FILTRE 4:3", ItemKind::Toggle, [] { return std::string(Options::video.stretch_filter_linear ? "LINEAR" : "NEAREST"); }, [](int) { Screen::get()->toggleStretchFilter(); }});
[] { return std::string(Options::video.stretch_filter_linear ? "LINEAR" : "NEAREST"); },
[](int) { Screen::get()->toggleStretchFilter(); }});
// RENDER INFO // RENDER INFO
items_.push_back({"RENDER INFO", ItemKind::Cycle, items_.push_back({"RENDER INFO", ItemKind::Cycle, [] {
[] {
switch (Options::render_info.position) { switch (Options::render_info.position) {
case Options::RenderInfoPosition::OFF: return std::string("OFF"); case Options::RenderInfoPosition::OFF: return std::string("OFF");
case Options::RenderInfoPosition::TOP: return std::string("TOP"); case Options::RenderInfoPosition::TOP: return std::string("TOP");
case Options::RenderInfoPosition::BOTTOM: return std::string("BOTTOM"); case Options::RenderInfoPosition::BOTTOM: return std::string("BOTTOM");
} }
return std::string("OFF"); return std::string("OFF"); }, [](int dir) { Overlay::cycleRenderInfo(dir); }});
},
[](int) { Overlay::toggleRenderInfo(); }});
} }
// --- Dibuix --- // --- Dibuix ---
@@ -161,10 +144,10 @@ namespace Menu {
} }
static void drawBorder(Uint32* buf, int x, int y, int w, int h, Uint32 color) { static void drawBorder(Uint32* buf, int x, int y, int w, int h, Uint32 color) {
fillRect(buf, x, y, w, 1, color); // top fillRect(buf, x, y, w, 1, color); // top
fillRect(buf, x, y + h - 1, w, 1, color); // bottom fillRect(buf, x, y + h - 1, w, 1, color); // bottom
fillRect(buf, x, y, 1, h, color); // left fillRect(buf, x, y, 1, h, color); // left
fillRect(buf, x + w - 1, y, 1, h, color); // right fillRect(buf, x + w - 1, y, 1, h, color); // right
} }
// --- API pública --- // --- API pública ---

View File

@@ -170,19 +170,13 @@ namespace Overlay {
notifications_.push_back(notif); notifications_.push_back(notif);
} }
void toggleRenderInfo() { void toggleRenderInfo() { cycleRenderInfo(+1); }
// Cicla: OFF → TOP → BOTTOM → OFF
switch (Options::render_info.position) { void cycleRenderInfo(int dir) {
case Options::RenderInfoPosition::OFF: // Seqüència: OFF → TOP → BOTTOM → OFF
Options::render_info.position = Options::RenderInfoPosition::TOP; int pos = static_cast<int>(Options::render_info.position);
break; pos = (pos + (dir >= 0 ? 1 : -1) + 3) % 3;
case Options::RenderInfoPosition::TOP: Options::render_info.position = static_cast<Options::RenderInfoPosition>(pos);
Options::render_info.position = Options::RenderInfoPosition::BOTTOM;
break;
case Options::RenderInfoPosition::BOTTOM:
Options::render_info.position = Options::RenderInfoPosition::OFF;
break;
}
} }
void setRenderInfoText(const char* text) { void setRenderInfoText(const char* text) {

View File

@@ -14,6 +14,7 @@ namespace Overlay {
// Activa/desactiva la info de renderitzat (FPS, driver, shader, preset) // Activa/desactiva la info de renderitzat (FPS, driver, shader, preset)
void toggleRenderInfo(); void toggleRenderInfo();
void cycleRenderInfo(int dir); // dir=+1 avant, -1 endarrere
void setRenderInfoText(const char* text); void setRenderInfoText(const char* text);
// Gestió d'eixida amb doble ESC // Gestió d'eixida amb doble ESC

View File

@@ -240,6 +240,29 @@ void Screen::nextPreset() {
} }
} }
void Screen::prevShaderType() {
// Només dues opcions — prev == next
nextShaderType();
}
void Screen::prevPreset() {
if (!shader_backend_ || !shader_backend_->isHardwareAccelerated()) return;
if (shader_backend_->getActiveShader() == Rendering::ShaderType::POSTFX) {
if (Options::postfx_presets.empty()) return;
int n = static_cast<int>(Options::postfx_presets.size());
Options::current_postfx_preset = (Options::current_postfx_preset - 1 + n) % n;
Options::video.current_postfx_preset = Options::postfx_presets[Options::current_postfx_preset].name;
applyCurrentPostFXPreset();
} else {
if (Options::crtpi_presets.empty()) return;
int n = static_cast<int>(Options::crtpi_presets.size());
Options::current_crtpi_preset = (Options::current_crtpi_preset - 1 + n) % n;
Options::video.current_crtpi_preset = Options::crtpi_presets[Options::current_crtpi_preset].name;
applyCurrentCrtPiPreset();
}
}
auto Screen::getCurrentPresetName() const -> const char* { auto Screen::getCurrentPresetName() const -> const char* {
if (!shader_backend_ || !shader_backend_->isHardwareAccelerated()) return "---"; if (!shader_backend_ || !shader_backend_->isHardwareAccelerated()) return "---";
if (shader_backend_->getActiveShader() == Rendering::ShaderType::POSTFX) { if (shader_backend_->getActiveShader() == Rendering::ShaderType::POSTFX) {

View File

@@ -29,7 +29,9 @@ class Screen {
void toggleIntegerScale(); void toggleIntegerScale();
void toggleStretchFilter(); void toggleStretchFilter();
void nextShaderType(); // Cicla PostFX ↔ CrtPi (F7) void nextShaderType(); // Cicla PostFX ↔ CrtPi (F7)
void prevShaderType(); // Cicla al revés
void nextPreset(); // Cicla presets del shader actiu (F8) void nextPreset(); // Cicla presets del shader actiu (F8)
void prevPreset(); // Cicla presets al revés
[[nodiscard]] auto getCurrentPresetName() const -> const char*; [[nodiscard]] auto getCurrentPresetName() const -> const char*;
void setActiveShader(Rendering::ShaderType type); void setActiveShader(Rendering::ShaderType type);
void applyCurrentPostFXPreset(); void applyCurrentPostFXPreset();

View File

@@ -120,6 +120,8 @@ void Director::handleEvents() {
if (event.key.scancode == SDL_SCANCODE_ESCAPE) { if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
Menu::close(); Menu::close();
JI_SetInputBlocked(false); JI_SetInputBlocked(false);
// Empassa l'ESC fins al release perquè el joc no la veja per polling
esc_swallow_until_release_ = true;
} else { } else {
Menu::handleKey(event.key.scancode); Menu::handleKey(event.key.scancode);
} }
@@ -128,6 +130,11 @@ void Director::handleEvents() {
if (Menu::isOpen() && event.type == SDL_EVENT_KEY_UP) { if (Menu::isOpen() && event.type == SDL_EVENT_KEY_UP) {
continue; // no deixem passar KEY_UP al joc tampoc continue; // no deixem passar KEY_UP al joc tampoc
} }
// Allibera el bloqueig d'ESC quan l'usuari la deixa anar
if (event.type == SDL_EVENT_KEY_UP && event.key.scancode == SDL_SCANCODE_ESCAPE && esc_swallow_until_release_) {
esc_swallow_until_release_ = false;
continue;
}
// ESC: interceptem KEY_DOWN per bloquejar-la ABANS que el joc la veja per polling // ESC: interceptem KEY_DOWN per bloquejar-la ABANS que el joc la veja per polling
if (event.type == SDL_EVENT_KEY_DOWN && event.key.scancode == SDL_SCANCODE_ESCAPE && !event.key.repeat) { if (event.type == SDL_EVENT_KEY_DOWN && event.key.scancode == SDL_SCANCODE_ESCAPE && !event.key.repeat) {
esc_blocked_ = true; // Bloqueja ESC per polling immediatament esc_blocked_ = true; // Bloqueja ESC per polling immediatament

View File

@@ -32,7 +32,7 @@ class Director {
auto consumeKeyPressed() -> bool; auto consumeKeyPressed() -> bool;
// Indica si ESC està bloquejada (el joc no l'ha de veure) // Indica si ESC està bloquejada (el joc no l'ha de veure)
auto isEscBlocked() const -> bool { return esc_blocked_; } auto isEscBlocked() const -> bool { return esc_blocked_ || esc_swallow_until_release_; }
private: private:
Director() = default; Director() = default;
@@ -56,4 +56,7 @@ class Director {
std::atomic<bool> game_thread_done_{false}; std::atomic<bool> game_thread_done_{false};
std::atomic<bool> key_pressed_{false}; std::atomic<bool> key_pressed_{false};
std::atomic<bool> esc_blocked_{false}; std::atomic<bool> esc_blocked_{false};
// Quan el menú tanca amb ESC, empassem-nos l'ESC fins que l'usuari la deixe anar,
// per no fer eixir el joc al proper poll de JI_KeyPressed.
std::atomic<bool> esc_swallow_until_release_{false};
}; };