Optimize/simplify dialog shading slightly

- move creation to c'tor
 - apply position and size with one method instead of two
This commit is contained in:
Stephen Anthony 2020-11-14 22:35:05 -03:30
parent bda86befb4
commit 85d0c9227c
5 changed files with 80 additions and 48 deletions

View File

@ -104,41 +104,49 @@ const Common::Rect& FBSurfaceSDL2::dstRect() const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FBSurfaceSDL2::setSrcPos(uInt32 x, uInt32 y) void FBSurfaceSDL2::setSrcPos(uInt32 x, uInt32 y)
{ {
if(x != static_cast<uInt32>(mySrcR.x) || y != static_cast<uInt32>(mySrcR.y)) if(setSrcPosInternal(x, y))
{
setSrcPosInternal(x, y);
reinitializeBlitter(); reinitializeBlitter();
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FBSurfaceSDL2::setSrcSize(uInt32 w, uInt32 h) void FBSurfaceSDL2::setSrcSize(uInt32 w, uInt32 h)
{ {
if(w != static_cast<uInt32>(mySrcR.w) || h != static_cast<uInt32>(mySrcR.h)) if(setSrcSizeInternal(w, h))
{ reinitializeBlitter();
setSrcSizeInternal(w, h); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FBSurfaceSDL2::setSrcRect(const Common::Rect& r)
{
const bool posChanged = setSrcPosInternal(r.x(), r.y()),
sizeChanged = setSrcSizeInternal(r.w(), r.h());
if(posChanged || sizeChanged)
reinitializeBlitter(); reinitializeBlitter();
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FBSurfaceSDL2::setDstPos(uInt32 x, uInt32 y) void FBSurfaceSDL2::setDstPos(uInt32 x, uInt32 y)
{ {
if(x != static_cast<uInt32>(myDstR.x) || y != static_cast<uInt32>(myDstR.y)) if(setDstPosInternal(x, y))
{
setDstPosInternal(x, y);
reinitializeBlitter(); reinitializeBlitter();
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FBSurfaceSDL2::setDstSize(uInt32 w, uInt32 h) void FBSurfaceSDL2::setDstSize(uInt32 w, uInt32 h)
{ {
if(w != static_cast<uInt32>(myDstR.w) || h != static_cast<uInt32>(myDstR.h)) if(setDstSizeInternal(w, h))
{ reinitializeBlitter();
setDstSizeInternal(w, h); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FBSurfaceSDL2::setDstRect(const Common::Rect& r)
{
const bool posChanged = setDstPosInternal(r.x(), r.y()),
sizeChanged = setDstSizeInternal(r.w(), r.h());
if(posChanged || sizeChanged)
reinitializeBlitter(); reinitializeBlitter();
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -48,8 +48,11 @@ class FBSurfaceSDL2 : public FBSurface
const Common::Rect& dstRect() const override; const Common::Rect& dstRect() const override;
void setSrcPos(uInt32 x, uInt32 y) override; void setSrcPos(uInt32 x, uInt32 y) override;
void setSrcSize(uInt32 w, uInt32 h) override; void setSrcSize(uInt32 w, uInt32 h) override;
void setSrcRect(const Common::Rect& r) override;
void setDstPos(uInt32 x, uInt32 y) override; void setDstPos(uInt32 x, uInt32 y) override;
void setDstSize(uInt32 w, uInt32 h) override; void setDstSize(uInt32 w, uInt32 h) override;
void setDstRect(const Common::Rect& r) override;
void setVisible(bool visible) override; void setVisible(bool visible) override;
void translateCoords(Int32& x, Int32& y) const override; void translateCoords(Int32& x, Int32& y) const override;
@ -67,21 +70,41 @@ class FBSurfaceSDL2 : public FBSurface
void applyAttributes() override; void applyAttributes() override;
private: private:
inline void setSrcPosInternal(uInt32 x, uInt32 y) { inline bool setSrcPosInternal(uInt32 x, uInt32 y) {
if(x != static_cast<uInt32>(mySrcR.x) || y != static_cast<uInt32>(mySrcR.y))
{
mySrcR.x = x; mySrcR.y = y; mySrcR.x = x; mySrcR.y = y;
mySrcGUIR.moveTo(x, y); mySrcGUIR.moveTo(x, y);
return true;
} }
inline void setSrcSizeInternal(uInt32 w, uInt32 h) { return false;
}
inline bool setSrcSizeInternal(uInt32 w, uInt32 h) {
if(w != static_cast<uInt32>(mySrcR.w) || h != static_cast<uInt32>(mySrcR.h))
{
mySrcR.w = w; mySrcR.h = h; mySrcR.w = w; mySrcR.h = h;
mySrcGUIR.setWidth(w); mySrcGUIR.setHeight(h); mySrcGUIR.setWidth(w); mySrcGUIR.setHeight(h);
return true;
} }
inline void setDstPosInternal(uInt32 x, uInt32 y) { return false;
}
inline bool setDstPosInternal(uInt32 x, uInt32 y) {
if(x != static_cast<uInt32>(myDstR.x) || y != static_cast<uInt32>(myDstR.y))
{
myDstR.x = x; myDstR.y = y; myDstR.x = x; myDstR.y = y;
myDstGUIR.moveTo(x, y); myDstGUIR.moveTo(x, y);
return true;
} }
inline void setDstSizeInternal(uInt32 w, uInt32 h) { return false;
}
inline bool setDstSizeInternal(uInt32 w, uInt32 h) {
if(w != static_cast<uInt32>(myDstR.w) || h != static_cast<uInt32>(myDstR.h))
{
myDstR.w = w; myDstR.h = h; myDstR.w = w; myDstR.h = h;
myDstGUIR.setWidth(w); myDstGUIR.setHeight(h); myDstGUIR.setWidth(w); myDstGUIR.setHeight(h);
return true;
}
return false;
} }
void createSurface(uInt32 width, uInt32 height, const uInt32* data); void createSurface(uInt32 width, uInt32 height, const uInt32* data);
@ -103,7 +126,7 @@ class FBSurfaceSDL2 : public FBSurface
{ScalingInterpolation::none}; {ScalingInterpolation::none};
SDL_Surface* mySurface{nullptr}; SDL_Surface* mySurface{nullptr};
SDL_Rect mySrcR{0, 0, 0, 0}, myDstR{0, 0, 0, 0}; SDL_Rect mySrcR{-1, -1, -1, -1}, myDstR{-1, -1, -1, -1};
bool myIsVisible{true}; bool myIsVisible{true};
bool myIsStatic{false}; bool myIsStatic{false};

View File

@ -292,11 +292,14 @@ class FBSurface
These methods set the origin point and width/height for the These methods set the origin point and width/height for the
specified service. They are defined as separate x/y and w/h specified service. They are defined as separate x/y and w/h
methods since these items are sometimes set separately. methods since these items are sometimes set separately.
Other times they are set together, so we can use a Rect instead.
*/ */
virtual void setSrcPos(uInt32 x, uInt32 y) = 0; virtual void setSrcPos(uInt32 x, uInt32 y) = 0;
virtual void setSrcSize(uInt32 w, uInt32 h) = 0; virtual void setSrcSize(uInt32 w, uInt32 h) = 0;
virtual void setSrcRect(const Common::Rect& r) = 0;
virtual void setDstPos(uInt32 x, uInt32 y) = 0; virtual void setDstPos(uInt32 x, uInt32 y) = 0;
virtual void setDstSize(uInt32 w, uInt32 h) = 0; virtual void setDstSize(uInt32 w, uInt32 h) = 0;
virtual void setDstRect(const Common::Rect& r) = 0;
/** /**
This method should be called to enable/disable showing the surface This method should be called to enable/disable showing the surface

View File

@ -53,6 +53,18 @@ Dialog::Dialog(OSystem& instance, DialogContainer& parent, const GUI::Font& font
{ {
_flags = Widget::FLAG_ENABLED | Widget::FLAG_BORDER | Widget::FLAG_CLEARBG; _flags = Widget::FLAG_ENABLED | Widget::FLAG_BORDER | Widget::FLAG_CLEARBG;
setTitle(title); setTitle(title);
// Create shading surface
uInt32 data = 0xff000000;
_shadeSurface = instance.frameBuffer().allocateSurface(
1, 1, ScalingInterpolation::sharp, &data);
FBSurface::Attributes& attr = _shadeSurface->attributes();
attr.blending = true;
attr.blendalpha = 25; // darken background dialogs by 25%
_shadeSurface->applyAttributes();
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -251,24 +263,7 @@ void Dialog::render()
{ {
cerr << " shade " << typeid(*this).name() << endl; cerr << " shade " << typeid(*this).name() << endl;
if(_shadeSurface == nullptr) _shadeSurface->setDstRect(_surface->dstRect());
{
uInt32 data = 0xff000000;
_shadeSurface = instance().frameBuffer().allocateSurface(
1, 1, ScalingInterpolation::sharp, &data);
FBSurface::Attributes& attr = _shadeSurface->attributes();
attr.blending = true;
attr.blendalpha = 25; // darken background dialogs by 25%
_shadeSurface->applyAttributes();
}
const Common::Rect& rect = _surface->dstRect();
_shadeSurface->setDstPos(rect.x(), rect.y());
_shadeSurface->setDstSize(rect.w(), rect.h());
_shadeSurface->render(); _shadeSurface->render();
} }
} }

View File

@ -44,8 +44,11 @@ class FBSurfaceLIBRETRO : public FBSurface
const Common::Rect& dstRect() const override { return myDstGUIR; } const Common::Rect& dstRect() const override { return myDstGUIR; }
void setSrcPos(uInt32 x, uInt32 y) override { } void setSrcPos(uInt32 x, uInt32 y) override { }
void setSrcSize(uInt32 w, uInt32 h) override { } void setSrcSize(uInt32 w, uInt32 h) override { }
void setSrcRect(const Common::Rect& r) override { }
void setDstPos(uInt32 x, uInt32 y) override { } void setDstPos(uInt32 x, uInt32 y) override { }
void setDstSize(uInt32 w, uInt32 h) override { } void setDstSize(uInt32 w, uInt32 h) override { }
void setDstRect(const Common::Rect& r) override { }
void setVisible(bool visible) override { } void setVisible(bool visible) override { }
void translateCoords(Int32& x, Int32& y) const override { } void translateCoords(Int32& x, Int32& y) const override { }