CUGL 1.3
Cornell University Game Library
CUTexture.h
1 //
2 // CUTexture.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides the class for representing a 2D OpenGL texture. This
6 // class also provides support for texture atlases. Any non-repeating texture
7 // can produce a subtexture. A subtexture wraps the same texture data (and so
8 // does not require a context switch in the rendering pipeline), but has
9 // different start and end boundaries, as defined by minS, maxS, minT and maxT
10 //
11 // CUGL MIT License:
12 // This software is provided 'as-is', without any express or implied
13 // warranty. In no event will the authors be held liable for any damages
14 // arising from the use of this software.
15 //
16 // Permission is granted to anyone to use this software for any purpose,
17 // including commercial applications, and to alter it and redistribute it
18 // freely, subject to the following restrictions:
19 //
20 // 1. The origin of this software must not be misrepresented; you must not
21 // claim that you wrote the original software. If you use this software
22 // in a product, an acknowledgment in the product documentation would be
23 // appreciated but is not required.
24 //
25 // 2. Altered source versions must be plainly marked as such, and must not
26 // be misrepresented as being the original software.
27 //
28 // 3. This notice may not be removed or altered from any source distribution.
29 //
30 // Author: Walker White
31 // Version: 6/23/16
32 
33 #ifndef _CU_TEXTURE_H__
34 #define _CU_TEXTURE_H__
35 
36 #include <cugl/math/CUMathBase.h>
37 #include <cugl/math/CUSize.h>
38 
39 namespace cugl {
40 
54 class Texture : public std::enable_shared_from_this<Texture> {
55 #pragma mark Values
56 public:
64  enum class PixelFormat : GLenum {
66  RGBA = GL_RGBA,
68  RGB = GL_RGBA,
70  RED = GL_RED,
72  ALPHA = GL_ALPHA,
74  UNDEFINED = GL_RG
75  };
76 
77 private:
79  GLuint _buffer;
80 
82  unsigned int _width;
83 
85  unsigned int _height;
86 
88  PixelFormat _pixelFormat;
89 
91  std::string _name;
92 
94  GLuint _minFilter;
95 
97  GLuint _magFilter;
98 
100  GLuint _wrapS;
101 
103  GLuint _wrapT;
104 
106  bool _hasMipmaps;
107 
109 
110  std::shared_ptr<Texture> _parent;
111 
113  GLfloat _minS;
114 
116  GLfloat _maxS;
117 
119  GLfloat _minT;
120 
122  GLfloat _maxT;
123 
125  bool _active;
126 
127 #pragma mark -
128 #pragma mark Constructors
129 public:
136  Texture();
137 
141  ~Texture() { dispose(); }
142 
148  void dispose();
149 
165  bool init(int width, int height, PixelFormat format = PixelFormat::RGBA);
166 
183  bool initWithData(const void *data, int width, int height,
184  PixelFormat format = PixelFormat::RGBA);
185 
203  bool initWithFile(const std::string& filename);
204 
205 #pragma mark -
206 #pragma mark Static Constructors
207 
222  static std::shared_ptr<Texture> alloc(int width, int height,
223  PixelFormat format = PixelFormat::RGBA) {
224  std::shared_ptr<Texture> result = std::make_shared<Texture>();
225  return (result->init(width, height, (cugl::Texture::PixelFormat)format) ? result : nullptr);
226  }
227 
244  static std::shared_ptr<Texture> allocWithData(const void *data, int width, int height,
245  PixelFormat format = PixelFormat::RGBA) {
246  std::shared_ptr<Texture> result = std::make_shared<Texture>();
247  return (result->initWithData(data, width, height, (cugl::Texture::PixelFormat)format) ? result : nullptr);
248 
249  }
250 
251 
269  static std::shared_ptr<Texture> allocWithFile(const std::string& filename) {
270  std::shared_ptr<Texture> result = std::make_shared<Texture>();
271  return (result->initWithFile(filename) ? result : nullptr);
272  }
273 
274 #pragma mark -
275 #pragma mark Setters
276 
288  const Texture& operator=(const void *data) {
289  return set(data);
290  }
291 
304  const Texture& set(const void *data);
305 
306 #pragma mark -
307 #pragma mark Attributes
308 
313  bool isReady() const { return _buffer != 0; }
314 
326  bool isActive() const {
327  return (_parent != nullptr ? _parent->isActive() : _active);
328  }
329 
338  bool hasMipMaps() const {
339  return (_parent != nullptr ? _parent->hasMipMaps() : _hasMipmaps);
340  }
341 
349  void buildMipMaps();
350 
358  GLuint getBuffer() { return _buffer; }
359 
368  void setName(std::string name) { _name = name; }
369 
378  const std::string& getName() const { return _name; }
379 
385  unsigned int getWidth() const { return _width; }
386 
392  unsigned int getHeight() const { return _height; }
393 
399  Size getSize() { return Size((float)_width,(float)_height); }
400 
409  PixelFormat getFormat() const { return _pixelFormat; }
410 
419  GLuint getMinFilter() const {
420  return (_parent != nullptr ? _parent->getMinFilter() : _minFilter);
421  }
422 
431  GLuint getMagFilter() const {
432  return (_parent != nullptr ? _parent->getMagFilter() : _magFilter);
433  }
434 
443  void setMinFilter(GLuint minFilter);
444 
453  void setMagFilter(GLuint magFilter);
454 
462  GLuint getWrapS() const {
463  return (_parent != nullptr ? _parent->getWrapS() : _wrapS);
464  }
465 
473  GLuint getWrapT() const {
474  return (_parent != nullptr ? _parent->getWrapT() : _wrapT);
475  }
476 
482  void setWrapS(GLuint wrap);
483 
489  void setWrapT(GLuint wrap);
490 
491 
492 #pragma mark -
493 #pragma mark Atlas Support
494 
501  const std::shared_ptr<Texture>& getParent() const { return _parent; }
502 
510  std::shared_ptr<Texture> getParent() { return _parent; }
511 
531  std::shared_ptr<Texture> getSubTexture(GLfloat minS, GLfloat maxS, GLfloat minT, GLfloat maxT);
532 
540  bool isSubTexture() const { return _parent != nullptr; }
541 
550  GLfloat getMinS() const { return _minS; }
551 
560  GLfloat getMinT() const { return _minT; }
561 
570  GLfloat getMaxS() const { return _maxS; }
571 
580  GLfloat getMaxT() const { return _maxT; }
581 
582 // TODO:: Ninepatch support
583 
584 #pragma mark -
585 #pragma mark Rendering
586 
591  void bind();
592 
598  void unbind();
599 
600 #pragma mark -
601 #pragma mark Conversions
602 
612  std::string toString(bool verbose = false) const;
613 
615  operator std::string() const { return toString(); }
616 
617 };
618 
619 }
620 
621 #endif /* _CU_TEXTURE_H__ */
cugl::Texture::getWrapT
GLuint getWrapT() const
Definition: CUTexture.h:473
cugl::Texture::getMaxS
GLfloat getMaxS() const
Definition: CUTexture.h:570
cugl::Texture::setWrapS
void setWrapS(GLuint wrap)
cugl::Texture::getSize
Size getSize()
Definition: CUTexture.h:399
cugl::Texture::operator=
const Texture & operator=(const void *data)
Definition: CUTexture.h:288
cugl::Texture::getParent
const std::shared_ptr< Texture > & getParent() const
Definition: CUTexture.h:501
cugl::Texture::bind
void bind()
cugl::Texture::dispose
void dispose()
cugl::Texture::getHeight
unsigned int getHeight() const
Definition: CUTexture.h:392
cugl::Texture::setWrapT
void setWrapT(GLuint wrap)
cugl::Texture::getParent
std::shared_ptr< Texture > getParent()
Definition: CUTexture.h:510
cugl::Texture::getMinFilter
GLuint getMinFilter() const
Definition: CUTexture.h:419
cugl::Texture::getSubTexture
std::shared_ptr< Texture > getSubTexture(GLfloat minS, GLfloat maxS, GLfloat minT, GLfloat maxT)
cugl::Texture::PixelFormat::UNDEFINED
cugl::Size
Definition: CUSize.h:57
cugl::Texture::initWithData
bool initWithData(const void *data, int width, int height, PixelFormat format=PixelFormat::RGBA)
cugl::Texture::PixelFormat::RED
cugl::Texture
Definition: CUTexture.h:54
cugl::Texture::getMaxT
GLfloat getMaxT() const
Definition: CUTexture.h:580
cugl::Texture::PixelFormat::RGB
cugl::Texture::getFormat
PixelFormat getFormat() const
Definition: CUTexture.h:409
cugl::Texture::initWithFile
bool initWithFile(const std::string &filename)
cugl::Texture::setName
void setName(std::string name)
Definition: CUTexture.h:368
cugl::Texture::isSubTexture
bool isSubTexture() const
Definition: CUTexture.h:540
cugl::Texture::Texture
Texture()
cugl::Texture::getMagFilter
GLuint getMagFilter() const
Definition: CUTexture.h:431
cugl::Texture::PixelFormat::ALPHA
cugl::Texture::isReady
bool isReady() const
Definition: CUTexture.h:313
cugl::Texture::getWidth
unsigned int getWidth() const
Definition: CUTexture.h:385
cugl::Texture::setMagFilter
void setMagFilter(GLuint magFilter)
cugl::Texture::getMinS
GLfloat getMinS() const
Definition: CUTexture.h:550
cugl::Texture::unbind
void unbind()
cugl::Texture::setMinFilter
void setMinFilter(GLuint minFilter)
cugl::Texture::init
bool init(int width, int height, PixelFormat format=PixelFormat::RGBA)
cugl::Texture::PixelFormat
PixelFormat
Definition: CUTexture.h:64
cugl::Texture::allocWithData
static std::shared_ptr< Texture > allocWithData(const void *data, int width, int height, PixelFormat format=PixelFormat::RGBA)
Definition: CUTexture.h:244
cugl::Texture::PixelFormat::RGBA
cugl::Texture::~Texture
~Texture()
Definition: CUTexture.h:141
cugl::Texture::getName
const std::string & getName() const
Definition: CUTexture.h:378
cugl::Texture::allocWithFile
static std::shared_ptr< Texture > allocWithFile(const std::string &filename)
Definition: CUTexture.h:269
cugl::Texture::getBuffer
GLuint getBuffer()
Definition: CUTexture.h:358
cugl::Texture::isActive
bool isActive() const
Definition: CUTexture.h:326
cugl::Texture::toString
std::string toString(bool verbose=false) const
cugl::Texture::buildMipMaps
void buildMipMaps()
cugl::Texture::hasMipMaps
bool hasMipMaps() const
Definition: CUTexture.h:338
cugl::Texture::set
const Texture & set(const void *data)
cugl::Texture::getWrapS
GLuint getWrapS() const
Definition: CUTexture.h:462
cugl::Texture::getMinT
GLfloat getMinT() const
Definition: CUTexture.h:560
cugl::Texture::alloc
static std::shared_ptr< Texture > alloc(int width, int height, PixelFormat format=PixelFormat::RGBA)
Definition: CUTexture.h:222