Gorgon Game Engine
Texture.h
Go to the documentation of this file.
1 #pragma once
2 
3 #pragma warning(disable:4250)
4 
5 #include "../Geometry/Size.h"
6 #include "../Geometry/Bounds.h"
7 #include "../GL.h"
8 #include "../Graphics.h"
9 #include "../Containers/Image.h"
10 #include "Drawables.h"
11 
12 namespace Gorgon { namespace Graphics {
13 
17  class Texture : public virtual TextureSource {
18  public:
19 
21  Texture() {
22  Set(0, ColorMode::Invalid, {0, 0});
23  }
24 
26  Texture(GL::Texture id, ColorMode mode, const Geometry::Size &size) {
27  Set(id, mode, size);
28  }
29 
31  Texture(GL::Texture id, ColorMode mode, const Geometry::Size &size, const Geometry::Bounds &location) {
32  Set(id, mode, size, location);
33  }
34 
36  Texture(const Containers::Image &image) : Texture(GL::GenerateTexture(image), image.GetMode(), image.GetSize()) {
37  owner=true;
38  }
39 
41  Texture(Texture &other) : id(other.id), size(other.size), owner(false) {
42  memcpy(coordinates, other.coordinates, sizeof(coordinates));
43  }
44 
47  Texture(Texture &&other) : id(other.id), mode(other.mode), size(other.size), owner(other.owner) {
48  memcpy(coordinates, other.coordinates, sizeof(coordinates));
49  other.owner=false;
50  }
51 
53  void Swap(Texture &other) {
54  using std::swap;
55 
56  swap(id, other.id);
57  swap(size, other.size);
58  swap(mode, other.mode);
60  swap(owner, other.owner);
61  }
62 
63  virtual ~Texture() {
64  Destroy();
65  }
66 
69  void Set(const Containers::Image &image) {
70  Destroy();
71 
72  id=GL::GenerateTexture(image);
73  size=image.GetSize();
74  mode = image.GetMode();
75  owner=true;
76 
78  }
79 
82  void Set(GL::Texture id) {
83  if(owner) {
85  }
86 
87  owner = false;
88  this->id=id;
89  }
90 
93  void Set(GL::Texture id, ColorMode mode, const Geometry::Size &size) {
94  Destroy();
95 
96  this->id=id;
97  this->size=size;
98  this->mode=mode;
99 
100  memcpy(coordinates, fullcoordinates, sizeof(fullcoordinates));
101  }
102 
108  void Set(GL::Texture id, ColorMode mode, const Geometry::Size &size, const Geometry::Bounds &location) {
109  Destroy();
110 
111  this->id=id;
112  this->size=location.GetSize();
113  this->mode=mode;
114 
115  coordinates[0] ={float(location.Left)/size.Width, float(location.Top)/size.Height};
116  coordinates[1] ={float(location.Right)/size.Width, float(location.Top)/size.Height};
117  coordinates[2] ={float(location.Right)/size.Width, float(location.Bottom)/size.Height};
118  coordinates[3] ={float(location.Left)/size.Width, float(location.Bottom)/size.Height};
119  }
120 
121 
124  void Assume(GL::Texture id, ColorMode mode, const Geometry::Size &size) {
125  Set(id, mode, size);
126 
127  owner = true;
128  }
129 
135  void Assume(GL::Texture id, ColorMode mode, const Geometry::Size &size, const Geometry::Bounds &location) {
136  Set(id, mode, size, location);
137 
138  owner = true;
139  }
140 
144  this->size = size;
145  this->mode = mode;
146 
147  owner = true;
148  }
149 
151  virtual GL::Texture GetID() const override final {
152  return id;
153  }
154 
156  virtual Geometry::Size GetImageSize() const override final {
157  return size;
158  }
159 
161  virtual const Geometry::Pointf *GetCoordinates() const override final {
162  return coordinates;
163  }
164 
165  ColorMode GetMode() const override {
166  return mode;
167  }
168 
170  void Destroy() {
171  if(owner) {
172  GL::DestroyTexture(id);
173  }
174  id=0;
175  owner=false;
176  size={0, 0};
177  }
178 
180  GL::Texture Release() {
181  auto id=this->id;
182  this->id=0;
183  owner=false;
184  size={0, 0};
185 
186  return id;
187  }
188 
189  protected:
190 
192  GL::Texture id = 0;
193 
196 
199 
201  bool owner = false;
202 
207  };
208 
209 
211  class TextureImage : public virtual Texture, public virtual Image {
212  public:
215 
217  TextureImage(TextureImage &other) : Texture(other) {
218  }
219 
222  id=other.id;
223  size=other.size;
224  owner=other.owner;
225  mode=other.mode;
226  memcpy(coordinates, other.coordinates, sizeof(coordinates));
227  other.owner=false;
228  other.id = 0;
229  }
230 
232  TextureImage(GL::Texture id, ColorMode mode, const Geometry::Size &size) : Texture(id, mode, size) {
233  }
234 
236  TextureImage(GL::Texture id, ColorMode mode, const Geometry::Size &size, const Geometry::Bounds &location) : Texture(id, mode, size, location) {
237  }
238 
240  TextureImage(const Containers::Image &image) : Texture(image) {
241  }
242 
243  };
244 
245 
246  class TextureProvider : public virtual TextureImage, public virtual ImageProvider {
247  public:
250 
253  }
254 
256  TextureProvider(TextureProvider &&other) : Texture(std::move(other)) {
257  }
258 
261  }
262 
264  TextureProvider(GL::Texture id, ColorMode mode, const Geometry::Size &size, const Geometry::Bounds &location) : Texture(id, mode, size, location) {
265  }
266 
268  TextureProvider(const Containers::Image &image) : Texture(image) {
269  }
270 
271  //types are derived not to type the same code for every class
272  virtual auto MoveOutProvider() -> decltype(*this) override {
273  auto ret = new std::remove_reference<decltype(*this)>::type(std::move(*this));
274 
275  return *ret;
276  }
277 
278  virtual Geometry::Size GetSize() const override {
279  auto diff = coordinates[2] - coordinates[0];
280 
281  return {int(diff.X * size.Width + 0.9999), int(diff.Y * size.Height + 0.9999)};
282  }
283 
284  protected:
285  Geometry::Size getsize() const override {
286  return GetSize();
287  }
288  };
289 
290 } }
Gorgon::Graphics::TextureProvider::TextureProvider
TextureProvider(GL::Texture id, ColorMode mode, const Geometry::Size &size, const Geometry::Bounds &location)
Atlas constructor, specifies a region of the texture, size is for the entirity of the texture.
Definition: Texture.h:264
Gorgon::Geometry::basic_Bounds
This class represents boundaries of 2D objects.
Definition: Bounds.h:27
Gorgon::swap
void swap(Event< Source_, Args_... > &l, Event< Source_, Args_... > &r)
Swaps two events.
Definition: Event.h:351
Gorgon::GL::GenerateTexture
Texture GenerateTexture(const Containers::Image &data)
This function generates a texture from the given image data.
Definition: OpenGL.cpp:146
Gorgon::Containers::basic_Image::GetMode
Graphics::ColorMode GetMode() const
Returns the color mode of the image.
Definition: Image.h:1311
Gorgon::Graphics::TextureProvider::MoveOutProvider
virtual auto MoveOutProvider() -> decltype(*this) override
This function moves this animation provider into a new provider.
Definition: Texture.h:272
Gorgon::Graphics::ColorMode
ColorMode
Color modes for images.
Definition: Color.h:16
Gorgon::Graphics::Texture
This class represents an image depends on a GL texture.
Definition: Texture.h:17
Gorgon::Graphics::Texture::mode
ColorMode mode
Color mode of the texture, necessary to choose correct texture.
Definition: Texture.h:198
Gorgon::Graphics::ColorMode::Invalid
@ Invalid
This is used to mark invalid color data.
Gorgon::Graphics::Texture::GetCoordinates
virtual const Geometry::Pointf * GetCoordinates() const override final
Returns the coordinates of the texture to be used. Declared final to allow inlining.
Definition: Texture.h:161
Gorgon::Graphics::Texture::~Texture
virtual ~Texture()
Definition: Texture.h:63
Gorgon::Geometry::basic_Size::Height
T_ Height
Height of this size object.
Definition: Size.h:261
Gorgon::Graphics::Texture::Texture
Texture(GL::Texture id, ColorMode mode, const Geometry::Size &size)
Regular, full texture constructor.
Definition: Texture.h:26
Drawables.h
Gorgon::Graphics::Texture::Texture
Texture(Texture &other)
Copies a texture. This newly created texture will not assume ownership.
Definition: Texture.h:41
Gorgon::Graphics::Texture::Release
GL::Texture Release()
Releases the texture id that might be owned by this object without destroying it.
Definition: Texture.h:180
Gorgon::Graphics::TextureProvider::TextureProvider
TextureProvider(TextureProvider &&other)
Move constructor.
Definition: Texture.h:256
Gorgon::Graphics::Texture::CreateEmpty
void CreateEmpty(const Geometry::Size &size, ColorMode mode)
Create an empty texture.
Definition: Texture.h:142
Gorgon::Graphics::TextureProvider::TextureProvider
TextureProvider(TextureProvider &other)
Copy constructor.
Definition: Texture.h:252
Gorgon::Graphics::Texture::Texture
Texture(const Containers::Image &image)
This constructor creates a new texture from the given Image.
Definition: Texture.h:36
Gorgon::Graphics::TextureImage::TextureImage
TextureImage(const Containers::Image &image)
This constructor creates a new texture from the given Image.
Definition: Texture.h:240
Gorgon::GL::GenerateEmptyTexture
Texture GenerateEmptyTexture(const Geometry::Size &size, Graphics::ColorMode mode)
This function generates a texture from the given image data.
Definition: OpenGL.cpp:154
Gorgon::Geometry::basic_Bounds::Left
T_ Left
Left-most boundary.
Definition: Bounds.h:399
Gorgon::Graphics::Texture::Assume
void Assume(GL::Texture id, ColorMode mode, const Geometry::Size &size, const Geometry::Bounds &location)
Sets the texture to the given id with the given size.
Definition: Texture.h:135
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Size
This class represents a 2D geometric size.
Definition: Size.h:23
Gorgon::Graphics::Texture::GetImageSize
virtual Geometry::Size GetImageSize() const override final
Returns the size of the texture in pixels. Declared final to allow inlining.
Definition: Texture.h:156
Gorgon::Graphics::Image
This is an interface for solid texture based image.
Definition: Drawables.h:511
Gorgon::Graphics::Texture::Texture
Texture(Texture &&other)
Moves a texture.
Definition: Texture.h:47
Gorgon::Graphics::Texture::Set
void Set(GL::Texture id)
Sets the texture to the given id without any modification to size or color mode.
Definition: Texture.h:82
Gorgon::Graphics::Texture::owner
bool owner
Whether this object owns this texture.
Definition: Texture.h:201
Gorgon::Graphics::TextureImage
This is a solid texture based image class.
Definition: Texture.h:211
Gorgon::Graphics::Texture::size
Geometry::Size size
Size of the texture.
Definition: Texture.h:195
Gorgon::GL::DestroyTexture
void DestroyTexture(Texture texture)
Destroys the given texture.
Definition: OpenGL.cpp:213
Gorgon::Graphics::Texture::id
GL::Texture id
GL texture id.
Definition: Texture.h:192
Gorgon::Geometry::basic_Point
This class represents a 2D point.
Definition: Point.h:32
Gorgon::Graphics::TextureImage::TextureImage
TextureImage(GL::Texture id, ColorMode mode, const Geometry::Size &size)
Regular, full texture constructor.
Definition: Texture.h:232
Gorgon::Graphics::TextureImage::TextureImage
TextureImage()
Default constructor, creates an empty texture.
Definition: Texture.h:214
Gorgon::Graphics::TextureProvider::TextureProvider
TextureProvider()
Default constructor, creates an empty texture.
Definition: Texture.h:249
Gorgon::Graphics::Texture::GetID
virtual GL::Texture GetID() const override final
Returns GL::Texture to be drawn. Declared final to allow inlining.
Definition: Texture.h:151
Gorgon::Graphics::Texture::Swap
void Swap(Texture &other)
Swaps two textures.
Definition: Texture.h:53
Gorgon::Graphics::TextureImage::TextureImage
TextureImage(TextureImage &&other)
Move constructor.
Definition: Texture.h:221
Gorgon::Graphics::Texture::Texture
Texture()
Default constructor, creates an empty texture.
Definition: Texture.h:21
Gorgon::Geometry::basic_Bounds::Right
T_ Right
Right-most boundary.
Definition: Bounds.h:405
Gorgon::UI::Graphics
@ Graphics
Definition: Template.h:164
Gorgon::Graphics::TextureSource
This interface represents a GL texture source.
Definition: Graphics.h:480
Gorgon::Geometry::basic_Bounds::Bottom
T_ Bottom
Bottom-most boundary.
Definition: Bounds.h:408
Gorgon::Graphics::Texture::GetMode
ColorMode GetMode() const override
Definition: Texture.h:165
Gorgon::Containers::basic_Image
This class is a container for image data.
Definition: Image.h:19
Gorgon::Graphics::TextureImage::TextureImage
TextureImage(TextureImage &other)
Copy constructor.
Definition: Texture.h:217
Gorgon::Geometry::basic_Size::Width
T_ Width
Width of this size object.
Definition: Size.h:258
Gorgon::Geometry::basic_Bounds::GetSize
basic_Size< T_ > GetSize() const
Returns the size of the bounds object.
Definition: Bounds.h:141
Gorgon::Graphics::Texture::Set
void Set(const Containers::Image &image)
Sets the texture to the given id with the given size.
Definition: Texture.h:69
Gorgon::Graphics::Texture::Set
void Set(GL::Texture id, ColorMode mode, const Geometry::Size &size, const Geometry::Bounds &location)
Sets the texture to the given id with the given size.
Definition: Texture.h:108
Gorgon::Graphics::TextureImage::TextureImage
TextureImage(GL::Texture id, ColorMode mode, const Geometry::Size &size, const Geometry::Bounds &location)
Atlas constructor, specifies a region of the texture, size is for the entirity of the texture.
Definition: Texture.h:236
Gorgon::Graphics::TextureProvider::getsize
Geometry::Size getsize() const override
Should return the exact size of this object.
Definition: Texture.h:285
Gorgon::Graphics::Texture::coordinates
Geometry::Pointf coordinates[4]
Readily calculated texture coordinates of the image.
Definition: Texture.h:206
Gorgon::Graphics::Texture::Texture
Texture(GL::Texture id, ColorMode mode, const Geometry::Size &size, const Geometry::Bounds &location)
Atlas constructor, specifies a region of the texture.
Definition: Texture.h:31
Gorgon::Graphics::TextureProvider::TextureProvider
TextureProvider(GL::Texture id, ColorMode mode, const Geometry::Size &size)
Regular, full texture constructor.
Definition: Texture.h:260
Gorgon::Graphics::Texture::Assume
void Assume(GL::Texture id, ColorMode mode, const Geometry::Size &size)
Sets the texture to the given id with the given size.
Definition: Texture.h:124
Gorgon::Graphics::TextureProvider::TextureProvider
TextureProvider(const Containers::Image &image)
This constructor creates a new texture from the given Image.
Definition: Texture.h:268
Gorgon::Graphics::Texture::Destroy
void Destroy()
Remove the texture from this object. If this object is the owner of the texture, then it is destroyed...
Definition: Texture.h:170
Gorgon::Graphics::TextureProvider
Definition: Texture.h:246
Gorgon::Graphics::Texture::Set
void Set(GL::Texture id, ColorMode mode, const Geometry::Size &size)
Sets the texture to the given id with the given size.
Definition: Texture.h:93
Gorgon::Graphics::TextureSource::fullcoordinates
static const Geometry::Pointf fullcoordinates[4]
Coordinates that selects the entire texture to be used.
Definition: Graphics.h:501
Gorgon::Graphics::ImageProvider
Definition: Animations.h:85
Gorgon::Containers::basic_Image::GetSize
Geometry::Size GetSize() const
Returns the size of the image.
Definition: Image.h:1291
Gorgon::Geometry::basic_Bounds::Top
T_ Top
Top-most boundary.
Definition: Bounds.h:402
Gorgon::Graphics::TextureProvider::GetSize
virtual Geometry::Size GetSize() const override
Definition: Texture.h:278