image.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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
38namespace 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:
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
131
133 void get_alignment(Origin &origin, float &x, float &y) const;
134
137
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
234 TextureWrapMode wrap_s,
235 TextureWrapMode wrap_t);
236
238 void set_linear_filter(bool linear_filter = true);
239
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}
2D Graphics Canvas
Definition canvas.h:72
Color description class.
Definition color.h:46
Floating point color description class (for float).
Definition color.h:799
Virtual File System (VFS).
Definition file_system.h:47
Image Import Description Class.
Definition image_import_description.h:48
Image class.
Definition image.h:59
Image(Canvas &canvas, const std::string &filename, const ImageImportDescription &import_desc=ImageImportDescription())
Constructs a Image.
void get_alignment(Origin &origin, float &x, float &y) const
Returns translation hot-spot.
float get_scale_y() const
Returns y scale.
Colorf get_color() const
Returns current color.
float get_height() const
Return the height of the image.
static Image load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc)
Loads a Sprite from a XML resource definition.
void draw(Canvas &canvas, const Rectf &src, const Quadf &dest) const
Draw image on graphic context.
float get_alpha() const
Returns current alpha.
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()
Constructs a null instance.
bool is_null() const
Returns true if this object is invalid.
Definition image.h:110
void set_scale(float x, float y)
Set scale for x and y directions individually.
void draw(Canvas &canvas, const Rectf &dest) const
Draw image on graphic context.
void set_subimage(Canvas &canvas, int x, int y, const PixelBuffer &image, const Rect &src_rect, int level=0)
Image(Subtexture &sub_texture)
Constructs an image from a subtexture.
bool operator!=(const Image &other) const
Inequality operator.
Definition image.h:154
static Resource< Image > resource(Canvas &canvas, const std::string &id, const ResourceManager &resources)
Retrieves a Sprite resource from the resource manager.
Image(Canvas &canvas, const PixelBuffer &pixelbuffer, const Rect &rect)
Constructs a Image from a pixelbuffer.
float get_width() const
Return the width of the image.
Subtexture get_texture() const
Return the texture of the image.
bool operator<(const Image &other) const
Less than operator.
Definition image.h:160
Image clone() const
Copies all information from this image to another, excluding the graphics that remain shared.
void draw(Canvas &canvas, const Quadf &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_color(const Color &c)
Set color.
Definition image.h:228
bool operator==(const Image &other) const
Equality operator.
Definition image.h:148
Image(Texture2D texture, const Rect &rect)
Constructs an image from a texture.
void draw(Canvas &canvas, float x, float y) const
Draw image on graphic context.
float get_scale_x() const
Returns x scale.
void set_alpha(float alpha)
Sets transparency.
virtual ~Image()
void draw(Canvas &canvas, const Rectf &src, const Rectf &dest) const
Draw image on graphic context.
void set_alignment(Origin origin, float x=0, float y=0)
Sets translation hotspot.
void set_color(const Colorf &color)
Sets the color.
void throw_if_null() const
Throw an exception if this object is invalid.
Sizef get_size() const
Return the size of the image.
Pixel data container.
Definition pixel_buffer.h:68
2D quad structure - Float
Definition quad.h:217
2D (left,top,right,bottom) rectangle structure - Integer
Definition rect.h:489
2D (left,top,right,bottom) rectangle structure - Float
Definition rect.h:503
Resource manager.
Definition resource_manager.h:44
Resource proxy of a specific type.
Definition resource.h:58
2D (width,height) size structure - Float
Definition size.h:184
Sub-texture description.
Definition subtexture.h:45
2D texture object class.
Definition texture_2d.h:41
XML Resource Document.
Definition xml_resource_document.h:48
Origin
Alignment origins.
Definition origin.h:39
TextureWrapMode
Texture coordinate wrapping modes.
Definition texture.h:65
Definition clanapp.h:36
@ color
value is an url