|
@@ -14,6 +14,7 @@
|
|
|
#include "SDL_Extensions.h"
|
|
|
|
|
|
#include "../render/ColorFilter.h"
|
|
|
+#include "../render/Colors.h"
|
|
|
#include "../render/CBitmapHandler.h"
|
|
|
#include "../render/CDefFile.h"
|
|
|
#include "../render/Graphics.h"
|
|
@@ -22,6 +23,59 @@
|
|
|
|
|
|
class SDLImageLoader;
|
|
|
|
|
|
+//First 8 colors in def palette used for transparency
|
|
|
+static const SDL_Color sourcePalette[8] = {
|
|
|
+ {0, 255, 255, SDL_ALPHA_OPAQUE},
|
|
|
+ {255, 150, 255, SDL_ALPHA_OPAQUE},
|
|
|
+ {255, 100, 255, SDL_ALPHA_OPAQUE},
|
|
|
+ {255, 50, 255, SDL_ALPHA_OPAQUE},
|
|
|
+ {255, 0, 255, SDL_ALPHA_OPAQUE},
|
|
|
+ {255, 255, 0, SDL_ALPHA_OPAQUE},
|
|
|
+ {180, 0, 255, SDL_ALPHA_OPAQUE},
|
|
|
+ {0, 255, 0, SDL_ALPHA_OPAQUE}
|
|
|
+};
|
|
|
+
|
|
|
+static const ColorRGBA targetPalette[8] = {
|
|
|
+ {0, 0, 0, 0 }, // 0 - transparency ( used in most images )
|
|
|
+ {0, 0, 0, 64 }, // 1 - shadow border ( used in battle, adventure map def's )
|
|
|
+ {0, 0, 0, 64 }, // 2 - shadow border ( used in fog-of-war def's )
|
|
|
+ {0, 0, 0, 128}, // 3 - shadow body ( used in fog-of-war def's )
|
|
|
+ {0, 0, 0, 128}, // 4 - shadow body ( used in battle, adventure map def's )
|
|
|
+ {0, 0, 0, 0 }, // 5 - selection / owner flag ( used in battle, adventure map def's )
|
|
|
+ {0, 0, 0, 128}, // 6 - shadow body below selection ( used in battle def's )
|
|
|
+ {0, 0, 0, 64 } // 7 - shadow border below selection ( used in battle def's )
|
|
|
+};
|
|
|
+
|
|
|
+static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2)
|
|
|
+{
|
|
|
+ return c1*a1 / 256 + c2*a2*(255 - a1) / 256 / 256;
|
|
|
+}
|
|
|
+
|
|
|
+static ColorRGBA addColors(const ColorRGBA & base, const ColorRGBA & over)
|
|
|
+{
|
|
|
+ return ColorRGBA(
|
|
|
+ mixChannels(over.r, base.r, over.a, base.a),
|
|
|
+ mixChannels(over.g, base.g, over.a, base.a),
|
|
|
+ mixChannels(over.b, base.b, over.a, base.a),
|
|
|
+ ui8(over.a + base.a * (255 - over.a) / 256)
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+static bool colorsSimilar (const SDL_Color & lhs, const SDL_Color & rhs)
|
|
|
+{
|
|
|
+ // it seems that H3 does not requires exact match to replace colors -> (255, 103, 255) gets interpreted as shadow
|
|
|
+ // exact logic is not clear and requires extensive testing with image editing
|
|
|
+ // potential reason is that H3 uses 16-bit color format (565 RGB bits), meaning that 3 least significant bits are lost in red and blue component
|
|
|
+ static const int threshold = 8;
|
|
|
+
|
|
|
+ int diffR = static_cast<int>(lhs.r) - rhs.r;
|
|
|
+ int diffG = static_cast<int>(lhs.g) - rhs.g;
|
|
|
+ int diffB = static_cast<int>(lhs.b) - rhs.b;
|
|
|
+ int diffA = static_cast<int>(lhs.a) - rhs.a;
|
|
|
+
|
|
|
+ return std::abs(diffR) < threshold && std::abs(diffG) < threshold && std::abs(diffB) < threshold && std::abs(diffA) < threshold;
|
|
|
+}
|
|
|
+
|
|
|
int IImage::width() const
|
|
|
{
|
|
|
return dimensions().x;
|
|
@@ -83,7 +137,7 @@ SDLImageShared::SDLImageShared(const ImagePath & filename)
|
|
|
}
|
|
|
|
|
|
|
|
|
-void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, uint8_t alpha, EImageBlitMode mode) const
|
|
|
+void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const
|
|
|
{
|
|
|
if (!surf)
|
|
|
return;
|
|
@@ -109,6 +163,7 @@ void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Poin
|
|
|
|
|
|
destShift += dest;
|
|
|
|
|
|
+ SDL_SetSurfaceColorMod(surf, colorMultiplier.r, colorMultiplier.g, colorMultiplier.b);
|
|
|
SDL_SetSurfaceAlphaMod(surf, alpha);
|
|
|
|
|
|
if (alpha != SDL_ALPHA_OPAQUE || (mode != EImageBlitMode::OPAQUE && surf->format->Amask != 0))
|
|
@@ -127,21 +182,19 @@ void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Poin
|
|
|
{
|
|
|
CSDL_Ext::blitSurface(surf, sourceRect, where, destShift);
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-const SDL_Palette * SDLImageShared::getPalette() const
|
|
|
-{
|
|
|
- if (originalPalette == nullptr)
|
|
|
- throw std::runtime_error("Palette not found!");
|
|
|
-
|
|
|
- return originalPalette;
|
|
|
+ if (surf->format->palette)
|
|
|
+ SDL_SetSurfacePalette(surf, originalPalette);
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<SDLImageShared> SDLImageShared::scaleFast(const Point & size) const
|
|
|
+std::shared_ptr<ISharedImage> SDLImageShared::scaleFast(const Point & size, SDL_Palette * palette) const
|
|
|
{
|
|
|
float scaleX = float(size.x) / dimensions().x;
|
|
|
float scaleY = float(size.y) / dimensions().y;
|
|
|
|
|
|
+ if (palette && surf->format->palette)
|
|
|
+ SDL_SetSurfacePalette(surf, palette);
|
|
|
+
|
|
|
auto scaled = CSDL_Ext::scaleSurface(surf, (int)(surf->w * scaleX), (int)(surf->h * scaleY));
|
|
|
|
|
|
if (scaled->format && scaled->format->palette) // fix color keying, because SDL loses it at this point
|
|
@@ -162,6 +215,9 @@ std::shared_ptr<SDLImageShared> SDLImageShared::scaleFast(const Point & size) co
|
|
|
// erase our own reference
|
|
|
SDL_FreeSurface(scaled);
|
|
|
|
|
|
+ if (surf->format->palette)
|
|
|
+ SDL_SetSurfacePalette(surf, originalPalette);
|
|
|
+
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -175,12 +231,6 @@ void SDLImageIndexed::playerColored(PlayerColor player)
|
|
|
graphics->setPlayerPalette(currentPalette, player);
|
|
|
}
|
|
|
|
|
|
-void SDLImageIndexed::setFlagColor(PlayerColor player)
|
|
|
-{
|
|
|
- if(player.isValidPlayer() || player==PlayerColor::NEUTRAL)
|
|
|
- graphics->setPlayerFlagColor(currentPalette, player);
|
|
|
-}
|
|
|
-
|
|
|
bool SDLImageShared::isTransparent(const Point & coords) const
|
|
|
{
|
|
|
if (surf)
|
|
@@ -197,7 +247,7 @@ Point SDLImageShared::dimensions() const
|
|
|
std::shared_ptr<IImage> SDLImageShared::createImageReference(EImageBlitMode mode)
|
|
|
{
|
|
|
if (surf && surf->format->palette)
|
|
|
- return std::make_shared<SDLImageIndexed>(shared_from_this(), mode);
|
|
|
+ return std::make_shared<SDLImageIndexed>(shared_from_this(), originalPalette, mode);
|
|
|
else
|
|
|
return std::make_shared<SDLImageRGB>(shared_from_this(), mode);
|
|
|
}
|
|
@@ -241,8 +291,6 @@ void SDLImageShared::savePalette()
|
|
|
|
|
|
void SDLImageIndexed::shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove)
|
|
|
{
|
|
|
- const SDL_Palette * originalPalette = image->getPalette();
|
|
|
-
|
|
|
std::vector<SDL_Color> shifterColors(colorsToMove);
|
|
|
|
|
|
for(uint32_t i=0; i<colorsToMove; ++i)
|
|
@@ -253,11 +301,12 @@ void SDLImageIndexed::shiftPalette(uint32_t firstColorID, uint32_t colorsToMove,
|
|
|
|
|
|
void SDLImageIndexed::adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask)
|
|
|
{
|
|
|
- const SDL_Palette * originalPalette = image->getPalette();
|
|
|
-
|
|
|
// Note: here we skip first colors in the palette that are predefined in H3 images
|
|
|
for(int i = 0; i < currentPalette->ncolors; i++)
|
|
|
{
|
|
|
+ if (i < std::size(sourcePalette) && colorsSimilar(sourcePalette[i], originalPalette->colors[i]))
|
|
|
+ continue;
|
|
|
+
|
|
|
if(i < std::numeric_limits<uint32_t>::digits && ((colorsToSkipMask >> i) & 1) == 1)
|
|
|
continue;
|
|
|
|
|
@@ -265,13 +314,16 @@ void SDLImageIndexed::adjustPalette(const ColorFilter & shifter, uint32_t colors
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-SDLImageIndexed::SDLImageIndexed(const std::shared_ptr<SDLImageShared> & image, EImageBlitMode mode)
|
|
|
+SDLImageIndexed::SDLImageIndexed(const std::shared_ptr<ISharedImage> & image, SDL_Palette * originalPalette, EImageBlitMode mode)
|
|
|
:SDLImageBase::SDLImageBase(image, mode)
|
|
|
+ ,originalPalette(originalPalette)
|
|
|
{
|
|
|
- auto originalPalette = image->getPalette();
|
|
|
|
|
|
currentPalette = SDL_AllocPalette(originalPalette->ncolors);
|
|
|
SDL_SetPaletteColors(currentPalette, originalPalette->colors, 0, originalPalette->ncolors);
|
|
|
+
|
|
|
+ setOverlayColor(Colors::TRANSPARENCY);
|
|
|
+ setShadowTransparency(0);
|
|
|
}
|
|
|
|
|
|
SDLImageIndexed::~SDLImageIndexed()
|
|
@@ -279,42 +331,96 @@ SDLImageIndexed::~SDLImageIndexed()
|
|
|
SDL_FreePalette(currentPalette);
|
|
|
}
|
|
|
|
|
|
-void SDLImageIndexed::setSpecialPalette(const IImage::SpecialPalette & specialPalette, uint32_t colorsToSkipMask)
|
|
|
+void SDLImageIndexed::setShadowTransparency(float factor)
|
|
|
{
|
|
|
- size_t last = std::min<size_t>(specialPalette.size(), currentPalette->ncolors);
|
|
|
+ ColorRGBA shadow50(0, 0, 0, 128 * factor);
|
|
|
+ ColorRGBA shadow25(0, 0, 0, 64 * factor);
|
|
|
+
|
|
|
+ // seems to be used unconditionally
|
|
|
+ currentPalette->colors[1] = CSDL_Ext::toSDL(shadow25);
|
|
|
+ currentPalette->colors[4] = CSDL_Ext::toSDL(shadow50);
|
|
|
+
|
|
|
+ // seems to be used only if color matches
|
|
|
+ if (colorsSimilar(originalPalette->colors[2], sourcePalette[2]))
|
|
|
+ currentPalette->colors[2] = CSDL_Ext::toSDL(shadow25);
|
|
|
|
|
|
- for (size_t i = 0; i < last; ++i)
|
|
|
+ if (colorsSimilar(originalPalette->colors[3], sourcePalette[3]))
|
|
|
+ currentPalette->colors[3] = CSDL_Ext::toSDL(shadow50);
|
|
|
+}
|
|
|
+
|
|
|
+void SDLImageIndexed::setOverlayColor(const ColorRGBA & color)
|
|
|
+{
|
|
|
+ for (int i : {5,6,7})
|
|
|
{
|
|
|
- if(i < std::numeric_limits<uint32_t>::digits && ((colorsToSkipMask >> i) & 1) == 1)
|
|
|
- currentPalette->colors[i] = CSDL_Ext::toSDL(specialPalette[i]);
|
|
|
+ if (colorsSimilar(originalPalette->colors[i], sourcePalette[i]))
|
|
|
+ currentPalette->colors[i] = CSDL_Ext::toSDL(addColors(targetPalette[i], color));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void SDLImageIndexed::setShadowEnabled(bool on)
|
|
|
+{
|
|
|
+ if (on)
|
|
|
+ setShadowTransparency(1.0);
|
|
|
+ else
|
|
|
+ setShadowTransparency(0);
|
|
|
+
|
|
|
+ shadowEnabled = on;
|
|
|
+}
|
|
|
+
|
|
|
+void SDLImageIndexed::setBodyEnabled(bool on)
|
|
|
+{
|
|
|
+ if (on)
|
|
|
+ adjustPalette(ColorFilter::genEmptyShifter(), 0);
|
|
|
+ else
|
|
|
+ adjustPalette(ColorFilter::genAlphaShifter(0), 0);
|
|
|
+
|
|
|
+ bodyEnabled = on;
|
|
|
+}
|
|
|
+
|
|
|
+void SDLImageIndexed::setOverlayEnabled(bool on)
|
|
|
+{
|
|
|
+ if (on)
|
|
|
+ setOverlayColor(Colors::WHITE_TRUE);
|
|
|
+ else
|
|
|
+ setOverlayColor(Colors::TRANSPARENCY);
|
|
|
+ overlayEnabled = on;
|
|
|
+}
|
|
|
+
|
|
|
SDLImageShared::~SDLImageShared()
|
|
|
{
|
|
|
SDL_FreeSurface(surf);
|
|
|
SDL_FreePalette(originalPalette);
|
|
|
}
|
|
|
|
|
|
-SDLImageBase::SDLImageBase(const std::shared_ptr<SDLImageShared> & image, EImageBlitMode mode)
|
|
|
+SDLImageBase::SDLImageBase(const std::shared_ptr<ISharedImage> & image, EImageBlitMode mode)
|
|
|
:image(image)
|
|
|
, alphaValue(SDL_ALPHA_OPAQUE)
|
|
|
, blitMode(mode)
|
|
|
{}
|
|
|
|
|
|
+std::shared_ptr<ISharedImage> SDLImageBase::getSharedImage() const
|
|
|
+{
|
|
|
+ return image;
|
|
|
+}
|
|
|
+
|
|
|
void SDLImageRGB::draw(SDL_Surface * where, const Point & pos, const Rect * src) const
|
|
|
{
|
|
|
- image->draw(where, nullptr, pos, src, alphaValue, blitMode);
|
|
|
+ image->draw(where, nullptr, pos, src, Colors::WHITE_TRUE, alphaValue, blitMode);
|
|
|
}
|
|
|
|
|
|
void SDLImageIndexed::draw(SDL_Surface * where, const Point & pos, const Rect * src) const
|
|
|
{
|
|
|
- image->draw(where, currentPalette, pos, src, alphaValue, blitMode);
|
|
|
+ image->draw(where, currentPalette, pos, src, Colors::WHITE_TRUE, alphaValue, blitMode);
|
|
|
+}
|
|
|
+
|
|
|
+void SDLImageIndexed::scaleFast(const Point & size)
|
|
|
+{
|
|
|
+ image = image->scaleFast(size, currentPalette);
|
|
|
}
|
|
|
|
|
|
-void SDLImageBase::scaleFast(const Point & size)
|
|
|
+void SDLImageRGB::scaleFast(const Point & size)
|
|
|
{
|
|
|
- image = image->scaleFast(size);
|
|
|
+ image = image->scaleFast(size, nullptr);
|
|
|
}
|
|
|
|
|
|
void SDLImageBase::exportBitmap(const boost::filesystem::path & path) const
|
|
@@ -342,13 +448,25 @@ void SDLImageBase::setBlitMode(EImageBlitMode mode)
|
|
|
blitMode = mode;
|
|
|
}
|
|
|
|
|
|
-void SDLImageRGB::setSpecialPalette(const SpecialPalette & SpecialPalette, uint32_t colorsToSkipMask)
|
|
|
-{}
|
|
|
+void SDLImageRGB::setShadowEnabled(bool on)
|
|
|
+{
|
|
|
+ // Not supported. Theoretically we can try to extract all pixels of specific colors, but better to use 8-bit images or composite images
|
|
|
+}
|
|
|
|
|
|
-void SDLImageRGB::playerColored(PlayerColor player)
|
|
|
+void SDLImageRGB::setBodyEnabled(bool on)
|
|
|
+{
|
|
|
+ // Not supported. Theoretically we can try to extract all pixels of specific colors, but better to use 8-bit images or composite images
|
|
|
+}
|
|
|
+
|
|
|
+void SDLImageRGB::setOverlayEnabled(bool on)
|
|
|
+{
|
|
|
+ // Not supported. Theoretically we can try to extract all pixels of specific colors, but better to use 8-bit images or composite images
|
|
|
+}
|
|
|
+
|
|
|
+void SDLImageRGB::setOverlayColor(const ColorRGBA & color)
|
|
|
{}
|
|
|
|
|
|
-void SDLImageRGB::setFlagColor(PlayerColor player)
|
|
|
+void SDLImageRGB::playerColored(PlayerColor player)
|
|
|
{}
|
|
|
|
|
|
void SDLImageRGB::shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove)
|