Kiwano Engine v1.3.x
Sprite.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/2d/Actor.h>
23
24namespace kiwano
25{
26
36class KGE_API Sprite : public Actor
37{
38public:
39 Sprite();
40
45 Sprite(RefPtr<Image> image, const Rect& src_rect = Rect());
46
49 RefPtr<Image> GetImage() const;
50
53 RefPtr<Bitmap> GetBitmap() const;
54
60 void SetImage(RefPtr<Image> image, const Rect& src_rect = Rect(), bool reset_size = true);
61
67 void SetBitmap(RefPtr<Bitmap> bitmap, const Rect& src_rect = Rect(), bool reset_size = true);
68
71 Rect GetSourceRect() const;
72
76 void SetSourceRect(const Rect& src_rect);
77
78 void OnRender(RenderContext& ctx) override;
79
80protected:
81 bool CheckVisibility(RenderContext& ctx) const override;
82
83private:
84 bool is_bitmap_ = false;
85 RefPtr<Image> image_;
86 Rect src_rect_;
87};
88
92{
93 return image_;
94}
95
97{
98 return is_bitmap_ ? RefPtr<Bitmap>(dynamic_cast<Bitmap*>(image_.Get())) : nullptr;
99}
100
102{
103 return src_rect_;
104}
105
106inline void Sprite::SetSourceRect(const Rect& src_rect)
107{
108 src_rect_ = src_rect;
109}
110
111} // namespace kiwano
角色
Definition: Actor.h:63
位图
Definition: Bitmap.h:95
引用计数智能指针
Definition: RefBasePtr.hpp:35
渲染上下文
Definition: RenderContext.h:62
精灵
Definition: Sprite.h:37
RefPtr< Image > GetImage() const
获取图像
Definition: Sprite.h:91
void SetSourceRect(const Rect &src_rect)
设置源矩形
Definition: Sprite.h:106
RefPtr< Bitmap > GetBitmap() const
获取位图(仅当设置为位图时生效)
Definition: Sprite.h:96
Rect GetSourceRect() const
获取源矩形
Definition: Sprite.h:101