Fix Bug where scaled maps where not drawn correctly

fix #4
master
Unknown 2019-03-29 04:24:11 +01:00
parent 665e0339a3
commit 35b8324099
2 changed files with 29 additions and 47 deletions

View File

@ -8,11 +8,13 @@
*/
#include "PixelAttributes.h"
#include <cstring> // memcpy
using namespace std;
PixelAttribute::AlphaMixingMode PixelAttribute::m_mixMode = PixelAttribute::AlphaMixCumulative;
void PixelAttributes::setParameters(int width, int lines, int nextY, int scale, bool defaultEmpty)
{
freeAttributes();
@ -28,20 +30,13 @@ void PixelAttributes::setParameters(int width, int lines, int nextY, int scale,
m_firstUnshadedY = 0;
m_scale = scale;
PixelAttribute pa;
pa.m_a = 0;
pa.nextEmpty = false;
m_pixelAttributes = vector<vector<PixelAttribute>>(m_lineCount, vector<PixelAttribute>(m_width, PixelAttribute()));
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;
if (defaultEmpty)
for (int i = 0; i < m_lineCount; i++)
for (int j = 0; j < m_width; j++) {
m_pixelAttributes[i][j].nextEmpty = (j - 1) % (16 / scale) == 0;
}
}
}
}
void PixelAttributes::scroll(int keepY)
@ -50,19 +45,20 @@ void PixelAttributes::scroll(int keepY)
if (scroll > 0) {
int i;
for (i = m_previousLine; i + scroll <= m_lastLine; i++) {
auto tmp = m_pixelAttributes[i];
m_pixelAttributes[i] = m_pixelAttributes[i + scroll];
m_pixelAttributes[i + scroll] = tmp;
m_pixelAttributes[i].swap(m_pixelAttributes[i + scroll]);
}
size_t lineLength = m_width * sizeof(PixelAttribute);
for (; i <= m_lastLine; ++i) {
m_pixelAttributes[i] = m_pixelAttributes[m_emptyLine];
// TODO: get rid of memcpy. This seems non-trivial.
memcpy(m_pixelAttributes[i].data(), m_pixelAttributes[m_emptyLine].data(), lineLength);
}
m_firstY += scroll;
m_nextY = m_firstY;
m_firstUnshadedY -= scroll;
if (m_firstUnshadedY < m_firstY) m_firstUnshadedY = m_firstY;
if (m_firstUnshadedY < m_firstY)
m_firstUnshadedY = m_firstY;
}
}
@ -115,14 +111,14 @@ void PixelAttributes::renderShading(double emphasis, bool drawAlpha)
d = 3;
}
d = d * 12 / 255 * emphasis;
#define pixel (m_pixelAttributes[y][x])
#define pixel (m_pixelAttributes[y][x])
//PixelAttribute &pixel = m_pixelAttributes[y][x];
if (drawAlpha)
d = d * (1 - pixel.m_t);
pixel.m_r = colorSafeBounds(pixel.m_r + d);
pixel.m_g = colorSafeBounds(pixel.m_g + d);
pixel.m_b = colorSafeBounds(pixel.m_b + d);
#undef pixel
#undef pixel
}
}
m_firstUnshadedY = y - yCoord2Line(0);
@ -234,7 +230,7 @@ void PixelAttribute::mixUnder(const PixelAttribute &p)
m_h = p.m_h;
}
else if (m_a == 1)
; // Nothing to do: pixel is already fully opaque.
; // Nothing to do: pixel is already fully opaque.
else if ((m_mixMode & AlphaMixCumulative) == AlphaMixCumulative || (m_mixMode == AlphaMixAverage && p.m_a == 1)) {
PixelAttribute pp(p);
#ifdef DEBUG
@ -287,16 +283,3 @@ 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;
}

View File

@ -17,7 +17,8 @@
#include <stdexcept>
#include <vector>
class PixelAttribute {
class PixelAttribute
{
public:
enum AlphaMixingMode {
AlphaMixDarkenBit = 0x01,
@ -27,10 +28,10 @@ public:
};
static void setMixMode(AlphaMixingMode mode);
PixelAttribute() = default;
// PixelAttribute(const PixelAttribute &p);
// PixelAttribute(const PixelAttribute &p);
PixelAttribute(const Color &color, double height);
PixelAttribute(const ColorEntry &entry, double height);
bool nextEmpty{true};
bool nextEmpty{false};
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); }
@ -51,19 +52,18 @@ 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};
double m_h{ std::numeric_limits<double>::quiet_NaN() };
double m_h{std::numeric_limits<double>::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;
friend class PixelAttributes;
};
class PixelAttributes
@ -134,15 +134,15 @@ inline PixelAttribute &PixelAttributes::attribute(int y, int x)
// operator=(p);
//}
inline PixelAttribute::PixelAttribute(const Color &color, double height) :
nextEmpty(false), m_n(0), m_h(height), m_t(0), m_a(color.a/255.0),
m_r(color.r/255.0), m_g(color.g/255.0), m_b(color.b/255.0)
inline PixelAttribute::PixelAttribute(const Color &color, double height)
: nextEmpty(false), m_n(0), m_h(height), m_t(0), m_a(color.a / 255.0),
m_r(color.r / 255.0), m_g(color.g / 255.0), m_b(color.b / 255.0)
{
}
inline PixelAttribute::PixelAttribute(const ColorEntry &entry, double height) :
nextEmpty(false), m_n(0), m_h(height), m_t(entry.t/255.0), m_a(entry.a/255.0),
m_r(entry.r/255.0), m_g(entry.g/255.0), m_b(entry.b/255.0)
inline PixelAttribute::PixelAttribute(const ColorEntry &entry, double height)
: nextEmpty(false), m_n(0), m_h(height), m_t(entry.t / 255.0), m_a(entry.a / 255.0),
m_r(entry.r / 255.0), m_g(entry.g / 255.0), m_b(entry.b / 255.0)
{
}
@ -157,4 +157,3 @@ inline PixelAttribute &PixelAttribute::operator=(const PixelAttribute &p)
m_b = p.m_b;
return *this;
}