CUGL 1.1
Cornell University Game Library
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 <cugl/math/CUPoly2.h>
39 #include <cugl/2d/CUNode.h>
40 #include <cugl/renderer/CUTexture.h>
41 #include <cugl/renderer/CUVertex.h>
42 
43 namespace cugl {
44 
84 class TexturedNode : public Node {
85 #pragma mark Values
86 protected:
88  std::string _classname;
89 
91  std::shared_ptr<Texture> _texture;
92 
95 
97  bool _absolute;
99  bool _stretch;
100 
102  bool _rendered;
104  std::vector<Vertex2> _vertices;
105 
109  GLenum _srcFactor;
111  GLenum _dstFactor;
112 
117 
118 #pragma mark -
119 #pragma mark Constructors
120 public:
127  TexturedNode();
128 
133 
143  virtual void dispose() override;
144 
154  virtual bool init() override {
155  return initWithTexture(nullptr, Rect::ZERO);
156  }
157 
171  bool init(const std::vector<Vec2>& vertices) {
172  return initWithTexture(nullptr, vertices);
173  }
174 
186  bool init(const Poly2& poly) {
187  return initWithTexture(nullptr, poly);
188  }
189 
203  bool init(const Rect& rect) {
204  return initWithTexture(nullptr, rect);
205  }
206 
217  bool initWithFile(const std::string& filename);
218 
229  bool initWithFile(const std::string& filename, const std::vector<Vec2>& vertices) {
230  _polygon.set(vertices);
231  return initWithFile(filename, _polygon);
232  }
233 
242  bool initWithFile(const std::string& filename, const Poly2& poly);
243 
254  bool initWithFile(const std::string& filename, const Rect& rect);
255 
266  bool initWithTexture(const std::shared_ptr<Texture>& texture);
267 
278  bool initWithTexture(const std::shared_ptr<Texture>& texture, const std::vector<Vec2>& vertices) {
279  setPolygon(vertices);
280  return initWithTexture(texture, _polygon);
281  }
282 
291  bool initWithTexture(const std::shared_ptr<Texture>& texture, const Poly2& poly);
292 
303  bool initWithTexture(const std::shared_ptr<Texture>& texture, const Rect& rect);
304 
331  virtual bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue> data) override;
332 
333 #pragma mark -
334 #pragma mark Attributes
335 
344  void setTexture(const std::string &filename) {
345  std::shared_ptr<Texture> texture = Texture::allocWithFile(filename);
346  setTexture(texture);
347  }
348 
358  void setTexture(const std::shared_ptr<Texture>& texture);
359 
365  std::shared_ptr<Texture>& getTexture() { return _texture; }
366 
372  const std::shared_ptr<Texture>& getTexture() const { return _texture; }
373 
381  virtual void setPolygon(const std::vector<Vec2>& vertices) = 0;
382 
388  virtual void setPolygon(const Poly2& poly);
389 
397  virtual void setPolygon(const Rect& rect) = 0;
398 
404  const Poly2& getPolygon() const { return _polygon; }
405 
420  virtual void shiftPolygon(float dx, float dy);
421 
432  const Rect& getBoundingRect() const { return _polygon.getBounds(); }
433 
452  void setBlendFunc(GLenum srcFactor, GLenum dstFactor) { _srcFactor = srcFactor; _dstFactor = dstFactor; }
453 
466  GLenum getSourceBlendFactor() const { return _srcFactor; }
467 
480  GLenum getDestinationBlendFactor() const { return _srcFactor; }
481 
497  void setBlendEquation(GLenum equation) { _blendEquation = equation; }
498 
511  GLenum getBlendEquation() const { return _blendEquation; }
512 
522  void flipHorizontal(bool flag) { _flipHorizontal = flag; updateTextureCoords(); }
523 
529  bool isFlipHorizontal() const { return _flipHorizontal; }
530 
540  void flipVertical(bool flag) { _flipVertical = flag; updateTextureCoords(); }
541 
547  bool isFlipVertical() const { return _flipVertical; }
548 
559  virtual std::string toString(bool verbose=false) const override;
560 
561 
562 #pragma mark -
563 #pragma mark Scaling Attributes
564 
579  bool isAbsolute() const { return _absolute; }
580 
596  void setAbsolute(bool flag) {
597  _absolute = flag;
599  }
600 
624  virtual void setAnchor(const Vec2& anchor) override {
625  if (!_absolute) { Node::setAnchor(anchor); }
626  }
627 
640  virtual void setContentSize(const Size& size) override;
641 
655  virtual void setContentSize(float width, float height) override {
656  setContentSize(Size(width, height));
657  }
658 
659 
660 #pragma mark -
661 #pragma mark Rendering
662 
685  virtual void draw(const std::shared_ptr<SpriteBatch>& batch,
686  const Mat4& transform, Color4 tint) override = 0;
687 
692 
693 #pragma mark -
694 #pragma mark Internal Helpers
695 protected:
699  virtual void generateRenderData();
700 
704  void clearRenderData();
705 
713  void updateTextureCoords();
714 
717 
718 };
719 
720 }
721 
722 #endif /* __CU_TEXTURED_NODE_H__ */
Definition: CUSize.h:57
GLenum getDestinationBlendFactor() const
Definition: CUTexturedNode.h:480
virtual void setContentSize(float width, float height) override
Definition: CUTexturedNode.h:655
bool init(const Poly2 &poly)
Definition: CUTexturedNode.h:186
bool initWithTexture(const std::shared_ptr< Texture > &texture)
Definition: CUTexturedNode.h:84
static const Rect ZERO
Definition: CURect.h:54
bool _stretch
Definition: CUTexturedNode.h:99
virtual bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data) override
bool isFlipVertical() const
Definition: CUTexturedNode.h:547
GLenum _srcFactor
Definition: CUTexturedNode.h:109
void setBlendFunc(GLenum srcFactor, GLenum dstFactor)
Definition: CUTexturedNode.h:452
Definition: CUPoly2.h:115
bool init(const Rect &rect)
Definition: CUTexturedNode.h:203
void flipVertical(bool flag)
Definition: CUTexturedNode.h:540
Definition: CUVec2.h:61
const std::shared_ptr< Texture > & getTexture() const
Definition: CUTexturedNode.h:372
bool initWithFile(const std::string &filename, const std::vector< Vec2 > &vertices)
Definition: CUTexturedNode.h:229
std::vector< Vertex2 > _vertices
Definition: CUTexturedNode.h:104
std::string _classname
Definition: CUTexturedNode.h:88
GLenum getBlendEquation() const
Definition: CUTexturedNode.h:511
const Rect & getBounds() const
Definition: CUPoly2.h:873
Definition: CUNode.h:92
virtual std::string toString(bool verbose=false) const override
void updateTextureCoords()
void setTexture(const std::string &filename)
Definition: CUTexturedNode.h:344
bool _flipVertical
Definition: CUTexturedNode.h:116
const Rect & getBoundingRect() const
Definition: CUTexturedNode.h:432
bool init(const std::vector< Vec2 > &vertices)
Definition: CUTexturedNode.h:171
GLenum getSourceBlendFactor() const
Definition: CUTexturedNode.h:466
virtual void setContentSize(const Size &size) override
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:114
GLenum _blendEquation
Definition: CUTexturedNode.h:107
virtual void dispose() override
static const Vec2 ANCHOR_BOTTOM_LEFT
Definition: CUVec2.h:82
bool isFlipHorizontal() const
Definition: CUTexturedNode.h:529
virtual void setAnchor(const Vec2 &anchor) override
Definition: CUTexturedNode.h:624
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override=0
virtual bool init() override
Definition: CUTexturedNode.h:154
Vec2 _anchor
Definition: CUNode.h:109
virtual void shiftPolygon(float dx, float dy)
Definition: CURect.h:45
const Poly2 & getPolygon() const
Definition: CUTexturedNode.h:404
bool _rendered
Definition: CUTexturedNode.h:102
Definition: CUSceneLoader.h:77
CU_DISALLOW_COPY_AND_ASSIGN(TexturedNode)
void setAbsolute(bool flag)
Definition: CUTexturedNode.h:596
void flipHorizontal(bool flag)
Definition: CUTexturedNode.h:522
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:278
Poly2 _polygon
Definition: CUTexturedNode.h:94
std::shared_ptr< Texture > _texture
Definition: CUTexturedNode.h:91
virtual void setPolygon(const std::vector< Vec2 > &vertices)=0
GLenum _dstFactor
Definition: CUTexturedNode.h:111
Definition: CUColor4.h:1084
~TexturedNode()
Definition: CUTexturedNode.h:132
bool isAbsolute() const
Definition: CUTexturedNode.h:579
Definition: CUAction.h:51
Definition: CUMat4.h:92
std::shared_ptr< Texture > & getTexture()
Definition: CUTexturedNode.h:365
void setBlendEquation(GLenum equation)
Definition: CUTexturedNode.h:497
bool initWithFile(const std::string &filename)
void refresh()
Definition: CUTexturedNode.h:691
bool _absolute
Definition: CUTexturedNode.h:97