Magic_Game/core/objects/Sprite.cpp

149 lines
3.3 KiB
C++
Raw Normal View History

2018-10-03 22:02:46 +08:00
// Copyright (c) 2016-2018 Easy2D - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
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
2018-10-16 14:13:15 +08:00
easy2d::Sprite::Sprite()
2018-09-04 22:42:34 +08:00
: image_(nullptr)
2018-01-30 16:45:38 +08:00
{
}
2018-10-16 14:13:15 +08:00
easy2d::Sprite::Sprite(Image * image)
2018-09-04 22:42:34 +08:00
: image_(nullptr)
2018-01-30 16:45:38 +08:00
{
2018-10-06 10:25:29 +08:00
Load(image);
2018-01-30 16:45:38 +08:00
}
2018-10-16 14:13:15 +08:00
easy2d::Sprite::Sprite(const Resource& res)
2018-09-04 22:42:34 +08:00
: image_(nullptr)
2018-01-30 16:45:38 +08:00
{
2018-10-06 10:25:29 +08:00
Load(res);
2018-01-30 16:45:38 +08:00
}
2018-10-16 14:13:15 +08:00
easy2d::Sprite::Sprite(const Resource& res, const Rect& crop_rect)
2018-09-04 22:42:34 +08:00
: image_(nullptr)
2018-01-30 16:45:38 +08:00
{
2018-10-06 10:25:29 +08:00
Load(res);
2018-09-04 22:42:34 +08:00
Crop(crop_rect);
}
2018-10-16 14:13:15 +08:00
easy2d::Sprite::Sprite(const String & file_name)
2018-09-04 22:42:34 +08:00
: image_(nullptr)
{
2018-10-06 10:25:29 +08:00
Load(file_name);
}
2018-10-16 14:13:15 +08:00
easy2d::Sprite::Sprite(const String & file_name, const Rect & crop_rect)
2018-09-04 22:42:34 +08:00
: image_(nullptr)
{
2018-10-06 10:25:29 +08:00
Load(file_name);
2018-09-04 22:42:34 +08:00
Crop(crop_rect);
2018-01-30 16:45:38 +08:00
}
2018-10-16 14:13:15 +08:00
easy2d::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-10-16 14:13:15 +08:00
bool easy2d::Sprite::Load(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-10-16 14:13:15 +08:00
bool easy2d::Sprite::Load(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-10-06 10:25:29 +08:00
if (image_->Load(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-10-16 14:13:15 +08:00
bool easy2d::Sprite::Load(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-10-06 10:25:29 +08:00
if (image_->Load(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-10-16 14:13:15 +08:00
void easy2d::Sprite::Crop(const Rect& crop_rect)
2018-09-30 14:54:43 +08:00
{
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-10-16 14:13:15 +08:00
easy2d::Image * easy2d::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-10-16 14:13:15 +08:00
void easy2d::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
)
);
}
}