From 1412186915afa9650dfe119de7ad411f4a43f628 Mon Sep 17 00:00:00 2001 From: fallahn Date: Sat, 24 Aug 2024 17:52:45 +0100 Subject: [PATCH] clamp config settings to something sane when loading --- crogine/src/core/App.cpp | 22 ++++++--- samples/scratchpad/scratchpad.vcxproj | 1 + samples/scratchpad/scratchpad.vcxproj.filters | 3 ++ .../src/pseuthe/PseutheBackgroundState.cpp | 45 +++++++++++++++++-- .../src/pseuthe/PseutheBackgroundState.hpp | 9 ++++ .../scratchpad/src/pseuthe/PseutheConsts.hpp | 16 ++++--- .../scratchpad/src/pseuthe/PseuthePostFX.inl | 42 +++++++++++++++++ 7 files changed, 120 insertions(+), 18 deletions(-) create mode 100644 samples/scratchpad/src/pseuthe/PseuthePostFX.inl diff --git a/crogine/src/core/App.cpp b/crogine/src/core/App.cpp index d1dba9258..b017f2e7e 100644 --- a/crogine/src/core/App.cpp +++ b/crogine/src/core/App.cpp @@ -946,21 +946,23 @@ void App::removeWindows(const GuiClient* c) App::WindowSettings App::loadSettings() const { - WindowSettings settings; + SDL_DisplayMode mode; + SDL_GetDesktopDisplayMode(0, &mode); + WindowSettings settings; ConfigFile cfg; if (cfg.loadFromFile(m_prefPath + cfgName, false)) { const auto& properties = cfg.getProperties(); for (const auto& prop : properties) { - if (prop.getName() == "width" && prop.getValue() > 0) + if (prop.getName() == "width") { - settings.width = prop.getValue(); + settings.width = std::clamp(prop.getValue(), std::min(640, mode.w - 1), mode.w); } - else if (prop.getName() == "height" && prop.getValue() > 0) + else if (prop.getName() == "height") { - settings.height = prop.getValue(); + settings.height = std::clamp(prop.getValue(), std::min(480, mode.h - 1), mode.h); } else if (prop.getName() == "fullscreen") { @@ -980,7 +982,12 @@ App::WindowSettings App::loadSettings() const } else if (prop.getName() == "window_size") { + const float modeWidth = static_cast(mode.w); + const float modeHeight = static_cast(mode.h); + settings.windowedSize = prop.getValue(); + settings.windowedSize.x = std::clamp(settings.windowedSize.x, std::min(640.f, modeWidth - 1.f), modeWidth); + settings.windowedSize.x = std::clamp(settings.windowedSize.y, std::min(640.f, modeHeight - 1.f), modeHeight); } } @@ -1005,12 +1012,13 @@ App::WindowSettings App::loadSettings() const { auto name = p.getName(); auto found = name.find("channel"); - if (found != std::string::npos) + if (found != std::string::npos + && name.size() > found + 7) { auto ident = name.substr(found + 7); try { - auto channel = std::stoi(ident); + auto channel = std::clamp(std::stoi(ident), 0, static_cast(AudioMixer::MaxChannels)); AudioMixer::setVolume(p.getValue(), channel); } catch (...) diff --git a/samples/scratchpad/scratchpad.vcxproj b/samples/scratchpad/scratchpad.vcxproj index 8876ed03c..6c36ad230 100644 --- a/samples/scratchpad/scratchpad.vcxproj +++ b/samples/scratchpad/scratchpad.vcxproj @@ -420,6 +420,7 @@ + diff --git a/samples/scratchpad/scratchpad.vcxproj.filters b/samples/scratchpad/scratchpad.vcxproj.filters index 4c8376f7b..4d0117ec5 100644 --- a/samples/scratchpad/scratchpad.vcxproj.filters +++ b/samples/scratchpad/scratchpad.vcxproj.filters @@ -532,5 +532,8 @@ Header Files\pseuthe + + Header Files\pseuthe + \ No newline at end of file diff --git a/samples/scratchpad/src/pseuthe/PseutheBackgroundState.cpp b/samples/scratchpad/src/pseuthe/PseutheBackgroundState.cpp index b59c17278..7bd5c05da 100644 --- a/samples/scratchpad/src/pseuthe/PseutheBackgroundState.cpp +++ b/samples/scratchpad/src/pseuthe/PseutheBackgroundState.cpp @@ -29,6 +29,7 @@ namespace { #include "PseutheShaders.inl" +#include "PseuthePostFX.inl" constexpr std::uint8_t Alpha = 60; const std::vector BackgroundColours = @@ -95,8 +96,18 @@ bool PseutheBackgroundState::simulate(float dt) void PseutheBackgroundState::render() { - + //m_backgroundBuffer.clear(cro::Colour::Black); m_gameScene.render(); + //m_backgroundBuffer.display(); + + + //m_blurBufferH.clear(cro::Colour::Black); + //m_backgroundQuad.draw(); + //m_blurBufferH.display(); + + + //m_blurQuadH.draw(); + } //private @@ -113,8 +124,19 @@ void PseutheBackgroundState::addSystems() void PseutheBackgroundState::loadAssets() { - - + m_resources.shaders.loadFromString(ShaderID::BlurH, cro::SimpleDrawable::getDefaultVertexShader(), GaussianFrag, "#define HORIZONTAL\n"); + m_resources.shaders.loadFromString(ShaderID::BlurV, cro::SimpleDrawable::getDefaultVertexShader(), GaussianFrag, "#define VERTICAL\n"); + + m_backgroundBuffer.create(SceneSize.x/2, SceneSize.y/2, false); + m_backgroundBuffer.setSmooth(true); + m_backgroundQuad.setTexture(m_backgroundBuffer.getTexture()); + m_backgroundQuad.setScale(glm::vec2(2.f)); + //m_backgroundQuad.setShader(m_resources.shaders.get(ShaderID::BlurH)); + + m_blurBufferH.create(SceneSize.x, SceneSize.y, false); + m_blurQuadH.setTexture(m_blurBufferH.getTexture()); + + } void PseutheBackgroundState::createScene() @@ -228,9 +250,24 @@ void PseutheBackgroundState::createScene() }; auto& cam = m_gameScene.getActiveCamera().getComponent(); - cam.resizeCallback = std::bind(cameraCallback, std::placeholders::_1); + cam.resizeCallback = std::bind(&cameraCallback, std::placeholders::_1); cameraCallback(cam); + //cam.resizeCallback = [&](cro::Camera& c) + // { + // c.setOrthographic(0.f, SceneSizeFloat.x, 0.f, SceneSizeFloat.y, NearPlane, FarPlane);; + // c.viewport = { 0.f, 0.f, 1.f, 1.f }; + + // auto winSize = glm::vec2(cro::App::getWindow().getSize()); + // auto viewScale = winSize.x / SceneSizeFloat.x; + // m_blurQuadH.setScale(glm::vec2(viewScale)); + + // float offset = (winSize.y - (SceneSize.y * viewScale)) / 2.f; + // m_blurQuadH.setPosition(glm::vec2(0.f, offset)); + // }; + //cam.resizeCallback(cam); + + m_gameScene.getActiveCamera().getComponent().setPosition({ 0.f, 0.f, 2.f }); } diff --git a/samples/scratchpad/src/pseuthe/PseutheBackgroundState.hpp b/samples/scratchpad/src/pseuthe/PseutheBackgroundState.hpp index 84b570228..4669771de 100644 --- a/samples/scratchpad/src/pseuthe/PseutheBackgroundState.hpp +++ b/samples/scratchpad/src/pseuthe/PseutheBackgroundState.hpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include class PseutheBackgroundState final : public cro::State, public cro::GuiClient { @@ -26,6 +28,13 @@ class PseutheBackgroundState final : public cro::State, public cro::GuiClient cro::Scene m_gameScene; cro::ResourceCollection m_resources; + cro::RenderTexture m_blurBufferH; + cro::SimpleQuad m_blurQuadH; + + cro::RenderTexture m_backgroundBuffer; + cro::SimpleQuad m_backgroundQuad; + + //TODO we could arrange some of these in a UBO struct ShaderBlocks final { diff --git a/samples/scratchpad/src/pseuthe/PseutheConsts.hpp b/samples/scratchpad/src/pseuthe/PseutheConsts.hpp index d43ff6468..981b183ae 100644 --- a/samples/scratchpad/src/pseuthe/PseutheConsts.hpp +++ b/samples/scratchpad/src/pseuthe/PseutheConsts.hpp @@ -18,13 +18,13 @@ static inline constexpr float ParticleDepth = -8.f; static inline constexpr float BallDepth = 0.f; static inline constexpr float ScreenFadeDepth = 6.f; -constexpr std::size_t BallCount = 19; -constexpr std::int32_t MinBallSize = 40; -constexpr std::int32_t MaxBallSize = 92; -constexpr float BallSize = 128.f; //this is the sprite size +static inline constexpr std::size_t BallCount = 19; +static inline constexpr std::int32_t MinBallSize = 40; +static inline constexpr std::int32_t MaxBallSize = 92; +static inline constexpr float BallSize = 128.f; //this is the sprite size //constexpr cro::Colour PlayerColour(std::uint8_t(200u), 200u, 230u, 180u); -constexpr cro::Colour PlayerColour(std::uint8_t(140u), 140u, 161u, 180u); +static inline constexpr cro::Colour PlayerColour(std::uint8_t(140u), 140u, 161u, 180u); struct ShaderID final { @@ -32,11 +32,13 @@ struct ShaderID final { LightRay, Ball, - + BlurH, BlurV, Count }; }; +static inline constexpr float NearPlane = -10.f; +static inline constexpr float FarPlane = 20.f; static inline void cameraCallback(cro::Camera& cam) { const auto windowSize = glm::vec2(cro::App::getWindow().getSize()); @@ -47,5 +49,5 @@ static inline void cameraCallback(cro::Camera& cam) const float bottom = (1.f - height) / 2.f; cam.viewport = { 0.f, bottom, 1.f, height }; - cam.setOrthographic(0.f, SceneSizeFloat.x, 0.f, SceneSizeFloat.y, -10.f, 20.f); + cam.setOrthographic(0.f, SceneSizeFloat.x, 0.f, SceneSizeFloat.y, NearPlane, FarPlane); } diff --git a/samples/scratchpad/src/pseuthe/PseuthePostFX.inl b/samples/scratchpad/src/pseuthe/PseuthePostFX.inl new file mode 100644 index 000000000..5be6fe7a4 --- /dev/null +++ b/samples/scratchpad/src/pseuthe/PseuthePostFX.inl @@ -0,0 +1,42 @@ +#pragma once + +#include + +//based on https://www.rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/ + +static inline const std::string GaussianFrag = +R"( +uniform sampler2D u_texture; + +VARYING_IN vec4 v_colour; + +OUTPUT + +uniform float offset[3] = float[](0.0, 1.3846153846, 3.2307692308); +uniform float weight[3] = float[](0.2270270270, 0.3162162162, 0.0702702703); + +#if defined(HORIZONTAL) +const float TextureSize = 1920.0; //TODO set this at compile tile from const vals +#define OFFSET vec2(offset[i], 0.0) +#else +const float TextureSize = 1080.0; +#define OFFSET vec2(0.0, offset[i]) +#endif + +void main() +{ + vec4 colour = TEXTURE(u_texture, vec2(gl_FragCoord) / TextureSize) * weight[0]; + + for (int i = 1; i < 3; i++) + { + colour += TEXTURE(u_texture, (vec2(gl_FragCoord) + OFFSET) / TextureSize) * weight[i]; + colour += TEXTURE(u_texture, (vec2(gl_FragCoord) - OFFSET) / TextureSize) * weight[i]; + } + + FRAG_OUT.rgb = colour.rgb; + FRAG_OUT.a = 1.0; + + FRAG_OUT *= v_colour; +} + +)";