Magic_Game/src/kiwano/2d/Sprite.cpp

115 lines
2.7 KiB
C++
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// 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:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// 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.
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/Sprite.h>
#include <kiwano/render/RenderContext.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-02-06 16:54:47 +08:00
2020-07-22 21:08:48 +08:00
Sprite::Sprite() {}
Sprite::Sprite(const String& file_path)
2020-02-06 16:54:47 +08:00
{
2020-07-22 21:08:48 +08:00
Load(file_path);
2020-02-06 16:54:47 +08:00
}
2020-07-22 21:08:48 +08:00
Sprite::Sprite(const Resource& res)
2020-02-06 16:54:47 +08:00
{
2020-07-22 21:08:48 +08:00
Load(res);
2020-02-06 16:54:47 +08:00
}
2020-10-10 21:12:37 +08:00
Sprite::Sprite(TexturePtr texture)
2020-02-06 16:54:47 +08:00
{
SetFrame(SpriteFrame(texture));
2020-02-06 16:54:47 +08:00
}
2020-07-22 21:08:48 +08:00
Sprite::Sprite(const String& file_path, const Rect& crop_rect)
2020-02-11 22:56:12 +08:00
{
SetFrame(SpriteFrame(file_path, crop_rect));
2020-02-11 22:56:12 +08:00
}
2020-07-22 21:08:48 +08:00
Sprite::Sprite(const Resource& res, const Rect& crop_rect)
2020-02-11 22:56:12 +08:00
{
SetFrame(SpriteFrame(res, crop_rect));
2020-02-11 22:56:12 +08:00
}
2020-10-10 21:12:37 +08:00
Sprite::Sprite(TexturePtr texture, const Rect& crop_rect)
{
SetFrame(SpriteFrame(texture, crop_rect));
}
Sprite::Sprite(const SpriteFrame& frame)
{
SetFrame(frame);
2020-10-10 21:12:37 +08:00
}
2020-01-21 10:09:55 +08:00
Sprite::~Sprite() {}
2020-10-10 21:12:37 +08:00
bool Sprite::Load(const String& file_path)
2020-01-21 10:09:55 +08:00
{
SpriteFrame frame(file_path);
if (frame.IsValid())
2020-01-21 10:09:55 +08:00
{
SetFrame(frame);
2020-01-21 10:09:55 +08:00
return true;
}
2020-07-26 00:22:32 +08:00
Fail("Sprite::Load failed");
2020-01-21 10:09:55 +08:00
return false;
}
2019-08-18 17:49:13 +08:00
2020-10-10 21:12:37 +08:00
bool Sprite::Load(const Resource& res)
2020-01-21 10:09:55 +08:00
{
SpriteFrame frame(res);
if (frame.IsValid())
2020-01-21 10:09:55 +08:00
{
SetFrame(frame);
2020-01-21 10:09:55 +08:00
return true;
}
2020-07-26 00:22:32 +08:00
Fail("Sprite::Load failed");
2020-01-21 10:09:55 +08:00
return false;
}
void Sprite::SetFrame(const SpriteFrame& frame)
2020-02-13 12:05:20 +08:00
{
frame_ = frame;
SetSize(frame_.GetSize());
2020-02-13 12:05:20 +08:00
if (!frame_.IsValid())
2020-10-08 01:22:10 +08:00
{
Fail("Sprite::SetFrame failed");
2020-10-08 01:22:10 +08:00
}
}
2020-01-21 10:09:55 +08:00
void Sprite::OnRender(RenderContext& ctx)
{
if (frame_.IsValid())
{
ctx.DrawTexture(*frame_.GetTexture(), &frame_.GetCropRect(), &GetBounds());
}
2020-01-21 10:09:55 +08:00
}
2020-01-21 10:09:55 +08:00
bool Sprite::CheckVisibility(RenderContext& ctx) const
{
return frame_.IsValid() && Actor::CheckVisibility(ctx);
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano