Magic_Game/core/Node/Sprite.cpp

117 lines
1.9 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()
: _image(nullptr)
2018-01-30 16:45:38 +08:00
{
}
e2d::Sprite::Sprite(Image * image)
: _image(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 Resource& res)
: _image(nullptr)
2018-01-30 16:45:38 +08:00
{
open(res);
2018-01-30 16:45:38 +08:00
}
e2d::Sprite::Sprite(const Resource& res, const Rect& cropRect)
: _image(nullptr)
2018-01-30 16:45:38 +08:00
{
open(res);
crop(cropRect);
}
e2d::Sprite::Sprite(const String & fileName)
: _image(nullptr)
{
open(fileName);
}
e2d::Sprite::Sprite(const String & fileName, const Rect & cropRect)
: _image(nullptr)
{
open(fileName);
2018-05-22 23:36:46 +08:00
crop(cropRect);
2018-01-30 16:45:38 +08:00
}
e2d::Sprite::~Sprite()
2018-01-30 16:45:38 +08:00
{
2018-07-07 01:43:41 +08:00
GC::safeRelease(_image);
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-07-07 01:43:41 +08:00
GC::release(_image);
_image = image;
2018-07-07 01:43:41 +08:00
GC::retain(_image);
2018-01-30 16:45:38 +08:00
Node::setSize(_image->getWidth(), _image->getHeight());
return true;
}
return false;
}
bool e2d::Sprite::open(const Resource& res)
{
if (!_image)
{
_image = new (e2d::autorelease) Image();
2018-07-07 01:43:41 +08:00
GC::retain(_image);
}
if (_image->open(res))
{
Node::setSize(_image->getWidth(), _image->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(const String & fileName)
2018-01-30 16:45:38 +08:00
{
return open(Resource(fileName));
2018-01-30 16:45:38 +08:00
}
2018-05-22 23:36:46 +08:00
void e2d::Sprite::crop(const Rect& cropRect)
2018-01-30 16:45:38 +08:00
{
2018-05-22 23:36:46 +08:00
_image->crop(cropRect);
2018-03-01 19:28:22 +08:00
Node::setSize(
2018-07-04 12:49:05 +08:00
std::min(std::max(cropRect.size.width, 0.0), _image->getSourceWidth() - _image->getCropX()),
std::min(std::max(cropRect.size.height, 0.0), _image->getSourceHeight() - _image->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
{
return _image;
2018-01-30 16:45:38 +08:00
}
void e2d::Sprite::onRender()
2018-01-30 16:45:38 +08:00
{
if (_image && _image->getBitmap())
2018-01-30 16:45:38 +08:00
{
// <20><>ȡͼƬ<CDBC>ü<EFBFBD>λ<EFBFBD><CEBB>
float fCropX = float(_image->getCropX());
float fCropY = float(_image->getCropY());
2018-02-03 22:04:43 +08:00
// <20><>ȾͼƬ
Renderer::getInstance()->getRenderTarget()->DrawBitmap(
_image->getBitmap(),
D2D1::RectF(0, 0, _width, _height),
_displayOpacity,
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,
fCropX + _width,
fCropY + _height
2018-01-30 16:45:38 +08:00
)
);
}
}