From ef2e1b468026f3010e8ace29102d0b8177ccb836 Mon Sep 17 00:00:00 2001 From: Geoffray Levasseur Date: Tue, 3 Nov 2026 10:32:22 +0100 Subject: [PATCH] added missing properties --- widget/include/Cevo/CevoGraphicSet.h | 22 +++++ widget/include/Cevo/CevoScreenTools.h | 25 +++++- widget/src/CevoGraphicSet.cpp | 27 ++++++ widget/src/CevoScreenTools.cpp | 116 ++++++++++++++++++++++++++ 4 files changed, 189 insertions(+), 1 deletion(-) diff --git a/widget/include/Cevo/CevoGraphicSet.h b/widget/include/Cevo/CevoGraphicSet.h index e8d2219..f3f4f45 100644 --- a/widget/include/Cevo/CevoGraphicSet.h +++ b/widget/include/Cevo/CevoGraphicSet.h @@ -34,6 +34,10 @@ #include #include +#include +#include +#include + namespace Cevo { class GraphicSet; @@ -67,6 +71,10 @@ public: QString name; Dpi::Bitmap data; Dpi::Bitmap mask; + // Usage count per picture slot (10-column grid, 49px-tall rows) - used + // by Tribes.pas's least-used-picture-first selection heuristic. + std::vector pixUsed; + void resetPixUsed(); // Looks up by index first, falling back to name; nullptr (with a // warning) if neither resolves to an item. @@ -85,4 +93,18 @@ private: constexpr const char *GraphicSetFileRootNode = "GraphicSet"; constexpr const char *GraphicSetFileExt = ".grs"; +// TGraphicSets equivalent: an owning registry of loaded GraphicSets, keyed +// by name (ScreenTools.pas's global `GrExt`). Composition over +// std::vector>, same pattern as Brains. +class GraphicSets { +public: + GraphicSet *searchByName(const QString &name) const; + GraphicSet &addNew(const QString &name); + void resetPixUsed(); + void clear() { m_items.clear(); } + +private: + std::vector> m_items; +}; + } // namespace Cevo diff --git a/widget/include/Cevo/CevoScreenTools.h b/widget/include/Cevo/CevoScreenTools.h index fea0cd3..c22877b 100644 --- a/widget/include/Cevo/CevoScreenTools.h +++ b/widget/include/Cevo/CevoScreenTools.h @@ -28,6 +28,7 @@ #pragma once +#include "Cevo/CevoGraphicSet.h" #include "Dpi/DpiGraphics.h" #include @@ -39,7 +40,6 @@ namespace Cevo { class Texture; -class GraphicSet; class GraphicSetItem; // Delphi/VCL TColor ints are packed as $00BBGGRR (red in the low byte, the @@ -48,6 +48,29 @@ class GraphicSetItem; // constructing QColor from the raw hex value directly. QColor colorFromDelphi(int value); +// Global display gamma correction setting, in percent (100 = no +// correction) - ScreenTools.pas's `Gamma`. Call applyGammaToBitmap() +// (automatically done by loadGraphicSet()) after changing it. +extern int gamma; +void applyGammaToBitmap(Dpi::Bitmap &bitmap); +// Parses a "RRGGBB" hex string (leading spaces ignored, missing trailing +// digits treated as 0) into a gamma-corrected QColor. +QColor hexStringToColor(const QString &text); + +// ScreenTools.pas's global `GrExt` registry of loaded GraphicSets. +extern GraphicSets grExt; +// Directory loadGraphicSet() resolves names against - the application +// layer (game/'s Global::getGraphicsDir()) must set this at startup; +// widget/ has no Directories.pas port of its own to call directly. +extern QString graphicsDir; +// Equivalent of ScreenTools.pas's LoadGraphicSet(): loads (or returns the +// already-cached) named .png from getGraphicsDir(), building its +// transparency mask (magenta/half-magenta color-keyed, matching the +// original exactly since sprite() still does raster-op AND/OR +// compositing against data/mask, not alpha blending) and companion .grs +// item metadata if present. Returns nullptr if the file doesn't exist. +GraphicSet *loadGraphicSet(const QString &name, bool transparency = true); + enum class FontType { Normal, Small, Tiny, Caption, Button, Count }; // Fonts, the main background texture and the system sprite sheets are diff --git a/widget/src/CevoGraphicSet.cpp b/widget/src/CevoGraphicSet.cpp index 5719f11..6ff5115 100644 --- a/widget/src/CevoGraphicSet.cpp +++ b/widget/src/CevoGraphicSet.cpp @@ -122,4 +122,31 @@ void GraphicSet::saveToFile(const QString &fileName) const xml.writeEndDocument(); } +void GraphicSet::resetPixUsed() +{ + pixUsed.assign(static_cast(data.height() / 49 * 10), 0); +} + +GraphicSet *GraphicSets::searchByName(const QString &name) const +{ + for (const auto &item : m_items) + if (item->name == name) + return item.get(); + return nullptr; +} + +GraphicSet &GraphicSets::addNew(const QString &name) +{ + auto newSet = std::make_unique(); + newSet->name = name; + m_items.push_back(std::move(newSet)); + return *m_items.back(); +} + +void GraphicSets::resetPixUsed() +{ + for (const auto &item : m_items) + item->resetPixUsed(); +} + } // namespace Cevo diff --git a/widget/src/CevoScreenTools.cpp b/widget/src/CevoScreenTools.cpp index 3b9a3c1..c9a32f1 100644 --- a/widget/src/CevoScreenTools.cpp +++ b/widget/src/CevoScreenTools.cpp @@ -31,7 +31,10 @@ #include "Cevo/CevoTexture.h" #include "Dpi/DpiPixelPointer.h" +#include + #include +#include #include namespace Cevo { @@ -48,6 +51,119 @@ QColor colorFromDelphi(int value) return QColor(value & 0xFF, (value >> 8) & 0xFF, (value >> 16) & 0xFF); } +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 fileName = graphicsDir + QLatin1Char('/') + name; + // Do not apply gamma during file load as it would affect transparency colors too. + result->data.loadFromFile(fileName); + if (result->data.width() == 0 || result->data.height() == 0) + return nullptr; + + const QString grsFileName = QFileInfo(fileName).path() + QLatin1Char('/') + + QFileInfo(fileName).completeBaseName() + QLatin1String(GraphicSetFileExt); + if (QFileInfo::exists(grsFileName)) + result->loadFromFile(grsFileName); + + result->resetPixUsed(); + + if (transparency) { + constexpr QRgb TransparentColor1 = 0xFF00FF; + constexpr QRgb TransparentColor2 = 0x7F007F; + 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) {