Files
c-evo-evo/widget/src/CevoScreenTools.cpp
T
2026-11-03 16:01:54 +01:00

780 lines
29 KiB
C++

// This file is part of c-evo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoGraphicSet.h"
#include "Cevo/CevoTexture.h"
#include "Dpi/DpiPixelPointer.h"
#include <QFileInfo>
#include <algorithm>
#include <cmath>
#include <cstdlib>
namespace Cevo {
std::array<Dpi::Font, static_cast<size_t>(FontType::Count)> UniFont;
Texture *MainTexture = nullptr;
GraphicSet *HGrSystem = nullptr;
GraphicSet *HGrSystem2 = nullptr;
GraphicSet *Templates = nullptr;
Dpi::Bitmap *Colors = nullptr;
Dpi::Bitmap *bigImp = nullptr;
Dpi::Bitmap paper;
Dpi::Bitmap drawBuffer;
void drawBufferEnsureSize(int width, int height)
{
if (drawBuffer.width() >= width && drawBuffer.height() >= height)
return;
drawBuffer.setSize(std::max(drawBuffer.width(), width), std::max(drawBuffer.height(), height));
drawBuffer.canvas().begin();
drawBuffer.canvas().fillRect(QRect(0, 0, drawBuffer.width(), drawBuffer.height()));
drawBuffer.canvas().end();
}
QColor colorFromDelphi(int value)
{
return QColor(value & 0xFF, (value >> 8) & 0xFF, (value >> 16) & 0xFF);
}
int colorToDelphi(const QColor &color)
{
return color.red() | (color.green() << 8) | (color.blue() << 16);
}
int gamma = 100;
GraphicSets grExt;
QString graphicsDir;
namespace {
std::array<std::uint8_t, 256> gammaLookupTable;
void initGammaLookupTable()
{
gammaLookupTable[0] = 0;
for (int i = 1; i <= 255; ++i) {
const double p = std::round(255.0 * std::exp(std::log(i / 255.0) * 100.0 / gamma));
Q_ASSERT(p >= 0 && p < 256);
gammaLookupTable[static_cast<std::size_t>(i)] = static_cast<std::uint8_t>(p);
}
}
int hexCharToInt(QChar c)
{
if (c >= QLatin1Char('0') && c <= QLatin1Char('9'))
return c.toLatin1() - '0';
if (c >= QLatin1Char('A') && c <= QLatin1Char('F'))
return c.toLatin1() - 'A' + 10;
if (c >= QLatin1Char('a') && c <= QLatin1Char('f'))
return c.toLatin1() - 'a' + 10;
return 0;
}
} // namespace
void applyGammaToBitmap(Dpi::Bitmap &bitmap)
{
if (gamma == 100)
return;
initGammaLookupTable();
Dpi::PixelPointer pixel(&bitmap.image(), 0, 0);
for (int y = 0; y < bitmap.height(); ++y) {
for (int x = 0; x < bitmap.width(); ++x) {
const QRgb c = pixel.pixelARGB();
pixel.setPixelARGB(qRgb(gammaLookupTable[static_cast<std::size_t>(qRed(c))],
gammaLookupTable[static_cast<std::size_t>(qGreen(c))],
gammaLookupTable[static_cast<std::size_t>(qBlue(c))]));
pixel.nextPixel();
}
pixel.nextLine();
}
}
QColor hexStringToColor(const QString &text)
{
QString s = text;
while (!s.isEmpty() && s.at(0) == QLatin1Char(' '))
s.remove(0, 1);
s += QStringLiteral("000000");
if (gamma == 100) {
return QColor(0x10 * hexCharToInt(s.at(0)) + hexCharToInt(s.at(1)),
0x10 * hexCharToInt(s.at(2)) + hexCharToInt(s.at(3)),
0x10 * hexCharToInt(s.at(4)) + hexCharToInt(s.at(5)));
}
initGammaLookupTable();
return QColor(gammaLookupTable[static_cast<std::size_t>(0x10 * hexCharToInt(s.at(0)) + hexCharToInt(s.at(1)))],
gammaLookupTable[static_cast<std::size_t>(0x10 * hexCharToInt(s.at(2)) + hexCharToInt(s.at(3)))],
gammaLookupTable[static_cast<std::size_t>(0x10 * hexCharToInt(s.at(4)) + hexCharToInt(s.at(5)))]);
}
GraphicSet *loadGraphicSet(const QString &name, bool transparency)
{
GraphicSet *result = grExt.searchByName(name);
if (result)
return result;
result = &grExt.addNew(name);
const QString baseFileName = graphicsDir + QLatin1Char('/') + name;
// The original's LoadGraphicFile auto-appends ".png" when the name has
// no extension, but that is a by-value parameter so it does NOT affect
// the (possibly extension-less) base name used below for the .grs
// sidecar path - replicate both quirks exactly.
QString imageFileName = baseFileName;
if (QFileInfo(imageFileName).suffix().isEmpty())
imageFileName += QLatin1String(".png");
// Do not apply gamma during file load as it would affect transparency colors too.
result->data.loadFromFile(imageFileName);
if (result->data.width() == 0 || result->data.height() == 0)
return nullptr;
const QFileInfo baseInfo(baseFileName);
const QString grsFileName = baseInfo.path() + QLatin1Char('/') +
baseInfo.completeBaseName() + QLatin1String(GraphicSetFileExt);
if (QFileInfo::exists(grsFileName))
result->loadFromFile(grsFileName);
result->resetPixUsed();
if (transparency) {
result->mask.setSize(result->data.width(), result->data.height());
Dpi::PixelPointer dataPixel(&result->data.image(), 0, 0);
Dpi::PixelPointer maskPixel(&result->mask.image(), 0, 0);
for (int y = 0; y < result->data.height(); ++y) {
for (int x = 0; x < result->data.width(); ++x) {
const QRgb originalColor = dataPixel.pixelARGB() & 0xFFFFFFu;
if (originalColor == TransparentColor1 || originalColor == TransparentColor2) {
maskPixel.setPixelRGB(qRgb(0xFF, 0xFF, 0xFF));
dataPixel.setPixelRGB(0);
} else {
maskPixel.setPixelRGB(0);
}
dataPixel.nextPixel();
maskPixel.nextPixel();
}
dataPixel.nextLine();
maskPixel.nextLine();
}
if (gamma != 100)
applyGammaToBitmap(result->data);
}
return result;
}
void bitBltBitmap(Dpi::Bitmap &dest, int x, int y, int width, int height,
Dpi::Bitmap &src, int xSrc, int ySrc, RasterOp rop)
{
if (xSrc < 0) { x -= xSrc; width -= xSrc; xSrc = 0; }
if (ySrc < 0) { y -= ySrc; height -= ySrc; ySrc = 0; }
if (x < 0) { width += x; xSrc -= x; x = 0; }
if (y < 0) { height += y; ySrc -= y; y = 0; }
if (x + width > dest.width()) width = dest.width() - x;
if (y + height > dest.height()) height = dest.height() - y;
if (width < 0 || height < 0)
return;
Dpi::PixelPointer srcPixel(&src.image(), xSrc, ySrc);
Dpi::PixelPointer dstPixel(&dest.image(), x, y);
for (int yy = 0; yy < height; ++yy) {
for (int xx = 0; xx < width; ++xx) {
switch (rop) {
case RasterOp::Copy:
dstPixel.setPixelRGB(srcPixel.pixelRGB());
break;
case RasterOp::And:
dstPixel.setPixelRGB(dstPixel.pixelRGB() & srcPixel.pixelRGB());
break;
case RasterOp::Paint:
dstPixel.setPixelRGB(dstPixel.pixelRGB() | srcPixel.pixelRGB());
break;
case RasterOp::Invert:
dstPixel.setPixelRGB(~dstPixel.pixelRGB());
break;
}
dstPixel.nextPixel();
srcPixel.nextPixel();
}
dstPixel.nextLine();
srcPixel.nextLine();
}
}
void unshareBitmap(Dpi::Bitmap &) {}
void bitmapReplaceColor(Dpi::Bitmap &dst, int x, int y, int width, int height, QColor oldColor, QColor newColor)
{
const QRgb oldRgb = oldColor.rgb() & 0xFFFFFFu;
const QRgb newRgb = newColor.rgb() & 0xFFFFFFu;
Dpi::PixelPointer pixel(&dst.image(), x, y);
for (int yy = 0; yy < height; ++yy) {
for (int xx = 0; xx < width; ++xx) {
if ((pixel.pixelRGB() & 0xFFFFFFu) == oldRgb)
pixel.setPixelRGB(newRgb);
pixel.nextPixel();
}
pixel.nextLine();
}
}
void glowFrame(Dpi::Bitmap &dst, int x0, int y0, int width, int height, QColor cl)
{
constexpr int glowRange = 8;
const int red = cl.red();
const int green = cl.green();
const int blue = cl.blue();
for (int y = -glowRange + 1; y <= height - 1 + glowRange - 1; ++y) {
for (int x = -glowRange + 1; x <= width - 1 + glowRange - 1; ++x) {
int r;
if (x < 0) {
if (y < 0)
r = static_cast<int>(std::lround(std::sqrt(static_cast<double>(x * x + y * y))));
else if (y >= height)
r = static_cast<int>(
std::lround(std::sqrt(static_cast<double>(x * x + (y - (height - 1)) * (y - (height - 1))))));
else
r = -x;
} else if (x >= width) {
if (y < 0)
r = static_cast<int>(
std::lround(std::sqrt(static_cast<double>((x - (width - 1)) * (x - (width - 1)) + y * y))));
else if (y >= height)
r = static_cast<int>(std::lround(std::sqrt(static_cast<double>(
(x - (width - 1)) * (x - (width - 1)) + (y - (height - 1)) * (y - (height - 1))))));
else
r = x - (width - 1);
} else if (y < 0) {
r = -y;
} else if (y >= height) {
r = y - (height - 1);
} else {
continue;
}
if (r == 0)
r = 1;
if (r < glowRange) {
const int px = x0 + x;
const int py = y0 + y;
if (px < 0 || py < 0 || px >= dst.width() || py >= dst.height())
continue;
const QRgb old = dst.image().pixel(px, py);
auto mix = [&](int oldC, int clC) { return (oldC * (r - 1) + clC * (glowRange - r)) / (glowRange - 1); };
dst.image().setPixel(px, py, qRgb(mix(qRed(old), red), mix(qGreen(old), green), mix(qBlue(old), blue)));
}
}
}
}
void sprite(Dpi::Canvas &canvas, GraphicSet &graphicSet, int xDst, int yDst,
int width, int height, int xGr, int yGr)
{
Dpi::Bitmap scratch(width, height);
bitBltBitmap(scratch, 0, 0, width, height, graphicSet.mask, xGr, yGr, RasterOp::And);
bitBltBitmap(scratch, 0, 0, width, height, graphicSet.data, xGr, yGr, RasterOp::Paint);
canvas.draw(xDst, yDst, scratch.image());
}
void sprite(Dpi::Canvas &canvas, int xDst, int yDst, const GraphicSetItem &item)
{
if (!item.graphicSet)
return;
sprite(canvas, *item.graphicSet, xDst, yDst, item.width, item.height, item.left, item.top);
}
void frame(Dpi::Canvas &canvas, int x0, int y0, int x1, int y1, QColor cl0, QColor cl1)
{
canvas.moveTo(x0, y1);
canvas.pen().setColor(cl0);
canvas.lineTo(x0, y0);
canvas.lineTo(x1, y0);
canvas.pen().setColor(cl1);
canvas.lineTo(x1, y1);
canvas.lineTo(x0, y1);
}
void dLine(Dpi::Canvas &canvas, int x0, int x1, int y, QColor cl0, QColor cl1)
{
canvas.pen().setColor(cl0);
canvas.moveTo(x0, y);
canvas.lineTo(x1, y);
canvas.pen().setColor(cl1);
canvas.moveTo(x0 + 1, y + 1);
canvas.lineTo(x1 + 1, y + 1);
canvas.setPixel(x0, y + 1, cl0);
canvas.setPixel(x1, y, cl1);
}
void rframe(Dpi::Canvas &canvas, int x0, int y0, int x1, int y1, QColor cl0, QColor cl1)
{
canvas.pen().setColor(cl0);
canvas.moveTo(x0, y0 + 1);
canvas.lineTo(x0, y1);
canvas.moveTo(x0 + 1, y0);
canvas.lineTo(x1, y0);
canvas.pen().setColor(cl1);
canvas.moveTo(x1, y0 + 1);
canvas.lineTo(x1, y1);
canvas.moveTo(x0 + 1, y1);
canvas.lineTo(x1, y1);
}
void btnFrame(Dpi::Canvas &canvas, const QRect &rect, const Texture &texture)
{
rframe(canvas, rect.left() - 1, rect.top() - 1, rect.right(), rect.bottom(),
texture.colorBevelShade, texture.colorBevelLight);
}
void frameImage(Dpi::Canvas &canvas, Dpi::Bitmap &src, int x, int y, int width, int height, int xSrc, int ySrc,
bool isControl)
{
if (isControl) {
frame(canvas, x - 1, y - 1, x + width, y + height, colorFromDelphi(0xB0B0B0), Qt::white);
rframe(canvas, x - 2, y - 2, x + width + 1, y + height + 1, Qt::white, colorFromDelphi(0xB0B0B0));
} else {
frame(canvas, x - 1, y - 1, x + width, y + height, Qt::black, Qt::black);
}
canvas.copyRect(QRect(x, y, width, height), src.canvas(), QRect(xSrc, ySrc, width, height));
}
void editFrame(Dpi::Canvas &canvas, const QRect &rect, const Texture &texture)
{
frame(canvas, rect.left() - 1, rect.top() - 1, rect.right(), rect.bottom(), Qt::black, Qt::black);
frame(canvas, rect.left() - 2, rect.top() - 2, rect.right() + 1, rect.bottom() + 1, Qt::black, Qt::black);
frame(canvas, rect.left() - 3, rect.top() - 3, rect.right() + 2, rect.bottom() + 1, Qt::black, Qt::black);
rframe(canvas, rect.left() - 4, rect.top() - 4, rect.right() + 3, rect.bottom() + 2, texture.colorBevelShade,
texture.colorBevelLight);
}
void corner(Dpi::Canvas &, int, int, int, const Texture &)
{
// Unimplemented in the original ScreenTools.pas too (body is commented out there).
}
void fill(Dpi::Canvas &canvas, int left, int top, int width, int height, int xOffset, int yOffset)
{
Q_UNUSED(xOffset);
Q_UNUSED(yOffset);
if (!MainTexture || MainTexture->width() <= 0 || MainTexture->height() <= 0)
return;
const int w = std::min(width, MainTexture->width());
const int h = std::min(height, MainTexture->height());
const int yy = height / MainTexture->height();
const int xx = width / MainTexture->width();
for (int y = 0; y <= yy; ++y) {
for (int x = 0; x <= xx; ++x) {
canvas.copyRect(QRect(left + x * MainTexture->width(), top + y * MainTexture->height(), w, h),
MainTexture->canvas(), QRect(0, 0, w, h));
}
}
}
void fill(Dpi::Canvas &canvas, const QRect &rect, const QPoint &offset)
{
fill(canvas, rect.left(), rect.top(), rect.width(), rect.height(), offset.x(), offset.y());
}
void fillSeamless(Dpi::Canvas &canvas, int left, int top, int width, int height, int xOffset, int yOffset,
Dpi::Bitmap &texture)
{
if (texture.width() <= 0 || texture.height() <= 0)
return;
while (xOffset < 0)
xOffset += texture.width();
while (yOffset < 0)
yOffset += texture.height();
for (int y = (top + yOffset) / texture.height(); y <= (top + yOffset + height - 1) / texture.height(); ++y) {
int y0cut = top + yOffset - y * texture.height();
if (y0cut < 0)
y0cut = 0;
int y1cut = (y + 1) * texture.height() - (top + yOffset + height);
if (y1cut < 0)
y1cut = 0;
for (int x = (left + xOffset) / texture.width(); x <= (left + xOffset + width - 1) / texture.width(); ++x) {
int x0cut = left + xOffset - x * texture.width();
if (x0cut < 0)
x0cut = 0;
int x1cut = (x + 1) * texture.width() - (left + xOffset + width);
if (x1cut < 0)
x1cut = 0;
canvas.copyRect(QRect(x * texture.width() + x0cut - xOffset, y * texture.height() + y0cut - yOffset,
texture.width() - x0cut - x1cut, texture.height() - y0cut - y1cut),
texture.canvas(), QRect(x0cut, y0cut, texture.width() - x0cut - x1cut,
texture.height() - y0cut - y1cut));
}
}
}
void fillRectSeamless(Dpi::Canvas &canvas, int x0, int y0, int x1, int y1, int xOffset, int yOffset,
Dpi::Bitmap &texture)
{
fillSeamless(canvas, x0, y0, x1 - x0, y1 - y0, xOffset, yOffset, texture);
}
void texturize(Dpi::Bitmap &dest, Dpi::Bitmap &texture, QColor transparentColor)
{
if (texture.width() <= 0 || texture.height() <= 0)
return;
const QRgb transparentRgb = transparentColor.rgb() & 0xFFFFFFu;
Dpi::PixelPointer dstPixel(&dest.image(), 0, 0);
for (int y = 0; y < dest.height(); ++y) {
for (int x = 0; x < dest.width(); ++x) {
if ((dstPixel.pixelRGB() & 0xFFFFFFu) == transparentRgb) {
Dpi::PixelPointer srcPixel(&texture.image(), x % texture.width(), y % texture.height());
dstPixel.setPixelRGB(srcPixel.pixelRGB());
}
dstPixel.nextPixel();
}
dstPixel.nextLine();
}
}
void paintBackground(Dpi::Canvas &canvas, int left, int top, int width, int height, int formWidth, int formHeight)
{
Q_UNUSED(formWidth);
Q_UNUSED(formHeight);
fill(canvas, left, top, width, height, 0, 0);
}
void paintProgressBar(Dpi::Canvas &canvas, int kind, int x, int y, int pos, int growth, int max,
const Texture &texture)
{
if (pos > max)
pos = max;
if (growth < 0) {
pos += growth;
if (pos < 0) {
growth -= pos;
if (growth > max)
growth = max;
pos = 0;
}
} else if (pos + growth > max) {
growth = max - pos;
}
frame(canvas, x - 1, y - 1, x + max, y + 7, Qt::black, Qt::black);
rframe(canvas, x - 2, y - 2, x + max + 1, y + 8, texture.colorBevelShade, texture.colorBevelLight);
if (!HGrSystem)
return;
Dpi::Canvas &src = HGrSystem->data.canvas();
for (int i = 0; i < pos / 8; ++i)
canvas.copyRect(QRect(x + i * 8, y, 8, 7), src, QRect(104, 9 + 8 * kind, 8, 7));
canvas.copyRect(QRect(x + 8 * (pos / 8), y, pos - 8 * (pos / 8), 7), src, QRect(104, 9 + 8 * kind, pos - 8 * (pos / 8), 7));
if (growth > 0) {
for (int i = 0; i < growth / 8; ++i)
canvas.copyRect(QRect(x + pos + i * 8, y, 8, 7), src, QRect(112, 9 + 8 * kind, 8, 7));
canvas.copyRect(QRect(x + pos + 8 * (growth / 8), y, growth - 8 * (growth / 8), 7), src,
QRect(112, 9 + 8 * kind, growth - 8 * (growth / 8), 7));
} else if (growth < 0) {
for (int i = 0; i < -growth / 8; ++i)
canvas.copyRect(QRect(x + pos + i * 8, y, 8, 7), src, QRect(104, 1, 8, 7));
canvas.copyRect(QRect(x + pos + 8 * (-growth / 8), y, -growth - 8 * (-growth / 8), 7), src,
QRect(104, 1, -growth - 8 * (-growth / 8), 7));
}
canvas.brush().setStyle(Qt::SolidPattern);
canvas.brush().setColor(Qt::black);
canvas.fillRect(QRect(x + pos + std::abs(growth), y, max - pos - std::abs(growth), 7));
canvas.brush().setStyle(Qt::NoBrush);
}
namespace {
// %c=combat %f=food %l=luxury %m=manufacture %n=nation %o=output
// %p=production %r=research %t=terrain %w=welfare, each a 10px-wide icon.
const QString kIconChars = QStringLiteral("cflmnoprtw");
const int kIconKinds[] = { 6, 0, 8, 17, 7, 16, 2, 12, 4, 13 };
void paintIcon(Dpi::Canvas &canvas, int x, int y, int kind)
{
if (!HGrSystem)
return;
sprite(canvas, *HGrSystem, x, y + 6, 10, 10, 66 + kind % 11 * 11, 115 + kind / 11 * 11);
}
} // namespace
void biColorTextOut(Dpi::Canvas &canvas, QColor mainColor, QColor backColor, int x, int y, const QString &text)
{
const bool skipShadow = backColor == QColor(0x7F, 0x00, 0x7F);
for (int pass = 0; pass < 2; ++pass) {
const bool shadow = pass == 0;
if (shadow && skipShadow)
continue;
const int drawX = shadow ? x + 1 : x;
const int drawY = shadow ? y + 1 : y;
canvas.font().setColor(shadow ? backColor : mainColor);
QString s = text;
int xp = drawX;
for (;;) {
const int p = s.indexOf(QLatin1Char('%'));
const bool hasIcon = p >= 0 && p + 1 < s.length() && kIconChars.contains(s.at(p + 1));
if (!hasIcon) {
canvas.textOut(xp, drawY, s);
break;
}
const QString head = s.left(p);
canvas.textOut(xp, drawY, head);
xp += canvas.textWidth(head);
if (!shadow)
paintIcon(canvas, xp + 1, drawY, kIconKinds[kIconChars.indexOf(s.at(p + 1))]);
xp += 10;
s.remove(0, p + 2);
}
}
}
int biColorTextWidth(Dpi::Canvas &canvas, const QString &text)
{
int result = 1;
QString s = text;
for (;;) {
const int p = s.indexOf(QLatin1Char('%'));
if (p < 0 || p == s.length() - 1) {
result += canvas.textWidth(s);
break;
}
if (!kIconChars.contains(s.at(p + 1)))
result += canvas.textWidth(s.left(p + 2));
else
result += canvas.textWidth(s.left(p)) + 10;
s.remove(0, p + 2);
}
return result;
}
void risedTextOut(Dpi::Canvas &canvas, int x, int y, const QString &text)
{
biColorTextOut(canvas, Qt::white, Qt::black, x, y, text);
}
void loweredTextOut(Dpi::Canvas &canvas, int cl, const Texture &texture, int x, int y, const QString &text)
{
if (cl == -2) {
const QColor shade = texture.colorBevelShade;
const QColor mixedShade(shade.red() / 2, shade.green() / 2, shade.blue() / 2);
biColorTextOut(canvas, mixedShade, texture.colorBevelLight, x, y, text);
} else if (cl < 0) {
biColorTextOut(canvas, texture.colorTextShade, texture.colorTextLight, x, y, text);
} else {
biColorTextOut(canvas, colorFromDelphi(cl), texture.colorTextLight, x, y, text);
}
}
void imageOpB(Dpi::Bitmap &dst, Dpi::Bitmap &src, int xDst, int yDst, int xSrc, int ySrc, int width, int height)
{
if (xDst < 0) { width += xDst; xSrc -= xDst; xDst = 0; }
if (yDst < 0) { height += yDst; ySrc -= yDst; yDst = 0; }
if (xDst + width > dst.width()) width = dst.width() - xDst;
if (yDst + height > dst.height()) height = dst.height() - yDst;
if (width < 0 || height < 0)
return;
Dpi::PixelPointer srcPixel(&src.image(), xSrc, ySrc);
Dpi::PixelPointer dstPixel(&dst.image(), xDst, yDst);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
const int brightness = qBlue(srcPixel.pixelARGB()); // greyscale template, one byte
const QRgb d = dstPixel.pixelARGB();
auto scale = [brightness](int c) { const int v = (c * brightness) >> 7; return v > 255 ? 255 : v; };
dstPixel.setPixelARGB(qRgb(scale(qRed(d)), scale(qGreen(d)), scale(qBlue(d))));
dstPixel.nextPixel();
srcPixel.nextPixel();
}
dstPixel.nextLine();
srcPixel.nextLine();
}
}
namespace {
// Shared by ImageOp_BCC/ImageOp_CBC: Src is a template whose B channel is
// the background blend amplitude and whose G/R channels are the amplitudes
// for colorA/colorB respectively. colorA/colorB use the Delphi $BBGGRR byte
// order (see colorFromDelphi).
void blendTemplate(Dpi::Bitmap &dst, Dpi::Bitmap &src, int xDst, int yDst, int xSrc, int ySrc,
int width, int height, int colorA, int colorB)
{
if (xDst < 0) { width += xDst; xSrc -= xDst; xDst = 0; }
if (yDst < 0) { height += yDst; ySrc -= yDst; yDst = 0; }
if (xDst + width > dst.width()) width = dst.width() - xDst;
if (yDst + height > dst.height()) height = dst.height() - yDst;
if (width < 0 || height < 0)
return;
Dpi::PixelPointer srcPixel(&src.image(), xSrc, ySrc);
Dpi::PixelPointer dstPixel(&dst.image(), xDst, yDst);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
const QRgb s = srcPixel.pixelARGB();
const int trans = qBlue(s) * 2;
const int ampA = qGreen(s) * 2;
const int ampB = qRed(s) * 2;
if (trans != 0xFF) {
const QRgb d = dstPixel.pixelARGB();
auto mix = [&](int dstChannel, int shift) {
const int value = (dstChannel * trans + ((colorB >> shift) & 0xFF) * ampB +
((colorA >> shift) & 0xFF) * ampA) / 0xFF;
return value > 255 ? 255 : value;
};
dstPixel.setPixelARGB(qRgb(mix(qRed(d), 0), mix(qGreen(d), 8), mix(qBlue(d), 16)));
}
dstPixel.nextPixel();
srcPixel.nextPixel();
}
dstPixel.nextLine();
srcPixel.nextLine();
}
}
} // namespace
void imageOpBCC(Dpi::Bitmap &dst, Dpi::Bitmap &src, int xDst, int yDst, int xSrc, int ySrc,
int width, int height, int color1, int color2)
{
blendTemplate(dst, src, xDst, yDst, xSrc, ySrc, width, height, color1, color2);
}
void imageOpCBC(Dpi::Bitmap &dst, Dpi::Bitmap &src, int xDst, int yDst, int xSrc, int ySrc,
int width, int height, int color0, int color2)
{
blendTemplate(dst, src, xDst, yDst, xSrc, ySrc, width, height, color0, color2);
}
void imageOpCCC(Dpi::Bitmap &bmp, int x, int y, int width, int height, int color0, int color1, int color2)
{
if (x + width > bmp.width()) width = bmp.width() - x;
if (y + height > bmp.height()) height = bmp.height() - y;
if (width < 0 || height < 0)
return;
Dpi::PixelPointer pixel(&bmp.image(), x, y);
for (int yy = 0; yy < height; ++yy) {
for (int xx = 0; xx < width; ++xx) {
const QRgb p = pixel.pixelARGB();
const int b = qBlue(p), g = qGreen(p), r = qRed(p);
auto mix = [&](int shift) {
return ((b * ((color0 >> shift) & 0xFF) + g * ((color1 >> shift) & 0xFF) +
r * ((color2 >> shift) & 0xFF)) >> 8) & 0xFF;
};
pixel.setPixelARGB(qRgb(mix(0), mix(8), mix(16)));
pixel.nextPixel();
}
pixel.nextLine();
}
}
int turnToYear(int turn)
{
int result = -4000;
if (turn <= 0)
return result;
auto consume = [&](int span, int yearsPerTurn) {
const int n = std::min(span, turn);
result += n * yearsPerTurn;
turn -= n;
};
consume(60, 50); // Year -4000..-1000, Turn 0..60
if (turn == 0) return result;
consume(40, 25); // Year -1000..0, Turn 60..100
if (turn == 0) return result;
consume(75, 20); // Year 0..1500, Turn 100..175
if (turn == 0) return result;
consume(25, 10); // Year 1500..1750, Turn 175..200
if (turn == 0) return result;
consume(50, 2); // Year 1750..1850, Turn 200..250
if (turn == 0) return result;
result += turn; // Year 1850.., Turn 250..
return result;
}
QString movementToString(int movement)
{
QString result;
if (movement >= 1000) {
result += QChar(QLatin1Char('0').unicode() + movement / 1000);
movement %= 1000;
}
result += QChar(QLatin1Char('0').unicode() + movement / 100);
movement %= 100;
if (movement > 0) {
result += QLatin1Char('.');
result += QChar(QLatin1Char('0').unicode() + movement / 10);
movement %= 10;
if (movement > 0)
result += QChar(QLatin1Char('0').unicode() + movement);
}
return result;
}
namespace {
void gradient(Dpi::Canvas &canvas, int x, int y, int dx, int dy, int width, int height, QColor color,
const int (&brightness)[16])
{
auto clamp = [](int v) { return v < 0 ? 0 : (v >= 256 ? 255 : v); };
for (int i = 0; i < 16; ++i) {
canvas.pen().setColor(QColor(clamp(color.red() + brightness[i]), clamp(color.green() + brightness[i]),
clamp(color.blue() + brightness[i])));
canvas.moveTo(x + dx * i, y + dy * i);
canvas.lineTo(x + dx * i + width, y + dy * i + height);
}
canvas.pen().setColor(Qt::black);
canvas.moveTo(x + 1, y + 16 * dy + height);
canvas.lineTo(x + 16 * dx + width, y + 16 * dy + height);
canvas.lineTo(x + 16 * dx + width, y);
}
} // namespace
void lightGradient(Dpi::Canvas &canvas, int x, int y, int width, QColor color)
{
static constexpr int brightness[16] = { 16, 12, 8, 4, 0, -4, -8, -12, -16, -20, -24, -28, -32, -36, -40, -44 };
gradient(canvas, x, y, 0, 1, width, 0, color, brightness);
}
void darkGradient(Dpi::Canvas &canvas, int x, int y, int width, int kind)
{
static constexpr int brightness[16] = { 16, 12, 8, 4, 0, -4, -8, -36, 0, -20, -24, -28, -32, -36, -40, -44 };
const QColor color = HGrSystem ? HGrSystem->data.image().pixelColor(187, 137 + kind) : QColor(Qt::black);
gradient(canvas, x, y, 0, 1, width, 0, color, brightness);
}
void numberBar(Dpi::Canvas &canvas, int x, int y, const QString &cap, int val, const Texture &texture)
{
if (val <= 0)
return;
dLine(canvas, x - 2, x + 170, y + 16, texture.colorBevelShade, texture.colorBevelLight);
loweredTextOut(canvas, -1, texture, x - 2, y, cap);
const QString s = QString::number(val);
risedTextOut(canvas, x + 170 - biColorTextWidth(canvas, s), y, s);
}
} // namespace Cevo