From d3a7a71ea7e06c8c00c209e910948bc070e53e68 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 10 Jan 2024 15:53:59 +0100 Subject: [PATCH] better window resizing Signed-off-by: Tim --- src/core/emu_main.c | 26 ++++++++++++++++++-------- src/gui/main_window.c | 41 +++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/core/emu_main.c b/src/core/emu_main.c index 8df70a3..41f4ad4 100644 --- a/src/core/emu_main.c +++ b/src/core/emu_main.c @@ -140,26 +140,36 @@ int32_t emu_main(options_config *config, ui_scale *scale) { while (!IsKeyPressed(KEY_ESCAPE)) { BeginDrawing(); + ClearBackground(BLACK); if (IsWindowResized()) { - config->display_scaling = GetScreenWidth() / DISPLAY_WIDTH; + config->display_scaling = (uint32_t) fminf(GetScreenWidth() / (float) DISPLAY_WIDTH, + GetScreenHeight() / (float) DISPLAY_HEIGHT); + scale->window_width = DISPLAY_WIDTH * config->display_scaling; scale->window_height = DISPLAY_HEIGHT * config->display_scaling; - SetWindowSize(scale->window_width, scale->window_height); - scale->button_width = (float) (scale->window_width / 4.8); - scale->button_height = (float) ((float) scale->window_height / 16); - scale->button_x = (float) ((float) scale->window_width / 2 - (scale->window_width / 9.6)); - scale->font_size = (scale->window_height / 12); + scale->button_width = (float) (GetScreenWidth() / 4.8); + scale->button_height = (float) ((float) GetScreenHeight() / 16); + scale->button_x = (float) ((float) GetScreenWidth() / 2 - (GetScreenWidth() / 9.6)); + scale->font_size = (GetScreenWidth() / (int32_t) config->display_scaling); } UnloadDroppedFiles(LoadDroppedFiles()); // Draw Pixels from virtual Texture - DrawTextureEx(chip8.display, (Vector2) {0, 0}, 0, (float) config->display_scaling, WHITE); + DrawTexturePro(chip8.display, (Rectangle) {0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT}, + (Rectangle) {(GetScreenWidth() - scale->window_width) / 2, + (GetScreenHeight() - scale->window_height) / 2, scale->window_width, + scale->window_height}, (Vector2) {0, 0}, 0, WHITE); + + DrawRectangleLines(((GetScreenWidth() - scale->window_width) / 2), + (((GetScreenHeight() - scale->window_height) / 2)), scale->window_width, + scale->window_height, DARKGRAY); if (config->show_fps) { - DrawText(TextFormat("%dhz", GetFPS() + 1), (int32_t) config->display_scaling, (int32_t) config->display_scaling / 2, + DrawText(TextFormat("%dhz", GetFPS() + 1), (int32_t) config->display_scaling, + (int32_t) config->display_scaling / 2, (int32_t) (GetScreenHeight() / (config->display_scaling * 1.5)), DARKGREEN); } diff --git a/src/gui/main_window.c b/src/gui/main_window.c index 149b8e2..dda74be 100644 --- a/src/gui/main_window.c +++ b/src/gui/main_window.c @@ -53,10 +53,10 @@ void main_window(options_config *config) { InitWindow(scale.window_width, scale.window_height, WINDOW_TITLE VERSION); scale.button_width = (float) (scale.window_width / 4.8); - scale.button_height = (float) ((float) scale.window_height / 16); + scale.button_height = (float) ((float) GetScreenHeight() / 16); scale.button_x = (float) ((float) scale.window_width / 2 - (scale.window_width / 9.6)); - scale.font_size = (scale.window_height / 12); + scale.font_size = (GetScreenHeight() / (int32_t)config->display_scaling); GuiLoadStyleDark(); enum menu_state_counter { @@ -70,15 +70,16 @@ void main_window(options_config *config) { ClearBackground(BLACK); if (IsWindowResized()) { - config->display_scaling = GetScreenWidth() / DISPLAY_WIDTH; + config->display_scaling = (uint32_t) fminf(GetScreenWidth() / (float) DISPLAY_WIDTH, + GetScreenHeight() / (float) DISPLAY_HEIGHT); + scale.window_width = DISPLAY_WIDTH * config->display_scaling; scale.window_height = DISPLAY_HEIGHT * config->display_scaling; - SetWindowSize(scale.window_width, scale.window_height); - scale.button_width = (float) (scale.window_width / 4.8); - scale.button_height = (float) ((float) scale.window_height / 16); - scale.button_x = (float) ((float) scale.window_width / 2 - (scale.window_width / 9.6)); - scale.font_size = (scale.window_height / 12); + scale.button_width = (float) (GetScreenWidth() / 4.8); + scale.button_height = (float) ((float) GetScreenHeight() / 16); + scale.button_x = (float) ((float) GetScreenWidth() / 2 - (GetScreenWidth() / 9.6)); + scale.font_size = (GetScreenWidth() / (int32_t) config->display_scaling); } if (IsFileDropped()) { // Initialize Emulation @@ -92,22 +93,22 @@ void main_window(options_config *config) { switch (menu_state) { /*-------------------------------------------------------------------------------------------------------------*/ case (normal): - DrawText("Chisel8 Emulator", scale.window_width / 2 - (scale.font_size * 4), (scale.window_height / 12), + DrawText("Chisel8 Emulator", GetScreenWidth() / 2 - (scale.font_size * 4), (GetScreenHeight() / 12), scale.font_size, RAYWHITE); - if (GuiButton((Rectangle) {scale.button_x, scale.window_height / 2 - scale.window_height / 6 - 10, + if (GuiButton((Rectangle) {scale.button_x, GetScreenHeight() / 2 - GetScreenHeight() / 6 - 10, scale.button_width, scale.button_height}, GuiIconText(ICON_CPU, "Load ROM"))) { menu_state = init; } - if (GuiButton((Rectangle) {scale.button_x, scale.window_height / 2 - scale.window_height / 16, + if (GuiButton((Rectangle) {scale.button_x, GetScreenHeight() / 2 - GetScreenHeight() / 16, scale.button_width, scale.button_height}, GuiIconText(ICON_GEAR, "Settings"))) { menu_state = options; } - if (GuiButton((Rectangle) {scale.button_x, scale.window_height - (scale.window_height / 6), + if (GuiButton((Rectangle) {scale.button_x, GetScreenHeight() - (GetScreenHeight() / 6), scale.button_width, scale.button_height}, GuiIconText(ICON_EXIT, "Quit"))) { EndDrawing(); @@ -122,25 +123,25 @@ void main_window(options_config *config) { // TODO: Call function from options_window source file and actually add options to choose from // TODO: Should allow the user to change the window scaling and other options... - DrawText("Settings", scale.window_width / 2 - (scale.font_size * 2), (scale.window_height / 12), + DrawText("Settings", GetScreenWidth() / 2 - (scale.font_size * 2), (GetScreenHeight() / 12), scale.font_size, RAYWHITE); if (config->volume != 0) { - if (GuiButton((Rectangle) {scale.button_x, scale.window_height - (scale.window_height / 2), + if (GuiButton((Rectangle) {scale.button_x, GetScreenHeight() - (GetScreenHeight() / 2), scale.button_width, scale.button_height}, GuiIconText(ICON_AUDIO, "Mute Audio"))) { temp = config->volume; config->volume = 0.0f; } } else { - if (GuiButton((Rectangle) {scale.button_x, scale.window_height - (scale.window_height / 2), + if (GuiButton((Rectangle) {scale.button_x, GetScreenHeight() - (GetScreenHeight() / 2), scale.button_width, scale.button_height}, GuiIconText(ICON_AUDIO, "Unmute Audio"))) { config->volume = temp; } } - if (GuiButton((Rectangle) {scale.button_x, scale.window_height - (scale.window_height / 6), + if (GuiButton((Rectangle) {scale.button_x, GetScreenHeight() - (GetScreenHeight() / 6), scale.button_width, scale.button_height}, GuiIconText(ICON_REREDO_FILL, "Return"))) { menu_state = normal; @@ -151,12 +152,12 @@ void main_window(options_config *config) { /*-------------------------------------------------------------------------------------------------------------*/ case (init): - DrawRectangle(0, 0, scale.window_width, scale.window_height, Fade(RAYWHITE, 0.2f)); + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.2f)); DrawText("Please drag a ROM file into the window", - scale.window_width / 2 - (scale.font_size * 10) + (scale.font_size / 2), - (scale.window_height / 2) - scale.font_size * 1.5, scale.font_size, RAYWHITE); + GetScreenWidth() / 2 - (scale.font_size * 10) + (scale.font_size / 2), + (GetScreenHeight() / 2) - scale.font_size * 1.5, scale.font_size, RAYWHITE); - if (GuiButton((Rectangle) {scale.button_x, scale.window_height - (scale.window_height / 6), + if (GuiButton((Rectangle) {scale.button_x, GetScreenHeight() - (GetScreenHeight() / 6), scale.button_width, scale.button_height}, GuiIconText(ICON_REREDO_FILL, "Return"))) { menu_state = normal;