CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUTexturedNode.h
1 //
2 // CUTexturedNode.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides an abstract class for textured scene graph nodes. It
6 // is the core scene graph node in CUGL.
7 //
8 // You should never instantiate an object of this class. Instead, you should
9 // use one of the concrete subclasses: WireNode, PolygonNode, or PathNode.
10 // Because it is abstract, it has no allocators. It only has initializers.
11 //
12 // CUGL zlib License:
13 // This software is provided 'as-is', without any express or implied
14 // warranty. In no event will the authors be held liable for any damages
15 // arising from the use of this software.
16 //
17 // Permission is granted to anyone to use this software for any purpose,
18 // including commercial applications, and to alter it and redistribute it
19 // freely, subject to the following restrictions:
20 //
21 // 1. The origin of this software must not be misrepresented; you must not
22 // claim that you wrote the original software. If you use this software
23 // in a product, an acknowledgment in the product documentation would be
24 // appreciated but is not required.
25 //
26 // 2. Altered source versions must be plainly marked as such, and must not
27 // be misrepresented as being the original software.
28 //
29 // 3. This notice may not be removed or altered from any source distribution.
30 //
31 // Author: Walker White
32 // Version: 6/27/16
33 
34 #ifndef __CU_TEXTURED_NODE_H__
35 #define __CU_TEXTURED_NODE_H__
36 
37 #include <string>
38 #include "../math/CUPoly2.h"
39 #include "CUNode.h"
40 #include "../renderer/CUTexture.h"
41 #include "../renderer/CUVertex.h"
42 
43 namespace cugl {
83 class TexturedNode : public Node {
84 #pragma mark Values
85 protected:
87  std::string _classname;
88 
90  std::shared_ptr<Texture> _texture;
91 
94 
96  bool _absolute;
97 
99  bool _rendered;
101  std::vector<Vertex2> _vertices;
102 
106  GLenum _srcFactor;
108  GLenum _dstFactor;
109 
114 
115 #pragma mark -
116 #pragma mark Constructors
117 public:
124  TexturedNode();
125 
129  virtual ~TexturedNode() { _texture = nullptr; }
130 
140  virtual void dispose() override;
141 
151  virtual bool init() override {
152  return initWithTexture(nullptr, Rect::ZERO);
153  }
154 
168  bool init(const std::vector<Vec2>& vertices) {
169  return initWithTexture(nullptr, vertices);
170  }
171 
183  bool init(const Poly2& poly) {
184  return initWithTexture(nullptr, poly);
185  }
186 
200  bool init(const Rect& rect) {
201  return initWithTexture(nullptr, rect);
202  }
203 
214  bool initWithFile(const std::string& filename);
215 
226  bool initWithFile(const std::string& filename, const std::vector<Vec2>& vertices) {
227  _polygon.set(vertices);
228  return initWithFile(filename, _polygon);
229  }
230 
239  bool initWithFile(const std::string& filename, const Poly2& poly);
240 
251  bool initWithFile(const std::string& filename, const Rect& rect);
252 
263  bool initWithTexture(const std::shared_ptr<Texture>& texture);
264 
275  bool initWithTexture(const std::shared_ptr<Texture>& texture, const std::vector<Vec2>& vertices) {
276  setPolygon(vertices);
277  return initWithTexture(texture, _polygon);
278  }
279 
288  bool initWithTexture(const std::shared_ptr<Texture>& texture, const Poly2& poly);
289 
300  bool initWithTexture(const std::shared_ptr<Texture>& texture, const Rect& rect);
301 
302 
303 #pragma mark -
304 #pragma mark Attributes
305 
314  void setTexture(const std::string &filename) {
315  std::shared_ptr<Texture> texture = Texture::allocWithFile(filename);
316  setTexture(texture);
317  }
318 
328  void setTexture(const std::shared_ptr<Texture>& texture);
329 
335  std::shared_ptr<Texture>& getTexture() { return _texture; }
336 
342  const std::shared_ptr<Texture>& getTexture() const { return _texture; }
343 
351  virtual void setPolygon(const std::vector<Vec2>& vertices) = 0;
352 
358  virtual void setPolygon(const Poly2& poly);
359 
367  virtual void setPolygon(const Rect& rect) = 0;
368 
374  const Poly2& getPolygon() const { return _polygon; }
375 
390  virtual void shiftPolygon(float dx, float dy);
391 
402  const Rect& getBoundingRect() const { return _polygon.getBounds(); }
403 
422  void setBlendFunc(GLenum srcFactor, GLenum dstFactor);
423 
436  GLenum getSourceBlendFactor() const { return _srcFactor; }
437 
450  GLenum getDestinationBlendFactor() const { return _srcFactor; }
451 
467  void setBlendEquation(GLenum equation);
468 
481  GLenum getBlendEquation() const { return _blendEquation; }
482 
492  void flipHorizontal(bool flag) { _flipHorizontal = flag; updateTextureCoords(); }
493 
499  bool isFlipHorizontal() const { return _flipHorizontal; }
500 
510  void flipVertical(bool flag) { _flipVertical = flag; updateTextureCoords(); }
511 
517  bool isFlipVertical() const { return _flipVertical; }
518 
529  virtual std::string toString(bool verbose=false) const override;
530 
531 #pragma mark -
532 #pragma mark Absolute Position
533 
548  bool isAbsolute() const { return _absolute; }
549 
565  void setAbsolute(bool flag) {
566  _absolute = flag;
568  }
569 
593  virtual void setAnchor(const Vec2& anchor) override {
594  if (!_absolute) { Node::setAnchor(anchor); }
595  }
596 
597 
598 #pragma mark -
599 #pragma mark Rendering
600 
623  virtual void draw(const std::shared_ptr<SpriteBatch>& batch,
624  const Mat4& transform, Color4 tint) override = 0;
625 
626  void refresh() { clearRenderData(); generateRenderData(); }
627 
628 #pragma mark -
629 #pragma mark Internal Helpers
630 protected:
634  virtual void generateRenderData();
635 
639  void clearRenderData();
640 
648  void updateTextureCoords();
649 
652 
653 };
654 
655 }
656 
657 #endif /* __CU_TEXTURED_NODE_H__ */
GLenum getDestinationBlendFactor() const
Definition: CUTexturedNode.h:450
bool init(const Poly2 &poly)
Definition: CUTexturedNode.h:183
bool initWithTexture(const std::shared_ptr< Texture > &texture)
Definition: CUTexturedNode.h:83
static const Rect ZERO
Definition: CURect.h:54
bool isFlipVertical() const
Definition: CUTexturedNode.h:517
GLenum _srcFactor
Definition: CUTexturedNode.h:106
void setBlendFunc(GLenum srcFactor, GLenum dstFactor)
Definition: CUPoly2.h:115
bool init(const Rect &rect)
Definition: CUTexturedNode.h:200
void flipVertical(bool flag)
Definition: CUTexturedNode.h:510
Definition: CUVec2.h:61
const std::shared_ptr< Texture > & getTexture() const
Definition: CUTexturedNode.h:342
bool initWithFile(const std::string &filename, const std::vector< Vec2 > &vertices)
Definition: CUTexturedNode.h:226
std::vector< Vertex2 > _vertices
Definition: CUTexturedNode.h:101
std::string _classname
Definition: CUTexturedNode.h:87
GLenum getBlendEquation() const
Definition: CUTexturedNode.h:481
const Rect & getBounds() const
Definition: CUPoly2.h:834
Definition: CUNode.h:92
virtual std::string toString(bool verbose=false) const override
void updateTextureCoords()
void setTexture(const std::string &filename)
Definition: CUTexturedNode.h:314
bool _flipVertical
Definition: CUTexturedNode.h:113
const Rect & getBoundingRect() const
Definition: CUTexturedNode.h:402
bool init(const std::vector< Vec2 > &vertices)
Definition: CUTexturedNode.h:168
GLenum getSourceBlendFactor() const
Definition: CUTexturedNode.h:436
virtual void setAnchor(const Vec2 &anchor)
static std::shared_ptr< Texture > allocWithFile(const std::string &filename)
Definition: CUTexture.h:245
bool _flipHorizontal
Definition: CUTexturedNode.h:111
GLenum _blendEquation
Definition: CUTexturedNode.h:104
virtual void dispose() override
static const Vec2 ANCHOR_BOTTOM_LEFT
Definition: CUVec2.h:82
bool isFlipHorizontal() const
Definition: CUTexturedNode.h:499
virtual void setAnchor(const Vec2 &anchor) override
Definition: CUTexturedNode.h:593
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override=0
virtual bool init() override
Definition: CUTexturedNode.h:151
Vec2 _anchor
Definition: CUNode.h:109
virtual void shiftPolygon(float dx, float dy)
Definition: CURect.h:45
const Poly2 & getPolygon() const
Definition: CUTexturedNode.h:374
bool _rendered
Definition: CUTexturedNode.h:99
CU_DISALLOW_COPY_AND_ASSIGN(TexturedNode)
void setAbsolute(bool flag)
Definition: CUTexturedNode.h:565
void flipHorizontal(bool flag)
Definition: CUTexturedNode.h:492
Poly2 & set(const std::vector< Vec2 > &vertices)
virtual void generateRenderData()
bool initWithTexture(const std::shared_ptr< Texture > &texture, const std::vector< Vec2 > &vertices)
Definition: CUTexturedNode.h:275
Poly2 _polygon
Definition: CUTexturedNode.h:93
std::shared_ptr< Texture > _texture
Definition: CUTexturedNode.h:90
virtual void setPolygon(const std::vector< Vec2 > &vertices)=0
GLenum _dstFactor
Definition: CUTexturedNode.h:108
Definition: CUColor4.h:1104
bool isAbsolute() const
Definition: CUTexturedNode.h:548
Definition: CUAnimationNode.h:52
Definition: CUMat4.h:92
std::shared_ptr< Texture > & getTexture()
Definition: CUTexturedNode.h:335
virtual ~TexturedNode()
Definition: CUTexturedNode.h:129
void setBlendEquation(GLenum equation)
bool initWithFile(const std::string &filename)
bool _absolute
Definition: CUTexturedNode.h:96