mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-07 00:58:39 +02:00
- shadows for CWindowObject-based windows
- better error messages on read error - fixed crash on level-up window
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
#include "CIntObjectClasses.h"
|
||||
|
||||
#include "../CBitmapHandler.h"
|
||||
#include "SDL_Pixels.h"
|
||||
#include "SDL_Extensions.h"
|
||||
#include "../Graphics.h"
|
||||
#include "../CAnimation.h"
|
||||
@ -1341,11 +1342,11 @@ std::string CGStatusBar::getCurrent()
|
||||
return text;
|
||||
}
|
||||
|
||||
CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::Cornsilk*/, const std::string &Text /*= ""*/)
|
||||
: CLabel(x, y, Font, Align, Color, Text)
|
||||
{
|
||||
init();
|
||||
}
|
||||
//CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::Cornsilk*/, const std::string &Text /*= ""*/)
|
||||
//: CLabel(x, y, Font, Align, Color, Text)
|
||||
//{
|
||||
// init();
|
||||
//}
|
||||
|
||||
CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::Cornsilk*/)
|
||||
: CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
|
||||
@ -1597,6 +1598,7 @@ void CFocusable::moveFocus()
|
||||
|
||||
CWindowObject::CWindowObject(int options_, std::string imageName, Point centerAt):
|
||||
CIntObject(getUsedEvents(options_), Point()),
|
||||
shadow(nullptr),
|
||||
options(options_),
|
||||
background(createBg(imageName, options & PLAYER_COLORED))
|
||||
{
|
||||
@ -1609,10 +1611,14 @@ CWindowObject::CWindowObject(int options_, std::string imageName, Point centerAt
|
||||
pos = background->center(centerAt);
|
||||
else
|
||||
center(centerAt);
|
||||
|
||||
if (!(options & SHADOW_DISABLED))
|
||||
setShadow(true);
|
||||
}
|
||||
|
||||
CWindowObject::CWindowObject(int options_, std::string imageName):
|
||||
CIntObject(getUsedEvents(options_), Point()),
|
||||
shadow(nullptr),
|
||||
options(options_),
|
||||
background(createBg(imageName, options & PLAYER_COLORED))
|
||||
{
|
||||
@ -1625,6 +1631,14 @@ CWindowObject::CWindowObject(int options_, std::string imageName):
|
||||
pos = background->center();
|
||||
else
|
||||
center(Point(screen->w/2, screen->h/2));
|
||||
|
||||
if (!(options & SHADOW_DISABLED))
|
||||
setShadow(true);
|
||||
}
|
||||
|
||||
CWindowObject::~CWindowObject()
|
||||
{
|
||||
setShadow(false);
|
||||
}
|
||||
|
||||
CPicture * CWindowObject::createBg(std::string imageName, bool playerColored)
|
||||
@ -1649,6 +1663,8 @@ void CWindowObject::setBackground(std::string filename)
|
||||
|
||||
if (background)
|
||||
pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
|
||||
|
||||
updateShadow();
|
||||
}
|
||||
|
||||
int CWindowObject::getUsedEvents(int options)
|
||||
@ -1658,6 +1674,117 @@ int CWindowObject::getUsedEvents(int options)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CWindowObject::updateShadow()
|
||||
{
|
||||
setShadow(false);
|
||||
if (!(options & SHADOW_DISABLED))
|
||||
setShadow(true);
|
||||
}
|
||||
|
||||
void CWindowObject::setShadow(bool on)
|
||||
{
|
||||
vstd::clear_pointer(shadow);
|
||||
|
||||
//size of shadow
|
||||
static const int size = 8;
|
||||
|
||||
if (on == bool(shadow))
|
||||
return;
|
||||
|
||||
//object too small to cast shadow
|
||||
if (pos.h <= size || pos.w <= size)
|
||||
return;
|
||||
|
||||
if (on)
|
||||
{
|
||||
|
||||
//helper to set last row
|
||||
auto blitAlphaRow = [](SDL_Surface *surf, size_t row)
|
||||
{
|
||||
Uint8 * ptr = (Uint8*)surf->pixels + surf->pitch * (row);
|
||||
|
||||
for (size_t i=0; i< surf->w; i++)
|
||||
{
|
||||
Channels::px<4>::a.set(ptr, 128);
|
||||
ptr+=4;
|
||||
}
|
||||
};
|
||||
|
||||
// helper to set last column
|
||||
auto blitAlphaCol = [](SDL_Surface *surf, size_t col)
|
||||
{
|
||||
Uint8 * ptr = (Uint8*)surf->pixels + 4 * (col);
|
||||
|
||||
for (size_t i=0; i< surf->h; i++)
|
||||
{
|
||||
Channels::px<4>::a.set(ptr, 128);
|
||||
ptr+= surf->pitch;
|
||||
}
|
||||
};
|
||||
|
||||
static SDL_Surface * shadowCornerTempl = nullptr;
|
||||
static SDL_Surface * shadowBottomTempl = nullptr;
|
||||
static SDL_Surface * shadowRightTempl = nullptr;
|
||||
|
||||
//one-time initialization
|
||||
if (!shadowCornerTempl)
|
||||
{
|
||||
//create "template" surfaces
|
||||
shadowCornerTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, size);
|
||||
shadowBottomTempl = CSDL_Ext::createSurfaceWithBpp<4>(1, size);
|
||||
shadowRightTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, 1);
|
||||
|
||||
Uint32 shadowColor = SDL_MapRGBA(shadowCornerTempl->format, 0, 0, 0, 192);
|
||||
|
||||
//fill with shadow body color
|
||||
SDL_FillRect(shadowCornerTempl, NULL, shadowColor);
|
||||
SDL_FillRect(shadowBottomTempl, NULL, shadowColor);
|
||||
SDL_FillRect(shadowRightTempl, NULL, shadowColor);
|
||||
|
||||
//fill last row and column with more transparent color
|
||||
blitAlphaCol(shadowRightTempl , size-1);
|
||||
blitAlphaCol(shadowCornerTempl, size-1);
|
||||
blitAlphaRow(shadowBottomTempl, size-1);
|
||||
blitAlphaRow(shadowCornerTempl, size-1);
|
||||
}
|
||||
|
||||
OBJ_CONSTRUCTION_CAPTURING_ALL;
|
||||
|
||||
//FIXME: do something with this points
|
||||
Point shadowStart;
|
||||
if (options & BORDERED)
|
||||
shadowStart = Point(size - 14, size - 14);
|
||||
else
|
||||
shadowStart = Point(size, size);
|
||||
|
||||
Point shadowPos;
|
||||
if (options & BORDERED)
|
||||
shadowPos = Point(pos.w + 14, pos.h + 14);
|
||||
else
|
||||
shadowPos = Point(pos.w, pos.h);
|
||||
|
||||
Point fullsize;
|
||||
if (options & BORDERED)
|
||||
fullsize = Point(pos.w + 28, pos.h + 29);
|
||||
else
|
||||
fullsize = Point(pos.w, pos.h);
|
||||
|
||||
//create base 8x8 piece of shadow
|
||||
SDL_Surface * shadowCorner = CSDL_Ext::copySurface(shadowCornerTempl);
|
||||
SDL_Surface * shadowBottom = CSDL_Ext::scaleSurfaceFast(shadowBottomTempl, fullsize.x - size, size);
|
||||
SDL_Surface * shadowRight = CSDL_Ext::scaleSurfaceFast(shadowRightTempl, size, fullsize.y - size);
|
||||
|
||||
blitAlphaCol(shadowBottom, 0);
|
||||
blitAlphaRow(shadowRight, 0);
|
||||
|
||||
//generate "shadow" object with these 3 pieces in it
|
||||
shadow = new CIntObject;
|
||||
shadow->addChild(new CPicture(shadowCorner, shadowPos.x, shadowPos.y));
|
||||
shadow->addChild(new CPicture(shadowRight, shadowPos.x, shadowStart.y));
|
||||
shadow->addChild(new CPicture(shadowBottom, shadowStart.x, shadowPos.y));
|
||||
}
|
||||
}
|
||||
|
||||
void CWindowObject::showAll(SDL_Surface *to)
|
||||
{
|
||||
CIntObject::showAll(to);
|
||||
|
Reference in New Issue
Block a user