Magic_Game/core/base/Sprite.cpp

137 lines
2.9 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"
#include <algorithm>
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(spImage 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);
}
Sprite::Sprite(String const& file_name)
2018-11-08 00:21:59 +08:00
: image_(nullptr)
{
Load(file_name);
}
Sprite::Sprite(String const& file_name, const Rect & crop_rect)
2018-11-08 00:21:59 +08:00
: image_(nullptr)
{
Load(file_name);
Crop(crop_rect);
}
2018-01-30 16:45:38 +08:00
2018-11-08 00:21:59 +08:00
Sprite::~Sprite()
{
}
2018-01-30 16:45:38 +08:00
bool Sprite::Load(spImage 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;
}
bool Sprite::Load(String const& file_name)
{
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(file_name))
{
Node::SetSize(image_->GetWidth(), image_->GetHeight());
return true;
}
2018-11-08 00:21:59 +08:00
}
return false;
2018-01-30 16:45:38 +08:00
}
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(
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())
);
}
spImage const& Sprite::GetImage() const
{
2018-11-08 00:21:59 +08:00
return image_;
}
2018-01-30 16:45:38 +08:00
void Sprite::OnDraw()
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
{
2018-11-18 20:26:41 +08:00
devices::Graphics::Instance()->DrawImage(image_);
2018-11-08 00:21:59 +08:00
}
2018-01-30 16:45:38 +08:00
}
2018-11-08 00:21:59 +08:00
}