From b1290d28036d87b4f67b70dc7ae97ae7169d7c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Wed, 16 Apr 2025 08:43:48 +0200 Subject: [PATCH] Unsplit RendererBasic Some functionality was split into RendererBasic.cpp from Renderer.cpp in f0ffa2eeb1ea, where the FONTEDITOR macro was removed, whose purpose was to exclude all simulation-aware code from the font editor. I'm not entirely sure why this split was necessary; the font editor had been using Graphics and not Renderer even back then. I probably just took the simple route and split everything not under #ifndef FONTEDITOR into its own file. The only thing it relied on in RendererBasic.cpp was Gradient, which this commit splits off. --- src/graphics/Gradient.cpp | 28 ++++ src/graphics/Gradient.h | 15 ++ src/graphics/Renderer.cpp | 214 ++++++++++++++++++++++++++++ src/graphics/Renderer.h | 8 -- src/graphics/RendererBasic.cpp | 249 --------------------------------- src/graphics/meson.build | 2 +- src/gui/interface/Slider.cpp | 4 +- 7 files changed, 260 insertions(+), 260 deletions(-) create mode 100644 src/graphics/Gradient.cpp create mode 100644 src/graphics/Gradient.h delete mode 100644 src/graphics/RendererBasic.cpp diff --git a/src/graphics/Gradient.cpp b/src/graphics/Gradient.cpp new file mode 100644 index 000000000..b5a911750 --- /dev/null +++ b/src/graphics/Gradient.cpp @@ -0,0 +1,28 @@ +#include "Gradient.h" + +std::vector Gradient(std::vector stops, int resolution) +{ + std::vector table(resolution, 0x000000_rgb); + if (stops.size() >= 2) + { + std::sort(stops.begin(), stops.end()); + auto stop = -1; + for (auto i = 0; i < resolution; ++i) + { + auto point = i / (float)resolution; + while (stop < (int)stops.size() - 1 && stops[stop + 1].point <= point) + { + ++stop; + } + if (stop < 0 || stop >= (int)stops.size() - 1) + { + continue; + } + auto &left = stops[stop]; + auto &right = stops[stop + 1]; + auto f = (point - left.point) / (right.point - left.point); + table[i] = left.color.Blend(right.color.WithAlpha(uint8_t(f * 0xFF))); + } + } + return table; +} diff --git a/src/graphics/Gradient.h b/src/graphics/Gradient.h new file mode 100644 index 000000000..9aeda7ce6 --- /dev/null +++ b/src/graphics/Gradient.h @@ -0,0 +1,15 @@ +#pragma once +#include "Pixel.h" +#include + +struct GradientStop +{ + RGB color; + float point; + + bool operator <(const GradientStop &other) const + { + return point < other.point; + } +}; +std::vector Gradient(std::vector stops, int resolution); diff --git a/src/graphics/Renderer.cpp b/src/graphics/Renderer.cpp index af5659b31..3dfb293da 100644 --- a/src/graphics/Renderer.cpp +++ b/src/graphics/Renderer.cpp @@ -1,8 +1,11 @@ #include "Renderer.h" +#include "Gradient.h" #include "Misc.h" #include "VideoBuffer.h" +#include "RasterDrawMethodsImpl.h" #include "common/tpt-rand.h" #include "common/tpt-compat.h" +#include "gui/game/RenderPreset.h" #include "simulation/Simulation.h" #include "simulation/ElementGraphics.h" #include "simulation/ElementClasses.h" @@ -1303,3 +1306,214 @@ int HeatToColour(float temp) color.Blue = uint8_t(color.Blue * 0.7f); return color.Pack(); } + +const std::vector Renderer::renderModePresets = { + { + "Alternative Velocity Display", + RENDER_EFFE | RENDER_BASC, + DISPLAY_AIRC, + 0, + }, + { + "Velocity Display", + RENDER_EFFE | RENDER_BASC, + DISPLAY_AIRV, + 0, + }, + { + "Pressure Display", + RENDER_EFFE | RENDER_BASC, + DISPLAY_AIRP, + 0, + }, + { + "Persistent Display", + RENDER_EFFE | RENDER_BASC, + DISPLAY_PERS, + 0, + }, + { + "Fire Display", + RENDER_FIRE | RENDER_SPRK | RENDER_EFFE | RENDER_BASC, + 0, + 0, + }, + { + "Blob Display", + RENDER_FIRE | RENDER_SPRK | RENDER_EFFE | RENDER_BLOB, + 0, + 0, + }, + { + "Heat Display", + RENDER_BASC, + DISPLAY_AIRH, + COLOUR_HEAT, + }, + { + "Fancy Display", + RENDER_FIRE | RENDER_SPRK | RENDER_GLOW | RENDER_BLUR | RENDER_EFFE | RENDER_BASC, + DISPLAY_WARP, + 0, + }, + { + "Nothing Display", + RENDER_BASC, + 0, + 0, + }, + { + "Heat Gradient Display", + RENDER_BASC, + 0, + COLOUR_GRAD, + }, + { + "Life Gradient Display", + RENDER_BASC, + 0, + COLOUR_LIFE, + }, +}; + +void Renderer::Clear() +{ + if(displayMode & DISPLAY_PERS) + { + std::copy(persistentVideo.begin(), persistentVideo.end(), video.RowIterator({ 0, 0 })); + } + else + { + std::fill_n(video.data(), WINDOWW * YRES, 0); + } +} + +void Renderer::DrawBlob(Vec2 pos, RGB colour) +{ + BlendPixel(pos + Vec2{ +1, 0 }, colour.WithAlpha(112)); + BlendPixel(pos + Vec2{ -1, 0 }, colour.WithAlpha(112)); + BlendPixel(pos + Vec2{ 0, 1 }, colour.WithAlpha(112)); + BlendPixel(pos + Vec2{ 0, -1 }, colour.WithAlpha(112)); + BlendPixel(pos + Vec2{ 1, -1 }, colour.WithAlpha(64)); + BlendPixel(pos + Vec2{ -1, -1 }, colour.WithAlpha(64)); + BlendPixel(pos + Vec2{ 1, 1 }, colour.WithAlpha(64)); + BlendPixel(pos + Vec2{ -1, +1 }, colour.WithAlpha(64)); +} + +float temp[CELL*3][CELL*3]; +float fire_alphaf[CELL*3][CELL*3]; +float glow_alphaf[11][11]; +float blur_alphaf[7][7]; +void Renderer::prepare_alpha(int size, float intensity) +{ + fireIntensity = intensity; + //TODO: implement size + int x,y,i,j; + float multiplier = 255.0f*fireIntensity; + + memset(temp, 0, sizeof(temp)); + for (x=0; x Renderer::flameTable; +std::vector Renderer::plasmaTable; +std::vector Renderer::heatTable; +std::vector Renderer::clfmTable; +std::vector Renderer::firwTable; +static bool tablesPopulated = false; +static std::mutex tablesPopulatedMx; +void Renderer::PopulateTables() +{ + std::lock_guard g(tablesPopulatedMx); + if (!tablesPopulated) + { + tablesPopulated = true; + flameTable = Gradient({ + { 0x000000_rgb, 0.00f }, + { 0x60300F_rgb, 0.50f }, + { 0xDFBF6F_rgb, 0.90f }, + { 0xAF9F0F_rgb, 1.00f }, + }, 200); + plasmaTable = Gradient({ + { 0x000000_rgb, 0.00f }, + { 0x301040_rgb, 0.25f }, + { 0x301060_rgb, 0.50f }, + { 0xAFFFFF_rgb, 0.90f }, + { 0xAFFFFF_rgb, 1.00f }, + }, 200); + heatTable = Gradient({ + { 0x2B00FF_rgb, 0.00f }, + { 0x003CFF_rgb, 0.01f }, + { 0x00C0FF_rgb, 0.05f }, + { 0x00FFEB_rgb, 0.08f }, + { 0x00FF14_rgb, 0.19f }, + { 0x4BFF00_rgb, 0.25f }, + { 0xC8FF00_rgb, 0.37f }, + { 0xFFDC00_rgb, 0.45f }, + { 0xFF0000_rgb, 0.71f }, + { 0xFF00DC_rgb, 1.00f }, + }, 1024); + clfmTable = Gradient({ + { 0x000000_rgb, 0.00f }, + { 0x0A0917_rgb, 0.10f }, + { 0x19163C_rgb, 0.20f }, + { 0x28285E_rgb, 0.30f }, + { 0x343E77_rgb, 0.40f }, + { 0x49769A_rgb, 0.60f }, + { 0x57A0B4_rgb, 0.80f }, + { 0x5EC4C6_rgb, 1.00f }, + }, 200); + firwTable = Gradient({ + { 0xFF00FF_rgb, 0.00f }, + { 0x0000FF_rgb, 0.20f }, + { 0x00FFFF_rgb, 0.40f }, + { 0x00FF00_rgb, 0.60f }, + { 0xFFFF00_rgb, 0.80f }, + { 0xFF0000_rgb, 1.00f }, + }, 200); + } +} + +Renderer::Renderer() +{ + PopulateTables(); + + memset(fire_r, 0, sizeof(fire_r)); + memset(fire_g, 0, sizeof(fire_g)); + memset(fire_b, 0, sizeof(fire_b)); + + //Set defauly display modes + prepare_alpha(CELL, 1.0f); + ClearAccumulation(); +} + +void Renderer::ClearAccumulation() +{ + std::fill(&fire_r[0][0], &fire_r[0][0] + NCELL, 0); + std::fill(&fire_g[0][0], &fire_g[0][0] + NCELL, 0); + std::fill(&fire_b[0][0], &fire_b[0][0] + NCELL, 0); + std::fill(persistentVideo.begin(), persistentVideo.end(), 0); +} + +void Renderer::ApplySettings(const RendererSettings &newSettings) +{ + if (!(newSettings.renderMode & FIREMODE) && (renderMode & FIREMODE)) + { + ClearAccumulation(); + } + if (!(newSettings.displayMode & DISPLAY_PERS) && (displayMode & DISPLAY_PERS)) + { + ClearAccumulation(); + } + static_cast(*this) = newSettings; +} + +template struct RasterDrawMethods; diff --git a/src/graphics/Renderer.h b/src/graphics/Renderer.h index a14ca3b9d..e554de3c4 100644 --- a/src/graphics/Renderer.h +++ b/src/graphics/Renderer.h @@ -80,14 +80,6 @@ public: const RenderableSimulation *sim = nullptr; - struct GradientStop - { - RGB color; - float point; - - bool operator <(const GradientStop &other) const; - }; - static std::vector Gradient(std::vector stops, int resolution); static std::unique_ptr WallIcon(int wallID, Vec2 size); static const std::vector renderModePresets; diff --git a/src/graphics/RendererBasic.cpp b/src/graphics/RendererBasic.cpp deleted file mode 100644 index 72d643205..000000000 --- a/src/graphics/RendererBasic.cpp +++ /dev/null @@ -1,249 +0,0 @@ -#include -#include "gui/game/RenderPreset.h" -#include "RasterDrawMethodsImpl.h" -#include "Renderer.h" -#include "simulation/ElementClasses.h" -#include "simulation/ElementGraphics.h" - -const std::vector Renderer::renderModePresets = { - { - "Alternative Velocity Display", - RENDER_EFFE | RENDER_BASC, - DISPLAY_AIRC, - 0, - }, - { - "Velocity Display", - RENDER_EFFE | RENDER_BASC, - DISPLAY_AIRV, - 0, - }, - { - "Pressure Display", - RENDER_EFFE | RENDER_BASC, - DISPLAY_AIRP, - 0, - }, - { - "Persistent Display", - RENDER_EFFE | RENDER_BASC, - DISPLAY_PERS, - 0, - }, - { - "Fire Display", - RENDER_FIRE | RENDER_SPRK | RENDER_EFFE | RENDER_BASC, - 0, - 0, - }, - { - "Blob Display", - RENDER_FIRE | RENDER_SPRK | RENDER_EFFE | RENDER_BLOB, - 0, - 0, - }, - { - "Heat Display", - RENDER_BASC, - DISPLAY_AIRH, - COLOUR_HEAT, - }, - { - "Fancy Display", - RENDER_FIRE | RENDER_SPRK | RENDER_GLOW | RENDER_BLUR | RENDER_EFFE | RENDER_BASC, - DISPLAY_WARP, - 0, - }, - { - "Nothing Display", - RENDER_BASC, - 0, - 0, - }, - { - "Heat Gradient Display", - RENDER_BASC, - 0, - COLOUR_GRAD, - }, - { - "Life Gradient Display", - RENDER_BASC, - 0, - COLOUR_LIFE, - }, -}; - -void Renderer::Clear() -{ - if(displayMode & DISPLAY_PERS) - { - std::copy(persistentVideo.begin(), persistentVideo.end(), video.RowIterator({ 0, 0 })); - } - else - { - std::fill_n(video.data(), WINDOWW * YRES, 0); - } -} - -void Renderer::DrawBlob(Vec2 pos, RGB colour) -{ - BlendPixel(pos + Vec2{ +1, 0 }, colour.WithAlpha(112)); - BlendPixel(pos + Vec2{ -1, 0 }, colour.WithAlpha(112)); - BlendPixel(pos + Vec2{ 0, 1 }, colour.WithAlpha(112)); - BlendPixel(pos + Vec2{ 0, -1 }, colour.WithAlpha(112)); - BlendPixel(pos + Vec2{ 1, -1 }, colour.WithAlpha(64)); - BlendPixel(pos + Vec2{ -1, -1 }, colour.WithAlpha(64)); - BlendPixel(pos + Vec2{ 1, 1 }, colour.WithAlpha(64)); - BlendPixel(pos + Vec2{ -1, +1 }, colour.WithAlpha(64)); -} - -float temp[CELL*3][CELL*3]; -float fire_alphaf[CELL*3][CELL*3]; -float glow_alphaf[11][11]; -float blur_alphaf[7][7]; -void Renderer::prepare_alpha(int size, float intensity) -{ - fireIntensity = intensity; - //TODO: implement size - int x,y,i,j; - float multiplier = 255.0f*fireIntensity; - - memset(temp, 0, sizeof(temp)); - for (x=0; x Renderer::flameTable; -std::vector Renderer::plasmaTable; -std::vector Renderer::heatTable; -std::vector Renderer::clfmTable; -std::vector Renderer::firwTable; -static bool tablesPopulated = false; -static std::mutex tablesPopulatedMx; -void Renderer::PopulateTables() -{ - std::lock_guard g(tablesPopulatedMx); - if (!tablesPopulated) - { - tablesPopulated = true; - flameTable = Gradient({ - { 0x000000_rgb, 0.00f }, - { 0x60300F_rgb, 0.50f }, - { 0xDFBF6F_rgb, 0.90f }, - { 0xAF9F0F_rgb, 1.00f }, - }, 200); - plasmaTable = Gradient({ - { 0x000000_rgb, 0.00f }, - { 0x301040_rgb, 0.25f }, - { 0x301060_rgb, 0.50f }, - { 0xAFFFFF_rgb, 0.90f }, - { 0xAFFFFF_rgb, 1.00f }, - }, 200); - heatTable = Gradient({ - { 0x2B00FF_rgb, 0.00f }, - { 0x003CFF_rgb, 0.01f }, - { 0x00C0FF_rgb, 0.05f }, - { 0x00FFEB_rgb, 0.08f }, - { 0x00FF14_rgb, 0.19f }, - { 0x4BFF00_rgb, 0.25f }, - { 0xC8FF00_rgb, 0.37f }, - { 0xFFDC00_rgb, 0.45f }, - { 0xFF0000_rgb, 0.71f }, - { 0xFF00DC_rgb, 1.00f }, - }, 1024); - clfmTable = Gradient({ - { 0x000000_rgb, 0.00f }, - { 0x0A0917_rgb, 0.10f }, - { 0x19163C_rgb, 0.20f }, - { 0x28285E_rgb, 0.30f }, - { 0x343E77_rgb, 0.40f }, - { 0x49769A_rgb, 0.60f }, - { 0x57A0B4_rgb, 0.80f }, - { 0x5EC4C6_rgb, 1.00f }, - }, 200); - firwTable = Gradient({ - { 0xFF00FF_rgb, 0.00f }, - { 0x0000FF_rgb, 0.20f }, - { 0x00FFFF_rgb, 0.40f }, - { 0x00FF00_rgb, 0.60f }, - { 0xFFFF00_rgb, 0.80f }, - { 0xFF0000_rgb, 1.00f }, - }, 200); - } -} - -Renderer::Renderer() -{ - PopulateTables(); - - memset(fire_r, 0, sizeof(fire_r)); - memset(fire_g, 0, sizeof(fire_g)); - memset(fire_b, 0, sizeof(fire_b)); - - //Set defauly display modes - prepare_alpha(CELL, 1.0f); - ClearAccumulation(); -} - -void Renderer::ClearAccumulation() -{ - std::fill(&fire_r[0][0], &fire_r[0][0] + NCELL, 0); - std::fill(&fire_g[0][0], &fire_g[0][0] + NCELL, 0); - std::fill(&fire_b[0][0], &fire_b[0][0] + NCELL, 0); - std::fill(persistentVideo.begin(), persistentVideo.end(), 0); -} - -void Renderer::ApplySettings(const RendererSettings &newSettings) -{ - if (!(newSettings.renderMode & FIREMODE) && (renderMode & FIREMODE)) - { - ClearAccumulation(); - } - if (!(newSettings.displayMode & DISPLAY_PERS) && (displayMode & DISPLAY_PERS)) - { - ClearAccumulation(); - } - static_cast(*this) = newSettings; -} - -template struct RasterDrawMethods; - -bool Renderer::GradientStop::operator <(const GradientStop &other) const -{ - return point < other.point; -} - -std::vector Renderer::Gradient(std::vector stops, int resolution) -{ - std::vector table(resolution, 0x000000_rgb); - if (stops.size() >= 2) - { - std::sort(stops.begin(), stops.end()); - auto stop = -1; - for (auto i = 0; i < resolution; ++i) - { - auto point = i / (float)resolution; - while (stop < (int)stops.size() - 1 && stops[stop + 1].point <= point) - { - ++stop; - } - if (stop < 0 || stop >= (int)stops.size() - 1) - { - continue; - } - auto &left = stops[stop]; - auto &right = stops[stop + 1]; - auto f = (point - left.point) / (right.point - left.point); - table[i] = left.color.Blend(right.color.WithAlpha(uint8_t(f * 0xFF))); - } - } - return table; -} diff --git a/src/graphics/meson.build b/src/graphics/meson.build index 8334e066e..e32568d9e 100644 --- a/src/graphics/meson.build +++ b/src/graphics/meson.build @@ -1,8 +1,8 @@ graphics_files = files( + 'Gradient.cpp', 'Graphics.cpp', 'RasterGraphics.cpp', 'FontReader.cpp', - 'RendererBasic.cpp', ) powder_graphics_files = files( 'Renderer.cpp', diff --git a/src/gui/interface/Slider.cpp b/src/gui/interface/Slider.cpp index a4fa3228a..0f2a5cb70 100644 --- a/src/gui/interface/Slider.cpp +++ b/src/gui/interface/Slider.cpp @@ -1,6 +1,6 @@ #include "Slider.h" #include "graphics/Graphics.h" -#include "graphics/Renderer.h" +#include "graphics/Gradient.h" namespace ui { @@ -70,7 +70,7 @@ void Slider::SetColour(Colour col1, Colour col2) { this->col1 = col1; this->col2 = col2; - bgGradient = Renderer::Gradient({ + bgGradient = Gradient({ { col1.NoAlpha(), 0.f }, { col2.NoAlpha(), 1.f }, }, Size.X-7);