Magic_Game/Easy2D/Node/ESpriteFrame.cpp

95 lines
2.2 KiB
C++
Raw Normal View History

#include "..\enodes.h"
e2d::ESpriteFrame::ESpriteFrame()
: m_fSourceClipX(0)
, m_fSourceClipY(0)
, m_fSourceClipWidth(0)
, m_fSourceClipHeight(0)
, m_pTexture(nullptr)
{
}
e2d::ESpriteFrame::ESpriteFrame(ETexture * texture)
: ESpriteFrame()
{
_setTexture(texture);
}
e2d::ESpriteFrame::ESpriteFrame(const EString & imageFileName)
: ESpriteFrame()
{
_setTexture(new ETexture(imageFileName));
}
e2d::ESpriteFrame::ESpriteFrame(const EString & resourceName, const EString & resourceType)
: ESpriteFrame()
{
_setTexture(new ETexture(resourceName, resourceType));
}
e2d::ESpriteFrame::ESpriteFrame(ETexture * texture, float x, float y, float width, float height)
: ESpriteFrame()
{
_setTexture(texture);
_clipTexture(x, y, width, height);
}
e2d::ESpriteFrame::ESpriteFrame(const EString & imageFileName, float x, float y, float width, float height)
: ESpriteFrame()
{
_setTexture(new ETexture(imageFileName));
_clipTexture(x, y, width, height);
}
e2d::ESpriteFrame::ESpriteFrame(const EString & resourceName, const EString & resourceType, float x, float y, float width, float height)
: ESpriteFrame()
{
_setTexture(new ETexture(resourceName, resourceType));
_clipTexture(x, y, width, height);
}
e2d::ESpriteFrame::~ESpriteFrame()
{
2017-10-21 19:09:31 +08:00
SafeRelease(&m_pTexture);
}
float e2d::ESpriteFrame::getWidth() const
{
return m_fSourceClipWidth;
}
float e2d::ESpriteFrame::getHeight() const
{
return m_fSourceClipHeight;
}
e2d::ETexture * e2d::ESpriteFrame::getTexture() const
{
return m_pTexture;
}
void e2d::ESpriteFrame::_setTexture(ETexture * texture)
{
if (texture)
{
2017-10-21 19:09:31 +08:00
SafeRelease(&m_pTexture);
m_pTexture = texture;
m_pTexture->retain();
m_fSourceClipX = 0;
m_fSourceClipY = 0;
m_fSourceClipWidth = texture->getSourceWidth();
m_fSourceClipHeight = texture->getSourceHeight();
}
}
void e2d::ESpriteFrame::_clipTexture(float x, float y, float width, float height)
{
if (m_pTexture)
{
m_fSourceClipX = min(max(x, 0), m_pTexture->getSourceWidth());
m_fSourceClipY = min(max(y, 0), m_pTexture->getSourceHeight());
m_fSourceClipWidth = min(max(width, 0), m_pTexture->getSourceWidth() - m_fSourceClipX);
m_fSourceClipHeight = min(max(height, 0), m_pTexture->getSourceHeight() - m_fSourceClipY);
}
}