image.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 */
28 
29 #pragma once
30 
31 #include <memory>
32 #include "../../Core/Math/origin.h"
33 #include "../../Core/Resources/resource.h"
34 #include "color.h"
35 #include "../Image/image_import_description.h"
36 #include "../Render/texture.h"
37 
38 namespace clan
39 {
42 
43  class GraphicContext;
44  class FileSystem;
45  class Rect;
46  class Size;
47  class Rectf;
48  class Image_Impl;
49  class Texture2D;
50  class Subtexture;
51  class PixelBuffer;
52  class ResourceManager;
53  class XMLResourceDocument;
54  class Canvas;
55  class Quadf;
56 
58  class Image
59  {
60  public:
62  Image();
63 
68  Image(Texture2D texture, const Rect &rect);
69 
73  Image(Subtexture &sub_texture);
74 
80  Image(Canvas &canvas, const PixelBuffer &pixelbuffer, const Rect &rect);
81 
87  Image(Canvas &canvas, const std::string &filename, const ImageImportDescription &import_desc = ImageImportDescription());
88 
95  Image(Canvas &canvas, const std::string &filename, const FileSystem &fs, const ImageImportDescription &import_desc = ImageImportDescription());
96 
97  virtual ~Image();
98 
104  static Resource<Image> resource(Canvas &canvas, const std::string &id, const ResourceManager &resources);
105 
107  static Image load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc);
108 
110  bool is_null() const { return !impl; }
111  explicit operator bool() const { return bool(impl); }
112 
114  void throw_if_null() const;
115 
117 
118  float get_scale_x() const;
119 
121 
122  float get_scale_y() const;
123 
125 
126  float get_alpha() const;
127 
129 
130  Colorf get_color() const;
131 
133  void get_alignment(Origin &origin, float &x, float &y) const;
134 
136  Subtexture get_texture() const;
137 
139  Sizef get_size() const;
140 
142  float get_width() const;
143 
145  float get_height() const;
146 
148  bool operator==(const Image &other) const
149  {
150  return impl == other.impl;
151  }
152 
154  bool operator!=(const Image &other) const
155  {
156  return impl != other.impl;
157  }
158 
160  bool operator<(const Image &other) const
161  {
162  return impl < other.impl;
163  }
164 
166  Image clone() const;
167 
172  void draw(
173  Canvas &canvas,
174  float x,
175  float y) const;
176 
182  void draw(
183  Canvas &canvas,
184  const Rectf &src,
185  const Rectf &dest) const;
186 
191  void draw(
192  Canvas &canvas,
193  const Rectf &dest) const;
194 
200  void draw(
201  Canvas &canvas,
202  const Rectf &src,
203  const Quadf &dest) const;
204 
209  void draw(
210  Canvas &canvas,
211  const Quadf &dest) const;
212 
214 
215  void set_scale(float x, float y);
216 
218 
219  void set_alpha(float alpha);
220 
222 
223  void set_color(const Colorf &color);
224 
228  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); }
229 
231  void set_alignment(Origin origin, float x = 0, float y = 0);
232 
233  void set_wrap_mode(
234  TextureWrapMode wrap_s,
235  TextureWrapMode wrap_t);
236 
238  void set_linear_filter(bool linear_filter = true);
239 
249  void set_subimage(
250  Canvas &canvas,
251  int x,
252  int y,
253  const PixelBuffer &image,
254  const Rect &src_rect,
255  int level = 0);
256 
257  private:
258  std::shared_ptr<Image_Impl> impl;
259  };
260 
262 }
static Image load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc)
Loads a Sprite from a XML resource definition.
Definition: clanapp.h:35
Floating point color description class (for float).
Definition: color.h:798
void set_color(const Color &c)
Set color.
Definition: image.h:228
Sub-texture description.
Definition: subtexture.h:44
Resource proxy of a specific type.
Definition: resource.h:57
void set_scale(float x, float y)
Set scale for x and y directions individually.
Colorf get_color() const
Returns current color.
2D Graphics Canvas
Definition: canvas.h:71
Type b
Definition: vec4.h:81
void set_alpha(float alpha)
Sets transparency.
TextureWrapMode
Texture coordinate wrapping modes.
Definition: texture.h:64
Pixel data container.
Definition: pixel_buffer.h:67
Origin
Alignment origins.
Definition: origin.h:38
Image Import Description Class.
Definition: image_import_description.h:47
unsigned char get_alpha() const
Returns the alpha color component, in the range 0-255.
Definition: color.h:74
float get_alpha() const
Returns current alpha.
bool operator!=(const Image &other) const
Inequality operator.
Definition: image.h:154
unsigned char get_red() const
Returns the red color component, in the range 0-255.
Definition: color.h:77
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...
void set_wrap_mode(TextureWrapMode wrap_s, TextureWrapMode wrap_t)
unsigned char get_blue() const
Returns the blue color component, in the range 0-255.
Definition: color.h:83
bool operator==(const Image &other) const
Equality operator.
Definition: image.h:148
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:488
Type r
Definition: vec4.h:79
bool operator<(const Image &other) const
Less than operator.
Definition: image.h:160
2D texture object class.
Definition: texture_2d.h:40
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:502
Image clone() const
Copies all information from this image to another, excluding the graphics that remain shared...
2D quad structure - Float
Definition: quad.h:216
Virtual File System (VFS).
Definition: file_system.h:46
Type a
Definition: vec4.h:82
Resource manager.
Definition: resource_manager.h:43
void draw(Canvas &canvas, float x, float y) const
Draw image on graphic context.
Type g
Definition: vec4.h:80
static Resource< Image > resource(Canvas &canvas, const std::string &id, const ResourceManager &resources)
Retrieves a Sprite resource from the resource manager.
void set_subimage(Canvas &canvas, int x, int y, const PixelBuffer &image, const Rect &src_rect, int level=0)
virtual ~Image()
Image class.
Definition: image.h:58
bool is_null() const
Returns true if this object is invalid.
Definition: image.h:110
Sizef get_size() const
Return the size of the image.
void throw_if_null() const
Throw an exception if this object is invalid.
float get_scale_y() const
Returns y scale.
Subtexture get_texture() const
Return the texture of the image.
XML Resource Document.
Definition: xml_resource_document.h:47
unsigned char get_green() const
Returns the green color component, in the range 0-255.
Definition: color.h:80
float get_height() const
Return the height of the image.
float get_scale_x() const
Returns x scale.
void set_alignment(Origin origin, float x=0, float y=0)
Sets translation hotspot.
void get_alignment(Origin &origin, float &x, float &y) const
Returns translation hot-spot.
Color description class.
Definition: color.h:45
2D (width,height) size structure - Float
Definition: size.h:183
void set_color(const Colorf &color)
Sets the color.
float get_width() const
Return the width of the image.
Image()
Constructs a null instance.