Magic_Game/core/Node/Sprite.cpp

133 lines
2.4 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dnode.h"
2018-01-30 16:45:38 +08:00
e2d::Sprite::Sprite()
2018-02-03 22:04:43 +08:00
: m_pImage(nullptr)
2018-01-30 16:45:38 +08:00
{
}
e2d::Sprite::Sprite(Image * image)
2018-02-03 22:04:43 +08:00
: m_pImage(nullptr)
2018-01-30 16:45:38 +08:00
{
2018-03-02 23:49:57 +08:00
open(image);
2018-01-30 16:45:38 +08:00
}
e2d::Sprite::Sprite(String strFilePath)
2018-02-03 22:04:43 +08:00
: m_pImage(nullptr)
2018-01-30 16:45:38 +08:00
{
open(strFilePath);
2018-01-30 16:45:38 +08:00
}
e2d::Sprite::Sprite(int resNameId, String resType)
2018-02-03 22:04:43 +08:00
: m_pImage(nullptr)
2018-01-30 16:45:38 +08:00
{
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);
2018-04-06 11:31:24 +08:00
crop(x, y, width, height);
2018-01-30 16:45:38 +08:00
}
e2d::Sprite::~Sprite()
2018-01-30 16:45:38 +08:00
{
}
bool e2d::Sprite::open(Image * image)
2018-01-30 16:45:38 +08:00
{
if (image)
{
SafeRelease(&m_pImage);
m_pImage = image;
m_pImage->retain();
2018-03-01 19:28:22 +08:00
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;
2018-01-30 16:45:38 +08:00
}
return false;
2018-01-30 16:45:38 +08:00
}
bool e2d::Sprite::open(int resNameId, String resType)
2018-01-30 16:45:38 +08:00
{
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;
2018-01-30 16:45:38 +08:00
}
2018-04-06 11:31:24 +08:00
void e2d::Sprite::crop(double x, double y, double width, double height)
2018-01-30 16:45:38 +08:00
{
2018-04-06 11:31:24 +08:00
m_pImage->crop(x, y, width, height);
2018-03-01 19:28:22 +08:00
Node::setSize(
2018-04-06 11:31:24 +08:00
min(max(width, 0), m_pImage->getSourceWidth() - m_pImage->getCropX()),
min(max(height, 0), m_pImage->getSourceHeight() - m_pImage->getCropY())
2018-02-03 22:04:43 +08:00
);
2018-01-30 16:45:38 +08:00
}
e2d::Image * e2d::Sprite::getImage() const
2018-01-30 16:45:38 +08:00
{
2018-02-03 22:04:43 +08:00
return m_pImage;
2018-01-30 16:45:38 +08:00
}
void e2d::Sprite::onRender()
2018-01-30 16:45:38 +08:00
{
2018-02-03 22:04:43 +08:00
if (m_pImage && m_pImage->getBitmap())
2018-01-30 16:45:38 +08:00
{
// <20><>ȡͼƬ<CDBC>ü<EFBFBD>λ<EFBFBD><CEBB>
2018-04-06 11:31:24 +08:00
float fCropX = static_cast<float>(m_pImage->getCropX());
float fCropY = static_cast<float>(m_pImage->getCropY());
2018-02-03 22:04:43 +08:00
// <20><>ȾͼƬ
Renderer::getRenderTarget()->DrawBitmap(
2018-02-03 22:04:43 +08:00
m_pImage->getBitmap(),
2018-02-27 21:07:43 +08:00
D2D1::RectF(0, 0, m_fWidth, m_fHeight),
2018-01-30 16:45:38 +08:00
m_fDisplayOpacity,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
D2D1::RectF(
2018-04-06 11:31:24 +08:00
fCropX,
fCropY,
fCropX + m_fWidth,
fCropY + m_fHeight
2018-01-30 16:45:38 +08:00
)
);
}
}
void e2d::Sprite::destroy()
{
Node::destroy();
SafeRelease(&m_pImage);
}