Modernize PixelAttributes
Tests have shown that the vectors are as fast as the C-Arraysmaster
parent
d40bd789e8
commit
8e58e84218
|
@ -8,17 +8,11 @@
|
|||
*/
|
||||
|
||||
#include "PixelAttributes.h"
|
||||
#include <cstring> // 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; i++)
|
||||
for (int j=0; j<m_width; j++) {
|
||||
m_pixelAttributes[i][j].m_a=0;
|
||||
if (defaultEmpty)
|
||||
m_pixelAttributes[i][j].nextEmpty = (j - 1) % (16 / scale) == 0;
|
||||
else
|
||||
m_pixelAttributes[i][j].nextEmpty = false;
|
||||
m_pixelAttributes = vector<vector<PixelAttribute>>(m_lineCount, vector<PixelAttribute>(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;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
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<std::vector<PixelAttribute>> m_pixelAttributes;
|
||||
int m_width{};
|
||||
int m_firstY{};
|
||||
int m_nextY{};
|
||||
|
|
Loading…
Reference in New Issue