Magic_Game/core/Node/Sprite.cpp

133 lines
2.4 KiB
C++

#include "..\e2dnode.h"
e2d::Sprite::Sprite()
: m_pImage(nullptr)
{
}
e2d::Sprite::Sprite(Image * image)
: m_pImage(nullptr)
{
open(image);
}
e2d::Sprite::Sprite(String strFilePath)
: m_pImage(nullptr)
{
open(strFilePath);
}
e2d::Sprite::Sprite(int resNameId, String resType)
: m_pImage(nullptr)
{
open(resNameId, resType);
}
e2d::Sprite::Sprite(String strFilePath, double x, double y, double width, double height)
: m_pImage(nullptr)
{
open(strFilePath);
crop(x, y, width, height);
}
e2d::Sprite::Sprite(int resNameId, String resType, double x, double y, double width, double height)
: m_pImage(nullptr)
{
open(resNameId, resType);
crop(x, y, width, height);
}
e2d::Sprite::~Sprite()
{
}
bool e2d::Sprite::open(Image * image)
{
if (image)
{
SafeRelease(&m_pImage);
m_pImage = image;
m_pImage->retain();
Node::setSize(m_pImage->getWidth(), m_pImage->getHeight());
return true;
}
return false;
}
bool e2d::Sprite::open(String strFilePath)
{
if (!m_pImage)
{
m_pImage = new (std::nothrow) Image();
m_pImage->retain();
}
if (m_pImage->open(strFilePath))
{
Node::setSize(m_pImage->getWidth(), m_pImage->getHeight());
return true;
}
return false;
}
bool e2d::Sprite::open(int resNameId, String resType)
{
if (!m_pImage)
{
m_pImage = new (std::nothrow) Image();
m_pImage->retain();
}
if (m_pImage->open(resNameId, resType))
{
Node::setSize(m_pImage->getWidth(), m_pImage->getHeight());
return true;
}
return false;
}
void e2d::Sprite::crop(double x, double y, double width, double height)
{
m_pImage->crop(x, y, width, height);
Node::setSize(
min(max(width, 0), m_pImage->getSourceWidth() - m_pImage->getCropX()),
min(max(height, 0), m_pImage->getSourceHeight() - m_pImage->getCropY())
);
}
e2d::Image * e2d::Sprite::getImage() const
{
return m_pImage;
}
void e2d::Sprite::onRender()
{
if (m_pImage && m_pImage->getBitmap())
{
// »ñȡͼƬ²Ã¼ôλÖÃ
float fCropX = static_cast<float>(m_pImage->getCropX());
float fCropY = static_cast<float>(m_pImage->getCropY());
// äÖȾͼƬ
Renderer::getRenderTarget()->DrawBitmap(
m_pImage->getBitmap(),
D2D1::RectF(0, 0, m_fWidth, m_fHeight),
m_fDisplayOpacity,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
D2D1::RectF(
fCropX,
fCropY,
fCropX + m_fWidth,
fCropY + m_fHeight
)
);
}
}
void e2d::Sprite::destroy()
{
Node::destroy();
SafeRelease(&m_pImage);
}