From ced3e697bd29baf4ddc42871109a878a62bb286f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Fri, 18 Apr 2025 11:54:23 -0400 Subject: [PATCH] Fix `Graphics.fadeout`/`.fadein` causing screen flashing in libretro builds --- binding-sandbox/graphics-binding.h | 8 ++++++-- src/display/graphics.cpp | 12 ++++++------ src/display/graphics.h | 4 ++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/binding-sandbox/graphics-binding.h b/binding-sandbox/graphics-binding.h index 51573a95..34a5d76b 100644 --- a/binding-sandbox/graphics-binding.h +++ b/binding-sandbox/graphics-binding.h @@ -127,13 +127,15 @@ namespace mkxp_sandbox { SANDBOX_COROUTINE(coro, int32_t duration; int32_t i; + int32_t brightness; VALUE operator()(VALUE self, VALUE value) { BOOST_ASIO_CORO_REENTER (this) { SANDBOX_AWAIT_AND_SET(duration, rb_num2int, value); + brightness = shState->graphics().getBrightness(); for (i = 0; i < duration; ++i) { - shState->graphics().fadeout(duration, i, i); + shState->graphics().fadeout(duration, i, i, brightness); SANDBOX_YIELD; } } @@ -149,13 +151,15 @@ namespace mkxp_sandbox { SANDBOX_COROUTINE(coro, int32_t duration; int32_t i; + int32_t brightness; VALUE operator()(VALUE self, VALUE value) { BOOST_ASIO_CORO_REENTER (this) { SANDBOX_AWAIT_AND_SET(duration, rb_num2int, value); + brightness = shState->graphics().getBrightness(); for (i = 0; i < duration; ++i) { - shState->graphics().fadein(duration, i, i); + shState->graphics().fadein(duration, i, i, brightness); SANDBOX_YIELD; } } diff --git a/src/display/graphics.cpp b/src/display/graphics.cpp index d7b3c165..8f492022 100644 --- a/src/display/graphics.cpp +++ b/src/display/graphics.cpp @@ -1458,14 +1458,14 @@ void Graphics::wait(int duration, int start, int stop) { } } -void Graphics::fadeout(int duration, int start, int stop) { +void Graphics::fadeout(int duration, int start, int stop, int brightness) { FBO::unbind(); - float curr = p->brightness; + float curr = brightness >= 0 && brightness <= 255 ? brightness : p->brightness; float diff = 255.0f - curr; - for (int i = std::min(stop, duration - 1); i >= start; --i) { - setBrightness(diff + (curr / duration) * i); + for (int i = start; i <= stop && i < duration;) { + setBrightness(diff + (curr / duration) * (duration - ++i)); if (p->frozen) { int scaleIsSpecial = GLMeta::blitScaleIsSpecial(p->integerScaleBuffer, false, IntRect(0, 0, p->scSize.x, p->scSize.y), p->frozenScene, IntRect(0, 0, p->scRes.x, p->scRes.y)); @@ -1485,10 +1485,10 @@ void Graphics::fadeout(int duration, int start, int stop) { } } -void Graphics::fadein(int duration, int start, int stop) { +void Graphics::fadein(int duration, int start, int stop, int brightness) { FBO::unbind(); - float curr = p->brightness; + float curr = brightness >= 0 && brightness <= 255 ? brightness : p->brightness; float diff = 255.0f - curr; for (int i = start; i <= stop && i < duration;) { diff --git a/src/display/graphics.h b/src/display/graphics.h index a494cacc..09fe413d 100644 --- a/src/display/graphics.h +++ b/src/display/graphics.h @@ -55,8 +55,8 @@ public: DECL_ATTR( Brightness, int ) void wait(int duration, int start = 0, int stop = INT_MAX); - void fadeout(int duration, int start = 0, int stop = INT_MAX); - void fadein(int duration, int start = 0, int stop = INT_MAX); + void fadeout(int duration, int start = 0, int stop = INT_MAX, int brightness = -1); + void fadein(int duration, int start = 0, int stop = INT_MAX, int brightness = -1); Bitmap *snapToBitmap();