21 #include "hx3d/graphics/image.hpp" 22 #include "hx3d/graphics/texture.hpp" 24 #include "hx3d/utils/log.hpp" 29 Image::Image(): _width(0), _height(0), _buffer(nullptr), _texture(nullptr) {}
41 _buffer =
new unsigned char[width * height * 4] {};
45 unsigned int pos = x * 4 + (y * _width * 4);
46 _buffer[pos] = color.
r;
47 _buffer[pos + 1] = color.
g;
48 _buffer[pos + 2] = color.
b;
49 _buffer[pos + 3] = color.
a;
55 Log.
Error(
"Image: can't rect. width is too high.");
57 }
else if (y + h > _height) {
58 Log.
Error(
"Image: can't rect. height is too high.");
62 for (
auto j = y; j < y + h; ++j) {
63 for (
auto i = x; i < x + w; ++i) {
70 unsigned int pos = x * 4 + (y * _width * 4);
71 return Color(_buffer[pos], _buffer[pos + 1], _buffer[pos + 2], _buffer[pos + 3]);
76 Log.
Error(
"Image: attempt to access a non-created texture.");
82 _texture = Make<Texture>(*this);
87 Log.
Error(
"Image: attempt to update a non-created texture.");
91 Uint8* subBuffer =
new Uint8[w * h * 4];
92 for (
unsigned int j = y, count = 0; j < y + h; ++j) {
93 for (
unsigned int i = x; i < x + w; ++i, count += 4) {
95 Color color =
get(i, j);
96 subBuffer[count] = color.
r;
97 subBuffer[count+1] = color.
g;
98 subBuffer[count+2] = color.
b;
99 subBuffer[count+3] = color.
a;
103 _texture->updateZone(x, y, w, h, subBuffer);
unsigned char a
Alpha component.
unsigned char g
Green component.
void setRect(unsigned int x, unsigned int y, unsigned int w, unsigned int h, Color color)
Set a rectangle with a color.
Four [0..255] components defined color.
unsigned int getHeight()
Get the image height.
void buildTexture()
Recreate a texture from the image.
void set(unsigned int x, unsigned int y, Color color)
Set a pixel with a color.
void updateTextureZone(unsigned int x, unsigned int y, unsigned int w, unsigned int h)
Update an existing texture zone.
void Error(const std::string fmt,...)
Write an error message.
unsigned int getWidth()
Get the image width.
static hx3d::LogImpl Log
Current log implementation.
Ptr< Texture > getTexture()
Get the texture.
void create(unsigned int width, unsigned int height)
Initialize an image.
unsigned char b
Blue component.
unsigned char r
Red component.
Image()
Construct an empty image. Use create to create the image.
Color get(unsigned int x, unsigned int y)
Get a pixel color.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.