CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUPolygonNode.h
1 //
2 // CUPolygonNode.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a scene graph node that supports basic sprite graphics.
6 // The sprites do not have to be rectangular. They may be any shape represented
7 // by Poly2.
8 //
9 // This class uses our standard shared-pointer architecture.
10 //
11 // 1. The constructor does not perform any initialization; it just sets all
12 // attributes to their defaults.
13 //
14 // 2. All initialization takes place via init methods, which can fail if an
15 // object is initialized more than once.
16 //
17 // 3. All allocation takes place via static constructors which return a shared
18 // pointer.
19 //
20 // CUGL MIT License:
21 // This software is provided 'as-is', without any express or implied
22 // warranty. In no event will the authors be held liable for any damages
23 // arising from the use of this software.
24 //
25 // Permission is granted to anyone to use this software for any purpose,
26 // including commercial applications, and to alter it and redistribute it
27 // freely, subject to the following restrictions:
28 //
29 // 1. The origin of this software must not be misrepresented; you must not
30 // claim that you wrote the original software. If you use this software
31 // in a product, an acknowledgment in the product documentation would be
32 // appreciated but is not required.
33 //
34 // 2. Altered source versions must be plainly marked as such, and must not
35 // be misrepresented as being the original software.
36 //
37 // 3. This notice may not be removed or altered from any source distribution.
38 //
39 // Author: Walker White
40 // Version: 6/27/16
41 
42 #ifndef __CU_POLYGON_NODE_H__
43 #define __CU_POLYGON_NODE_H__
44 
45 #include <string>
46 #include <cugl/2d/CUTexturedNode.h>
47 #include <cugl/math/polygon/CUSimpleTriangulator.h>
48 
49 namespace cugl {
50 
79 class PolygonNode : public TexturedNode {
80 #pragma mark Values
81 protected:
84 
85 public:
86 #pragma mark -
87 #pragma mark Constructor
88 
97  _classname = "PolygonNode";
98  _name = "PolygonNode";
99  }
100 
109 
110 #pragma mark -
111 #pragma mark Static Constructors
112 
121  static std::shared_ptr<PolygonNode> alloc() {
122  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
123  return (node->init() ? node : nullptr);
124  }
125 
141  static std::shared_ptr<PolygonNode> alloc(const std::vector<Vec2>& vertices) {
142  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
143  return (node->init(vertices) ? node : nullptr);
144  }
145 
157  static std::shared_ptr<PolygonNode> alloc(const Poly2& poly) {
158  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
159  return (node->init(poly) ? node : nullptr);
160  }
161 
176  static std::shared_ptr<PolygonNode> alloc(const Rect& rect) {
177  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
178  return (node->init(rect) ? node : nullptr);
179  }
180 
191  static std::shared_ptr<PolygonNode> allocWithFile(const std::string& filename) {
192  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
193  return (node->initWithFile(filename) ? node : nullptr);
194  }
195 
208  static std::shared_ptr<PolygonNode> allocWithFile(const std::string& filename,
209  const std::vector<Vec2>& vertices) {
210  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
211  return (node->initWithFile(filename,vertices) ? node : nullptr);
212  }
213 
222  static std::shared_ptr<PolygonNode> allocWithFile(const std::string& filename, const Poly2& poly) {
223  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
224  return (node->initWithFile(filename,poly) ? node : nullptr);
225  }
226 
238  static std::shared_ptr<PolygonNode> allocWithFile(const std::string& filename, const Rect& rect) {
239  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
240  return (node->initWithFile(filename,rect) ? node : nullptr);
241  }
242 
253  static std::shared_ptr<PolygonNode> allocWithTexture(const std::shared_ptr<Texture>& texture) {
254  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
255  return (node->initWithTexture(texture) ? node : nullptr);
256  }
257 
270  static std::shared_ptr<PolygonNode> allocWithTexture(const std::shared_ptr<Texture>& texture,
271  const std::vector<Vec2>& vertices) {
272  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
273  return (node->initWithTexture(texture,vertices) ? node : nullptr);
274  }
283  static std::shared_ptr<PolygonNode> allocWithTexture(const std::shared_ptr<Texture>& texture,
284  const Poly2& poly) {
285  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
286  return (node->initWithTexture(texture,poly) ? node : nullptr);
287  }
288 
300  static std::shared_ptr<PolygonNode> allocWithTexture(const std::shared_ptr<Texture>& texture,
301  const Rect& rect) {
302  std::shared_ptr<PolygonNode> node = std::make_shared<PolygonNode>();
303  return (node->initWithTexture(texture,rect) ? node : nullptr);
304  }
305 
327  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
328  const std::shared_ptr<JsonValue>& data) {
329  std::shared_ptr<PolygonNode> result = std::make_shared<PolygonNode>();
330  if (!result->initWithData(loader,data)) { result = nullptr; }
331  return std::dynamic_pointer_cast<Node>(result);
332  }
333 
334 #pragma mark -
335 #pragma mark Attributes
336 
345  virtual void setPolygon(const std::vector<Vec2>& vertices) override;
346 
354  virtual void setPolygon(const Poly2& poly) override;
355 
366  virtual void setPolygon(const Rect& rect) override;
367 
368 #pragma mark -
369 #pragma mark Rendering
370 
393  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
394 
395 
396 #pragma mark -
397 #pragma mark Internal Helpers
398 private:
400  CU_DISALLOW_COPY_AND_ASSIGN(PolygonNode);
401 
402 };
403 
404 }
405 
406 #endif /* __CU_POLYGON_NODE_H__ */
cugl::Node::_name
std::string _name
Definition: CUNode.h:189
cugl::PolygonNode::allocWithTexture
static std::shared_ptr< PolygonNode > allocWithTexture(const std::shared_ptr< Texture > &texture)
Definition: CUPolygonNode.h:253
cugl::PolygonNode::PolygonNode
PolygonNode()
Definition: CUPolygonNode.h:96
cugl::PolygonNode::allocWithTexture
static std::shared_ptr< PolygonNode > allocWithTexture(const std::shared_ptr< Texture > &texture, const Poly2 &poly)
Definition: CUPolygonNode.h:283
cugl::PolygonNode::allocWithData
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data)
Definition: CUPolygonNode.h:327
cugl::PolygonNode::allocWithFile
static std::shared_ptr< PolygonNode > allocWithFile(const std::string &filename, const std::vector< Vec2 > &vertices)
Definition: CUPolygonNode.h:208
cugl::PolygonNode::alloc
static std::shared_ptr< PolygonNode > alloc(const std::vector< Vec2 > &vertices)
Definition: CUPolygonNode.h:141
cugl::PolygonNode::~PolygonNode
~PolygonNode()
Definition: CUPolygonNode.h:108
cugl::PolygonNode
Definition: CUPolygonNode.h:79
cugl::Color4
Definition: CUColor4.h:1084
cugl::PolygonNode::setPolygon
virtual void setPolygon(const std::vector< Vec2 > &vertices) override
cugl::PolygonNode::alloc
static std::shared_ptr< PolygonNode > alloc(const Poly2 &poly)
Definition: CUPolygonNode.h:157
cugl::TexturedNode::dispose
virtual void dispose() override
cugl::TexturedNode::_classname
std::string _classname
Definition: CUTexturedNode.h:88
cugl::Rect
Definition: CURect.h:45
cugl::Mat4
Definition: CUMat4.h:83
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::PolygonNode::allocWithFile
static std::shared_ptr< PolygonNode > allocWithFile(const std::string &filename, const Rect &rect)
Definition: CUPolygonNode.h:238
cugl::Poly2
Definition: CUPoly2.h:109
cugl::PolygonNode::draw
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
cugl::PolygonNode::_triangulator
static SimpleTriangulator _triangulator
Definition: CUPolygonNode.h:83
cugl::PolygonNode::allocWithFile
static std::shared_ptr< PolygonNode > allocWithFile(const std::string &filename)
Definition: CUPolygonNode.h:191
cugl::PolygonNode::allocWithTexture
static std::shared_ptr< PolygonNode > allocWithTexture(const std::shared_ptr< Texture > &texture, const Rect &rect)
Definition: CUPolygonNode.h:300
cugl::PolygonNode::allocWithTexture
static std::shared_ptr< PolygonNode > allocWithTexture(const std::shared_ptr< Texture > &texture, const std::vector< Vec2 > &vertices)
Definition: CUPolygonNode.h:270
cugl::PolygonNode::allocWithFile
static std::shared_ptr< PolygonNode > allocWithFile(const std::string &filename, const Poly2 &poly)
Definition: CUPolygonNode.h:222
cugl::SimpleTriangulator
Definition: CUSimpleTriangulator.h:66
cugl::PolygonNode::alloc
static std::shared_ptr< PolygonNode > alloc()
Definition: CUPolygonNode.h:121
cugl::TexturedNode
Definition: CUTexturedNode.h:84
cugl::PolygonNode::alloc
static std::shared_ptr< PolygonNode > alloc(const Rect &rect)
Definition: CUPolygonNode.h:176