Magic_Game/src/core/Sprite.cpp

105 lines
2.4 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.
#include "Sprite.h"
#include "render.h"
2018-01-30 16:45:38 +08:00
2018-11-08 00:21:59 +08:00
namespace easy2d
2018-01-30 16:45:38 +08:00
{
2018-11-08 00:21:59 +08:00
Sprite::Sprite()
: image_(nullptr)
{
}
2018-01-30 16:45:38 +08:00
Sprite::Sprite(ImagePtr const& image)
2018-11-08 00:21:59 +08:00
: image_(nullptr)
{
Load(image);
}
2018-01-30 16:45:38 +08:00
Sprite::Sprite(Resource const& res)
2018-11-08 00:21:59 +08:00
: image_(nullptr)
{
Load(res);
}
2018-01-30 16:45:38 +08:00
Sprite::Sprite(Resource const& res, const Rect& crop_rect)
2018-11-08 00:21:59 +08:00
: image_(nullptr)
{
Load(res);
Crop(crop_rect);
}
2018-11-08 00:21:59 +08:00
Sprite::~Sprite()
{
}
2018-01-30 16:45:38 +08:00
bool Sprite::Load(ImagePtr const& image)
2018-01-30 16:45:38 +08:00
{
2018-11-08 00:21:59 +08:00
if (image)
2018-09-07 00:28:54 +08:00
{
2018-11-08 00:21:59 +08:00
image_ = image;
2018-01-30 16:45:38 +08:00
2018-11-08 00:21:59 +08:00
Node::SetSize(image_->GetWidth(), image_->GetHeight());
return true;
}
return false;
}
bool Sprite::Load(Resource const& res)
{
2018-11-08 00:21:59 +08:00
if (!image_)
{
2018-11-17 23:24:16 +08:00
image_ = new (std::nothrow) Image;
2018-11-08 00:21:59 +08:00
}
if (image_)
2018-11-08 00:21:59 +08:00
{
if (image_->Load(res))
{
Node::SetSize(image_->GetWidth(), image_->GetHeight());
return true;
}
2018-11-08 00:21:59 +08:00
}
return false;
}
2018-11-08 00:21:59 +08:00
void Sprite::Crop(const Rect& crop_rect)
{
2018-11-08 00:21:59 +08:00
image_->Crop(crop_rect);
Node::SetSize(
2018-11-25 15:00:15 +08:00
std::min(std::max(crop_rect.size.x, 0.f), image_->GetSourceWidth() - image_->GetCropX()),
std::min(std::max(crop_rect.size.y, 0.f), image_->GetSourceHeight() - image_->GetCropY())
2018-11-08 00:21:59 +08:00
);
}
ImagePtr const& Sprite::GetImage() const
{
2018-11-08 00:21:59 +08:00
return image_;
}
2018-01-30 16:45:38 +08:00
2018-11-21 19:24:18 +08:00
void Sprite::OnRender()
2018-01-30 16:45:38 +08:00
{
2018-11-18 20:26:41 +08:00
if (image_)
2018-11-08 00:21:59 +08:00
{
RenderSystem::Instance()->DrawImage(image_, GetBounds());
2018-11-08 00:21:59 +08:00
}
2018-01-30 16:45:38 +08:00
}
2018-11-08 00:21:59 +08:00
}