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()
: _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 String& filePath)
: _image(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)
: _image(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)
: _image(nullptr)
{
open(filePath);
crop(x, y, width, height);
}
e2d::Sprite::Sprite(int resNameId, const String& resType, double x, double y, double width, double height)
: _image(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-19 01:10:37 +08:00
GC::release(_image);
_image = image;
_image->retain();
2018-01-30 16:45:38 +08:00
Node::setSize(_image->getWidth(), _image->getHeight());
return true;
}
return false;
}
bool e2d::Sprite::open(const String& filePath)
{
if (!_image)
{
2018-05-19 01:10:37 +08:00
_image = GC::create<Image>();
_image->retain();
}
if (_image->open(filePath))
{
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(int resNameId, const String& resType)
2018-01-30 16:45:38 +08:00
{
if (!_image)
{
2018-05-19 01:10:37 +08:00
_image = GC::create<Image>();
_image->retain();
}
if (_image->open(resNameId, resType))
{
Node::setSize(_image->getWidth(), _image->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
{
_image->crop(x, y, width, height);
2018-03-01 19:28:22 +08:00
Node::setSize(
min(max(width, 0), _image->getSourceWidth() - _image->getCropX()),
min(max(height, 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::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
)
);
}
}
void e2d::Sprite::onDestroy()
{
Node::onDestroy();
2018-05-19 01:10:37 +08:00
GC::release(_image);
}