Magic_Game/core/Node/Sprite.cpp

133 lines
2.3 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-05-08 17:40:36 +08:00
: _pImage(nullptr)
2018-01-30 16:45:38 +08:00
{
}
e2d::Sprite::Sprite(Image * image)
2018-05-08 17:40:36 +08:00
: _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(const String& filePath)
2018-05-08 17:40:36 +08:00
: _pImage(nullptr)
2018-01-30 16:45:38 +08:00
{
open(filePath);
2018-01-30 16:45:38 +08:00
}
e2d::Sprite::Sprite(int resNameId, const String& resType)
2018-05-08 17:40:36 +08:00
: _pImage(nullptr)
2018-01-30 16:45:38 +08:00
{
open(resNameId, resType);
}
e2d::Sprite::Sprite(const String& filePath, double x, double y, double width, double height)
2018-05-08 17:40:36 +08:00
: _pImage(nullptr)
{
open(filePath);
crop(x, y, width, height);
}
e2d::Sprite::Sprite(int resNameId, const String& resType, double x, double y, double width, double height)
2018-05-08 17:40:36 +08:00
: _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)
{
2018-05-08 17:40:36 +08:00
SafeRelease(&_pImage);
_pImage = image;
_pImage->retain();
2018-01-30 16:45:38 +08:00
2018-05-08 17:40:36 +08:00
Node::setSize(_pImage->getWidth(), _pImage->getHeight());
return true;
}
return false;
}
bool e2d::Sprite::open(const String& filePath)
{
2018-05-08 17:40:36 +08:00
if (!_pImage)
{
2018-05-08 17:40:36 +08:00
_pImage = new (std::nothrow) Image();
_pImage->retain();
}
2018-05-08 17:40:36 +08:00
if (_pImage->open(filePath))
{
2018-05-08 17:40:36 +08:00
Node::setSize(_pImage->getWidth(), _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, const String& resType)
2018-01-30 16:45:38 +08:00
{
2018-05-08 17:40:36 +08:00
if (!_pImage)
{
2018-05-08 17:40:36 +08:00
_pImage = new (std::nothrow) Image();
_pImage->retain();
}
2018-05-08 17:40:36 +08:00
if (_pImage->open(resNameId, resType))
{
2018-05-08 17:40:36 +08:00
Node::setSize(_pImage->getWidth(), _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-05-08 17:40:36 +08:00
_pImage->crop(x, y, width, height);
2018-03-01 19:28:22 +08:00
Node::setSize(
2018-05-08 17:40:36 +08:00
min(max(width, 0), _pImage->getSourceWidth() - _pImage->getCropX()),
min(max(height, 0), _pImage->getSourceHeight() - _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-05-08 17:40:36 +08:00
return _pImage;
2018-01-30 16:45:38 +08:00
}
void e2d::Sprite::onRender()
2018-01-30 16:45:38 +08:00
{
2018-05-08 17:40:36 +08:00
if (_pImage && _pImage->getBitmap())
2018-01-30 16:45:38 +08:00
{
// <20><>ȡͼƬ<CDBC>ü<EFBFBD>λ<EFBFBD><CEBB>
2018-05-08 17:40:36 +08:00
float fCropX = static_cast<float>(_pImage->getCropX());
float fCropY = static_cast<float>(_pImage->getCropY());
2018-02-03 22:04:43 +08:00
// <20><>ȾͼƬ
Renderer::getRenderTarget()->DrawBitmap(
2018-05-08 17:40:36 +08:00
_pImage->getBitmap(),
D2D1::RectF(0, 0, _fWidth, _fHeight),
_fDisplayOpacity,
2018-01-30 16:45:38 +08:00
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
D2D1::RectF(
2018-04-06 11:31:24 +08:00
fCropX,
fCropY,
2018-05-08 17:40:36 +08:00
fCropX + _fWidth,
fCropY + _fHeight
2018-01-30 16:45:38 +08:00
)
);
}
}
void e2d::Sprite::onDestroy()
{
Node::onDestroy();
2018-05-08 17:40:36 +08:00
SafeRelease(&_pImage);
}