CUGL 1.3
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 MIT 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 
326  virtual bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue>& data) override;
327 
328 #pragma mark -
329 #pragma mark Attributes
330 
339  void setTexture(const std::string &filename) {
340  std::shared_ptr<Texture> texture = Texture::allocWithFile(filename);
341  setTexture(texture);
342  }
343 
353  void setTexture(const std::shared_ptr<Texture>& texture);
354 
360  std::shared_ptr<Texture>& getTexture() { return _texture; }
361 
367  const std::shared_ptr<Texture>& getTexture() const { return _texture; }
368 
376  virtual void setPolygon(const std::vector<Vec2>& vertices) = 0;
377 
383  virtual void setPolygon(const Poly2& poly);
384 
392  virtual void setPolygon(const Rect& rect) = 0;
393 
399  const Poly2& getPolygon() const { return _polygon; }
400 
415  virtual void shiftPolygon(float dx, float dy);
416 
427  const Rect& getBoundingRect() const { return _polygon.getBounds(); }
428 
447  void setBlendFunc(GLenum srcFactor, GLenum dstFactor) { _srcFactor = srcFactor; _dstFactor = dstFactor; }
448 
461  GLenum getSourceBlendFactor() const { return _srcFactor; }
462 
475  GLenum getDestinationBlendFactor() const { return _srcFactor; }
476 
492  void setBlendEquation(GLenum equation) { _blendEquation = equation; }
493 
506  GLenum getBlendEquation() const { return _blendEquation; }
507 
517  void flipHorizontal(bool flag) { _flipHorizontal = flag; updateTextureCoords(); }
518 
524  bool isFlipHorizontal() const { return _flipHorizontal; }
525 
535  void flipVertical(bool flag) { _flipVertical = flag; updateTextureCoords(); }
536 
542  bool isFlipVertical() const { return _flipVertical; }
543 
554  virtual std::string toString(bool verbose=false) const override;
555 
556 
557 #pragma mark -
558 #pragma mark Scaling Attributes
559 
574  bool isAbsolute() const { return _absolute; }
575 
591  void setAbsolute(bool flag) {
592  _absolute = flag;
594  }
595 
619  virtual void setAnchor(const Vec2& anchor) override {
620  if (!_absolute) { Node::setAnchor(anchor); }
621  }
622 
635  virtual void setContentSize(const Size& size) override;
636 
650  virtual void setContentSize(float width, float height) override {
651  setContentSize(Size(width, height));
652  }
653 
654 
655 #pragma mark -
656 #pragma mark Rendering
657 
680  virtual void draw(const std::shared_ptr<SpriteBatch>& batch,
681  const Mat4& transform, Color4 tint) override = 0;
682 
687 
688 #pragma mark -
689 #pragma mark Internal Helpers
690 protected:
694  virtual void generateRenderData();
695 
699  void clearRenderData();
700 
708  void updateTextureCoords();
709 
712 
713 };
714 
715 }
716 
717 #endif /* __CU_TEXTURED_NODE_H__ */
cugl::TexturedNode::_texture
std::shared_ptr< Texture > _texture
Definition: CUTexturedNode.h:91
cugl::TexturedNode::initWithFile
bool initWithFile(const std::string &filename, const std::vector< Vec2 > &vertices)
Definition: CUTexturedNode.h:229
cugl::TexturedNode::setBlendEquation
void setBlendEquation(GLenum equation)
Definition: CUTexturedNode.h:492
cugl::TexturedNode::isFlipVertical
bool isFlipVertical() const
Definition: CUTexturedNode.h:542
cugl::TexturedNode::getPolygon
const Poly2 & getPolygon() const
Definition: CUTexturedNode.h:399
cugl::TexturedNode::clearRenderData
void clearRenderData()
cugl::TexturedNode::_rendered
bool _rendered
Definition: CUTexturedNode.h:102
cugl::Poly2::getBounds
const Rect & getBounds() const
Definition: CUPoly2.h:867
cugl::TexturedNode::init
virtual bool init() override
Definition: CUTexturedNode.h:154
cugl::Poly2::set
Poly2 & set(const std::vector< Vec2 > &vertices)
cugl::TexturedNode::toString
virtual std::string toString(bool verbose=false) const override
cugl::TexturedNode::shiftPolygon
virtual void shiftPolygon(float dx, float dy)
cugl::Node::setAnchor
virtual void setAnchor(const Vec2 &anchor)
cugl::TexturedNode::setContentSize
virtual void setContentSize(const Size &size) override
cugl::TexturedNode::init
bool init(const std::vector< Vec2 > &vertices)
Definition: CUTexturedNode.h:171
cugl::TexturedNode::getBoundingRect
const Rect & getBoundingRect() const
Definition: CUTexturedNode.h:427
cugl::TexturedNode::generateRenderData
virtual void generateRenderData()
cugl::TexturedNode::setTexture
void setTexture(const std::string &filename)
Definition: CUTexturedNode.h:339
cugl::TexturedNode::isAbsolute
bool isAbsolute() const
Definition: CUTexturedNode.h:574
cugl::TexturedNode::setAnchor
virtual void setAnchor(const Vec2 &anchor) override
Definition: CUTexturedNode.h:619
cugl::TexturedNode::_absolute
bool _absolute
Definition: CUTexturedNode.h:97
cugl::Size
Definition: CUSize.h:57
cugl::Color4
Definition: CUColor4.h:1084
cugl::TexturedNode::initWithTexture
bool initWithTexture(const std::shared_ptr< Texture > &texture, const std::vector< Vec2 > &vertices)
Definition: CUTexturedNode.h:278
cugl::TexturedNode::_flipHorizontal
bool _flipHorizontal
Definition: CUTexturedNode.h:114
cugl::TexturedNode::flipHorizontal
void flipHorizontal(bool flag)
Definition: CUTexturedNode.h:517
cugl::TexturedNode::setPolygon
virtual void setPolygon(const std::vector< Vec2 > &vertices)=0
cugl::TexturedNode::isFlipHorizontal
bool isFlipHorizontal() const
Definition: CUTexturedNode.h:524
cugl::TexturedNode::getTexture
const std::shared_ptr< Texture > & getTexture() const
Definition: CUTexturedNode.h:367
cugl::TexturedNode::dispose
virtual void dispose() override
cugl::Vec2::ANCHOR_BOTTOM_LEFT
static const Vec2 ANCHOR_BOTTOM_LEFT
Definition: CUVec2.h:82
cugl::TexturedNode::getTexture
std::shared_ptr< Texture > & getTexture()
Definition: CUTexturedNode.h:360
cugl::TexturedNode::_classname
std::string _classname
Definition: CUTexturedNode.h:88
cugl::Rect
Definition: CURect.h:45
cugl::Mat4
Definition: CUMat4.h:83
cugl::TexturedNode::initWithData
virtual bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) override
cugl::TexturedNode::_flipVertical
bool _flipVertical
Definition: CUTexturedNode.h:116
cugl::TexturedNode::refresh
void refresh()
Definition: CUTexturedNode.h:686
cugl::TexturedNode::updateTextureCoords
void updateTextureCoords()
cugl::TexturedNode::_dstFactor
GLenum _dstFactor
Definition: CUTexturedNode.h:111
cugl::TexturedNode::setAbsolute
void setAbsolute(bool flag)
Definition: CUTexturedNode.h:591
cugl::TexturedNode::flipVertical
void flipVertical(bool flag)
Definition: CUTexturedNode.h:535
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::TexturedNode::_polygon
Poly2 _polygon
Definition: CUTexturedNode.h:94
cugl::TexturedNode::~TexturedNode
~TexturedNode()
Definition: CUTexturedNode.h:132
cugl::TexturedNode::setBlendFunc
void setBlendFunc(GLenum srcFactor, GLenum dstFactor)
Definition: CUTexturedNode.h:447
cugl::Node
Definition: CUNode.h:92
cugl::Poly2
Definition: CUPoly2.h:109
cugl::TexturedNode::initWithFile
bool initWithFile(const std::string &filename)
cugl::Vec2
Definition: CUVec2.h:61
cugl::TexturedNode::_stretch
bool _stretch
Definition: CUTexturedNode.h:99
cugl::TexturedNode::init
bool init(const Poly2 &poly)
Definition: CUTexturedNode.h:186
cugl::TexturedNode::setContentSize
virtual void setContentSize(float width, float height) override
Definition: CUTexturedNode.h:650
cugl::TexturedNode::_blendEquation
GLenum _blendEquation
Definition: CUTexturedNode.h:107
cugl::TexturedNode::getDestinationBlendFactor
GLenum getDestinationBlendFactor() const
Definition: CUTexturedNode.h:475
cugl::TexturedNode::_srcFactor
GLenum _srcFactor
Definition: CUTexturedNode.h:109
cugl::TexturedNode::draw
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override=0
cugl::Texture::allocWithFile
static std::shared_ptr< Texture > allocWithFile(const std::string &filename)
Definition: CUTexture.h:269
cugl::TexturedNode::initWithTexture
bool initWithTexture(const std::shared_ptr< Texture > &texture)
cugl::TexturedNode
Definition: CUTexturedNode.h:84
cugl::TexturedNode::init
bool init(const Rect &rect)
Definition: CUTexturedNode.h:203
cugl::TexturedNode::CU_DISALLOW_COPY_AND_ASSIGN
CU_DISALLOW_COPY_AND_ASSIGN(TexturedNode)
cugl::TexturedNode::_vertices
std::vector< Vertex2 > _vertices
Definition: CUTexturedNode.h:104
cugl::Rect::ZERO
static const Rect ZERO
Definition: CURect.h:54
cugl::TexturedNode::TexturedNode
TexturedNode()
cugl::TexturedNode::getSourceBlendFactor
GLenum getSourceBlendFactor() const
Definition: CUTexturedNode.h:461
cugl::Node::_anchor
Vec2 _anchor
Definition: CUNode.h:109
cugl::TexturedNode::getBlendEquation
GLenum getBlendEquation() const
Definition: CUTexturedNode.h:506