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#include <kiwano/2d/SpriteFrame.h>
24
25namespace kiwano
26{
27
37class KGE_API Sprite : public Actor
38{
39public:
40 Sprite();
41
45 Sprite(StringView file_path);
46
50 Sprite(const Resource& res);
51
55 Sprite(RefPtr<Texture> texture);
56
61 Sprite(StringView file_path, const Rect& crop_rect);
62
67 Sprite(const Resource& res, const Rect& crop_rect);
68
73 Sprite(RefPtr<Texture> texture, const Rect& crop_rect);
74
78 Sprite(const SpriteFrame& frame);
79
80 virtual ~Sprite();
81
85 bool Load(StringView file_path);
86
90 bool Load(const Resource& res);
91
94 RefPtr<Texture> GetTexture() const;
95
98 Rect GetCropRect() const;
99
102 SpriteFrame GetFrame() const;
103
107 void SetCropRect(const Rect& crop_rect);
108
112 void SetFrame(const SpriteFrame& frame);
113
114 void OnRender(RenderContext& ctx) override;
115
116protected:
117 bool CheckVisibility(RenderContext& ctx) const override;
118
119private:
120 SpriteFrame frame_;
121};
122
126{
127 return frame_.GetTexture();
128}
129
131{
132 return frame_.GetCropRect();
133}
134
136{
137 return frame_;
138}
139
140inline void Sprite::SetCropRect(const Rect& crop_rect)
141{
142 frame_.SetCropRect(crop_rect);
143}
144
145} // namespace kiwano
角色
Definition: Actor.h:63
引用计数智能指针
Definition: RefBasePtr.hpp:35
渲染上下文
Definition: RenderContext.h:62
资源
Definition: Resource.h:41
精灵帧
Definition: SpriteFrame.h:34
RefPtr< Texture > GetTexture() const
获取纹理
Definition: SpriteFrame.h:131
void SetCropRect(const Rect &crop_rect)
裁剪精灵帧为矩形
Definition: SpriteFrame.cpp:84
const Rect & GetCropRect() const
获取裁剪矩形
Definition: SpriteFrame.h:126
精灵
Definition: Sprite.h:38
Rect GetCropRect() const
获取裁剪矩形
Definition: Sprite.h:130
void SetCropRect(const Rect &crop_rect)
使用矩形区域裁剪精灵
Definition: Sprite.h:140
RefPtr< Texture > GetTexture() const
获取图像
Definition: Sprite.h:125
SpriteFrame GetFrame() const
获取精灵帧
Definition: Sprite.h:135