sprite.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 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 Canvas;
48  class ResourceManager;
49  class Font_Impl;
50  class Subtexture;
51  class XMLResourceDocument;
52 
54  class Sprite
55  {
56  public:
58  {
60  };
61 
63  Sprite();
64 
69  Sprite(Canvas &canvas, const std::string &fullname, const ImageImportDescription &import_desc = ImageImportDescription());
70 
76  Sprite(Canvas &canvas, const std::string &filename, const FileSystem &file_system, const ImageImportDescription &import_desc = ImageImportDescription());
77 
83  Sprite(Canvas &canvas, IODevice &file, const std::string &image_type, const ImageImportDescription &import_desc = ImageImportDescription());
84 
88  Sprite(Canvas &canvas);
89 
90  virtual ~Sprite();
91 
97  static Resource<Sprite> resource(Canvas &canvas, const std::string &id, const ResourceManager &resources);
98 
100  static Sprite load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc);
101 
103  bool is_null() const { return !impl; }
104  explicit operator bool() const { return bool(impl); }
105 
107  void throw_if_null() const;
108 
110  Angle get_angle() const;
111 
113  Angle get_base_angle() const;
114 
116 
117  void get_scale(float &x, float &y) const;
118 
120 
121  float get_alpha() const;
122 
124 
125  Colorf get_color() const;
126 
128  bool get_linear_filter() const;
129 
131  void get_alignment(Origin &origin, int &x, int &y) const;
132 
134  void get_rotation_hotspot(Origin &origin, int &x, int &y) const;
135 
137  int get_current_frame() const;
138 
140  int get_frame_count() const;
141 
146  int get_frame_delay(int frame) const;
147 
149  Point get_frame_offset(int frame) const;
150 
152  Size get_frame_size(int frame) const;
153 
155  Subtexture get_frame_texture(int frame) const;
156 
158  int get_width() const;
159 
161  int get_height() const;
162 
164  Size get_size() const;
165 
167  int get_id() const;
168 
170  bool is_play_loop() const;
171 
173  bool is_play_backward() const;
174 
176  bool is_play_pingpong() const;
177 
179 
181 
183 
185  bool is_finished() const;
186 
188  bool is_looping() const;
189 
191  bool operator==(const Sprite &other) const
192  {
193  return impl == other.impl;
194  }
195 
197  bool operator!=(const Sprite &other) const
198  {
199  return impl != other.impl;
200  }
201 
203  bool operator<(const Sprite &other) const
204  {
205  return impl < other.impl;
206  }
207 
209  Sprite &operator =(const Sprite &copy);
210 
212 
213  void set_image_data(const Sprite &image_source);
214 
216  Sprite clone() const;
217 
224  void draw(
225  Canvas &canvas,
226  float x,
227  float y);
228 
229  void draw(
230  Canvas &canvas,
231  int x,
232  int y);
233 
234  void draw(
235  Canvas &canvas,
236  const Rectf &src,
237  const Rectf &dest);
238 
239  void draw(
240  Canvas &canvas,
241  const Rectf &dest);
242 
246  void update(int time_elapsed_ms);
247 
249  void set_angle(Angle angle);
250 
253 
255  void set_angle_yaw(Angle angle);
256 
258  void rotate(Angle angle);
259 
261  void rotate_pitch(Angle angle);
262 
264  void rotate_yaw(Angle angle);
265 
267  void set_base_angle(Angle angle);
268 
270 
271  void set_scale(float x, float y);
272 
274 
275  void set_alpha(float alpha);
276 
278 
279  void set_color(const Colorf &color);
280 
284  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); }
285 
287  void set_linear_filter(bool linear_filter = true);
288 
290  void set_alignment(Origin origin, int x = 0, int y = 0);
291 
293  void set_rotation_hotspot(Origin origin, int x = 0, int y = 0);
294 
297  void set_frame(unsigned int frame);
298 
302  void set_delay(int delay_ms);
303 
308  void set_frame_delay(int frame, int delay_ms);
309 
311  void set_frame_offset(int frame, Point offset);
312 
314  void set_id(int id);
315 
317 
319  void finish();
320 
322  void restart();
323 
325  void set_play_loop(bool loop = true);
326 
328  void set_play_pingpong(bool pingpong = true);
329 
331  void set_play_backward(bool backward = true);
332 
334  void set_show_on_finish(Sprite::ShowOnFinish show_on_finish);
335 
339  void add_frame(const Texture2D &texture);
340 
344  void add_frame(Canvas &canvas, const std::string &fullname, const ImageImportDescription &import_desc = ImageImportDescription());
345 
350  void add_frame(Canvas &canvas, IODevice &file, const std::string &image_type, const ImageImportDescription &import_desc = ImageImportDescription());
351 
356  void add_frame(Canvas &canvas, const std::string &filename, const FileSystem &file_system, const ImageImportDescription &import_desc = ImageImportDescription());
357 
363  void add_frames(const Texture2D &texture, Rect *frames, int num_frames);
364 
369  void add_frame(const Texture2D &texture, const Rect &frame);
370 
372 
380  void add_gridclipped_frames(Canvas &canvas,
381  const Texture2D &texture,
382  int xpos, int ypos,
383  int width, int height,
384  int xarray = 1, int yarray = 1,
385  int array_skipframes = 0,
386  int xspacing = 0, int yspacing = 0);
387 
389 
398  void add_alphaclipped_frames(Canvas &canvas,
399  const Texture2D &texture,
400  int xpos = 0, int ypos = 0,
401  float trans_limit = 0.05f);
402 
404 
413  const Texture2D &texture,
414  int xpos = 0, int ypos = 0,
415  float trans_limit = 0.05f);
416 
421 
422  private:
423  std::shared_ptr<Sprite_Impl> impl;
424 
425  friend class FontFamily_Impl;
426  };
427 
429 }
void get_rotation_hotspot(Origin &origin, int &x, int &y) const
Returns rotation hot-spot.
static Sprite load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc)
Loads a Sprite from a XML resource definition.
Angle get_base_angle() const
Returns the base angle in degrees - angle added to any rotation set with set_rotate() or rotate()...
bool operator<(const Sprite &other) const
Less than operator.
Definition: sprite.h:203
Definition: clanapp.h:35
Floating point color description class (for float).
Definition: color.h:798
Sub-texture description.
Definition: subtexture.h:44
Angle class.
Definition: angle.h:59
Resource proxy of a specific type.
Definition: resource.h:57
void add_frame(const Texture2D &texture)
Add frame.
void restart()
Restart animation.
bool is_play_backward() const
Returns true if animation is played in from right to left (starts at end).
2D Graphics Canvas
Definition: canvas.h:71
void set_show_on_finish(Sprite::ShowOnFinish show_on_finish)
Set what is shown when the animation is finished.
Type b
Definition: vec4.h:81
I/O Device interface.
Definition: iodevice.h:49
void draw(Canvas &canvas, float x, float y)
Draw sprite on graphic context.
void set_color(const Colorf &color)
Sets the color.
void set_angle_pitch(Angle angle)
Set absolute rotation pitch angle.
Sprite clone() const
Copies all information from this sprite to another, excluding the graphics that remain shared...
Origin
Alignment origins.
Definition: origin.h:38
void set_delay(int delay_ms)
Sets the delay for all frames.
Definition: sprite.h:59
static Resource< Sprite > resource(Canvas &canvas, const std::string &id, const ResourceManager &resources)
Retrieves a Sprite resource from the resource manager.
Image Import Description Class.
Definition: image_import_description.h:47
Sprite class.
Definition: sprite.h:54
unsigned char get_alpha() const
Returns the alpha color component, in the range 0-255.
Definition: color.h:74
void set_frame_delay(int frame, int delay_ms)
Sets the delay of a specific frame.
bool operator!=(const Sprite &other) const
Inequality operator.
Definition: sprite.h:197
unsigned char get_red() const
Returns the red color component, in the range 0-255.
Definition: color.h:77
void set_play_pingpong(bool pingpong=true)
Set to true if animation should loop, false otherwise.
void set_base_angle(Angle angle)
Sets the base angle in degrees - angle added to any rotation set with set_rotate() or rotate()...
void update(int time_elapsed_ms)
Call this function to update the animation.
unsigned char get_blue() const
Returns the blue color component, in the range 0-255.
Definition: color.h:83
int get_current_frame() const
Returns current frame in animation. 0 is first frame.
void set_angle_yaw(Angle angle)
Set absolute rotation yaw angle.
int get_id() const
Returns the attached id (if exists).
Sprite & operator=(const Sprite &copy)
Copy assignment operator.
void finish()
Finish animation.
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:488
Type r
Definition: vec4.h:79
Angle get_angle() const
Returns current angle in degrees.
bool is_looping() const
Returns true if animation has looped in the last update cycle.
int get_frame_delay(int frame) const
Returns the delay of a frame.
void set_alignment(Origin origin, int x=0, int y=0)
Sets translation hotspot.
2D texture object class.
Definition: texture_2d.h:40
void set_alpha(float alpha)
Sets transparency.
void get_scale(float &x, float &y) const
Returns scale for x and y.
void set_angle(Angle angle)
Set absolute rotation angle.
void set_color(const Color &c)
Set color.
Definition: sprite.h:284
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:502
void rotate_pitch(Angle angle)
Add angle in degrees to current pitch angle.
Colorf get_color() const
Returns current color.
void set_rotation_hotspot(Origin origin, int x=0, int y=0)
Sets rotation hotspot.
Virtual File System (VFS).
Definition: file_system.h:46
Signal< void()> & sig_animation_finished()
Sig animation finished.
Size get_size() const
Return the size of the current frame, shortcut for 'get_frame_size(get_current_frame())'.
void set_frame_offset(int frame, Point offset)
Sets the translate offset of a specific frame. 0 is first frame.
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...
Type a
Definition: vec4.h:82
Resource manager.
Definition: resource_manager.h:43
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.
Type g
Definition: vec4.h:80
float get_alpha() const
Returns current alpha.
friend class FontFamily_Impl
Definition: sprite.h:425
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).
Subtexture get_frame_texture(int frame) const
Return the texture of a frame. 0 is first frame.
void throw_if_null() const
Throw an exception if this object is invalid.
void rotate_yaw(Angle angle)
Add angle in degrees to current yaw angle.
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...
2D (x,y) point structure - Integer
Definition: point.h:61
void set_play_backward(bool backward=true)
Set to true if animation should go backward (start at end).
bool is_null() const
Returns true if this object is invalid.
Definition: sprite.h:103
Size get_frame_size(int frame) const
Returns the size of a frame. 0 is first frame.
int get_frame_count() const
Returns number of frames in animation.
void rotate(Angle angle)
Add angle in degrees to current angle.
Definition: sprite.h:59
virtual ~Sprite()
Sprite()
Constructs a null instance.
void set_frame(unsigned int frame)
Sets current animation frame. 0 is first frame.
int get_height() const
Return the height of the current frame, shortcut for 'get_frame_size(get_current_frame()).height'.
ShowOnFinish
Definition: sprite.h:57
void add_frames(const Texture2D &texture, Rect *frames, int num_frames)
Add frames.
Point get_frame_offset(int frame) const
Returns the translation offset of a frame. 0 is first frame.
void get_alignment(Origin &origin, int &x, int &y) const
Returns translation hot-spot.
2D (width,height) size structure - Integer
Definition: size.h:170
Definition: sprite.h:59
XML Resource Document.
Definition: xml_resource_document.h:47
bool is_finished() const
Returns true if animation is finished.
unsigned char get_green() const
Returns the green color component, in the range 0-255.
Definition: color.h:80
bool is_play_pingpong() const
Returns true if animation is played in ping-pong mode.
void set_image_data(const Sprite &image_source)
Sets the image data from another sprite, sharing animation state.
void set_scale(float x, float y)
Set scale for x and y directions individually.
void set_play_loop(bool loop=true)
Set to true if animation should loop, false otherwise.
bool operator==(const Sprite &other) const
Equality operator.
Definition: sprite.h:191
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).
Color description class.
Definition: color.h:45
ShowOnFinish get_show_on_finish() const
Returns an enum for what is shown when the animation is finished.
int get_width() const
Return the width of the current frame, shortcut for 'get_frame_size(get_current_frame()).width'.
void set_id(int id)
Sets an attached id.
bool is_play_loop() const
Returns true if animation is played in loop (more than once).