Skip to content

Commit

Permalink
clamp config settings to something sane when loading
Browse files Browse the repository at this point in the history
  • Loading branch information
fallahn committed Aug 24, 2024
1 parent 4aace8f commit 1412186
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 18 deletions.
22 changes: 15 additions & 7 deletions crogine/src/core/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>() > 0)
if (prop.getName() == "width")
{
settings.width = prop.getValue<int>();
settings.width = std::clamp(prop.getValue<std::int32_t>(), std::min(640, mode.w - 1), mode.w);
}
else if (prop.getName() == "height" && prop.getValue<int>() > 0)
else if (prop.getName() == "height")
{
settings.height = prop.getValue<int>();
settings.height = std::clamp(prop.getValue<std::int32_t>(), std::min(480, mode.h - 1), mode.h);
}
else if (prop.getName() == "fullscreen")
{
Expand All @@ -980,7 +982,12 @@ App::WindowSettings App::loadSettings() const
}
else if (prop.getName() == "window_size")
{
const float modeWidth = static_cast<float>(mode.w);
const float modeHeight = static_cast<float>(mode.h);

settings.windowedSize = prop.getValue<glm::vec2>();
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);
}
}

Expand All @@ -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<std::int32_t>(AudioMixer::MaxChannels));
AudioMixer::setVolume(p.getValue<float>(), channel);
}
catch (...)
Expand Down
1 change: 1 addition & 0 deletions samples/scratchpad/scratchpad.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@
<ItemGroup>
<None Include="src\interiormapping\IMShader.inl" />
<None Include="src\interiormapping\ProjectionShader.inl" />
<None Include="src\pseuthe\PseuthePostFX.inl" />
<None Include="src\pseuthe\PseutheShaders.inl" />
<None Include="src\retro\RetroShaders.inl" />
<None Include="src\ssao\BlurShaders.inl" />
Expand Down
3 changes: 3 additions & 0 deletions samples/scratchpad/scratchpad.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -532,5 +532,8 @@
<None Include="src\pseuthe\PseutheShaders.inl">
<Filter>Header Files\pseuthe</Filter>
</None>
<None Include="src\pseuthe\PseuthePostFX.inl">
<Filter>Header Files\pseuthe</Filter>
</None>
</ItemGroup>
</Project>
45 changes: 41 additions & 4 deletions samples/scratchpad/src/pseuthe/PseutheBackgroundState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace
{
#include "PseutheShaders.inl"
#include "PseuthePostFX.inl"

constexpr std::uint8_t Alpha = 60;
const std::vector<cro::Colour> BackgroundColours =
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -228,9 +250,24 @@ void PseutheBackgroundState::createScene()
};

auto& cam = m_gameScene.getActiveCamera().getComponent<cro::Camera>();
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<cro::Transform>().setPosition({ 0.f, 0.f, 2.f });
}

Expand Down
9 changes: 9 additions & 0 deletions samples/scratchpad/src/pseuthe/PseutheBackgroundState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <crogine/ecs/Scene.hpp>
#include <crogine/gui/GuiClient.hpp>
#include <crogine/graphics/ModelDefinition.hpp>
#include <crogine/graphics/RenderTexture.hpp>
#include <crogine/graphics/SimpleQuad.hpp>

class PseutheBackgroundState final : public cro::State, public cro::GuiClient
{
Expand All @@ -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
{
Expand Down
16 changes: 9 additions & 7 deletions samples/scratchpad/src/pseuthe/PseutheConsts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@ 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
{
enum
{
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());
Expand All @@ -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);
}
42 changes: 42 additions & 0 deletions samples/scratchpad/src/pseuthe/PseuthePostFX.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <string>

//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;
}
)";

0 comments on commit 1412186

Please sign in to comment.