Magic_Game/Easy2D/Node/ESpriteFrame.cpp

86 lines
2.0 KiB
C++

#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()
{
if (m_pTexture)
{
m_pTexture->release();
}
}
void e2d::ESpriteFrame::_setTexture(ETexture * texture)
{
if (texture)
{
if (m_pTexture)
{
m_pTexture->release();
}
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);
}
}