|
@@ -89,11 +89,12 @@ int IImage::height() const
|
|
|
return dimensions().y;
|
|
|
}
|
|
|
|
|
|
-SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group)
|
|
|
+SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group, int preScaleFactor)
|
|
|
: surf(nullptr),
|
|
|
margins(0, 0),
|
|
|
fullSize(0, 0),
|
|
|
- originalPalette(nullptr)
|
|
|
+ originalPalette(nullptr),
|
|
|
+ preScaleFactor(preScaleFactor)
|
|
|
{
|
|
|
SDLImageLoader loader(this);
|
|
|
data->loadFrame(frame, group, loader);
|
|
@@ -101,11 +102,12 @@ SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group
|
|
|
savePalette();
|
|
|
}
|
|
|
|
|
|
-SDLImageShared::SDLImageShared(SDL_Surface * from)
|
|
|
+SDLImageShared::SDLImageShared(SDL_Surface * from, int preScaleFactor)
|
|
|
: surf(nullptr),
|
|
|
margins(0, 0),
|
|
|
fullSize(0, 0),
|
|
|
- originalPalette(nullptr)
|
|
|
+ originalPalette(nullptr),
|
|
|
+ preScaleFactor(preScaleFactor)
|
|
|
{
|
|
|
surf = from;
|
|
|
if (surf == nullptr)
|
|
@@ -118,11 +120,12 @@ SDLImageShared::SDLImageShared(SDL_Surface * from)
|
|
|
fullSize.y = surf->h;
|
|
|
}
|
|
|
|
|
|
-SDLImageShared::SDLImageShared(const ImagePath & filename)
|
|
|
+SDLImageShared::SDLImageShared(const ImagePath & filename, int preScaleFactor)
|
|
|
: surf(nullptr),
|
|
|
margins(0, 0),
|
|
|
fullSize(0, 0),
|
|
|
- originalPalette(nullptr)
|
|
|
+ originalPalette(nullptr),
|
|
|
+ preScaleFactor(preScaleFactor)
|
|
|
{
|
|
|
surf = BitmapHandler::loadBitmap(filename);
|
|
|
|
|
@@ -136,6 +139,8 @@ SDLImageShared::SDLImageShared(const ImagePath & filename)
|
|
|
savePalette();
|
|
|
fullSize.x = surf->w;
|
|
|
fullSize.y = surf->h;
|
|
|
+
|
|
|
+ optimizeSurface();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -177,7 +182,7 @@ void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Poin
|
|
|
if (palette && surf->format->palette)
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
|
|
|
|
|
- if(surf->format->palette && mode == EImageBlitMode::ALPHA)
|
|
|
+ if(surf->format->palette && mode != EImageBlitMode::OPAQUE && mode != EImageBlitMode::COLORKEY)
|
|
|
{
|
|
|
CSDL_Ext::blit8bppAlphaTo24bpp(surf, sourceRect, where, destShift, alpha);
|
|
|
}
|
|
@@ -258,6 +263,13 @@ void SDLImageShared::optimizeSurface()
|
|
|
SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE);
|
|
|
SDL_BlitSurface(surf, &rectSDL, newSurface, nullptr);
|
|
|
|
|
|
+ if (SDL_HasColorKey(surf))
|
|
|
+ {
|
|
|
+ uint32_t colorKey;
|
|
|
+ SDL_GetColorKey(surf, &colorKey);
|
|
|
+ SDL_SetColorKey(newSurface, SDL_TRUE, colorKey);
|
|
|
+ }
|
|
|
+
|
|
|
SDL_FreeSurface(surf);
|
|
|
surf = newSurface;
|
|
|
|
|
@@ -266,7 +278,7 @@ void SDLImageShared::optimizeSurface()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palette * palette) const
|
|
|
+std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palette * palette) const
|
|
|
{
|
|
|
if (factor <= 0)
|
|
|
throw std::runtime_error("Unable to scale by integer value of " + std::to_string(factor));
|
|
@@ -274,9 +286,15 @@ std::shared_ptr<ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palet
|
|
|
if (palette && surf && surf->format->palette)
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
|
|
|
|
|
- SDL_Surface * scaled = CSDL_Ext::scaleSurfaceIntegerFactor(surf, factor, EScalingAlgorithm::XBRZ);
|
|
|
+ SDL_Surface * scaled = nullptr;
|
|
|
+ if(preScaleFactor == factor)
|
|
|
+ return shared_from_this();
|
|
|
+ else if(preScaleFactor == 1)
|
|
|
+ scaled = CSDL_Ext::scaleSurfaceIntegerFactor(surf, factor, EScalingAlgorithm::XBRZ);
|
|
|
+ else
|
|
|
+ scaled = CSDL_Ext::scaleSurface(surf, (surf->w / preScaleFactor) * factor, (surf->h / preScaleFactor) * factor);
|
|
|
|
|
|
- auto ret = std::make_shared<SDLImageShared>(scaled);
|
|
|
+ auto ret = std::make_shared<SDLImageShared>(scaled, preScaleFactor);
|
|
|
|
|
|
ret->fullSize.x = fullSize.x * factor;
|
|
|
ret->fullSize.y = fullSize.y * factor;
|
|
@@ -294,10 +312,10 @@ std::shared_ptr<ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palet
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<ISharedImage> SDLImageShared::scaleTo(const Point & size, SDL_Palette * palette) const
|
|
|
+std::shared_ptr<const ISharedImage> SDLImageShared::scaleTo(const Point & size, SDL_Palette * palette) const
|
|
|
{
|
|
|
- float scaleX = float(size.x) / dimensions().x;
|
|
|
- float scaleY = float(size.y) / dimensions().y;
|
|
|
+ float scaleX = float(size.x) / fullSize.x;
|
|
|
+ float scaleY = float(size.y) / fullSize.y;
|
|
|
|
|
|
if (palette && surf->format->palette)
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
|
@@ -311,7 +329,7 @@ std::shared_ptr<ISharedImage> SDLImageShared::scaleTo(const Point & size, SDL_Pa
|
|
|
else
|
|
|
CSDL_Ext::setDefaultColorKey(scaled);//just in case
|
|
|
|
|
|
- auto ret = std::make_shared<SDLImageShared>(scaled);
|
|
|
+ auto ret = std::make_shared<SDLImageShared>(scaled, preScaleFactor);
|
|
|
|
|
|
ret->fullSize.x = (int) round((float)fullSize.x * scaleX);
|
|
|
ret->fullSize.y = (int) round((float)fullSize.y * scaleY);
|
|
@@ -348,17 +366,17 @@ void SDLImageIndexed::playerColored(PlayerColor player)
|
|
|
bool SDLImageShared::isTransparent(const Point & coords) const
|
|
|
{
|
|
|
if (surf)
|
|
|
- return CSDL_Ext::isTransparent(surf, coords.x, coords.y);
|
|
|
+ return CSDL_Ext::isTransparent(surf, coords.x - margins.x, coords.y - margins.y);
|
|
|
else
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
Point SDLImageShared::dimensions() const
|
|
|
{
|
|
|
- return fullSize;
|
|
|
+ return fullSize / preScaleFactor;
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<IImage> SDLImageShared::createImageReference(EImageBlitMode mode)
|
|
|
+std::shared_ptr<IImage> SDLImageShared::createImageReference(EImageBlitMode mode) const
|
|
|
{
|
|
|
if (surf && surf->format->palette)
|
|
|
return std::make_shared<SDLImageIndexed>(shared_from_this(), originalPalette, mode);
|
|
@@ -366,10 +384,10 @@ std::shared_ptr<IImage> SDLImageShared::createImageReference(EImageBlitMode mode
|
|
|
return std::make_shared<SDLImageRGB>(shared_from_this(), mode);
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<ISharedImage> SDLImageShared::horizontalFlip() const
|
|
|
+std::shared_ptr<const ISharedImage> SDLImageShared::horizontalFlip() const
|
|
|
{
|
|
|
SDL_Surface * flipped = CSDL_Ext::horizontalFlip(surf);
|
|
|
- auto ret = std::make_shared<SDLImageShared>(flipped);
|
|
|
+ auto ret = std::make_shared<SDLImageShared>(flipped, preScaleFactor);
|
|
|
ret->fullSize = fullSize;
|
|
|
ret->margins.x = margins.x;
|
|
|
ret->margins.y = fullSize.y - surf->h - margins.y;
|
|
@@ -378,10 +396,10 @@ std::shared_ptr<ISharedImage> SDLImageShared::horizontalFlip() const
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<ISharedImage> SDLImageShared::verticalFlip() const
|
|
|
+std::shared_ptr<const ISharedImage> SDLImageShared::verticalFlip() const
|
|
|
{
|
|
|
SDL_Surface * flipped = CSDL_Ext::verticalFlip(surf);
|
|
|
- auto ret = std::make_shared<SDLImageShared>(flipped);
|
|
|
+ auto ret = std::make_shared<SDLImageShared>(flipped, preScaleFactor);
|
|
|
ret->fullSize = fullSize;
|
|
|
ret->margins.x = fullSize.x - surf->w - margins.x;
|
|
|
ret->margins.y = margins.y;
|
|
@@ -416,7 +434,7 @@ void SDLImageIndexed::shiftPalette(uint32_t firstColorID, uint32_t colorsToMove,
|
|
|
void SDLImageIndexed::adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask)
|
|
|
{
|
|
|
// If shadow is enabled, following colors must be skipped unconditionally
|
|
|
- if (shadowEnabled)
|
|
|
+ if (blitMode == EImageBlitMode::WITH_SHADOW || blitMode == EImageBlitMode::WITH_SHADOW_AND_OVERLAY)
|
|
|
colorsToSkipMask |= (1 << 0) + (1 << 1) + (1 << 4);
|
|
|
|
|
|
// Note: here we skip first colors in the palette that are predefined in H3 images
|
|
@@ -432,19 +450,14 @@ void SDLImageIndexed::adjustPalette(const ColorFilter & shifter, uint32_t colors
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-SDLImageIndexed::SDLImageIndexed(const std::shared_ptr<ISharedImage> & image, SDL_Palette * originalPalette, EImageBlitMode mode)
|
|
|
+SDLImageIndexed::SDLImageIndexed(const std::shared_ptr<const ISharedImage> & image, SDL_Palette * originalPalette, EImageBlitMode mode)
|
|
|
:SDLImageBase::SDLImageBase(image, mode)
|
|
|
,originalPalette(originalPalette)
|
|
|
{
|
|
|
-
|
|
|
currentPalette = SDL_AllocPalette(originalPalette->ncolors);
|
|
|
SDL_SetPaletteColors(currentPalette, originalPalette->colors, 0, originalPalette->ncolors);
|
|
|
|
|
|
- if (mode == EImageBlitMode::ALPHA)
|
|
|
- {
|
|
|
- setOverlayColor(Colors::TRANSPARENCY);
|
|
|
- setShadowTransparency(1.0);
|
|
|
- }
|
|
|
+ preparePalette();
|
|
|
}
|
|
|
|
|
|
SDLImageIndexed::~SDLImageIndexed()
|
|
@@ -491,36 +504,42 @@ void SDLImageIndexed::setOverlayColor(const ColorRGBA & color)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void SDLImageIndexed::setShadowEnabled(bool on)
|
|
|
+void SDLImageIndexed::preparePalette()
|
|
|
{
|
|
|
- if (on)
|
|
|
- setShadowTransparency(1.0);
|
|
|
-
|
|
|
- if (!on && blitMode == EImageBlitMode::ALPHA)
|
|
|
- setShadowTransparency(0.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);
|
|
|
+ switch(blitMode)
|
|
|
+ {
|
|
|
+ case EImageBlitMode::ONLY_SHADOW:
|
|
|
+ case EImageBlitMode::ONLY_OVERLAY:
|
|
|
+ adjustPalette(ColorFilter::genAlphaShifter(0), 0);
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
- if (!on && blitMode == EImageBlitMode::ALPHA)
|
|
|
- setOverlayColor(Colors::TRANSPARENCY);
|
|
|
+ switch(blitMode)
|
|
|
+ {
|
|
|
+ case EImageBlitMode::SIMPLE:
|
|
|
+ case EImageBlitMode::WITH_SHADOW:
|
|
|
+ case EImageBlitMode::ONLY_SHADOW:
|
|
|
+ case EImageBlitMode::WITH_SHADOW_AND_OVERLAY:
|
|
|
+ setShadowTransparency(1.0);
|
|
|
+ break;
|
|
|
+ case EImageBlitMode::ONLY_BODY:
|
|
|
+ case EImageBlitMode::ONLY_BODY_IGNORE_OVERLAY:
|
|
|
+ case EImageBlitMode::ONLY_OVERLAY:
|
|
|
+ setShadowTransparency(0.0);
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
- overlayEnabled = on;
|
|
|
+ switch(blitMode)
|
|
|
+ {
|
|
|
+ case EImageBlitMode::ONLY_OVERLAY:
|
|
|
+ case EImageBlitMode::WITH_SHADOW_AND_OVERLAY:
|
|
|
+ setOverlayColor(Colors::WHITE_TRUE);
|
|
|
+ break;
|
|
|
+ case EImageBlitMode::ONLY_SHADOW:
|
|
|
+ case EImageBlitMode::ONLY_BODY:
|
|
|
+ setOverlayColor(Colors::TRANSPARENCY);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
SDLImageShared::~SDLImageShared()
|
|
@@ -529,13 +548,13 @@ SDLImageShared::~SDLImageShared()
|
|
|
SDL_FreePalette(originalPalette);
|
|
|
}
|
|
|
|
|
|
-SDLImageBase::SDLImageBase(const std::shared_ptr<ISharedImage> & image, EImageBlitMode mode)
|
|
|
+SDLImageBase::SDLImageBase(const std::shared_ptr<const ISharedImage> & image, EImageBlitMode mode)
|
|
|
:image(image)
|
|
|
, alphaValue(SDL_ALPHA_OPAQUE)
|
|
|
, blitMode(mode)
|
|
|
{}
|
|
|
|
|
|
-std::shared_ptr<ISharedImage> SDLImageBase::getSharedImage() const
|
|
|
+std::shared_ptr<const ISharedImage> SDLImageBase::getSharedImage() const
|
|
|
{
|
|
|
return image;
|
|
|
}
|
|
@@ -600,21 +619,6 @@ void SDLImageBase::setBlitMode(EImageBlitMode mode)
|
|
|
blitMode = mode;
|
|
|
}
|
|
|
|
|
|
-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::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)
|
|
|
{}
|
|
|
|