From 2f97312bd8980b95cdeab73cd48f6afc266bf9c0 Mon Sep 17 00:00:00 2001 From: EntenKoeniq <81123713+EntenKoeniq@users.noreply.github.com> Date: Sun, 18 Apr 2021 04:19:46 +0200 Subject: [PATCH] Unnecessary "if" and "if ... else" blocks have been removed. --- sr/MathUtils.hpp | 7 ++- sr/Rect.hpp | 7 +-- sr/Texture.hpp | 116 +++++++++++++++++++++++------------------------ 3 files changed, 62 insertions(+), 68 deletions(-) diff --git a/sr/MathUtils.hpp b/sr/MathUtils.hpp index c2f1c3c..eb61e60 100644 --- a/sr/MathUtils.hpp +++ b/sr/MathUtils.hpp @@ -23,10 +23,9 @@ namespace sr const Vector side2{c.v[1] - a.v[1], b.v[1] - a.v[1], a.v[1] - p.v[1]}; const auto u = side1.cross(side2); - if (std::abs(u.v[2]) < std::numeric_limits::min()) // degenerate triangle (all three points in a line) - return Vector{T(-1), T(1), T(1)}; - - return Vector{T(1) - (u.v[0] + u.v[1]) / u.v[2], u.v[1] / u.v[2], u.v[0] / u.v[2]}; + return (std::abs(u.v[2]) < std::numeric_limits::min()) // degenerate triangle (all three points in a line) + ? Vector{T(-1), T(1), T(1)} + : Vector{T(1) - (u.v[0] + u.v[1]) / u.v[2], u.v[1] / u.v[2], u.v[0] / u.v[2]}; } } diff --git a/sr/Rect.hpp b/sr/Rect.hpp index 1b08549..465654f 100755 --- a/sr/Rect.hpp +++ b/sr/Rect.hpp @@ -114,11 +114,8 @@ namespace sr const T width, const T height) const noexcept { T t; - if ((t = x - position.v[0]) > size.v[0] || -t > width) - return false; - if ((t = y - position.v[1]) > size.v[1] || -t > height) - return false; - return true; + return !((t = x - position.v[0]) > size.v[0] || -t > width) && + !((t = y - position.v[1]) > size.v[1] || -t > height); } constexpr bool intersects(const Rect& r) const noexcept diff --git a/sr/Texture.hpp b/sr/Texture.hpp index 1deabfe..ed68fca 100644 --- a/sr/Texture.hpp +++ b/sr/Texture.hpp @@ -50,26 +50,26 @@ namespace sr { const auto pixelSize = getPixelSize(pixelFormat); - if (pixelSize > 0 && width > 0 && height > 0) - { - levels.push_back(std::vector(width * height * pixelSize)); + if (pixelSize <= 0 || width <= 0 || height <= 0) + return; - if (mipMaps) - { - std::size_t newWidth = width; - std::size_t newHeight = height; + levels.push_back(std::vector(width * height * pixelSize)); - while (newWidth > 1 || newHeight > 1) - { - newWidth >>= 1; - newHeight >>= 1; + if (!mipMaps) + return; + + std::size_t newWidth = width; + std::size_t newHeight = height; - if (newWidth < 1) newWidth = 1; - if (newHeight < 1) newHeight = 1; + while (newWidth > 1 || newHeight > 1) + { + newWidth >>= 1; + newHeight >>= 1; - levels.push_back(std::vector(newWidth * newHeight * pixelSize)); - } - } + if (newWidth < 1) newWidth = 1; + if (newHeight < 1) newHeight = 1; + + levels.push_back(std::vector(newWidth * newHeight * pixelSize)); } } @@ -86,20 +86,20 @@ namespace sr levels.push_back(std::vector(width * height * pixelSize)); if (mipMaps) - { - newWidth = width; - newHeight = height; + return; - while (newWidth > 1 || newHeight > 1) - { - newWidth >>= 1; - newHeight >>= 1; + newWidth = width; + newHeight = height; - if (newWidth < 1) newWidth = 1; - if (newHeight < 1) newHeight = 1; + while (newWidth > 1 || newHeight > 1) + { + newWidth >>= 1; + newHeight >>= 1; - levels.push_back(std::vector(newWidth * newHeight * pixelSize)); - } + if (newWidth < 1) newWidth = 1; + if (newHeight < 1) newHeight = 1; + + levels.push_back(std::vector(newWidth * newHeight * pixelSize)); } } @@ -188,38 +188,36 @@ namespace sr const auto textureY = static_cast(std::round(v)); return getPixel(textureX, textureY, 0); } - else - { - auto textureX0 = static_cast(u - 0.5F); - auto textureX1 = textureX0 + 1; - auto textureY0 = static_cast(v - 0.5F); - auto textureY1 = textureY0 + 1; - - textureX0 = clamp(textureX0, static_cast(0U), width - 1); - textureX1 = clamp(textureX1, static_cast(0U), width - 1); - textureY0 = clamp(textureY0, static_cast(0U), height - 1); - textureY1 = clamp(textureY1, static_cast(0U), height - 1); - - // TODO: calculate mip level - const Color color[4] = { - getPixel(textureX0, textureY0, 0), - getPixel(textureX1, textureY0, 0), - getPixel(textureX0, textureY1, 0), - getPixel(textureX1, textureY1, 0) - }; - - const auto x0 = u - (textureX0 + 0.5F); - const auto y0 = v - (textureY0 + 0.5F); - const auto x1 = (textureX0 + 1.5F) - u; - const auto y1 = (textureY0 + 1.5F) - v; - - return Color{ - color[0].r * x1 * y1 + color[1].r * x0 * y1 + color[2].r * x1 * y0 + color[3].r * x0 * y0, - color[0].g * x1 * y1 + color[1].g * x0 * y1 + color[2].g * x1 * y0 + color[3].g * x0 * y0, - color[0].b * x1 * y1 + color[1].b * x0 * y1 + color[2].b * x1 * y0 + color[3].b * x0 * y0, - color[0].a * x1 * y1 + color[1].a * x0 * y1 + color[2].a * x1 * y0 + color[3].a * x0 * y0 - }; - } + + auto textureX0 = static_cast(u - 0.5F); + auto textureX1 = textureX0 + 1; + auto textureY0 = static_cast(v - 0.5F); + auto textureY1 = textureY0 + 1; + + textureX0 = clamp(textureX0, static_cast(0U), width - 1); + textureX1 = clamp(textureX1, static_cast(0U), width - 1); + textureY0 = clamp(textureY0, static_cast(0U), height - 1); + textureY1 = clamp(textureY1, static_cast(0U), height - 1); + + // TODO: calculate mip level + const Color color[4] = { + getPixel(textureX0, textureY0, 0), + getPixel(textureX1, textureY0, 0), + getPixel(textureX0, textureY1, 0), + getPixel(textureX1, textureY1, 0) + }; + + const auto x0 = u - (textureX0 + 0.5F); + const auto y0 = v - (textureY0 + 0.5F); + const auto x1 = (textureX0 + 1.5F) - u; + const auto y1 = (textureY0 + 1.5F) - v; + + return Color{ + color[0].r * x1 * y1 + color[1].r * x0 * y1 + color[2].r * x1 * y0 + color[3].r * x0 * y0, + color[0].g * x1 * y1 + color[1].g * x0 * y1 + color[2].g * x1 * y0 + color[3].g * x0 * y0, + color[0].b * x1 * y1 + color[1].b * x0 * y1 + color[2].b * x1 * y0 + color[3].b * x0 * y0, + color[0].a * x1 * y1 + color[1].a * x0 * y1 + color[2].a * x1 * y0 + color[3].a * x0 * y0 + }; } return Color();