1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-15 01:24:45 +02:00

CPicture now uses IImage internally

This commit is contained in:
Ivan Savenko
2023-01-30 17:18:59 +02:00
parent e35a669eeb
commit 87218c63c4
11 changed files with 73 additions and 88 deletions

View File

@ -97,7 +97,7 @@ void CMessage::dispose()
SDL_Surface * CMessage::drawDialogBox(int w, int h, PlayerColor playerColor) SDL_Surface * CMessage::drawDialogBox(int w, int h, PlayerColor playerColor)
{ {
//prepare surface //prepare surface
SDL_Surface * ret = SDL_CreateRGBSurface(0, w, h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask); SDL_Surface * ret = CSDL_Ext::newSurface(w,h);
for (int i=0; i<w; i+=background->w)//background for (int i=0; i<w; i+=background->w)//background
{ {
for (int j=0; j<h; j+=background->h) for (int j=0; j<h; j+=background->h)

View File

@ -97,7 +97,7 @@ public:
void draw(SDL_Surface * where, int posX=0, int posY=0, const Rect *src=nullptr) const override; void draw(SDL_Surface * where, int posX=0, int posY=0, const Rect *src=nullptr) const override;
void draw(SDL_Surface * where, const Rect * dest, const Rect * src) const override; void draw(SDL_Surface * where, const Rect * dest, const Rect * src) const override;
std::shared_ptr<IImage> scaleFast(float scale) const override; std::shared_ptr<IImage> scaleFast(const Point & size) const override;
void exportBitmap(const boost::filesystem::path & path) const override; void exportBitmap(const boost::filesystem::path & path) const override;
void playerColored(PlayerColor player) override; void playerColored(PlayerColor player) override;
void setFlagColor(PlayerColor player) override; void setFlagColor(PlayerColor player) override;
@ -112,6 +112,8 @@ public:
void resetPalette(int colorID) override; void resetPalette(int colorID) override;
void resetPalette() override; void resetPalette() override;
void setAlpha(uint8_t value) override;
void setSpecialPallete(const SpecialPalette & SpecialPalette) override; void setSpecialPallete(const SpecialPalette & SpecialPalette) override;
friend class SDLImageLoader; friend class SDLImageLoader;
@ -143,6 +145,11 @@ std::shared_ptr<IImage> IImage::createFromFile( const std::string & path )
return std::shared_ptr<IImage>(new SDLImage(path)); return std::shared_ptr<IImage>(new SDLImage(path));
} }
std::shared_ptr<IImage> IImage::createFromSurface( SDL_Surface * source )
{
return std::shared_ptr<IImage>(new SDLImage(source, true));
}
// Extremely simple file cache. TODO: smarter, more general solution // Extremely simple file cache. TODO: smarter, more general solution
class CFileCache class CFileCache
{ {
@ -679,7 +686,11 @@ void SDLImage::draw(SDL_Surface* where, const Rect * dest, const Rect* src) cons
if(dest) if(dest)
destShift += dest->topLeft(); destShift += dest->topLeft();
if(surf->format->BitsPerPixel == 8) 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)
{ {
CSDL_Ext::blit8bppAlphaTo24bpp(surf, sourceRect, where, destShift); CSDL_Ext::blit8bppAlphaTo24bpp(surf, sourceRect, where, destShift);
} }
@ -689,9 +700,12 @@ void SDLImage::draw(SDL_Surface* where, const Rect * dest, const Rect* src) cons
} }
} }
std::shared_ptr<IImage> SDLImage::scaleFast(float scale) const std::shared_ptr<IImage> SDLImage::scaleFast(const Point & size) const
{ {
auto scaled = CSDL_Ext::scaleSurfaceFast(surf, (int)(surf->w * scale), (int)(surf->h * scale)); 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));
if (scaled->format && scaled->format->palette) // fix color keying, because SDL loses it at this point 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]); CSDL_Ext::setColorKey(scaled, scaled->format->palette->colors[0]);
@ -702,11 +716,11 @@ std::shared_ptr<IImage> SDLImage::scaleFast(float scale) const
SDLImage * ret = new SDLImage(scaled, false); SDLImage * ret = new SDLImage(scaled, false);
ret->fullSize.x = (int) round((float)fullSize.x * scale); ret->fullSize.x = (int) round((float)fullSize.x * scaleX);
ret->fullSize.y = (int) round((float)fullSize.y * scale); ret->fullSize.y = (int) round((float)fullSize.y * scaleY);
ret->margins.x = (int) round((float)margins.x * scale); ret->margins.x = (int) round((float)margins.x * scaleX);
ret->margins.y = (int) round((float)margins.y * scale); ret->margins.y = (int) round((float)margins.y * scaleY);
return std::shared_ptr<IImage>(ret); return std::shared_ptr<IImage>(ret);
} }
@ -721,6 +735,12 @@ void SDLImage::playerColored(PlayerColor player)
graphics->blueToPlayersAdv(surf, player); graphics->blueToPlayersAdv(surf, player);
} }
void SDLImage::setAlpha(uint8_t value)
{
CSDL_Ext::setAlpha (surf, value);
SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND);
}
void SDLImage::setFlagColor(PlayerColor player) void SDLImage::setFlagColor(PlayerColor player)
{ {
if(player < PlayerColor::PLAYER_LIMIT || player==PlayerColor::NEUTRAL) if(player < PlayerColor::PLAYER_LIMIT || player==PlayerColor::NEUTRAL)

View File

@ -44,7 +44,7 @@ public:
virtual void draw(SDL_Surface * where, int posX = 0, int posY = 0, const Rect * src = nullptr) const = 0; virtual void draw(SDL_Surface * where, int posX = 0, int posY = 0, const Rect * src = nullptr) const = 0;
virtual void draw(SDL_Surface * where, const Rect * dest, const Rect * src) const = 0; virtual void draw(SDL_Surface * where, const Rect * dest, const Rect * src) const = 0;
virtual std::shared_ptr<IImage> scaleFast(float scale) const = 0; virtual std::shared_ptr<IImage> scaleFast(const Point & size) const = 0;
virtual void exportBitmap(const boost::filesystem::path & path) const = 0; virtual void exportBitmap(const boost::filesystem::path & path) const = 0;
@ -67,6 +67,8 @@ public:
virtual void resetPalette(int colorID) = 0; virtual void resetPalette(int colorID) = 0;
virtual void resetPalette() = 0; virtual void resetPalette() = 0;
virtual void setAlpha(uint8_t value) = 0;
//only indexed bitmaps with 7 special colors //only indexed bitmaps with 7 special colors
virtual void setSpecialPallete(const SpecialPalette & SpecialPalette) = 0; virtual void setSpecialPallete(const SpecialPalette & SpecialPalette) = 0;
@ -78,6 +80,10 @@ public:
/// loads image from specified file. Returns 0-sized images on failure /// loads image from specified file. Returns 0-sized images on failure
static std::shared_ptr<IImage> createFromFile( const std::string & path ); static std::shared_ptr<IImage> createFromFile( const std::string & path );
/// temporary compatibility method. Creates IImage from existing SDL_Surface
/// Surface will be shared, called must still free it with SDL_FreeSurface
static std::shared_ptr<IImage> createFromSurface( SDL_Surface * source );
}; };
/// Class for handling animation /// Class for handling animation

View File

@ -852,7 +852,10 @@ void CSDL_Ext::blitSurface(SDL_Surface * src, const Rect & srcRectInput, SDL_Sur
SDL_Rect srcRect = CSDL_Ext::toSDL(srcRectInput); SDL_Rect srcRect = CSDL_Ext::toSDL(srcRectInput);
SDL_Rect dstRect = CSDL_Ext::toSDL(Rect(dstPoint, srcRectInput.dimensions())); SDL_Rect dstRect = CSDL_Ext::toSDL(Rect(dstPoint, srcRectInput.dimensions()));
SDL_UpperBlit(src, &srcRect, dst, &dstRect); int result = SDL_UpperBlit(src, &srcRect, dst, &dstRect);
if (result != 0)
logGlobal->error("SDL_UpperBlit failed! %s", SDL_GetError());
} }
void CSDL_Ext::blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest) void CSDL_Ext::blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest)

View File

@ -525,7 +525,7 @@ void CBonusSelection::CRegion::clickLeft(tribool down, bool previousState)
if(indeterminate(down)) if(indeterminate(down))
return; return;
if(!down && selectable && !CSDL_Ext::isTransparent(graphicsNotSelected->getSurface(), GH.getCursorPosition() - pos.topLeft())) if(!down && selectable && !graphicsNotSelected->getSurface()->isTransparent(GH.getCursorPosition() - pos.topLeft()))
{ {
CSH->setCampaignMap(idOfMapAndRegion); CSH->setCampaignMap(idOfMapAndRegion);
} }
@ -535,7 +535,7 @@ void CBonusSelection::CRegion::clickRight(tribool down, bool previousState)
{ {
// FIXME: For some reason "down" is only ever contain indeterminate_value // FIXME: For some reason "down" is only ever contain indeterminate_value
auto text = CSH->si->campState->camp->scenarios[idOfMapAndRegion].regionText; auto text = CSH->si->campState->camp->scenarios[idOfMapAndRegion].regionText;
if(!CSDL_Ext::isTransparent(graphicsNotSelected->getSurface(), GH.getCursorPosition() - pos.topLeft()) && text.size()) if(!graphicsNotSelected->getSurface()->isTransparent(GH.getCursorPosition() - pos.topLeft()) && text.size())
{ {
CRClickPopup::createAndPush(text); CRClickPopup::createAndPush(text);
} }

View File

@ -1434,7 +1434,7 @@ std::shared_ptr<IImage> CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CM
auto iter = cache.find(key); auto iter = cache.find(key);
if(iter == cache.end()) if(iter == cache.end())
{ {
auto scaled = fullSurface->scaleFast(worldViewCachedScale); auto scaled = fullSurface->scaleFast(fullSurface->dimensions() * worldViewCachedScale);
cache[key] = scaled; cache[key] = scaled;
return scaled; return scaled;
} }

View File

@ -35,15 +35,14 @@
#include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
#include "../../lib/CRandomGenerator.h" #include "../../lib/CRandomGenerator.h"
CPicture::CPicture(SDL_Surface *BG, const Point & position) CPicture::CPicture(std::shared_ptr<IImage> image, const Point & position)
: bg(BG) : bg(image)
, visible(true) , visible(true)
, needRefresh(false) , needRefresh(false)
{ {
BG->refcount += 1;
pos += position; pos += position;
pos.w = BG->w; pos.w = bg->width();
pos.h = BG->h; pos.h = bg->height();
} }
CPicture::CPicture( const std::string &bmpname, int x, int y ) CPicture::CPicture( const std::string &bmpname, int x, int y )
@ -55,7 +54,7 @@ CPicture::CPicture( const std::string &bmpname )
{} {}
CPicture::CPicture( const std::string &bmpname, const Point & position ) CPicture::CPicture( const std::string &bmpname, const Point & position )
: bg(BitmapHandler::loadBitmap(bmpname)) : bg(IImage::createFromFile(bmpname))
, visible(true) , visible(true)
, needRefresh(false) , needRefresh(false)
{ {
@ -65,8 +64,8 @@ CPicture::CPicture( const std::string &bmpname, const Point & position )
assert(bg); assert(bg);
if(bg) if(bg)
{ {
pos.w = bg->w; pos.w = bg->width();
pos.h = bg->h; pos.h = bg->height();
} }
else else
{ {
@ -74,34 +73,14 @@ CPicture::CPicture( const std::string &bmpname, const Point & position )
} }
} }
CPicture::CPicture(SDL_Surface * BG, const Rect &SrcRect, int x, int y) CPicture::CPicture(std::shared_ptr<IImage> image, const Rect &SrcRect, int x, int y)
: CPicture(BG, Point(x,y)) : CPicture(image, Point(x,y))
{ {
srcRect = SrcRect; srcRect = SrcRect;
pos.w = srcRect->w; pos.w = srcRect->w;
pos.h = srcRect->h; pos.h = srcRect->h;
} }
void CPicture::setSurface(SDL_Surface *to)
{
bg = to;
if (srcRect)
{
pos.w = srcRect->w;
pos.h = srcRect->h;
}
else
{
pos.w = bg->w;
pos.h = bg->h;
}
}
CPicture::~CPicture()
{
SDL_FreeSurface(bg);
}
void CPicture::show(SDL_Surface * to) void CPicture::show(SDL_Surface * to)
{ {
if (visible && needRefresh) if (visible && needRefresh)
@ -111,41 +90,25 @@ void CPicture::show(SDL_Surface * to)
void CPicture::showAll(SDL_Surface * to) void CPicture::showAll(SDL_Surface * to)
{ {
if(bg && visible) if(bg && visible)
{ bg->draw(to, pos.x, pos.y, srcRect.get_ptr());
if(srcRect)
CSDL_Ext::blitSurface(bg, *srcRect, to, pos.topLeft());
else
CSDL_Ext::blitAt(bg, pos, to);
}
}
void CPicture::convertToScreenBPP()
{
SDL_Surface *hlp = bg;
bg = SDL_ConvertSurface(hlp,screen->format,0);
CSDL_Ext::setDefaultColorKey(bg);
SDL_FreeSurface(hlp);
} }
void CPicture::setAlpha(int value) void CPicture::setAlpha(int value)
{ {
CSDL_Ext::setAlpha (bg, value); bg->setAlpha(value);
SDL_SetSurfaceBlendMode(bg,SDL_BLENDMODE_BLEND);
} }
void CPicture::scaleTo(Point size) void CPicture::scaleTo(Point size)
{ {
SDL_Surface * scaled = CSDL_Ext::scaleSurface(bg, size.x, size.y); bg = bg->scaleFast(size);
SDL_FreeSurface(bg); pos.w = bg->width();
pos.h = bg->height();
setSurface(scaled);
} }
void CPicture::colorize(PlayerColor player) void CPicture::colorize(PlayerColor player)
{ {
assert(bg); bg->playerColored(player);
graphics->blueToPlayersAdv(bg, player);
} }
CFilledTexture::CFilledTexture(std::string imageName, Rect position): CFilledTexture::CFilledTexture(std::string imageName, Rect position):
@ -264,7 +227,7 @@ void CAnimImage::showAll(SDL_Surface * to)
{ {
if(isScaled()) if(isScaled())
{ {
auto scaled = img->scaleFast(float(scaledSize.x) / img->width()); auto scaled = img->scaleFast(scaledSize);
scaled->draw(to, pos.x, pos.y); scaled->draw(to, pos.x, pos.y);
} }
else else
@ -418,9 +381,7 @@ void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
auto img = anim->getImage(frame, group); auto img = anim->getImage(frame, group);
if(img) if(img)
{ {
const ColorFilter alphaFilter = ColorFilter::genAlphaShifter(vstd::lerp(0.0f, 1.0f, alpha/255.0f)); img->setAlpha(alpha);
img->adjustPalette(alphaFilter);
img->draw(to, pos.x, pos.y, &src); img->draw(to, pos.x, pos.y, &src);
} }
} }

View File

@ -26,11 +26,7 @@ class IImage;
// Image class // Image class
class CPicture : public CIntObject class CPicture : public CIntObject
{ {
void setSurface(SDL_Surface *to); std::shared_ptr<IImage> bg;
SDL_Surface * bg;
void convertToScreenBPP();
public: public:
/// if set, only specified section of internal image will be rendered /// if set, only specified section of internal image will be rendered
@ -43,25 +39,22 @@ public:
/// Deprecated, use CIntObject::disable()/enable() instead /// Deprecated, use CIntObject::disable()/enable() instead
bool visible; bool visible;
SDL_Surface * getSurface() std::shared_ptr<IImage> getSurface()
{ {
return bg; return bg;
} }
/// wrap existing SDL_Surface /// wrap existing image
/// deprecated, do not use CPicture(std::shared_ptr<IImage> image, const Point & position);
CPicture(SDL_Surface * BG, const Point & position);
/// wrap section of existing SDL_Surface /// wrap section of an existing Image
CPicture(SDL_Surface *BG, const Rect &SrcRext, int x = 0, int y = 0); //wrap subrect of given surface CPicture(std::shared_ptr<IImage> image, const Rect &SrcRext, int x = 0, int y = 0); //wrap subrect of given surface
/// Loads image from specified file name /// Loads image from specified file name
CPicture(const std::string & bmpname); CPicture(const std::string & bmpname);
CPicture(const std::string & bmpname, const Point & position); CPicture(const std::string & bmpname, const Point & position);
CPicture(const std::string & bmpname, int x, int y); CPicture(const std::string & bmpname, int x, int y);
~CPicture();
/// set alpha value for whole surface. Note: may be messed up if surface is shared /// set alpha value for whole surface. Note: may be messed up if surface is shared
/// 0=transparent, 255=opaque /// 0=transparent, 255=opaque
void setAlpha(int value); void setAlpha(int value);

View File

@ -496,7 +496,7 @@ CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const std::stri
#endif #endif
} }
CTextInput::CTextInput(const Rect & Pos, SDL_Surface * srf) CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
:CFocusable(std::make_shared<CKeyboardFocusListener>(this)) :CFocusable(std::make_shared<CKeyboardFocusListener>(this))
{ {
pos += Pos.topLeft(); pos += Pos.topLeft();

View File

@ -14,6 +14,7 @@
#include "../gui/SDL_Extensions.h" #include "../gui/SDL_Extensions.h"
#include "../../lib/FunctionList.h" #include "../../lib/FunctionList.h"
class IImage;
class CSlider; class CSlider;
/// Base class for all text-related widgets. /// Base class for all text-related widgets.
@ -218,7 +219,7 @@ public:
CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB); CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB);
CTextInput(const Rect & Pos, const Point & bgOffset, const std::string & bgName, const CFunctionList<void(const std::string &)> & CB); CTextInput(const Rect & Pos, const Point & bgOffset, const std::string & bgName, const CFunctionList<void(const std::string &)> & CB);
CTextInput(const Rect & Pos, SDL_Surface * srf); CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf);
void clickLeft(tribool down, bool previousState) override; void clickLeft(tribool down, bool previousState) override;
void keyPressed(const SDL_KeyboardEvent & key) override; void keyPressed(const SDL_KeyboardEvent & key) override;

View File

@ -19,6 +19,7 @@
#include "../gui/SDL_Extensions.h" #include "../gui/SDL_Extensions.h"
#include "../gui/CGuiHandler.h" #include "../gui/CGuiHandler.h"
#include "../gui/CursorHandler.h" #include "../gui/CursorHandler.h"
#include "../gui/CAnimation.h"
#include "../battle/BattleInterface.h" #include "../battle/BattleInterface.h"
#include "../battle/BattleInterfaceClasses.h" #include "../battle/BattleInterfaceClasses.h"
@ -213,9 +214,9 @@ void CWindowObject::setShadow(bool on)
{ {
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE); OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
shadowParts.push_back(std::make_shared<CPicture>(shadowCorner, Point(shadowPos.x, shadowPos.y))); shadowParts.push_back(std::make_shared<CPicture>( IImage::createFromSurface(shadowCorner), Point(shadowPos.x, shadowPos.y)));
shadowParts.push_back(std::make_shared<CPicture>(shadowRight, Point(shadowPos.x, shadowStart.y))); shadowParts.push_back(std::make_shared<CPicture>( IImage::createFromSurface(shadowRight ), Point(shadowPos.x, shadowStart.y)));
shadowParts.push_back(std::make_shared<CPicture>(shadowBottom, Point(shadowStart.x, shadowPos.y))); shadowParts.push_back(std::make_shared<CPicture>( IImage::createFromSurface(shadowBottom), Point(shadowStart.x, shadowPos.y)));
} }
SDL_FreeSurface(shadowCorner); SDL_FreeSurface(shadowCorner);