From 753c5836e6604d31dbe5bec295cd4cef51d2fce8 Mon Sep 17 00:00:00 2001 From: Hannah Crawford Date: Tue, 21 Sep 2021 21:21:48 +0100 Subject: [PATCH] Made window resize around center --- src/uxnemu.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/uxnemu.c b/src/uxnemu.c index b832d44..2b5e054 100644 --- a/src/uxnemu.c +++ b/src/uxnemu.c @@ -85,6 +85,23 @@ audio_callback(void *u, Uint8 *stream, int len) (void)u; } +static void +set_window_size(SDL_Window *window, int w, int h) +{ + int win_x, win_y; + int win_cent_x, win_cent_y; + int old_win_sz_x, old_win_sz_y; + + SDL_GetWindowPosition(window, &win_x, &win_y); + SDL_GetWindowSize(window, &old_win_sz_x, &old_win_sz_y); + + win_cent_x = win_x + old_win_sz_x / 2; + win_cent_y = win_y + old_win_sz_y / 2; + + SDL_SetWindowPosition(window, win_cent_x - w / 2, win_cent_y - h / 2); + SDL_SetWindowSize(window, w, h); +} + static void inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory) { @@ -154,7 +171,7 @@ static void toggle_zoom(Uxn *u) { zoom = zoom == 3 ? 1 : zoom + 1; - SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom); + set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom); redraw(u); } @@ -205,7 +222,7 @@ set_size(Uint16 width, Uint16 height, int is_resize) if(gTexture == NULL || SDL_SetTextureBlendMode(gTexture, SDL_BLENDMODE_NONE)) return error("sdl_texture", SDL_GetError()); SDL_UpdateTexture(gTexture, NULL, ppu_screen, sizeof(Uint32)); - if(is_resize) SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom); + if(is_resize) set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom); reqdraw = 1; return 1; }