From 8e58e8421890eb934713d551614841e555391e02 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 11 May 2018 10:02:43 +0200 Subject: [PATCH] Modernize PixelAttributes Tests have shown that the vectors are as fast as the C-Arrays --- Minetestmapper/PixelAttributes.cpp | 61 +++++++++++++----------------- Minetestmapper/PixelAttributes.h | 7 +++- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/Minetestmapper/PixelAttributes.cpp b/Minetestmapper/PixelAttributes.cpp index 8e44d3d..4752f40 100644 --- a/Minetestmapper/PixelAttributes.cpp +++ b/Minetestmapper/PixelAttributes.cpp @@ -8,17 +8,11 @@ */ #include "PixelAttributes.h" -#include // memcpy using namespace std; PixelAttribute::AlphaMixingMode PixelAttribute::m_mixMode = PixelAttribute::AlphaMixCumulative; -PixelAttributes::~PixelAttributes() -{ - freeAttributes(); -} - void PixelAttributes::setParameters(int width, int lines, int nextY, int scale, bool defaultEmpty) { freeAttributes(); @@ -34,23 +28,20 @@ void PixelAttributes::setParameters(int width, int lines, int nextY, int scale, m_firstUnshadedY = 0; m_scale = scale; - m_pixelAttributes = new PixelAttribute *[m_lineCount]; - if (!m_pixelAttributes) - throw std::runtime_error("Failed to allocate memory for PixelAttributes"); + PixelAttribute pa; + pa.m_a = 0; + pa.nextEmpty = false; - for (int i = 0; i < m_lineCount; ++i) { - m_pixelAttributes[i] = new PixelAttribute[m_width]; - if (!m_pixelAttributes[i]) - throw std::runtime_error("Failed to allocate memory for PixelAttributes"); - } - for (int i=0; i>(m_lineCount, vector(m_width, pa)); + + if (defaultEmpty) { + int emptyColumns = (16 / scale); + for (int i = 0; i < m_lineCount; i++) { + for (int j = 0; j < m_width; j += emptyColumns) { + m_pixelAttributes[i][j + 1].nextEmpty = true; + } } + } } void PixelAttributes::scroll(int keepY) @@ -59,15 +50,13 @@ void PixelAttributes::scroll(int keepY) if (scroll > 0) { int i; for (i = m_previousLine; i + scroll <= m_lastLine; i++) { - PixelAttribute *tmp; - tmp = m_pixelAttributes[i]; + auto tmp = m_pixelAttributes[i]; m_pixelAttributes[i] = m_pixelAttributes[i + scroll]; m_pixelAttributes[i + scroll] = tmp; } - size_t lineLength = m_width * sizeof(PixelAttribute); for (; i <= m_lastLine; ++i) { - memcpy(m_pixelAttributes[i], m_pixelAttributes[m_emptyLine], lineLength); + m_pixelAttributes[i] = m_pixelAttributes[m_emptyLine]; } m_firstY += scroll; @@ -79,15 +68,7 @@ void PixelAttributes::scroll(int keepY) void PixelAttributes::freeAttributes() { - if (m_pixelAttributes) { - for (int i = 0; i < m_lineCount; ++i) { - if (m_pixelAttributes[i] != nullptr) { - delete[] m_pixelAttributes[i]; - } - } - delete[] m_pixelAttributes; - m_pixelAttributes = nullptr; - } + m_pixelAttributes.clear(); } @@ -307,3 +288,15 @@ void PixelAttribute::mixUnder(const PixelAttribute &p) #endif } +bool PixelAttribute::operator==(const PixelAttribute & p) +{ + return m_n == p.m_n && + m_h == p.m_h && + m_t == p.m_t && + m_a == p.m_a && + m_r == p.m_r && + m_g == p.m_g && + m_b == p.m_b && + nextEmpty == p.nextEmpty; +} + diff --git a/Minetestmapper/PixelAttributes.h b/Minetestmapper/PixelAttributes.h index 1879d41..a68d58e 100644 --- a/Minetestmapper/PixelAttributes.h +++ b/Minetestmapper/PixelAttributes.h @@ -15,6 +15,7 @@ #include #include #include +#include class PixelAttribute { public: @@ -50,6 +51,8 @@ public: void normalize(double count = 0, Color defaultColor = Color(127, 127, 127)); void add(const PixelAttribute &p); void mixUnder(const PixelAttribute &p); + bool operator==(const PixelAttribute &p); + bool operator!=(const PixelAttribute &p) { return !(*this == p); }; private: static AlphaMixingMode m_mixMode; double m_n{0}; @@ -67,7 +70,7 @@ class PixelAttributes { public: PixelAttributes() = default; - virtual ~PixelAttributes(); + ~PixelAttributes() = default; void setParameters(int width, int lines, int nextY, int scale, bool defaultEmpty); void scroll(int keepY); PixelAttribute &attribute(int y, int x); @@ -86,7 +89,7 @@ private: int m_lastLine{}; int m_emptyLine{}; int m_lineCount{}; - PixelAttribute **m_pixelAttributes{nullptr}; + std::vector> m_pixelAttributes; int m_width{}; int m_firstY{}; int m_nextY{};