image.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 */
28 
29 
30 #pragma once
31 
32 #include <memory>
33 #include "../../Core/Math/origin.h"
34 #include "../../Core/Resources/resource.h"
35 #include "color.h"
36 #include "../Image/image_import_description.h"
37 #include "../Render/texture.h"
38 
39 namespace clan
40 {
43 
44 class GraphicContext;
45 class FileSystem;
46 class Rect;
47 class Size;
48 class Rectf;
49 class Image_Impl;
50 class Texture2D;
51 class Subtexture;
52 class PixelBuffer;
53 class ResourceManager;
54 class XMLResourceDocument;
55 class Canvas;
56 class Quadf;
57 
59 class Image
60 {
63 public:
65  Image();
66 
71  Image(Texture2D texture, const Rect &rect);
72 
76  Image(Subtexture &sub_texture);
77 
83  Image(Canvas &canvas, const PixelBuffer &pixelbuffer, const Rect &rect);
84 
90  Image(Canvas &canvas, const std::string &filename, const ImageImportDescription &import_desc = ImageImportDescription ());
91 
98  Image(Canvas &canvas, const std::string &filename, const FileSystem &fs, const ImageImportDescription &import_desc = ImageImportDescription ());
99 
100  virtual ~Image();
102 
105 public:
111  static Resource<Image> resource(Canvas &canvas, const std::string &id, const ResourceManager &resources);
112 
114  static Image load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc);
115 
117 
120 public:
122  bool is_null() const { return !impl; }
123 
125  void throw_if_null() const;
126 
128 
129  float get_scale_x() const;
130 
132 
133  float get_scale_y() const;
134 
136 
137  float get_alpha() const;
138 
140 
141  Colorf get_color() const;
142 
144  void get_alignment(Origin &origin, float &x, float &y) const;
145 
148 
150  Sizef get_size() const;
151 
153  float get_width() const;
154 
156  float get_height() const;
157 
161 public:
163  bool operator==(const Image &other) const
164  {
165  return impl==other.impl;
166  }
167 
169  bool operator!=(const Image &other) const
170  {
171  return impl!=other.impl;
172  }
173 
175  bool operator<(const Image &other) const
176  {
177  return impl < other.impl;
178  }
180 
183 public:
185  Image clone() const;
186 
191  void draw(
192  Canvas &canvas,
193  float x,
194  float y) const;
195 
201  void draw(
202  Canvas &canvas,
203  const Rectf &src,
204  const Rectf &dest) const;
205 
210  void draw(
211  Canvas &canvas,
212  const Rectf &dest) const;
213 
219  void draw(
220  Canvas &canvas,
221  const Rectf &src,
222  const Quadf &dest) const;
223 
228  void draw(
229  Canvas &canvas,
230  const Quadf &dest) const;
231 
233 
234  void set_scale(float x, float y);
235 
237 
238  void set_alpha(float alpha);
239 
241 
242  void set_color(const Colorf &color);
243 
247  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);}
248 
250  void set_alignment(Origin origin, float x = 0, float y = 0);
251 
253  TextureWrapMode wrap_s,
254  TextureWrapMode wrap_t);
255 
257  void set_linear_filter(bool linear_filter = true);
258 
260 
263 private:
264  std::shared_ptr<Image_Impl> impl;
266 };
267 
268 }
269 
Image(Texture2D texture, const Rect &rect)
Constructs an image from a texture.
Image clone() const
Copies all information from this image to another, excluding the graphics that remain shared.
TextureWrapMode
Texture coordinate wrapping modes.
Definition: texture.h:66
bool is_null() const
Returns true if this object is invalid.
Definition: image.h:122
void set_alpha(float alpha)
Sets transparency.
Subtexture get_texture() const
Return the texture of the image.
Resource manager.
Definition: resource_manager.h:45
float get_height() const
Return the height of the image.
void set_color(const Colorf &color)
Sets the color.
void draw(Canvas &canvas, const Rectf &dest) const
Draw image on graphic context.
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:471
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...
Image(Subtexture &sub_texture)
Constructs an image from a subtexture.
Image Import Description Class.
Definition: image_import_description.h:49
void set_alignment(Origin origin, float x=0, float y=0)
Sets translation hotspot.
void draw(Canvas &canvas, float x, float y) const
Draw image on graphic context.
void set_color(const Color &c)
Set color.
Definition: image.h:247
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:485
Image(Canvas &canvas, const PixelBuffer &pixelbuffer, const Rect &rect)
Constructs a Image from a pixelbuffer.
static Image load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc)
Loads a Sprite from a XML resource definition.
Floating point color description class (for float).
Definition: color.h:661
2D quad structure - Float
Definition: quad.h:213
Origin
Alignment origins.
Definition: origin.h:41
2D texture object class.
Definition: texture_2d.h:42
float get_alpha() const
Returns current alpha.
Image()
Constructs a null instance.
void get_alignment(Origin &origin, float &x, float &y) const
Returns translation hot-spot.
2D Graphics Canvas
Definition: canvas.h:73
Color description class.
Definition: color.h:47
bool operator<(const Image &other) const
Less than operator.
Definition: image.h:175
Colorf get_color() const
Returns current color.
float get_width() const
Return the width of the image.
float get_scale_x() const
Returns x scale.
Image(Canvas &canvas, const std::string &filename, const ImageImportDescription &import_desc=ImageImportDescription())
Constructs a Image.
Definition: clanapp.h:36
void throw_if_null() const
Throw an exception if this object is invalid.
void draw(Canvas &canvas, const Quadf &dest) const
Draw image on graphic context.
Virtual File System (VFS).
Definition: file_system.h:48
XML Resource Document.
Definition: xml_resource_document.h:49
Image class.
Definition: image.h:60
Pixel data container.
Definition: pixel_buffer.h:69
bool operator==(const Image &other) const
Equality operator.
Definition: image.h:163
void draw(Canvas &canvas, const Rectf &src, const Rectf &dest) const
Draw image on graphic context.
void set_wrap_mode(TextureWrapMode wrap_s, TextureWrapMode wrap_t)
Image(Canvas &canvas, const std::string &filename, const FileSystem &fs, const ImageImportDescription &import_desc=ImageImportDescription())
Constructs a Image.
void set_scale(float x, float y)
Set scale for x and y directions individually.
void draw(Canvas &canvas, const Rectf &src, const Quadf &dest) const
Draw image on graphic context.
Resource proxy of a specific type.
Definition: resource.h:59
Sizef get_size() const
Return the size of the image.
Sub-texture description.
Definition: subtexture.h:46
float get_scale_y() const
Returns y scale.
2D (width,height) size structure - Float
Definition: size.h:170
bool operator!=(const Image &other) const
Inequality operator.
Definition: image.h:169
@ color
value is an url
virtual ~Image()
static Resource< Image > resource(Canvas &canvas, const std::string &id, const ResourceManager &resources)
Retrieves a Sprite resource from the resource manager.