2017-07-13 10:26:03 +02:00
|
|
|
/*
|
2023-02-01 16:42:03 +02:00
|
|
|
* SDLImage.cpp, part of VCMI engine
|
2017-07-13 10:26:03 +02:00
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "SDLImage.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "SDLImageLoader.h"
|
2022-09-18 14:47:49 +02:00
|
|
|
#include "SDL_Extensions.h"
|
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../render/ColorFilter.h"
|
|
|
|
#include "../render/CBitmapHandler.h"
|
|
|
|
#include "../render/CDefFile.h"
|
|
|
|
#include "../render/Graphics.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
2023-01-30 00:12:43 +02:00
|
|
|
#include "../../lib/JsonNode.h"
|
2011-10-08 04:23:46 +03:00
|
|
|
|
2023-01-30 19:55:32 +02:00
|
|
|
#include <SDL_surface.h>
|
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
class SDLImageLoader;
|
|
|
|
|
2022-11-25 00:26:14 +02:00
|
|
|
std::shared_ptr<IImage> IImage::createFromFile( const std::string & path )
|
|
|
|
{
|
|
|
|
return std::shared_ptr<IImage>(new SDLImage(path));
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
std::shared_ptr<IImage> IImage::createFromSurface( SDL_Surface * source )
|
|
|
|
{
|
|
|
|
return std::shared_ptr<IImage>(new SDLImage(source, true));
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
IImage::IImage() = default;
|
|
|
|
IImage::~IImage() = default;
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2022-12-11 22:09:57 +02:00
|
|
|
int IImage::width() const
|
|
|
|
{
|
|
|
|
return dimensions().x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int IImage::height() const
|
|
|
|
{
|
|
|
|
return dimensions().y;
|
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2018-07-25 00:36:48 +02:00
|
|
|
SDLImage::SDLImage(CDefFile * data, size_t frame, size_t group)
|
2017-09-08 13:25:12 +02:00
|
|
|
: surf(nullptr),
|
|
|
|
margins(0, 0),
|
2020-01-26 10:01:48 +02:00
|
|
|
fullSize(0, 0),
|
|
|
|
originalPalette(nullptr)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
SDLImageLoader loader(this);
|
|
|
|
data->loadFrame(frame, group, loader);
|
2020-01-25 11:21:26 +02:00
|
|
|
|
|
|
|
savePalette();
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:25:12 +02:00
|
|
|
SDLImage::SDLImage(SDL_Surface * from, bool extraRef)
|
|
|
|
: surf(nullptr),
|
|
|
|
margins(0, 0),
|
2020-01-26 10:01:48 +02:00
|
|
|
fullSize(0, 0),
|
|
|
|
originalPalette(nullptr)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
surf = from;
|
2020-01-25 11:21:26 +02:00
|
|
|
if (surf == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
savePalette();
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
if (extraRef)
|
|
|
|
surf->refcount++;
|
|
|
|
fullSize.x = surf->w;
|
|
|
|
fullSize.y = surf->h;
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:25:12 +02:00
|
|
|
SDLImage::SDLImage(const JsonNode & conf)
|
|
|
|
: surf(nullptr),
|
|
|
|
margins(0, 0),
|
2020-01-26 10:01:48 +02:00
|
|
|
fullSize(0, 0),
|
|
|
|
originalPalette(nullptr)
|
2017-09-08 13:25:12 +02:00
|
|
|
{
|
|
|
|
std::string filename = conf["file"].String();
|
|
|
|
|
|
|
|
surf = BitmapHandler::loadBitmap(filename);
|
|
|
|
|
|
|
|
if(surf == nullptr)
|
|
|
|
return;
|
|
|
|
|
2020-01-25 11:21:26 +02:00
|
|
|
savePalette();
|
|
|
|
|
2017-09-08 13:25:12 +02:00
|
|
|
const JsonNode & jsonMargins = conf["margins"];
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
margins.x = static_cast<int>(jsonMargins["left"].Integer());
|
|
|
|
margins.y = static_cast<int>(jsonMargins["top"].Integer());
|
2017-09-08 13:25:12 +02:00
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
fullSize.x = static_cast<int>(conf["width"].Integer());
|
|
|
|
fullSize.y = static_cast<int>(conf["height"].Integer());
|
2017-09-08 13:25:12 +02:00
|
|
|
|
|
|
|
if(fullSize.x == 0)
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
fullSize.x = margins.x + surf->w + (int)jsonMargins["right"].Integer();
|
2017-09-08 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(fullSize.y == 0)
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
fullSize.y = margins.y + surf->h + (int)jsonMargins["bottom"].Integer();
|
2017-09-08 13:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 00:36:48 +02:00
|
|
|
SDLImage::SDLImage(std::string filename)
|
2017-09-08 13:25:12 +02:00
|
|
|
: surf(nullptr),
|
|
|
|
margins(0, 0),
|
2020-01-26 10:01:48 +02:00
|
|
|
fullSize(0, 0),
|
|
|
|
originalPalette(nullptr)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
2011-07-18 18:21:16 +03:00
|
|
|
surf = BitmapHandler::loadBitmap(filename);
|
2011-05-28 13:05:25 +03:00
|
|
|
|
2018-07-25 00:36:48 +02:00
|
|
|
if(surf == nullptr)
|
2011-05-28 13:05:25 +03:00
|
|
|
{
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->error("Error: failed to load image %s", filename);
|
2013-11-24 14:36:51 +03:00
|
|
|
return;
|
2011-05-28 13:05:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-25 11:21:26 +02:00
|
|
|
savePalette();
|
2011-05-28 13:05:25 +03:00
|
|
|
fullSize.x = surf->w;
|
|
|
|
fullSize.y = surf->h;
|
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:57:42 +02:00
|
|
|
void SDLImage::draw(SDL_Surface *where, int posX, int posY, const Rect *src) const
|
2011-03-15 19:25:51 +02:00
|
|
|
{
|
2017-09-08 13:25:12 +02:00
|
|
|
if(!surf)
|
2011-02-20 11:24:53 +02:00
|
|
|
return;
|
2016-10-18 09:31:31 +02:00
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
Rect destRect(posX, posY, surf->w, surf->h);
|
2023-01-11 20:57:42 +02:00
|
|
|
draw(where, &destRect, src);
|
2016-11-07 23:19:53 +02:00
|
|
|
}
|
|
|
|
|
2023-01-17 22:01:35 +02:00
|
|
|
void SDLImage::draw(SDL_Surface* where, const Rect * dest, const Rect* src) const
|
2016-11-07 23:19:53 +02:00
|
|
|
{
|
|
|
|
if (!surf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Rect sourceRect(0, 0, surf->w, surf->h);
|
|
|
|
|
2016-11-08 21:14:38 +02:00
|
|
|
Point destShift(0, 0);
|
2016-11-07 23:19:53 +02:00
|
|
|
|
|
|
|
if(src)
|
2016-11-04 16:05:56 +02:00
|
|
|
{
|
2016-11-07 23:19:53 +02:00
|
|
|
if(src->x < margins.x)
|
|
|
|
destShift.x += margins.x - src->x;
|
|
|
|
|
|
|
|
if(src->y < margins.y)
|
|
|
|
destShift.y += margins.y - src->y;
|
|
|
|
|
2023-01-17 22:01:35 +02:00
|
|
|
sourceRect = Rect(*src).intersect(Rect(margins.x, margins.y, surf->w, surf->h));
|
2016-11-07 23:19:53 +02:00
|
|
|
|
|
|
|
sourceRect -= margins;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2016-11-07 23:19:53 +02:00
|
|
|
else
|
|
|
|
destShift = margins;
|
2016-10-27 14:07:12 +02:00
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
if(dest)
|
2023-01-17 22:01:35 +02:00
|
|
|
destShift += dest->topLeft();
|
2016-11-07 23:19:53 +02:00
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
uint8_t perSurfaceAlpha;
|
|
|
|
if (SDL_GetSurfaceAlphaMod(surf, &perSurfaceAlpha) != 0)
|
|
|
|
logGlobal->error("SDL_GetSurfaceAlphaMod faied! %s", SDL_GetError());
|
|
|
|
|
|
|
|
if(surf->format->BitsPerPixel == 8 && perSurfaceAlpha == SDL_ALPHA_OPAQUE)
|
2016-10-27 14:07:12 +02:00
|
|
|
{
|
2023-01-17 22:01:35 +02:00
|
|
|
CSDL_Ext::blit8bppAlphaTo24bpp(surf, sourceRect, where, destShift);
|
2016-10-27 14:07:12 +02:00
|
|
|
}
|
2016-11-04 16:05:56 +02:00
|
|
|
else
|
2016-11-07 23:15:46 +02:00
|
|
|
{
|
2023-01-17 22:01:35 +02:00
|
|
|
CSDL_Ext::blitSurface(surf, sourceRect, where, destShift);
|
2016-11-07 23:15:46 +02:00
|
|
|
}
|
2016-11-04 16:05:56 +02:00
|
|
|
}
|
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
std::shared_ptr<IImage> SDLImage::scaleFast(const Point & size) const
|
2016-11-07 23:19:53 +02:00
|
|
|
{
|
2023-01-30 17:18:59 +02:00
|
|
|
float scaleX = float(size.x) / width();
|
|
|
|
float scaleY = float(size.y) / height();
|
|
|
|
|
|
|
|
auto scaled = CSDL_Ext::scaleSurfaceFast(surf, (int)(surf->w * scaleX), (int)(surf->h * scaleY));
|
2016-11-07 23:19:53 +02:00
|
|
|
|
|
|
|
if (scaled->format && scaled->format->palette) // fix color keying, because SDL loses it at this point
|
|
|
|
CSDL_Ext::setColorKey(scaled, scaled->format->palette->colors[0]);
|
|
|
|
else if(scaled->format && scaled->format->Amask)
|
|
|
|
SDL_SetSurfaceBlendMode(scaled, SDL_BLENDMODE_BLEND);//just in case
|
|
|
|
else
|
|
|
|
CSDL_Ext::setDefaultColorKey(scaled);//just in case
|
|
|
|
|
2016-11-10 13:38:33 +02:00
|
|
|
SDLImage * ret = new SDLImage(scaled, false);
|
2016-11-08 21:14:38 +02:00
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
ret->fullSize.x = (int) round((float)fullSize.x * scaleX);
|
|
|
|
ret->fullSize.y = (int) round((float)fullSize.y * scaleY);
|
2016-11-08 21:14:38 +02:00
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
ret->margins.x = (int) round((float)margins.x * scaleX);
|
|
|
|
ret->margins.y = (int) round((float)margins.y * scaleY);
|
2016-11-08 21:14:38 +02:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
return std::shared_ptr<IImage>(ret);
|
2016-11-07 23:19:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 14:23:28 +02:00
|
|
|
void SDLImage::exportBitmap(const boost::filesystem::path& path) const
|
|
|
|
{
|
|
|
|
SDL_SaveBMP(surf, path.string().c_str());
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
void SDLImage::playerColored(PlayerColor player)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
graphics->blueToPlayersAdv(surf, player);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
void SDLImage::setAlpha(uint8_t value)
|
|
|
|
{
|
|
|
|
CSDL_Ext::setAlpha (surf, value);
|
|
|
|
SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND);
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
void SDLImage::setFlagColor(PlayerColor player)
|
|
|
|
{
|
|
|
|
if(player < PlayerColor::PLAYER_LIMIT || player==PlayerColor::NEUTRAL)
|
|
|
|
CSDL_Ext::setPlayerColor(surf, player);
|
|
|
|
}
|
|
|
|
|
2022-11-25 11:46:47 +02:00
|
|
|
bool SDLImage::isTransparent(const Point & coords) const
|
|
|
|
{
|
|
|
|
return CSDL_Ext::isTransparent(surf, coords.x, coords.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
Point SDLImage::dimensions() const
|
|
|
|
{
|
|
|
|
return fullSize;
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
void SDLImage::horizontalFlip()
|
|
|
|
{
|
2016-11-08 21:14:38 +02:00
|
|
|
margins.y = fullSize.y - surf->h - margins.y;
|
2016-11-07 23:19:53 +02:00
|
|
|
|
2016-11-08 21:14:38 +02:00
|
|
|
//todo: modify in-place
|
|
|
|
SDL_Surface * flipped = CSDL_Ext::horizontalFlip(surf);
|
2016-11-07 23:19:53 +02:00
|
|
|
SDL_FreeSurface(surf);
|
|
|
|
surf = flipped;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLImage::verticalFlip()
|
|
|
|
{
|
2016-11-08 21:14:38 +02:00
|
|
|
margins.x = fullSize.x - surf->w - margins.x;
|
2016-11-07 23:19:53 +02:00
|
|
|
|
2016-11-08 21:14:38 +02:00
|
|
|
//todo: modify in-place
|
|
|
|
SDL_Surface * flipped = CSDL_Ext::verticalFlip(surf);
|
2016-11-07 23:19:53 +02:00
|
|
|
SDL_FreeSurface(surf);
|
|
|
|
surf = flipped;
|
|
|
|
}
|
|
|
|
|
2020-01-25 11:21:26 +02:00
|
|
|
// Keep the original palette, in order to do color switching operation
|
|
|
|
void SDLImage::savePalette()
|
|
|
|
{
|
2020-01-26 04:49:53 +02:00
|
|
|
// For some images that don't have palette, skip this
|
2020-01-26 10:01:48 +02:00
|
|
|
if(surf->format->palette == nullptr)
|
2020-01-26 04:49:53 +02:00
|
|
|
return;
|
|
|
|
|
2020-01-26 10:01:48 +02:00
|
|
|
if(originalPalette == nullptr)
|
|
|
|
originalPalette = SDL_AllocPalette(DEFAULT_PALETTE_COLORS);
|
|
|
|
|
|
|
|
SDL_SetPaletteColors(originalPalette, surf->format->palette->colors, 0, DEFAULT_PALETTE_COLORS);
|
2020-01-25 11:21:26 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
void SDLImage::shiftPalette(int from, int howMany)
|
|
|
|
{
|
|
|
|
//works with at most 16 colors, if needed more -> increase values
|
|
|
|
assert(howMany < 16);
|
|
|
|
|
|
|
|
if(surf->format->palette)
|
|
|
|
{
|
|
|
|
SDL_Color palette[16];
|
|
|
|
|
|
|
|
for(int i=0; i<howMany; ++i)
|
|
|
|
{
|
|
|
|
palette[(i+1)%howMany] = surf->format->palette->colors[from + i];
|
|
|
|
}
|
2023-01-17 22:01:35 +02:00
|
|
|
CSDL_Ext::setColors(surf, palette, from, howMany);
|
2016-11-07 23:19:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 18:25:47 +02:00
|
|
|
void SDLImage::adjustPalette(const ColorFilter & shifter, size_t colorsToSkip)
|
2020-01-25 11:21:26 +02:00
|
|
|
{
|
2020-01-26 10:01:48 +02:00
|
|
|
if(originalPalette == nullptr)
|
|
|
|
return;
|
|
|
|
|
2020-01-25 11:21:26 +02:00
|
|
|
SDL_Palette* palette = surf->format->palette;
|
2020-01-26 10:01:48 +02:00
|
|
|
|
2023-01-30 18:25:47 +02:00
|
|
|
// Note: here we skip first colors in the palette that are predefined in H3 images
|
|
|
|
for(int i = colorsToSkip; i < palette->ncolors; i++)
|
2020-01-25 11:21:26 +02:00
|
|
|
{
|
2022-12-15 23:24:03 +02:00
|
|
|
palette->colors[i] = shifter.shiftColor(originalPalette->colors[i]);
|
2020-01-25 11:21:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLImage::resetPalette()
|
|
|
|
{
|
2020-01-26 10:01:48 +02:00
|
|
|
if(originalPalette == nullptr)
|
|
|
|
return;
|
|
|
|
|
2020-01-25 11:21:26 +02:00
|
|
|
// Always keept the original palette not changed, copy a new palette to assign to surface
|
2020-01-26 10:01:48 +02:00
|
|
|
SDL_SetPaletteColors(surf->format->palette, originalPalette->colors, 0, originalPalette->ncolors);
|
2020-01-25 11:21:26 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 23:24:03 +02:00
|
|
|
void SDLImage::resetPalette( int colorID )
|
|
|
|
{
|
|
|
|
if(originalPalette == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Always keept the original palette not changed, copy a new palette to assign to surface
|
|
|
|
SDL_SetPaletteColors(surf->format->palette, originalPalette->colors + colorID, colorID, 1);
|
|
|
|
}
|
|
|
|
|
2022-12-14 12:04:37 +02:00
|
|
|
void SDLImage::setSpecialPallete(const IImage::SpecialPalette & SpecialPalette)
|
2017-09-05 17:59:26 +02:00
|
|
|
{
|
|
|
|
if(surf->format->palette)
|
|
|
|
{
|
2023-01-17 22:01:35 +02:00
|
|
|
CSDL_Ext::setColors(surf, const_cast<SDL_Color *>(SpecialPalette.data()), 1, 7);
|
2017-09-05 17:59:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
SDLImage::~SDLImage()
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(surf);
|
2020-01-26 10:01:48 +02:00
|
|
|
|
|
|
|
if(originalPalette != nullptr)
|
|
|
|
{
|
|
|
|
SDL_FreePalette(originalPalette);
|
|
|
|
originalPalette = nullptr;
|
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|