hx3d  1
2D/3D Simple Game Framework
image.cpp
1 /*
2  32-bit image.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/image.hpp"
22 #include "hx3d/graphics/texture.hpp"
23 
24 #include "hx3d/utils/log.hpp"
25 
26 namespace hx3d {
27 namespace graphics {
28 
29 Image::Image(): _width(0), _height(0), _buffer(nullptr), _texture(nullptr) {}
30 
31 Image::Image(unsigned int width, unsigned int height) {
32  create(width, height);
33 }
34 
35 Image::~Image() {}
36 
37 void Image::create(unsigned int width, unsigned int height) {
38  _width = width;
39  _height = height;
40 
41  _buffer = new unsigned char[width * height * 4] {};
42 }
43 
44 void Image::set(unsigned int x, unsigned int y, Color color) {
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;
50 }
51 
52 void Image::setRect(unsigned int x, unsigned int y, unsigned int w, unsigned int h, Color color) {
53 
54  if (x + w > _width) {
55  Log.Error("Image: can't rect. width is too high.");
56  return;
57  } else if (y + h > _height) {
58  Log.Error("Image: can't rect. height is too high.");
59  return;
60  }
61 
62  for (auto j = y; j < y + h; ++j) {
63  for (auto i = x; i < x + w; ++i) {
64  set(i, j, color);
65  }
66  }
67 }
68 
69 Color Image::get(unsigned int x, unsigned int y) {
70  unsigned int pos = x * 4 + (y * _width * 4);
71  return Color(_buffer[pos], _buffer[pos + 1], _buffer[pos + 2], _buffer[pos + 3]);
72 }
73 
75  if (!_texture)
76  Log.Error("Image: attempt to access a non-created texture.");
77 
78  return _texture;
79 }
80 
82  _texture = Make<Texture>(*this);
83 }
84 
85 void Image::updateTextureZone(unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
86  if (!_texture) {
87  Log.Error("Image: attempt to update a non-created texture.");
88  return;
89  }
90 
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) {
94 
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;
100  }
101  }
102 
103  _texture->updateZone(x, y, w, h, subBuffer);
104 
105  delete[] subBuffer;
106 }
107 
108 unsigned int Image::getWidth() {
109  return _width;
110 }
111 
112 unsigned int Image::getHeight() {
113  return _height;
114 }
115 
116 } /* graphics */
117 } /* hx3d */
unsigned char a
Alpha component.
Definition: color.hpp:102
unsigned char g
Green component.
Definition: color.hpp:98
void setRect(unsigned int x, unsigned int y, unsigned int w, unsigned int h, Color color)
Set a rectangle with a color.
Definition: image.cpp:52
Four [0..255] components defined color.
Definition: color.hpp:32
unsigned int getHeight()
Get the image height.
Definition: image.cpp:112
void buildTexture()
Recreate a texture from the image.
Definition: image.cpp:81
void set(unsigned int x, unsigned int y, Color color)
Set a pixel with a color.
Definition: image.cpp:44
hx3d framework namespace
Definition: audio.hpp:26
void updateTextureZone(unsigned int x, unsigned int y, unsigned int w, unsigned int h)
Update an existing texture zone.
Definition: image.cpp:85
void Error(const std::string fmt,...)
Write an error message.
Definition: log.cpp:72
unsigned int getWidth()
Get the image width.
Definition: image.cpp:108
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
Ptr< Texture > getTexture()
Get the texture.
Definition: image.cpp:74
void create(unsigned int width, unsigned int height)
Initialize an image.
Definition: image.cpp:37
unsigned char b
Blue component.
Definition: color.hpp:100
unsigned char r
Red component.
Definition: color.hpp:96
Image()
Construct an empty image. Use create to create the image.
Definition: image.cpp:29
Color get(unsigned int x, unsigned int y)
Get a pixel color.
Definition: image.cpp:69
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34