initial port for Cevo game in C++ (libs)

This commit is contained in:
Geoffray Levasseur committed 2026-10-22 17:20:45 +02:00
commit 8436242032
375 files changed
+46282

No files matched your search

+330
View File
@@ -0,0 +1,330 @@
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoGraphicSet.h"
#include "Cevo/CevoTexture.h"
#include "Dpi/DpiPixelPointer.h"
#include <algorithm>
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;
QColor colorFromDelphi(int value)
{
return QColor(value & 0xFF, (value >> 8) & 0xFF, (value >> 16) & 0xFF);
}
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 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 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 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 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);
}
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();
}
}
} // namespace Cevo