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
147 Subtexture get_texture() const;
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
252 void set_wrap_mode(
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
clan::Image::get_height
float get_height() const
Return the height of the image.
clan
Definition: clanapp.h:35
clan::Colorf
Floating point color description class (for float).
Definition: color.h:660
clan::Image::get_scale_x
float get_scale_x() const
Returns x scale.
clan::Subtexture
Sub-texture description.
Definition: subtexture.h:45
clan::Image::get_width
float get_width() const
Return the width of the image.
clan::Resource
Resource proxy of a specific type.
Definition: resource.h:58
clan::Image::set_color
void set_color(const Colorf &color)
Sets the color.
clan::Canvas
2D Graphics Canvas
Definition: canvas.h:72
clan::TextureWrapMode
TextureWrapMode
Texture coordinate wrapping modes.
Definition: texture.h:65
clan::Image::set_color
void set_color(const Color &c)
Set color.
Definition: image.h:247
clan::Color::get_green
unsigned char get_green() const
Returns the green color component, in the range 0-255.
Definition: color.h:90
clan::Image::operator<
bool operator<(const Image &other) const
Less than operator.
Definition: image.h:175
clan::PixelBuffer
Pixel data container.
Definition: pixel_buffer.h:68
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::Key::x
clan::Image::~Image
virtual ~Image()
clan::ImageImportDescription
Image Import Description Class.
Definition: image_import_description.h:48
clan::Image::clone
Image clone() const
Copies all information from this image to another, excluding the graphics that remain shared...
clan::Color::get_alpha
unsigned char get_alpha() const
Returns the alpha color component, in the range 0-255.
Definition: color.h:84
clan::Image::load
static Image load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc)
Loads a Sprite from a XML resource definition.
clan::Image::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::Color::get_red
unsigned char get_red() const
Returns the red color component, in the range 0-255.
Definition: color.h:87
clan::Image::get_scale_y
float get_scale_y() const
Returns y scale.
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::Texture2D
2D texture object class.
Definition: texture_2d.h:41
clan::Image::Image
Image()
Constructs a null instance.
clan::Vec4::r
Type r
Definition: vec4.h:80
clan::Rectf
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:484
clan::Key::y
clan::Quadf
2D quad structure - Float
Definition: quad.h:212
clan::FileSystem
Virtual File System (VFS).
Definition: file_system.h:47
clan::Image::get_size
Sizef get_size() const
Return the size of the image.
clan::Image::is_null
bool is_null() const
Returns true if this object is invalid.
Definition: image.h:122
clan::Image::operator!=
bool operator!=(const Image &other) const
Inequality operator.
Definition: image.h:169
clan::ResourceManager
Resource manager.
Definition: resource_manager.h:44
clan::Image::get_color
Colorf get_color() const
Returns current color.
clan::Image::throw_if_null
void throw_if_null() const
Throw an exception if this object is invalid.
clan::Image::operator==
bool operator==(const Image &other) const
Equality operator.
Definition: image.h:163
clan::Image::draw
void draw(Canvas &canvas, float x, float y) const
Draw image on graphic context.
clan::Image::get_texture
Subtexture get_texture() const
Return the texture of the image.
clan::Image
Image class.
Definition: image.h:59
clan::Image::set_wrap_mode
void set_wrap_mode(TextureWrapMode wrap_s, TextureWrapMode wrap_t)
clan::Image::resource
static Resource< Image > resource(Canvas &canvas, const std::string &id, const ResourceManager &resources)
Retrieves a Sprite resource from the resource manager.
clan::Image::set_scale
void set_scale(float x, float y)
Set scale for x and y directions individually.
clan::Image::get_alignment
void get_alignment(Origin &origin, float &x, float &y) const
Returns translation hot-spot.
clan::Image::get_alpha
float get_alpha() const
Returns current alpha.
clan::XMLResourceDocument
XML Resource Document.
Definition: xml_resource_document.h:48
clan::Vec4::b
Type b
Definition: vec4.h:82
clan::Key::c
clan::Image::set_alpha
void set_alpha(float alpha)
Sets transparency.
clan::StyleValueType::color
value is an url
clan::Color
Color description class.
Definition: color.h:46
clan::Image::set_alignment
void set_alignment(Origin origin, float x=0, float y=0)
Sets translation hotspot.
clan::Vec4::g
Type g
Definition: vec4.h:81
clan::Sizef
2D (width,height) size structure - Float
Definition: size.h:169