Small optimization of PixelAttributes.

This commit is contained in:
Miroslav Bendík 2012-09-01 14:36:14 +02:00
parent 1edbba5a94
commit 4a8c041bc1
3 changed files with 45 additions and 11 deletions

View File

@ -7,28 +7,49 @@
* =====================================================================
*/
#include <cstdlib>
#include <cstring>
#include "PixelAttributes.h"
#include <algorithm>
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;
}
}
}

View File

@ -11,7 +11,7 @@
#define PIXELATTRIBUTES_H_ADZ35GYF
#include <limits>
#include <vector>
#include "config.h"
struct PixelAttribute {
PixelAttribute(): height(std::numeric_limits<int>::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 <std::vector <PixelAttribute> > 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 */

View File

@ -12,3 +12,5 @@
#else
#define PATH_SEPARATOR '/'
#endif
#define BLOCK_SIZE 16