From 77071c524e94819424b8be9115313ee65301706d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Thu, 7 Sep 2023 16:03:44 +0200 Subject: [PATCH] Fix rendering of fire effects when their intensity exceeds 1.0f This had been overlooked for a long time because intensity defaults to 1.0f and is seldom changed. The underlying issue is that an intensity value higher than 1.0f produces fire_alpha values above 255, which the new graphics code doesn't deal with properly. That said, that code works fine for every other case, so the solution is to add a special case for when fire_alpha is used. --- src/graphics/Pixel.h | 10 ++++++++++ src/graphics/RasterDrawMethods.h | 1 + src/graphics/RasterDrawMethodsImpl.h | 10 ++++++++++ src/graphics/Renderer.cpp | 2 +- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/graphics/Pixel.h b/src/graphics/Pixel.h index cce7336a9..43b90b4b6 100644 --- a/src/graphics/Pixel.h +++ b/src/graphics/Pixel.h @@ -64,6 +64,16 @@ struct alignas(alignof(uint32_t) > alignof(T) ? alignof(uint32_t) : alignof(T)) ); } + template>> + constexpr RGB AddFire(RGB other, int fireAlpha) const + { + return RGB( + std::min(0xFF, Red + (fireAlpha * other.Red) / 0xFF), + std::min(0xFF, Green + (fireAlpha * other.Green) / 0xFF), + std::min(0xFF, Blue + (fireAlpha * other.Blue) / 0xFF) + ); + } + // Decrement each component that is nonzero. template>> constexpr RGB Decay() const diff --git a/src/graphics/RasterDrawMethods.h b/src/graphics/RasterDrawMethods.h index 2172f8084..ee1060f8a 100644 --- a/src/graphics/RasterDrawMethods.h +++ b/src/graphics/RasterDrawMethods.h @@ -13,6 +13,7 @@ struct RasterDrawMethods void DrawPixel(Vec2, RGB); void BlendPixel(Vec2, RGBA); void AddPixel(Vec2, RGBA); + void AddFirePixel(Vec2, RGB, int fireAlpha); void XorPixel(Vec2); void DrawLine(Vec2, Vec2, RGB); diff --git a/src/graphics/RasterDrawMethodsImpl.h b/src/graphics/RasterDrawMethodsImpl.h index eb39f0d88..1621cc096 100644 --- a/src/graphics/RasterDrawMethodsImpl.h +++ b/src/graphics/RasterDrawMethodsImpl.h @@ -55,6 +55,16 @@ inline void RasterDrawMethods::AddPixel(Vec2 pos, RGBA co } } +template +inline void RasterDrawMethods::AddFirePixel(Vec2 pos, RGB colour, int fireAlpha) +{ + if (clipRect().Contains(pos)) + { + pixel &px = (static_cast(*this).video)[pos]; + px = RGB::Unpack(px).AddFire(colour, fireAlpha).Pack(); + } +} + template inline void RasterDrawMethods::XorPixel(Vec2 pos) { diff --git a/src/graphics/Renderer.cpp b/src/graphics/Renderer.cpp index 9e9a5742e..1f47d9452 100644 --- a/src/graphics/Renderer.cpp +++ b/src/graphics/Renderer.cpp @@ -1161,7 +1161,7 @@ void Renderer::render_fire() a = fire_alpha[y+CELL][x+CELL]; if (findingElement) a /= 2; - AddPixel({ i*CELL+x, j*CELL+y }, RGBA(r, g, b, a)); + AddFirePixel({ i*CELL+x, j*CELL+y }, RGB(r, g, b), a); } r *= 8; g *= 8;