added missing properties
This commit is contained in:
1 parent
63ff80cac9
commit
ef2e1b4680
4 files changed
+189
-1
No files matched your search
@@ -34,6 +34,10 @@
|
||||
#include <QRect>
|
||||
#include <QString>
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
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<std::uint8_t> 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<std::unique_ptr<GraphicSet>>, 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<std::unique_ptr<GraphicSet>> m_items;
|
||||
};
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -28,6 +28,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Cevo/CevoGraphicSet.h"
|
||||
#include "Dpi/DpiGraphics.h"
|
||||
|
||||
#include <QColor>
|
||||
@@ -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
|
||||
|
||||
@@ -122,4 +122,31 @@ void GraphicSet::saveToFile(const QString &fileName) const
|
||||
xml.writeEndDocument();
|
||||
}
|
||||
|
||||
void GraphicSet::resetPixUsed()
|
||||
{
|
||||
pixUsed.assign(static_cast<std::size_t>(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<GraphicSet>();
|
||||
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
|
||||
@@ -31,7 +31,10 @@
|
||||
#include "Cevo/CevoTexture.h"
|
||||
#include "Dpi/DpiPixelPointer.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
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<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 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)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user