jubilant-funicular
GLTexture.h
1 #ifndef NTA_GLTEXTURE_H_INCLUDED
2 #define NTA_GLTEXTURE_H_INCLUDED
3 
4 #include <GL/glew.h>
5 
6 #include "nta/Errors.h"
7 
8 namespace nta {
13  struct RawTexture {
14  bool operator==(const RawTexture& rhs) const {
15  return data == rhs.data;
16  }
17 
18  GLuint width, height;
19  GLenum format;
21  GLubyte* data = nullptr;
22  };
24  struct GLTexture {
25  GLTexture() : id(0) {}
26  GLTexture(const RawTexture& raw, GLint minFilt = GL_LINEAR_MIPMAP_LINEAR,
27  GLint magFilt = GL_LINEAR) : id(0) { init(raw, minFilt, magFilt); }
28  void init(const RawTexture& raw, GLint minFilt = GL_LINEAR_MIPMAP_LINEAR,
29  GLint magFilt = GL_LINEAR);
30 
31  static GLTexture no_texture() {
32  GLTexture ret;
33  ret.id = ret.width = ret.height = 0;
34  return ret;
35  }
37  static GLTexture combine_horizontal(const GLTexture& lhs, const GLTexture& rhs);
38  static GLTexture combine_vertical(const GLTexture& lhs, const GLTexture& rhs);
39 
40  bool is_valid() const {
41  return id != 0;
42  }
43  bool operator==(const GLTexture& rhs) const {
44  return id == rhs.id;
45  }
46  bool operator<(const GLTexture& rhs) const {
47  return id < rhs.id;
48  }
50  GLTexture operator+(const GLTexture& rhs) const {
51  return combine_horizontal(*this, rhs);
52  }
54  GLTexture operator*(const GLTexture& rhs) const {
55  return combine_vertical(*this, rhs);
56  }
57 
59  void destroy() { glDeleteTextures(1, &id); }
60 
62  GLuint id;
64  GLint width, height;
65  };
67  class ImageLoader {
68  private:
70  static Result<GLTexture> readImage(crstring filePath, GLint minFilt,
71  GLint magFilt, crvec2 dimensions);
72  static Result<RawTexture> readImage(crstring filePath, crvec2 dimensions);
73  public:
74  friend class ResourceManager;
75  };
76 }
77 
78 namespace std {
79  template <>
80  struct hash<nta::GLTexture> {
81  std::size_t operator()(const nta::GLTexture& tex) const {
82  return std::hash<GLuint>()(tex.id);
83  }
84  };
85 }
86 
87 #endif // NTA_GLTEXTURE_H_INCLUDED
nta::GLTexture::operator*
GLTexture operator*(const GLTexture &rhs) const
calls combine_vertical (not sure how I feel about making this an operator)
Definition: GLTexture.h:54
nta::ImageLoader::readImage
static Result< GLTexture > readImage(crstring filePath, GLint minFilt, GLint magFilt, crvec2 dimensions)
loads in any image file
Definition: GLTexture.cpp:183
nta::GLTexture::width
GLint width
the width and height, respectively, of the texture
Definition: GLTexture.h:64
nta::RawTexture::data
GLubyte * data
Data stored on the heap.
Definition: GLTexture.h:21
nta::GLTexture::id
GLuint id
the id of the texture
Definition: GLTexture.h:62
nta::Result
Definition: Errors.h:74
nta
Definition: Animation2D.h:6
nta::GLTexture::operator+
GLTexture operator+(const GLTexture &rhs) const
calls combine_horizontal
Definition: GLTexture.h:50
nta::ResourceManager
Definition: ResourceManager.h:16
nta::GLTexture::destroy
void destroy()
Deletes this texture.
Definition: GLTexture.h:59
nta::GLTexture
represents a texture (tied to a specific GL context)
Definition: GLTexture.h:24
nta::ImageLoader
loads images as GLTextures
Definition: GLTexture.h:67
nta::GLTexture::combine_horizontal
static GLTexture combine_horizontal(const GLTexture &lhs, const GLTexture &rhs)
Creates a new texture that is the result of placing lhs and rhs side by side.
Definition: GLTexture.cpp:42
nta::RawTexture
Definition: GLTexture.h:13