From 4a8c041bc118209dca959fec27e1eb7bca0a517b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bend=C3=ADk?= Date: Sat, 1 Sep 2012 14:36:14 +0200 Subject: [PATCH] Small optimization of PixelAttributes. --- PixelAttributes.cpp | 37 +++++++++++++++++++++++++++++-------- PixelAttributes.h | 17 ++++++++++++++--- config.h | 2 ++ 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/PixelAttributes.cpp b/PixelAttributes.cpp index 7fbf317..1aaff10 100644 --- a/PixelAttributes.cpp +++ b/PixelAttributes.cpp @@ -7,28 +7,49 @@ * ===================================================================== */ +#include +#include #include "PixelAttributes.h" -#include using namespace std; -PixelAttributes::PixelAttributes() +PixelAttributes::PixelAttributes(): + m_width(0) { - m_blockPixelAttributes.resize(17); // 16px + 1px gradient calculation + for (size_t i = 0; i < BlockCount; ++i) { + m_pixelAttributes[i] = 0; + } +} + +PixelAttributes::~PixelAttributes() +{ + freeAttributes(); } void PixelAttributes::setWidth(int width) { - for (size_t i = 0; i < 17; ++i) { - m_blockPixelAttributes[i].resize(width + 2); // Width + 1 px gradient calculation on both sides + freeAttributes(); + m_width = width; + for (size_t i = 0; i < BlockCount; ++i) { + m_pixelAttributes[i] = new PixelAttribute[m_width + 1]; } } void PixelAttributes::scroll() { - m_blockPixelAttributes[0] = m_blockPixelAttributes[16]; - for (size_t i = 1; i < 17; ++i) { - fill(m_blockPixelAttributes[i].begin(), m_blockPixelAttributes[i].end(), PixelAttribute()); + memcpy(m_pixelAttributes[FirstLine], m_pixelAttributes[LastLine], (m_width + 1) * sizeof(PixelAttribute)); + for (size_t i = 1; i < BlockCount - 1; ++i) { + memcpy(m_pixelAttributes[i], m_pixelAttributes[EmptyLine], (m_width + 1) * sizeof(PixelAttribute)); + } +} + +void PixelAttributes::freeAttributes() +{ + for (size_t i = 0; i < BlockCount; ++i) { + if (m_pixelAttributes[i] != 0) { + delete[] m_pixelAttributes[i]; + m_pixelAttributes[i] = 0; + } } } diff --git a/PixelAttributes.h b/PixelAttributes.h index acdea1f..cb19c00 100644 --- a/PixelAttributes.h +++ b/PixelAttributes.h @@ -11,7 +11,7 @@ #define PIXELATTRIBUTES_H_ADZ35GYF #include -#include +#include "config.h" struct PixelAttribute { PixelAttribute(): height(std::numeric_limits::min()) {}; @@ -25,12 +25,23 @@ class PixelAttributes { public: PixelAttributes(); + virtual ~PixelAttributes(); void setWidth(int width); void scroll(); - inline PixelAttribute &attribute(int z, int x) { return m_blockPixelAttributes[z + 1][x + 1]; }; + inline PixelAttribute &attribute(int z, int x) { return m_pixelAttributes[z + 1][x + 1]; }; private: - std::vector > m_blockPixelAttributes; + void freeAttributes(); + +private: + enum Line { + FirstLine = 0, + LastLine = BLOCK_SIZE, + EmptyLine = BLOCK_SIZE + 1, + BlockCount = BLOCK_SIZE + 2 + }; + PixelAttribute *m_pixelAttributes[BLOCK_SIZE + 2]; // 1px gradient + empty + int m_width; }; #endif /* end of include guard: PIXELATTRIBUTES_H_ADZ35GYF */ diff --git a/config.h b/config.h index f158e51..7ef8d1c 100644 --- a/config.h +++ b/config.h @@ -12,3 +12,5 @@ #else #define PATH_SEPARATOR '/' #endif + +#define BLOCK_SIZE 16