From d40bd789e84769c241a88f04d619a42971e6dbdc Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 10 May 2018 07:20:04 +0200 Subject: [PATCH] Tidy up PixelAttributes --- Minetestmapper/PixelAttributes.cpp | 25 +++------ Minetestmapper/PixelAttributes.h | 88 ++++++++++++++---------------- Minetestmapper/TileGenerator.h | 1 + 3 files changed, 52 insertions(+), 62 deletions(-) diff --git a/Minetestmapper/PixelAttributes.cpp b/Minetestmapper/PixelAttributes.cpp index fa82bbb..8e44d3d 100644 --- a/Minetestmapper/PixelAttributes.cpp +++ b/Minetestmapper/PixelAttributes.cpp @@ -7,20 +7,13 @@ * ===================================================================== */ -#include -#include -#include #include "PixelAttributes.h" +#include // memcpy using namespace std; PixelAttribute::AlphaMixingMode PixelAttribute::m_mixMode = PixelAttribute::AlphaMixCumulative; -PixelAttributes::PixelAttributes(): - m_pixelAttributes(0) -{ -} - PixelAttributes::~PixelAttributes() { freeAttributes(); @@ -31,7 +24,7 @@ void PixelAttributes::setParameters(int width, int lines, int nextY, int scale, freeAttributes(); m_width = width + 1; // 1px gradient calculation m_previousLine = 0; - m_firstLine = 1; + //m_firstLine = 1; m_lastLine = m_firstLine + lines - 1; m_emptyLine = m_lastLine + 1; m_lineCount = m_emptyLine + 1; @@ -88,12 +81,12 @@ void PixelAttributes::freeAttributes() { if (m_pixelAttributes) { for (int i = 0; i < m_lineCount; ++i) { - if (m_pixelAttributes[i] != 0) { + if (m_pixelAttributes[i] != nullptr) { delete[] m_pixelAttributes[i]; } } delete[] m_pixelAttributes; - m_pixelAttributes = 0; + m_pixelAttributes = nullptr; } } @@ -183,17 +176,17 @@ void PixelAttributes::renderShading(double emphasis, bool drawAlpha) // Color values with n>0 can be summed. // normalize() converts from n>0 to n==0 representation -void PixelAttribute::normalize(double count, Color defColor) +void PixelAttribute::normalize(double count, Color defaultColor) { if (!m_n) { // Already normalized return; } if (m_n < count) { - m_r += (defColor.r / 255.0) * (defColor.a / 255.0) * (count - m_n); - m_g += (defColor.g / 255.0) * (defColor.a / 255.0) * (count - m_n); - m_b += (defColor.b / 255.0) * (defColor.a / 255.0) * (count - m_n); - m_a += (defColor.a / 255.0) * (count - m_n); + m_r += (defaultColor.r / 255.0) * (defaultColor.a / 255.0) * (count - m_n); + m_g += (defaultColor.g / 255.0) * (defaultColor.a / 255.0) * (count - m_n); + m_b += (defaultColor.b / 255.0) * (defaultColor.a / 255.0) * (count - m_n); + m_a += (defaultColor.a / 255.0) * (count - m_n); m_h *= double(count) / m_n; m_t *= double(count) / m_n; m_n = count; diff --git a/Minetestmapper/PixelAttributes.h b/Minetestmapper/PixelAttributes.h index bdaaa2e..1879d41 100644 --- a/Minetestmapper/PixelAttributes.h +++ b/Minetestmapper/PixelAttributes.h @@ -7,16 +7,14 @@ * ===================================================================== */ -#ifndef PIXELATTRIBUTES_H_ADZ35GYF -#define PIXELATTRIBUTES_H_ADZ35GYF +#pragma once -#include +#include "Color.h" +#include #include #include +#include #include -#include -#include "config.h" -#include "Color.h" class PixelAttribute { public: @@ -27,25 +25,25 @@ public: AlphaMixAverage = 0x04, }; static void setMixMode(AlphaMixingMode mode); - PixelAttribute(): nextEmpty(true), m_n(0), m_h(NAN), m_t(0), m_a(0), m_r(0), m_g(0), m_b(0) {}; + PixelAttribute() = default; // PixelAttribute(const PixelAttribute &p); PixelAttribute(const Color &color, double height); PixelAttribute(const ColorEntry &entry, double height); - bool nextEmpty; - double h(void) const { return m_h / (m_n ? m_n : 1); } - double t(void) const { return m_t / (m_n ? m_n : 1); } - double a(void) const { return m_a / (m_n ? m_n : 1); } - double r(void) const { return m_r / (m_n ? m_a : 1); } - double g(void) const { return m_g / (m_n ? m_a : 1); } - double b(void) const { return m_b / (m_n ? m_a : 1); } - uint8_t red(void) const { return int(r() * 255 + 0.5); } - uint8_t green(void) const { return int(g() * 255 + 0.5); } - uint8_t blue(void) const { return int(b() * 255 + 0.5); } - uint8_t alpha(void) const { return int(a() * 255 + 0.5); } - uint8_t thicken(void) const { return int(t() * 255 + 0.5); } - unsigned height(void) const { return unsigned(h() + 0.5); } - bool isNormalized(void) const { return !m_n; } - Color color(void) const { return Color(red(), green(), blue(), alpha()); } + bool nextEmpty{true}; + double h() const { return m_h / (m_n ? m_n : 1); } + double t() const { return m_t / (m_n ? m_n : 1); } + double a() const { return m_a / (m_n ? m_n : 1); } + double r() const { return m_r / (m_n ? m_a : 1); } + double g() const { return m_g / (m_n ? m_a : 1); } + double b() const { return m_b / (m_n ? m_a : 1); } + uint8_t red() const { return int(r() * 255 + 0.5); } + uint8_t green() const { return int(g() * 255 + 0.5); } + uint8_t blue() const { return int(b() * 255 + 0.5); } + uint8_t alpha() const { return int(a() * 255 + 0.5); } + uint8_t thicken() const { return int(t() * 255 + 0.5); } + unsigned height() const { return unsigned(h() + 0.5); } + bool isNormalized() const { return !m_n; } + Color color() const { return Color(red(), green(), blue(), alpha()); } inline bool is_valid() const { return !std::isnan(m_h); } PixelAttribute &operator=(const PixelAttribute &p); @@ -54,13 +52,13 @@ public: void mixUnder(const PixelAttribute &p); private: static AlphaMixingMode m_mixMode; - double m_n; - double m_h; - double m_t; - double m_a; - double m_r; - double m_g; - double m_b; + double m_n{0}; + double m_h{ std::numeric_limits::quiet_NaN() }; + double m_t{0}; + double m_a{0}; + double m_r{0}; + double m_g{0}; + double m_b{0}; friend class PixelAttributes; }; @@ -68,33 +66,33 @@ friend class PixelAttributes; class PixelAttributes { public: - PixelAttributes(); + PixelAttributes() = default; virtual ~PixelAttributes(); void setParameters(int width, int lines, int nextY, int scale, bool defaultEmpty); void scroll(int keepY); PixelAttribute &attribute(int y, int x); void renderShading(double emphasis, bool drawAlpha); - int getNextY(void) { return m_nextY; } + int getNextY() { return m_nextY; } void setLastY(int y); - int getLastY(void) { return m_lastY; } + int getLastY() { return m_lastY; } private: int yCoord2Line(int y) { return y - m_firstY + m_firstLine; } void freeAttributes(); private: - int m_previousLine; - int m_firstLine; - int m_lastLine; - int m_emptyLine; - int m_lineCount; - PixelAttribute **m_pixelAttributes; - int m_width; - int m_firstY; - int m_nextY; - int m_lastY; - int m_firstUnshadedY; - int m_scale; + int m_previousLine{}; + const int m_firstLine{1}; + int m_lastLine{}; + int m_emptyLine{}; + int m_lineCount{}; + PixelAttribute **m_pixelAttributes{nullptr}; + int m_width{}; + int m_firstY{}; + int m_nextY{}; + int m_lastY{}; + int m_firstUnshadedY{}; + int m_scale{}; }; inline void PixelAttributes::setLastY(int y) @@ -157,5 +155,3 @@ inline PixelAttribute &PixelAttribute::operator=(const PixelAttribute &p) return *this; } -#endif /* end of include guard: PIXELATTRIBUTES_H_ADZ35GYF */ - diff --git a/Minetestmapper/TileGenerator.h b/Minetestmapper/TileGenerator.h index ffc90ee..f2e5613 100644 --- a/Minetestmapper/TileGenerator.h +++ b/Minetestmapper/TileGenerator.h @@ -25,6 +25,7 @@ #include "MapBlock.h" #include "PaintEngine.h" #include "PixelAttributes.h" +#include "config.h" #include "db.h" #define TILESIZE_CHUNK (INT_MIN)