From 05bf592dec254abdff48e9cc02426efdab77cf5b Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Mon, 29 Jan 2024 01:39:59 -0800 Subject: [PATCH] SDL: Refactor out layer recentering, fix for GB --- include/mgba/feature/video-backend.h | 1 + src/feature/video-backend.c | 20 ++++++++++++++++++++ src/platform/qt/DisplayGL.cpp | 17 +---------------- src/platform/sdl/gl-common.c | 17 +++++------------ 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/include/mgba/feature/video-backend.h b/include/mgba/feature/video-backend.h index 292dc9c82..adf6f8395 100644 --- a/include/mgba/feature/video-backend.h +++ b/include/mgba/feature/video-backend.h @@ -74,6 +74,7 @@ struct VideoShader { void VideoBackendGetFrame(const struct VideoBackend*, struct mRectangle* frame); void VideoBackendGetFrameSize(const struct VideoBackend*, unsigned* width, unsigned* height); +void VideoBackendRecenter(struct VideoBackend* v, unsigned scale); CXX_GUARD_END diff --git a/src/feature/video-backend.c b/src/feature/video-backend.c index ae2f0257d..ba13bbe7d 100644 --- a/src/feature/video-backend.c +++ b/src/feature/video-backend.c @@ -23,3 +23,23 @@ void VideoBackendGetFrameSize(const struct VideoBackend* v, unsigned* width, uns *width = frame.width; *height = frame.height; } + +void VideoBackendRecenter(struct VideoBackend* v, unsigned scale) { + static const int centeredLayers[] = {VIDEO_LAYER_BACKGROUND, -1}; + struct mRectangle frame = {0}; + v->imageSize(v, VIDEO_LAYER_IMAGE, &frame.width, &frame.height); + if (scale == 0) { + scale = 1; + } + + size_t i; + for (i = 0; centeredLayers[i] >= 0; ++i) { + int width, height; + struct mRectangle dims = {0}; + v->imageSize(v, centeredLayers[i], &width, &height); + dims.width = width * scale; + dims.height = height * scale; + mRectangleCenter(&frame, &dims); + v->setLayerDimensions(v, centeredLayers[i], &dims); + } +} diff --git a/src/platform/qt/DisplayGL.cpp b/src/platform/qt/DisplayGL.cpp index a1e806ef4..d2de7b9f4 100644 --- a/src/platform/qt/DisplayGL.cpp +++ b/src/platform/qt/DisplayGL.cpp @@ -658,22 +658,7 @@ void PainterGL::recenterLayers() { if (!m_context) { return; } - const static std::initializer_list centeredLayers{VIDEO_LAYER_BACKGROUND}; - int width, height; - mRectangle frame = {0}; - m_backend->imageSize(m_backend, VIDEO_LAYER_IMAGE, &width, &height); - frame.width = width; - frame.height = height; - unsigned scale = std::max(1U, m_context->videoScale()); - - for (VideoLayer l : centeredLayers) { - mRectangle dims{}; - m_backend->imageSize(m_backend, l, &width, &height); - dims.width = width * scale; - dims.height = height * scale; - mRectangleCenter(&frame, &dims); - m_backend->setLayerDimensions(m_backend, l, &dims); - } + VideoBackendRecenter(m_backend, std::max(1U, m_context->videoScale())); } void PainterGL::resize(const QSize& size) { diff --git a/src/platform/sdl/gl-common.c b/src/platform/sdl/gl-common.c index 4bd8643b7..1ce9100e8 100644 --- a/src/platform/sdl/gl-common.c +++ b/src/platform/sdl/gl-common.c @@ -135,19 +135,11 @@ void mSDLGLCommonRunloop(struct mSDLRenderer* renderer, void* user) { if (mSDLGLCommonLoadBackground(v)) { renderer->player.windowUpdated = true; - - struct mRectangle frame; - v->layerDimensions(v, VIDEO_LAYER_IMAGE, &frame); - int i; - for (i = 0; i < VIDEO_LAYER_IMAGE; ++i) { - struct mRectangle dims; - v->layerDimensions(v, i, &dims); - mRectangleCenter(&frame, &dims); - v->setLayerDimensions(v, i, &dims); - } - + VideoBackendRecenter(v, 1); #if SDL_VERSION_ATLEAST(2, 0, 0) - SDL_SetWindowSize(renderer->window, frame.width * renderer->ratio, frame.height * renderer->ratio); + int width, height; + v->imageSize(v, VIDEO_LAYER_IMAGE, &width, &height); + SDL_SetWindowSize(renderer->window, width * renderer->ratio, height * renderer->ratio); #endif } @@ -175,6 +167,7 @@ void mSDLGLCommonRunloop(struct mSDLRenderer* renderer, void* user) { dims.width = renderer->width; dims.height = renderer->height; v->setLayerDimensions(v, VIDEO_LAYER_IMAGE, &dims); + VideoBackendRecenter(v, 1); } if (mCoreSyncWaitFrameStart(&context->impl->sync)) {