sprite.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Kenneth Gangstoe
27 ** Mark Page
28 */
29
30 #pragma once
31
32 #include <memory>
33 #include "../../Core/Math/origin.h"
34 #include "../../Core/Signals/signal.h"
35 #include "../../Core/IOData/file_system.h"
36 #include "../../Core/Resources/resource.h"
37 #include "../Render/graphic_context.h"
38 #include "../Image/image_import_description.h"
39 #include "color.h"
40
41 namespace clan
42 {
45
46 class Sprite_Impl;
47 class CollisionOutline;
48 class Canvas;
49 class ResourceManager;
50 class Font_Impl;
51 class Subtexture;
52 class XMLResourceDocument;
53
55 class Sprite
56 {
59 public:
60 enum ShowOnFinish
61 {
62 show_blank, show_last_frame, show_first_frame
63 };
65
68 public:
70 Sprite();
71
76 Sprite(Canvas &canvas, const std::string &fullname, const ImageImportDescription &import_desc = ImageImportDescription ());
77
83 Sprite(Canvas &canvas, const std::string &filename, const FileSystem &file_system, const ImageImportDescription &import_desc = ImageImportDescription ());
84
90 Sprite(Canvas &canvas, IODevice &file, const std::string &image_type, const ImageImportDescription &import_desc = ImageImportDescription ());
91
95 Sprite(Canvas &canvas);
96
97 virtual ~Sprite();
99
102 public:
108 static Resource<Sprite> resource(Canvas &canvas, const std::string &id, const ResourceManager &resources);
109
111 static Sprite load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc);
113
116 public:
118 bool is_null() const { return !impl; }
119
121 void throw_if_null() const;
122
124 Angle get_angle() const;
125
127 Angle get_base_angle() const;
128
130
131 void get_scale(float &x, float &y) const;
132
134
135 float get_alpha() const;
136
138
139 Colorf get_color() const;
140
142 bool get_linear_filter() const;
143
145 void get_alignment(Origin &origin, int &x, int &y) const;
146
148 void get_rotation_hotspot(Origin &origin, int &x, int &y) const;
149
151 int get_current_frame() const;
152
154 int get_frame_count() const;
155
160 int get_frame_delay(int frame) const;
161
163 Point get_frame_offset(int frame) const;
164
166 Size get_frame_size(int frame) const;
167
169 Subtexture get_frame_texture(int frame) const;
170
172 int get_width() const;
173
175 int get_height() const;
176
178 Size get_size() const;
179
181 int get_id() const;
182
184 bool is_play_loop() const;
185
187 bool is_play_backward() const;
188
190 bool is_play_pingpong() const;
191
193
194 ShowOnFinish get_show_on_finish() const;
195
197
199 bool is_finished() const;
200
202 bool is_looping() const;
203
205
208 public:
210 bool operator==(const Sprite &other) const
211 {
212 return impl==other.impl;
213 }
214
216 bool operator!=(const Sprite &other) const
217 {
218 return impl!=other.impl;
219 }
220
222 bool operator<(const Sprite &other) const
223 {
224 return impl < other.impl;
225 }
227
230 public:
232 Sprite &operator =(const Sprite &copy);
233
235
236 void set_image_data(const Sprite &image_source);
237
239 Sprite clone() const;
240
247 void draw(
248 Canvas &canvas,
249 float x,
250 float y);
251
252 void draw(
253 Canvas &canvas,
254 int x,
255 int y);
256
257 void draw(
258 Canvas &canvas,
259 const Rectf &src,
260 const Rectf &dest);
261
262 void draw(
263 Canvas &canvas,
264 const Rectf &dest);
265
269 void update(int time_elapsed_ms);
270
272 void set_angle(Angle angle);
273
275 void set_angle_pitch(Angle angle);
276
278 void set_angle_yaw(Angle angle);
279
281 void rotate(Angle angle);
282
284 void rotate_pitch(Angle angle);
285
287 void rotate_yaw(Angle angle);
288
290 void set_base_angle(Angle angle);
291
293
294 void set_scale(float x, float y);
295
297
298 void set_alpha(float alpha);
299
301
302 void set_color(const Colorf &color);
303
307 void set_color(const Color& c) {Colorf color; color.r = c.get_red() / 255.0f; color.g = c.get_green() / 255.0f; color.b = c.get_blue() / 255.0f; color.a = c.get_alpha() / 255.0f; set_color(color);}
308
310 void set_linear_filter(bool linear_filter = true);
311
313 void set_alignment(Origin origin, int x = 0, int y = 0);
314
316 void set_rotation_hotspot(Origin origin, int x = 0, int y = 0);
317
320 void set_frame(unsigned int frame);
321
325 void set_delay(int delay_ms);
326
331 void set_frame_delay(int frame, int delay_ms);
332
334 void set_frame_offset(int frame, Point offset);
335
337 void set_id(int id);
338
340
342 void finish();
343
345 void restart();
346
348 void set_play_loop(bool loop = true);
349
351 void set_play_pingpong(bool pingpong = true);
352
354 void set_play_backward(bool backward = true);
355
357 void set_show_on_finish(Sprite::ShowOnFinish show_on_finish);
358
362 void add_frame(const Texture2D &texture);
363
367 void add_frame(Canvas &canvas, const std::string &fullname, const ImageImportDescription &import_desc = ImageImportDescription ());
368
373 void add_frame(Canvas &canvas, IODevice &file, const std::string &image_type, const ImageImportDescription &import_desc = ImageImportDescription ());
374
379 void add_frame(Canvas &canvas, const std::string &filename, const FileSystem &file_system, const ImageImportDescription &import_desc = ImageImportDescription ());
380
386 void add_frames(const Texture2D &texture, Rect *frames, int num_frames);
387
392 void add_frame(const Texture2D &texture, const Rect &frame);
393
395
403 void add_gridclipped_frames(Canvas &canvas,
404 const Texture2D &texture,
405 int xpos, int ypos,
406 int width, int height,
407 int xarray = 1, int yarray = 1,
408 int array_skipframes = 0,
409 int xspacing = 0, int yspacing = 0);
410
412
421 void add_alphaclipped_frames(Canvas &canvas,
422 const Texture2D &texture,
423 int xpos = 0, int ypos = 0,
424 float trans_limit = 0.05f);
425
427
435 void add_alphaclipped_frames_free(Canvas &canvas,
436 const Texture2D &texture,
437 int xpos = 0, int ypos = 0,
438 float trans_limit = 0.05f);
439
441
444 public:
448 Signal<void()> &sig_animation_finished();
450
453 private:
454 std::shared_ptr<Sprite_Impl> impl;
455
456 friend class FontFamily_Impl;
457
459 };
460
461 }
462
clan::Sprite::add_alphaclipped_frames_free
void add_alphaclipped_frames_free(Canvas &canvas, const Texture2D &texture, int xpos=0, int ypos=0, float trans_limit=0.05f)
Adds images separated with pure alpha (within trans_limit).
clan
Definition: clanapp.h:35
clan::Sprite::throw_if_null
void throw_if_null() const
Throw an exception if this object is invalid.
clan::Colorf
Floating point color description class (for float).
Definition: color.h:660
clan::Subtexture
Sub-texture description.
Definition: subtexture.h:45
clan::Sprite::set_angle_yaw
void set_angle_yaw(Angle angle)
Set absolute rotation yaw angle.
clan::Sprite::set_frame
void set_frame(unsigned int frame)
Sets current animation frame. 0 is first frame.
clan::Sprite::get_angle
Angle get_angle() const
Returns current angle in degrees.
clan::Sprite::is_looping
bool is_looping() const
Returns true if animation has looped in the last update cycle.
clan::Sprite::~Sprite
virtual ~Sprite()
clan::Angle
Angle class.
Definition: angle.h:62
clan::Resource
Resource proxy of a specific type.
Definition: resource.h:58
clan::Sprite::get_frame_delay
int get_frame_delay(int frame) const
Returns the delay of a frame.
clan::Canvas
2D Graphics Canvas
Definition: canvas.h:72
clan::IODevice
I/O Device interface.
Definition: iodevice.h:50
clan::Sprite::operator=
Sprite & operator=(const Sprite &copy)
Copy assignment operator.
clan::Sprite::set_alpha
void set_alpha(float alpha)
Sets transparency.
clan::Color::get_green
unsigned char get_green() const
Returns the green color component, in the range 0-255.
Definition: color.h:90
clan::Sprite::get_scale
void get_scale(float &x, float &y) const
Returns scale for x and y.
clan::Sprite::set_play_loop
void set_play_loop(bool loop=true)
Set to true if animation should loop, false otherwise.
clan::Color::get_blue
unsigned char get_blue() const
Returns the blue color component, in the range 0-255.
Definition: color.h:93
clan::Origin
Origin
Alignment origins.
Definition: origin.h:40
clan::Sprite::get_frame_offset
Point get_frame_offset(int frame) const
Returns the translation offset of a frame. 0 is first frame.
clan::Key::x
clan::Sprite::FontFamily_Impl
friend class FontFamily_Impl
Definition: sprite.h:456
clan::Sprite::show_first_frame
Definition: sprite.h:62
clan::ImageImportDescription
Image Import Description Class.
Definition: image_import_description.h:48
clan::Sprite
Sprite class.
Definition: sprite.h:55
clan::Sprite::resource
static Resource< Sprite > resource(Canvas &canvas, const std::string &id, const ResourceManager &resources)
Retrieves a Sprite resource from the resource manager.
clan::Color::get_alpha
unsigned char get_alpha() const
Returns the alpha color component, in the range 0-255.
Definition: color.h:84
clan::Sprite::set_frame_offset
void set_frame_offset(int frame, Point offset)
Sets the translate offset of a specific frame. 0 is first frame.
clan::Sprite::update
void update(int time_elapsed_ms)
Call this function to update the animation.
clan::Sprite::set_rotation_hotspot
void set_rotation_hotspot(Origin origin, int x=0, int y=0)
Sets rotation hotspot.
clan::Sprite::restart
void restart()
Restart animation.
clan::Color::get_red
unsigned char get_red() const
Returns the red color component, in the range 0-255.
Definition: color.h:87
clan::Sprite::is_play_backward
bool is_play_backward() const
Returns true if animation is played in from right to left (starts at end).
clan::Sprite::set_alignment
void set_alignment(Origin origin, int x=0, int y=0)
Sets translation hotspot.
clan::Sprite::get_height
int get_height() const
Return the height of the current frame, shortcut for 'get_frame_size(get_current_frame()).height'.
clan::Sprite::rotate_yaw
void rotate_yaw(Angle angle)
Add angle in degrees to current yaw angle.
clan::Sprite::load
static Sprite load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc)
Loads a Sprite from a XML resource definition.
clan::Sprite::get_base_angle
Angle get_base_angle() const
Returns the base angle in degrees - angle added to any rotation set with set_rotate() or rotate()...
clan::Sprite::set_play_pingpong
void set_play_pingpong(bool pingpong=true)
Set to true if animation should loop, false otherwise.
clan::StyleTokenType::string
@ keyword
clan::Rect
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:470
clan::Vec4::a
Type a
Definition: vec4.h:83
clan::Sprite::finish
void finish()
Finish animation.
clan::Sprite::is_finished
bool is_finished() const
Returns true if animation is finished.
clan::Sprite::set_color
void set_color(const Colorf &color)
Sets the color.
clan::Sprite::get_frame_count
int get_frame_count() const
Returns number of frames in animation.
clan::Texture2D
2D texture object class.
Definition: texture_2d.h:41
clan::Sprite::rotate
void rotate(Angle angle)
Add angle in degrees to current angle.
clan::Vec4::r
Type r
Definition: vec4.h:80
clan::Sprite::get_frame_size
Size get_frame_size(int frame) const
Returns the size of a frame. 0 is first frame.
clan::Key::f
clan::Rectf
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:484
clan::Sprite::get_id
int get_id() const
Returns the attached id (if exists).
clan::Key::y
clan::FileSystem
Virtual File System (VFS).
Definition: file_system.h:47
clan::Sprite::set_id
void set_id(int id)
Sets an attached id.
clan::Sprite::Sprite
Sprite()
Constructs a null instance.
clan::Sprite::set_scale
void set_scale(float x, float y)
Set scale for x and y directions individually.
clan::ResourceManager
Resource manager.
Definition: resource_manager.h:44
clan::StyleValueType::angle
value is a color
clan::Sprite::set_image_data
void set_image_data(const Sprite &image_source)
Sets the image data from another sprite, sharing animation state.
clan::Sprite::operator!=
bool operator!=(const Sprite &other) const
Inequality operator.
Definition: sprite.h:216
clan::Sprite::set_base_angle
void set_base_angle(Angle angle)
Sets the base angle in degrees - angle added to any rotation set with set_rotate() or rotate()...
clan::Sprite::set_linear_filter
void set_linear_filter(bool linear_filter=true)
Set to true if a linear filter should be used for scaling up and down, false if a nearest-point filte...
clan::Sprite::set_delay
void set_delay(int delay_ms)
Sets the delay for all frames.
clan::Sprite::get_size
Size get_size() const
Return the size of the current frame, shortcut for 'get_frame_size(get_current_frame())'.
clan::Signal< void()>
clan::Sprite::set_color
void set_color(const Color &c)
Set color.
Definition: sprite.h:307
clan::Sprite::rotate_pitch
void rotate_pitch(Angle angle)
Add angle in degrees to current pitch angle.
clan::Sprite::add_alphaclipped_frames
void add_alphaclipped_frames(Canvas &canvas, const Texture2D &texture, int xpos=0, int ypos=0, float trans_limit=0.05f)
Adds images separated with pure alpha (within trans_limit).
clan::Sprite::get_current_frame
int get_current_frame() const
Returns current frame in animation. 0 is first frame.
clan::Sprite::get_width
int get_width() const
Return the width of the current frame, shortcut for 'get_frame_size(get_current_frame()).width'.
clan::Sprite::add_frame
void add_frame(const Texture2D &texture)
Add frame.
clan::Sprite::get_frame_texture
Subtexture get_frame_texture(int frame) const
Return the texture of a frame. 0 is first frame.
clan::Sprite::set_angle_pitch
void set_angle_pitch(Angle angle)
Set absolute rotation pitch angle.
clan::Sprite::operator<
bool operator<(const Sprite &other) const
Less than operator.
Definition: sprite.h:222
clan::Sprite::is_play_pingpong
bool is_play_pingpong() const
Returns true if animation is played in ping-pong mode.
clan::Sprite::is_play_loop
bool is_play_loop() const
Returns true if animation is played in loop (more than once).
clan::Point
2D (x,y) point structure - Integer
Definition: point.h:62
clan::Sprite::draw
void draw(Canvas &canvas, float x, float y)
Draw sprite on graphic context.
clan::Sprite::add_gridclipped_frames
void add_gridclipped_frames(Canvas &canvas, const Texture2D &texture, int xpos, int ypos, int width, int height, int xarray=1, int yarray=1, int array_skipframes=0, int xspacing=0, int yspacing=0)
Adds images formed in a grid.
clan::Sprite::show_last_frame
Definition: sprite.h:62
clan::Sprite::get_color
Colorf get_color() const
Returns current color.
clan::Sprite::is_null
bool is_null() const
Returns true if this object is invalid.
Definition: sprite.h:118
clan::Sprite::set_show_on_finish
void set_show_on_finish(Sprite::ShowOnFinish show_on_finish)
Set what is shown when the animation is finished.
clan::Sprite::ShowOnFinish
ShowOnFinish
Definition: sprite.h:60
clan::Sprite::operator==
bool operator==(const Sprite &other) const
Equality operator.
Definition: sprite.h:210
clan::Size
2D (width,height) size structure - Integer
Definition: size.h:156
clan::Sprite::show_blank
Definition: sprite.h:62
clan::XMLResourceDocument
XML Resource Document.
Definition: xml_resource_document.h:48
clan::Vec4::b
Type b
Definition: vec4.h:82
clan::Sprite::set_angle
void set_angle(Angle angle)
Set absolute rotation angle.
clan::Sprite::get_alignment
void get_alignment(Origin &origin, int &x, int &y) const
Returns translation hot-spot.
clan::Sprite::get_linear_filter
bool get_linear_filter() const
Returns true if the sprite uses a linear filter for scaling up and down, false if a nearest-point fil...
clan::Sprite::get_show_on_finish
ShowOnFinish get_show_on_finish() const
Returns an enum for what is shown when the animation is finished.
clan::Sprite::get_alpha
float get_alpha() const
Returns current alpha.
clan::Key::c
clan::Sprite::set_play_backward
void set_play_backward(bool backward=true)
Set to true if animation should go backward (start at end).
clan::StyleValueType::color
value is an url
clan::Sprite::sig_animation_finished
Signal< void()> & sig_animation_finished()
Sig animation finished.
clan::Sprite::add_frames
void add_frames(const Texture2D &texture, Rect *frames, int num_frames)
Add frames.
clan::Sprite::set_frame_delay
void set_frame_delay(int frame, int delay_ms)
Sets the delay of a specific frame.
clan::Sprite::clone
Sprite clone() const
Copies all information from this sprite to another, excluding the graphics that remain shared...
clan::Color
Color description class.
Definition: color.h:46
clan::Sprite::get_rotation_hotspot
void get_rotation_hotspot(Origin &origin, int &x, int &y) const
Returns rotation hot-spot.
clan::Vec4::g
Type g
Definition: vec4.h:81