Magic_Game/core/objects/Sprite.cpp

129 lines
2.1 KiB
C++
Raw Normal View History

2018-09-06 23:26:32 +08:00
#include "..\e2dobject.h"
2018-09-05 13:17:07 +08:00
#include "..\e2dmodule.h"
2018-01-30 16:45:38 +08:00
e2d::Sprite::Sprite()
2018-09-04 22:42:34 +08:00
: image_(nullptr)
2018-01-30 16:45:38 +08:00
{
}
e2d::Sprite::Sprite(Image * image)
2018-09-04 22:42:34 +08:00
: image_(nullptr)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
Open(image);
2018-01-30 16:45:38 +08:00
}
e2d::Sprite::Sprite(const Resource& res)
2018-09-04 22:42:34 +08:00
: image_(nullptr)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
Open(res);
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
e2d::Sprite::Sprite(const Resource& res, const Rect& crop_rect)
: image_(nullptr)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
Open(res);
Crop(crop_rect);
}
2018-09-04 22:42:34 +08:00
e2d::Sprite::Sprite(const String & file_name)
: image_(nullptr)
{
2018-09-04 22:42:34 +08:00
Open(file_name);
}
2018-09-04 22:42:34 +08:00
e2d::Sprite::Sprite(const String & file_name, const Rect & crop_rect)
: image_(nullptr)
{
2018-09-04 22:42:34 +08:00
Open(file_name);
Crop(crop_rect);
2018-01-30 16:45:38 +08:00
}
e2d::Sprite::~Sprite()
2018-01-30 16:45:38 +08:00
{
2018-09-07 00:28:54 +08:00
SafeRelease(image_);
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
bool e2d::Sprite::Open(Image * image)
2018-01-30 16:45:38 +08:00
{
if (image)
{
2018-09-07 00:28:54 +08:00
if (image_)
{
image_->Release();
}
2018-09-04 22:42:34 +08:00
image_ = image;
image_->Retain();
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
Node::SetSize(image_->GetWidth(), image_->GetHeight());
return true;
}
return false;
}
2018-09-04 22:42:34 +08:00
bool e2d::Sprite::Open(const Resource& res)
{
2018-09-04 22:42:34 +08:00
if (!image_)
{
2018-09-07 00:28:54 +08:00
image_ = new Image();
2018-09-04 22:42:34 +08:00
image_->Retain();
}
2018-09-04 22:42:34 +08:00
if (image_->Open(res))
{
2018-09-04 22:42:34 +08:00
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
}
2018-09-04 22:42:34 +08:00
bool e2d::Sprite::Open(const String & file_name)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
if (!image_)
{
2018-09-07 00:28:54 +08:00
image_ = new Image();
2018-09-04 22:42:34 +08:00
image_->Retain();
}
2018-09-04 22:42:34 +08:00
if (image_->Open(file_name))
{
2018-09-04 22:42:34 +08:00
Node::SetSize(image_->GetWidth(), image_->GetHeight());
return true;
}
return false;
2018-01-30 16:45:38 +08:00
}
2018-09-30 14:54:43 +08:00
void e2d::Sprite::Crop(const Rect& crop_rect)
{
image_->Crop(crop_rect);
Node::SetSize(
std::min(std::max(crop_rect.size.width, 0.f), image_->GetSourceWidth() - image_->GetCropX()),
std::min(std::max(crop_rect.size.height, 0.f), image_->GetSourceHeight() - image_->GetCropY())
);
}
2018-09-04 22:42:34 +08:00
e2d::Image * e2d::Sprite::GetImage() const
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
return image_;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Sprite::Draw() const
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
if (image_ && image_->GetBitmap())
2018-01-30 16:45:38 +08:00
{
auto crop_pos = image_->GetCropPos();
2018-10-03 18:04:04 +08:00
Device::GetGraphics()->GetRenderTarget()->DrawBitmap(
2018-09-04 22:42:34 +08:00
image_->GetBitmap(),
D2D1::RectF(0, 0, transform_.size.width, transform_.size.height),
2018-09-04 22:42:34 +08:00
display_opacity_,
2018-01-30 16:45:38 +08:00
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
D2D1::RectF(
crop_pos.x,
crop_pos.y,
crop_pos.y + transform_.size.width,
crop_pos.y + transform_.size.height
2018-01-30 16:45:38 +08:00
)
);
}
}