// 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/DpiCommon.h" #include "Dpi/DpiPixelPointer.h" #include #include #include #include #include namespace Cevo { std::array(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(); } void loadFonts(const QString &scriptFileName) { QFile file(scriptFileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; FontType section = FontType::Normal; while (!file.atEnd()) { const QString line = QString::fromUtf8(file.readLine()).trimmed(); if (line.isEmpty()) continue; if (line.at(0) == QLatin1Char('#')) { if (line == QLatin1String("#SMALL")) section = FontType::Small; else if (line == QLatin1String("#TINY")) section = FontType::Tiny; else if (line == QLatin1String("#CAPTION")) section = FontType::Caption; else if (line == QLatin1String("#BUTTON")) section = FontType::Button; else section = FontType::Normal; continue; } const int comma = line.indexOf(QLatin1Char(',')); if (comma <= 0) continue; Dpi::Font &font = UniFont[static_cast(section)]; font.setName(line.left(comma).trimmed()); int size = 0; for (int i = comma + 1; i < line.length(); ++i) { const QChar c = line.at(i); if (c >= QLatin1Char('0') && c <= QLatin1Char('9')) size = size * 10 + c.digitValue(); else if (c == QLatin1Char('B') || c == QLatin1Char('b')) font.setBold(true); else if (c == QLatin1Char('I') || c == QLatin1Char('i')) font.setItalic(true); } font.setHeight(Dpi::scaleToNative(size)); // the script gives pixel heights } } 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 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(i)] = static_cast(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(qRed(c))], gammaLookupTable[static_cast(qGreen(c))], gammaLookupTable[static_cast(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(0x10 * hexCharToInt(s.at(0)) + hexCharToInt(s.at(1)))], gammaLookupTable[static_cast(0x10 * hexCharToInt(s.at(2)) + hexCharToInt(s.at(3)))], gammaLookupTable[static_cast(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(std::lround(std::sqrt(static_cast(x * x + y * y)))); else if (y >= height) r = static_cast( std::lround(std::sqrt(static_cast(x * x + (y - (height - 1)) * (y - (height - 1)))))); else r = -x; } else if (x >= width) { if (y < 0) r = static_cast( std::lround(std::sqrt(static_cast((x - (width - 1)) * (x - (width - 1)) + y * y)))); else if (y >= height) r = static_cast(std::lround(std::sqrt(static_cast( (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) { // dst = (dst AND mask) OR data: start from what is already on the canvas (when it is image backed). Dpi::Bitmap scratch(width, height); scratch.canvas().copyRect(QRect(0, 0, width, height), canvas, QRect(xDst, yDst, width, height)); scratch.canvas().end(); 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 cframe(Dpi::Canvas &canvas, int x0, int y0, int x1, int y1, int corner, QColor cl) { canvas.pen().setColor(cl); canvas.moveTo(x0, y0 + corner - 1); canvas.lineTo(x0, y0); canvas.lineTo(x0 + corner, y0); canvas.moveTo(x1, y0 + corner - 1); canvas.lineTo(x1, y0); canvas.lineTo(x1 - corner, y0); canvas.moveTo(x1, y1 - corner + 1); canvas.lineTo(x1, y1); canvas.lineTo(x1 - corner, y1); canvas.moveTo(x0, y1 - corner + 1); canvas.lineTo(x0, y1); canvas.lineTo(x0 + corner, 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 dump(Dpi::Bitmap &dst, GraphicSet &graphicSet, int xDst, int yDst, int width, int height, int xGr, int yGr) { bitBltBitmap(dst, xDst, yDst, width, height, graphicSet.data, xGr, yGr); } 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 fillLarge(Dpi::Canvas &canvas, int x0, int y0, int x1, int y1, int xm) { if (!MainTexture || MainTexture->width() <= 0 || y1 <= y0) return; const int textureWidth = MainTexture->width(); const int textureHeight = MainTexture->height(); const int bandHeight = y1 - y0; const auto band = [&](int i) { const int n = ((textureHeight / 2) / bandHeight) * 2; if (n <= 0) return 0; while (textureHeight / 2 + (i + 1) * bandHeight > textureHeight) i -= n; while (textureHeight / 2 + i * bandHeight < 0) i += n; return i; }; const auto blit = [&](int xDst, int width, int xSrc, int yBand) { canvas.copyRect(QRect(xDst, y0, width, bandHeight), MainTexture->canvas(), QRect(xSrc, textureHeight / 2 + band(yBand) * bandHeight, width, bandHeight)); }; const int rightCount = (x1 - xm) / textureWidth; for (int i = 0; i < rightCount; ++i) blit(xm + i * textureWidth, textureWidth, 0, i); blit(xm + rightCount * textureWidth, x1 - (xm + rightCount * textureWidth), 0, rightCount); const int leftCount = (xm - x0) / textureWidth; for (int i = 0; i < leftCount; ++i) blit(xm - (i + 1) * textureWidth, textureWidth, 0, -i - 1); blit(x0, xm - leftCount * textureWidth - x0, (leftCount + 1) * textureWidth - (xm - x0), -leftCount - 1); } 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); } void paintRelativeProgressBar(Dpi::Canvas &canvas, int kind, int x, int y, int size, int pos, int growth, int max, bool indicateComplete, const Texture &texture) { if (growth > 0) paintProgressBar(canvas, kind, x, y, pos * size / max, (growth * size + max / 2) / max, size, texture); else paintProgressBar(canvas, kind, x, y, pos * size / max, (growth * size - max / 2) / max, size, texture); if (indicateComplete && pos + growth >= max && HGrSystem) sprite(canvas, *HGrSystem, x + size - 10, y - 7, 23, 16, 1, 129); } void makeBlue(Dpi::Bitmap &dst, int x, int y, int width, int height) { Dpi::PixelPointer pixelPtr(&dst.image(), x, y); for (int yy = 0; yy < height; ++yy) { for (int xx = 0; xx < width; ++xx) { const QRgb px = pixelPtr.pixelRGB(); pixelPtr.setPixelRGB(qRgb(qRed(px) / 2, qGreen(px) / 2, qBlue(px) / 2)); pixelPtr.nextPixel(); } pixelPtr.nextLine(); } } void makeRed(Dpi::Bitmap &dst, int x, int y, int width, int height) { Dpi::PixelPointer pixelPtr(&dst.image(), x, y); for (int yy = 0; yy < height; ++yy) { for (int xx = 0; xx < width; ++xx) { const QRgb px = pixelPtr.pixelRGB(); const int gray = (qBlue(px) + qGreen(px) + qRed(px)) * 85 >> 8; pixelPtr.setPixelRGB(qRgb(gray, 0, 0)); pixelPtr.nextPixel(); } pixelPtr.nextLine(); } } void countBar(Dpi::Bitmap &dst, int x, int y, int w, int kind, const QString &cap, int val, const Texture &texture) { if (val == 0) return; Q_ASSERT(kind >= 0); if (!HGrSystem) return; Dpi::Canvas &canvas = dst.canvas(); const int xIcon = x - 5; const int yIcon = y + 15; dLine(canvas, x - 2, xIcon + w + 2, yIcon + 16, texture.colorBevelShade, texture.colorBevelLight); const QString s = QString::number(val); const int cl = val < 0 ? 0x0000FF : -1; loweredTextOut(canvas, cl, texture, x - 2, y, cap); loweredTextOut(canvas, cl, texture, xIcon + w + 2 - biColorTextWidth(canvas, s), yIcon, s); int sd, ld; if (kind == 12 && val >= 100) { // science with symbol for 100 val /= 10; sd = 14 * (val / 10 + val % 10 - 1); if (sd == 0) sd = 1; ld = sd < w - 44 ? sd : w - 44; for (int i = 0; i < val % 10; ++i) { bitBltBitmap(dst, xIcon + 4 + i * (14 * ld / sd), yIcon + 2 + 1, 14, 14, HGrSystem->mask, 67 + kind % 8 * 15, 70 + kind / 8 * 15, RasterOp::And); sprite(canvas, *HGrSystem, xIcon + 3 + i * (14 * ld / sd), yIcon + 2, 14, 14, 67 + kind % 8 * 15, 70 + kind / 8 * 15); } for (int i = 0; i < val / 10; ++i) { bitBltBitmap(dst, xIcon + 4 + (val % 10) * (14 * ld / sd) + i * (14 * ld / sd), yIcon + 3, 14, 14, HGrSystem->mask, 67 + 7 % 8 * 15, 70 + 7 / 8 * 15, RasterOp::And); sprite(canvas, *HGrSystem, xIcon + 3 + (val % 10) * (14 * ld / sd) + i * (14 * ld / sd), yIcon + 2, 14, 14, 67 + 7 % 8 * 15, 70 + 7 / 8 * 15); } } else { val = std::abs(val); sd = (val % 10 == 0) ? 14 * (val / 10 - 1) : 10 * (val % 10 - 1) + 14 * (val / 10); if (sd == 0) sd = 1; ld = sd < w - 44 ? sd : w - 44; for (int i = 0; i < val / 10; ++i) { bitBltBitmap(dst, xIcon + 4 + i * (14 * ld / sd), yIcon + 3, 14, 14, HGrSystem->mask, 67 + kind % 8 * 15, 70 + kind / 8 * 15, RasterOp::And); sprite(canvas, *HGrSystem, xIcon + 3 + i * (14 * ld / sd), yIcon + 2, 14, 14, 67 + kind % 8 * 15, 70 + kind / 8 * 15); } for (int i = 0; i < val % 10; ++i) { bitBltBitmap(dst, xIcon + 4 + (val / 10) * (14 * ld / sd) + i * (10 * ld / sd), yIcon + 7, 10, 10, HGrSystem->mask, 66 + kind % 11 * 11, 115 + kind / 11 * 11, RasterOp::And); sprite(canvas, *HGrSystem, xIcon + 3 + (val / 10) * (14 * ld / sd) + i * (10 * ld / sd), yIcon + 6, 10, 10, 66 + kind % 11 * 11, 115 + kind / 11 * 11); } } } 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 vLightGradient(Dpi::Canvas &canvas, int x, int y, int height, 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, 1, 0, 0, height, color, brightness); } void vDarkGradient(Dpi::Canvas &canvas, int x, int y, int height, 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, 1, 0, 0, height, 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); } void underlinedTitleValue(Dpi::Canvas &canvas, const QString &title, const QString &value, int x, int y, int width) { dLine(canvas, x, x + width, y + 19, MainTexture->colorBevelLight, MainTexture->colorBevelShade); risedTextOut(canvas, x, y, title); risedTextOut(canvas, x + width - biColorTextWidth(canvas, value), y, value); } void paintLogo(Dpi::Canvas &canvas, int x, int y, QColor lightColor, QColor shadeColor) { GraphicSetItem *logo = Templates ? Templates->searchByName(QStringLiteral("Logo")) : nullptr; if (!logo) return; drawBufferEnsureSize(logo->width, logo->height); drawBuffer.canvas().begin(); drawBuffer.canvas().copyRect(QRect(0, 0, logo->width, logo->height), canvas, QRect(x, y, logo->width, logo->height)); drawBuffer.canvas().end(); imageOpBCC(drawBuffer, Templates->data, 0, 0, logo->left, logo->top, logo->width, logo->height, colorToDelphi(lightColor), colorToDelphi(shadeColor)); canvas.copyRect(QRect(x, y, logo->width, logo->height), drawBuffer.canvas(), QRect(0, 0, logo->width, logo->height)); } void initCityMark(const Texture &texture) { if (!HGrSystem) return; const GraphicSetItem *mark1 = HGrSystem->searchByName(QStringLiteral("CityMark1")); const GraphicSetItem *mark2 = HGrSystem->searchByName(QStringLiteral("CityMark2")); if (!mark1 || !mark2) return; QImage &data = HGrSystem->data.image(); QImage &mask = HGrSystem->mask.image(); const QColor color = texture.colorMark; for (int x = 0; x < mark1->width; ++x) { for (int y = 0; y < mark1->height; ++y) { if ((mask.pixel(mark1->left + x, mark1->top + y) & 0xFFFFFFu) != 0) continue; const int intensity = qRed(data.pixel(mark1->left + x, mark1->top + y)); data.setPixel(mark2->left + x, mark2->top + y, qRgb(color.red() * intensity / 0xFF, color.green() * intensity / 0xFF, color.blue() * intensity / 0xFF)); } } // (the original copies mark1.Width rows here, which is square anyway) bitBltBitmap(HGrSystem->mask, mark2->left, mark2->top, mark1->width, mark1->width, HGrSystem->mask, mark1->left, mark1->top); } void darkenImage(Dpi::Bitmap &bitmap, int change) { QImage &image = bitmap.image(); for (int y = 0; y < image.height(); ++y) { for (int x = 0; x < image.width(); ++x) { const QRgb c = image.pixel(x, y); image.setPixel(x, y, qRgba(std::max(qRed(c) - change, 0), std::max(qGreen(c) - change, 0), std::max(qBlue(c) - change, 0), qAlpha(c))); } } } namespace { bool ornamentInitialized = false; } void initOrnament() { GraphicSetItem *ornament = HGrSystem2 ? HGrSystem2->searchByName(QStringLiteral("Ornament")) : nullptr; if (ornamentInitialized || !ornament || !MainTexture) return; const QColor light = MainTexture->colorBevelLight; const auto mix = [](int shade, int lightValue) { return ((shade & 0xFC) >> 2) * 3 + ((lightValue & 0xFC) >> 2); }; const QColor shade(mix(MainTexture->colorBevelShade.red(), light.red()), mix(MainTexture->colorBevelShade.green(), light.green()), mix(MainTexture->colorBevelShade.blue(), light.blue())); QImage &image = HGrSystem2->data.image(); const int left = Dpi::scaleToNative(ornament->left); const int top = Dpi::scaleToNative(ornament->top); for (int y = top; y < top + Dpi::scaleToNative(ornament->height) && y < image.height(); ++y) { for (int x = left; x < left + Dpi::scaleToNative(ornament->width) && x < image.width(); ++x) { const QRgb pixel = image.pixel(x, y) & 0xFFFFFFu; if (pixel == 0xFF0000u) // pure red marks the light part image.setPixelColor(x, y, light); else if (pixel == 0x0000FFu) // pure blue marks the shade part image.setPixelColor(x, y, shade); } } ornamentInitialized = true; } } // namespace Cevo