sprite.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 
31 #pragma once
32 
33 #include <memory>
34 #include "../api_display.h"
35 #include "../../Core/Math/origin.h"
36 #include "../../Core/Signals/signal_v0.h"
37 #include "../../Core/IOData/file_system.h"
38 #include "../../Core/Resources/resource.h"
39 #include "../Render/graphic_context.h"
40 #include "../Image/image_import_description.h"
41 #include "../Collision/collision_outline.h"
42 #include "color.h"
43 
44 namespace clan
45 {
48 
49 class Sprite_Impl;
50 class CollisionOutline;
51 class Canvas;
52 class ResourceManager;
53 class Font_Impl;
54 class Subtexture;
55 
57 class CL_API_DISPLAY Sprite
58 {
61 public:
63  {
64  show_blank, show_last_frame, show_first_frame
65  };
67 
70 public:
72  Sprite();
73 
78  Sprite(Canvas &canvas, const std::string &fullname, const ImageImportDescription &import_desc = ImageImportDescription ());
79 
85  Sprite(Canvas &canvas, const std::string &filename, const FileSystem &file_system, const ImageImportDescription &import_desc = ImageImportDescription ());
86 
92  Sprite(Canvas &canvas, IODevice &file, const std::string &image_type, const ImageImportDescription &import_desc = ImageImportDescription ());
93 
97  Sprite(Canvas &canvas);
98 
99  virtual ~Sprite();
101 
104 public:
110  static Resource<Sprite> resource(Canvas &canvas, const std::string &id, const ResourceManager &resources);
111 
113  static Sprite load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc);
115 
118 public:
120  bool is_null() const { return !impl; }
121 
123  void throw_if_null() const;
124 
126  Angle get_angle() const;
127 
129  Angle get_base_angle() const;
130 
132 
133  void get_scale(float &x, float &y) const;
134 
136 
137  float get_alpha() const;
138 
140 
141  Colorf get_color() const;
142 
144  bool get_linear_filter() const;
145 
147  void get_alignment(Origin &origin, int &x, int &y) const;
148 
150  void get_rotation_hotspot(Origin &origin, int &x, int &y) const;
151 
153  int get_current_frame() const;
154 
156  int get_frame_count() const;
157 
162  int get_frame_delay(int frame) const;
163 
165  Point get_frame_offset(int frame) const;
166 
168  Size get_frame_size(int frame) const;
169 
171  Subtexture get_frame_texture(int frame) const;
172 
174  int get_width() const;
175 
177  int get_height() const;
178 
180  Size get_size() const;
181 
183  int get_id() const;
184 
186  bool is_play_loop() const;
187 
189  bool is_play_backward() const;
190 
192  bool is_play_pingpong() const;
193 
195 
196  ShowOnFinish get_show_on_finish() const;
197 
199 
201  bool is_finished() const;
202 
204  bool is_looping() const;
205 
211  std::vector<CollisionOutline> create_collision_outlines(Canvas &canvas, int alpha_limit, OutlineAccuracy accuracy) const;
212 
218  CollisionOutline create_collision_outline(Canvas &canvas, int alpha_limit=128, OutlineAccuracy accuracy=accuracy_medium) const;
219 
221 
224 public:
226  bool operator==(const Sprite &other) const
227  {
228  return impl==other.impl;
229  }
230 
232  bool operator!=(const Sprite &other) const
233  {
234  return impl!=other.impl;
235  }
236 
238  bool operator<(const Sprite &other) const
239  {
240  return impl < other.impl;
241  }
243 
246 public:
248  Sprite &operator =(const Sprite &copy);
249 
251 
252  void set_image_data(const Sprite &image_source);
253 
255  Sprite clone() const;
256 
263  void draw(
264  Canvas &canvas,
265  float x,
266  float y);
267 
268  void draw(
269  Canvas &canvas,
270  int x,
271  int y);
272 
273  void draw(
274  Canvas &canvas,
275  const Rectf &src,
276  const Rectf &dest);
277 
278  void draw(
279  Canvas &canvas,
280  const Rectf &dest);
281 
285  void update(int time_elapsed_ms);
286 
288  void set_angle(Angle angle);
289 
291  void rotate(Angle angle);
292 
294  void set_base_angle(Angle angle);
295 
297 
298  void set_scale(float x, float y);
299 
301 
302  void set_alpha(float alpha);
303 
305 
306  void set_color(const Colorf &color);
307 
311  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);}
312 
314  void set_linear_filter(bool linear_filter = true);
315 
317  void set_alignment(Origin origin, int x = 0, int y = 0);
318 
320  void set_rotation_hotspot(Origin origin, int x = 0, int y = 0);
321 
324  void set_frame(unsigned int frame);
325 
329  void set_delay(int delay_ms);
330 
335  void set_frame_delay(int frame, int delay_ms);
336 
338  void set_frame_offset(int frame, Point offset);
339 
341  void set_id(int id);
342 
344 
346  void finish();
347 
349  void restart();
350 
352  void set_play_loop(bool loop = true);
353 
355  void set_play_pingpong(bool pingpong = true);
356 
358  void set_play_backward(bool backward = true);
359 
361  void set_show_on_finish(Sprite::ShowOnFinish show_on_finish);
362 
366  void add_frame(const Texture2D &texture);
367 
371  void add_frame(Canvas &canvas, const std::string &fullname, const ImageImportDescription &import_desc = ImageImportDescription ());
372 
377  void add_frame(Canvas &canvas, IODevice &file, const std::string &image_type, const ImageImportDescription &import_desc = ImageImportDescription ());
378 
383  void add_frame(Canvas &canvas, const std::string &filename, const FileSystem &file_system, const ImageImportDescription &import_desc = ImageImportDescription ());
384 
390  void add_frames(const Texture2D &texture, Rect *frames, int num_frames);
391 
396  void add_frame(const Texture2D &texture, const Rect &frame);
397 
399 
407  void add_gridclipped_frames(Canvas &canvas,
408  const Texture2D &texture,
409  int xpos, int ypos,
410  int width, int height,
411  int xarray = 1, int yarray = 1,
412  int array_skipframes = 0,
413  int xspacing = 0, int yspacing = 0);
414 
416 
425  void add_alphaclipped_frames(Canvas &canvas,
426  const Texture2D &texture,
427  int xpos = 0, int ypos = 0,
428  float trans_limit = 0.05f);
429 
431 
439  void add_alphaclipped_frames_free(Canvas &canvas,
440  const Texture2D &texture,
441  int xpos = 0, int ypos = 0,
442  float trans_limit = 0.05f);
443 
445 
448 public:
452  Signal_v0 &sig_animation_finished();
454 
457 private:
458  std::shared_ptr<Sprite_Impl> impl;
459 
460  friend class Font_Impl;
461 
463 };
464 
465 }
466 
Floating point color description class (for float).
Definition: color.h:661
Sub-texture description.
Definition: subtexture.h:46
Angle class.
Definition: angle.h:63
Resource proxy of a specific type.
Definition: resource.h:59
2D Graphics Canvas
Definition: canvas.h:70
I/O Device interface.
Definition: iodevice.h:51
unsigned char get_green() const
Returns the green color component, in the range 0-255.
Definition: color.h:91
Definition: outline_accuracy.h:45
unsigned char get_blue() const
Returns the blue color component, in the range 0-255.
Definition: color.h:94
Origin
Alignment origins.
Definition: origin.h:41
Image Import Description Class.
Definition: image_import_description.h:49
Sprite class.
Definition: sprite.h:57
unsigned char get_alpha() const
Returns the alpha color component, in the range 0-255.
Definition: color.h:85
unsigned char get_red() const
Returns the red color component, in the range 0-255.
Definition: color.h:88
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:453
Type a
Definition: vec4.h:84
2D texture object class.
Definition: texture_2d.h:42
Type r
Definition: vec4.h:81
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:467
Virtual File System (VFS).
Definition: file_system.h:48
Resource manager.
Definition: resource_manager.h:45
bool operator!=(const Sprite &other) const
Inequality operator.
Definition: sprite.h:232
Collision detection outline.
Definition: collision_outline.h:114
OutlineAccuracy
Outline optimization accuracy.
Definition: outline_accuracy.h:41
void set_color(const Color &c)
Set color.
Definition: sprite.h:311
bool operator<(const Sprite &other) const
Less than operator.
Definition: sprite.h:238
2D (x,y) point structure - Integer
Definition: point.h:63
Definition: sprite.h:64
bool is_null() const
Returns true if this object is invalid.
Definition: sprite.h:120
ShowOnFinish
Definition: sprite.h:62
bool operator==(const Sprite &other) const
Equality operator.
Definition: sprite.h:226
2D (width,height) size structure - Integer
Definition: size.h:157
XML Resource Document.
Definition: xml_resource_document.h:49
Type b
Definition: vec4.h:83
Signal_v0.
Definition: signal_v0.h:107
Color description class.
Definition: color.h:47
Type g
Definition: vec4.h:82