SDL now automatically resizes the window upon loading a ROM if it was not manually resized by the user

This commit is contained in:
Lior Halphon 2025-03-30 00:52:56 +03:00
parent 8a0ff891bd
commit 2b89923a6f
3 changed files with 31 additions and 1 deletions

View File

@ -45,6 +45,8 @@ static unsigned factor;
static SDL_Surface *converted_background = NULL;
bool screen_manually_resized = false;
void render_texture(void *pixels, void *previous)
{
if (renderer) {
@ -1253,6 +1255,7 @@ static void cycle_scaling_backwards(unsigned index)
}
update_viewport();
render_texture(NULL, NULL);
screen_manually_resized = false;
}
static void cycle_default_scale(unsigned index)
@ -1266,6 +1269,7 @@ static void cycle_default_scale(unsigned index)
rescale_window();
update_viewport();
screen_manually_resized = false;
}
static void cycle_default_scale_backwards(unsigned index)
@ -1279,6 +1283,7 @@ static void cycle_default_scale_backwards(unsigned index)
rescale_window();
update_viewport();
screen_manually_resized = false;
}
static void cycle_color_correction(unsigned index)
@ -2485,6 +2490,7 @@ void run_gui(bool is_running)
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
update_viewport();
render_texture(NULL, NULL);
screen_manually_resized = true;
}
if (event.window.type == SDL_WINDOWEVENT_MOVED
#if SDL_COMPILEDVERSION > 2018
@ -2639,6 +2645,7 @@ void run_gui(bool is_running)
}
update_swap_interval();
update_viewport();
screen_manually_resized = true;
}
else if (event_hotkey_code(&event) == SDL_SCANCODE_O) {
if (event.key.keysym.mod & MODIFIER) {

View File

@ -45,6 +45,7 @@ enum pending_command {
extern enum pending_command pending_command;
extern unsigned command_parameter;
extern char *dropped_state_file;
extern bool screen_manually_resized;
void update_viewport(void);
void run_gui(bool is_running);

View File

@ -133,6 +133,7 @@ retry: {
case SDL_WINDOWEVENT: {
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
screen_manually_resized = true;
update_viewport();
}
if (event.window.type == SDL_WINDOWEVENT_MOVED
@ -312,6 +313,7 @@ static void handle_events(GB_gameboy_t *gb)
case SDL_WINDOWEVENT: {
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
screen_manually_resized = true;
update_viewport();
}
if (event.window.type == SDL_WINDOWEVENT_MOVED
@ -556,6 +558,7 @@ static void handle_events(GB_gameboy_t *gb)
}
update_swap_interval();
update_viewport();
screen_manually_resized = true;
}
break;
@ -1035,8 +1038,16 @@ static void run(void)
pending_command = GB_SDL_NO_COMMAND;
restart:;
model = model_to_use();
bool should_resize = !screen_manually_resized;
signed current_window_width, current_window_height;
SDL_GetWindowSize(window, &current_window_width, &current_window_height);
if (GB_is_inited(&gb)) {
should_resize =
current_window_width == GB_get_screen_width(&gb) * configuration.default_scale &&
current_window_height == GB_get_screen_height(&gb) * configuration.default_scale;
if (doing_hot_swap) {
doing_hot_swap = false;
}
@ -1107,6 +1118,15 @@ restart:;
GB_switch_model_and_reset(&gb, model);
}
if (should_resize) {
signed width = GB_get_screen_width(&gb) * configuration.default_scale;
signed height = GB_get_screen_height(&gb) * configuration.default_scale;
signed x, y;
SDL_GetWindowPosition(window, &x, &y);
SDL_SetWindowSize(window, width, height);
SDL_SetWindowPosition(window, x - (width - current_window_width) / 2, y - (height - current_window_height) / 2);
}
/* Configure battery */
char battery_save_path[path_length + 5]; /* At the worst case, size is strlen(path) + 4 bytes for .sav + NULL */
replace_extension(filename, path_length, battery_save_path, ".sav");
@ -1417,7 +1437,9 @@ int main(int argc, char **argv)
configuration.allow_background_controllers? "1" : "0");
window = SDL_CreateWindow("SameBoy v" GB_VERSION, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
160 * configuration.default_scale, 144 * configuration.default_scale, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
(configuration.border_mode == GB_BORDER_ALWAYS? 256 : 160) * configuration.default_scale,
(configuration.border_mode == GB_BORDER_ALWAYS? 224 : 144) * configuration.default_scale,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
if (window == NULL) {
fputs(SDL_GetError(), stderr);
exit(1);