CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUTexture.h
1 //
2 // CUTexture.hpp
3 // CUGL
4 //
5 // Created by Walker White on 6/24/16.
6 // Copyright © 2016 Game Design Initiative at Cornell. All rights reserved.
7 //
8 
9 #ifndef _CU_TEXTURE_H__
10 #define _CU_TEXTURE_H__
11 
12 #include "../math/CUMathBase.h"
13 #include "../math/CUSize.h"
14 
15 namespace cugl {
16 
30 class Texture : public std::enable_shared_from_this<Texture> {
31 #pragma mark Values
32 public:
40  enum class PixelFormat : GLenum {
42  RGBA = GL_RGBA,
44  RGB = GL_RGBA,
46  RED = GL_RED,
48  ALPHA = GL_ALPHA,
50  UNDEFINED = GL_RG
51  };
52 
53 private:
55  GLuint _buffer;
56 
58  unsigned int _width;
59 
61  unsigned int _height;
62 
64  PixelFormat _pixelFormat;
65 
67  std::string _name;
68 
70  GLuint _minFilter;
71 
73  GLuint _magFilter;
74 
76  GLuint _wrapS;
77 
79  GLuint _wrapT;
80 
82  bool _hasMipmaps;
83 
85 
86  std::shared_ptr<Texture> _parent;
87 
89  GLfloat _minS;
90 
92  GLfloat _maxS;
93 
95  GLfloat _minT;
96 
98  GLfloat _maxT;
99 
101  bool _active;
102 
103 #pragma mark -
104 #pragma mark Constructors
105 public:
112  Texture();
113 
117  ~Texture() { dispose(); }
118 
124  void dispose();
125 
141  bool init(int width, int height, PixelFormat format = PixelFormat::RGBA);
142 
159  bool initWithData(const void *data, int width, int height,
160  PixelFormat format = PixelFormat::RGBA);
161 
179  bool initWithFile(const std::string& filename);
180 
181 #pragma mark -
182 #pragma mark Static Constructors
183 
198  static std::shared_ptr<Texture> alloc(int width, int height,
199  PixelFormat format = PixelFormat::RGBA) {
200  std::shared_ptr<Texture> result = std::make_shared<Texture>();
201  return (result->init(width, height, (cugl::Texture::PixelFormat)format) ? result : nullptr);
202  }
203 
220  static std::shared_ptr<Texture> allocWithData(const void *data, int width, int height,
221  PixelFormat format = PixelFormat::RGBA) {
222  std::shared_ptr<Texture> result = std::make_shared<Texture>();
223  return (result->initWithData(data, width, height, (cugl::Texture::PixelFormat)format) ? result : nullptr);
224 
225  }
226 
227 
245  static std::shared_ptr<Texture> allocWithFile(const std::string& filename) {
246  std::shared_ptr<Texture> result = std::make_shared<Texture>();
247  return (result->initWithFile(filename) ? result : nullptr);
248  }
249 
250 #pragma mark -
251 #pragma mark Setters
252 
264  const Texture& operator=(const void *data) {
265  return set(data);
266  }
267 
280  const Texture& set(const void *data);
281 
282 #pragma mark -
283 #pragma mark Attributes
284 
289  bool isReady() const { return _buffer != 0; }
290 
302  bool isActive() const {
303  return (_parent != nullptr ? _parent->isActive() : _active);
304  }
305 
314  bool hasMipMaps() const {
315  return (_parent != nullptr ? _parent->hasMipMaps() : _hasMipmaps);
316  }
317 
325  void buildMipMaps();
326 
334  GLuint getBuffer() { return _buffer; }
335 
344  void setName(std::string name) { _name = name; }
345 
354  const std::string& getName() const { return _name; }
355 
361  unsigned int getWidth() const { return _width; }
362 
368  unsigned int getHeight() const { return _height; }
369 
375  Size getSize() { return Size((float)_width,(float)_height); }
376 
385  PixelFormat getFormat() const { return _pixelFormat; }
386 
395  GLuint getMinFilter() const {
396  return (_parent != nullptr ? _parent->getMinFilter() : _minFilter);
397  }
398 
407  GLuint getMagFilter() const {
408  return (_parent != nullptr ? _parent->getMagFilter() : _magFilter);
409  }
410 
419  void setMinFilter(GLuint minFilter);
420 
429  void setMagFilter(GLuint magFilter);
430 
438  GLuint getWrapS() const {
439  return (_parent != nullptr ? _parent->getWrapS() : _wrapS);
440  }
441 
449  GLuint getWrapT() const {
450  return (_parent != nullptr ? _parent->getWrapT() : _wrapT);
451  }
452 
458  void setWrapS(GLuint wrap);
459 
465  void setWrapT(GLuint wrap);
466 
467 
468 #pragma mark -
469 #pragma mark Atlas Support
470 
477  const std::shared_ptr<Texture>& getParent() const { return _parent; }
478 
486  std::shared_ptr<Texture> getParent() { return _parent; }
487 
507  std::shared_ptr<Texture> getSubTexture(GLfloat minS, GLfloat maxS, GLfloat minT, GLfloat maxT);
508 
516  bool isSubTexture() const { return _parent != nullptr; }
517 
526  GLfloat getMinS() const { return _minS; }
527 
536  GLfloat getMinT() const { return _minT; }
537 
546  GLfloat getMaxS() const { return _maxS; }
547 
556  GLfloat getMaxT() const { return _maxT; }
557 
558 // TODO:: Ninepatch support
559 
560 #pragma mark -
561 #pragma mark Rendering
562 
567  void bind();
568 
574  void unbind();
575 
576 #pragma mark -
577 #pragma mark Conversions
578 
588  std::string toString(bool verbose = false) const;
589 
591  operator std::string() const { return toString(); }
592 
593 };
594 
595 }
596 
597 #endif /* _CU_TEXTURE_H__ */
void setMagFilter(GLuint magFilter)
Definition: CUSize.h:57
GLfloat getMinS() const
Definition: CUTexture.h:526
bool initWithData(const void *data, int width, int height, PixelFormat format=PixelFormat::RGBA)
GLfloat getMaxS() const
Definition: CUTexture.h:546
const Texture & set(const void *data)
std::shared_ptr< Texture > getParent()
Definition: CUTexture.h:486
const Texture & operator=(const void *data)
Definition: CUTexture.h:264
GLuint getMinFilter() const
Definition: CUTexture.h:395
std::string toString(bool verbose=false) const
GLuint getWrapT() const
Definition: CUTexture.h:449
bool isSubTexture() const
Definition: CUTexture.h:516
bool isReady() const
Definition: CUTexture.h:289
bool init(int width, int height, PixelFormat format=PixelFormat::RGBA)
GLfloat getMinT() const
Definition: CUTexture.h:536
void setWrapT(GLuint wrap)
unsigned int getWidth() const
Definition: CUTexture.h:361
GLuint getBuffer()
Definition: CUTexture.h:334
static std::shared_ptr< Texture > allocWithData(const void *data, int width, int height, PixelFormat format=PixelFormat::RGBA)
Definition: CUTexture.h:220
PixelFormat getFormat() const
Definition: CUTexture.h:385
static std::shared_ptr< Texture > allocWithFile(const std::string &filename)
Definition: CUTexture.h:245
bool hasMipMaps() const
Definition: CUTexture.h:314
~Texture()
Definition: CUTexture.h:117
void setMinFilter(GLuint minFilter)
unsigned int getHeight() const
Definition: CUTexture.h:368
GLfloat getMaxT() const
Definition: CUTexture.h:556
GLuint getMagFilter() const
Definition: CUTexture.h:407
GLuint getWrapS() const
Definition: CUTexture.h:438
Definition: CUTexture.h:30
Size getSize()
Definition: CUTexture.h:375
bool isActive() const
Definition: CUTexture.h:302
void setWrapS(GLuint wrap)
const std::string & getName() const
Definition: CUTexture.h:354
PixelFormat
Definition: CUTexture.h:40
std::shared_ptr< Texture > getSubTexture(GLfloat minS, GLfloat maxS, GLfloat minT, GLfloat maxT)
const std::shared_ptr< Texture > & getParent() const
Definition: CUTexture.h:477
Definition: CUAnimationNode.h:52
static std::shared_ptr< Texture > alloc(int width, int height, PixelFormat format=PixelFormat::RGBA)
Definition: CUTexture.h:198
void setName(std::string name)
Definition: CUTexture.h:344
bool initWithFile(const std::string &filename)
void buildMipMaps()