CUGL
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 zlib 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 "CUTexturedNode.h"
47 #include "../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 
108  virtual ~PolygonNode() { }
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 
306 #pragma mark -
307 #pragma mark Attributes
308 
317  virtual void setPolygon(const std::vector<Vec2>& vertices) override;
318 
326  virtual void setPolygon(const Poly2& poly) override;
327 
336  virtual void setPolygon(const Rect& rect) override;
337 
338 #pragma mark -
339 #pragma mark Rendering
340 
363  virtual void draw(const std::shared_ptr<SpriteBatch>& batch, const Mat4& transform, Color4 tint) override;
364 
365 
366 #pragma mark -
367 #pragma mark Internal Helpers
368 private:
370  CU_DISALLOW_COPY_AND_ASSIGN(PolygonNode);
371 
372 };
373 
374 }
375 
376 #endif /* __CU_POLYGON_NODE_H__ */
static std::shared_ptr< PolygonNode > alloc(const Rect &rect)
Definition: CUPolygonNode.h:176
Definition: CUTexturedNode.h:83
static std::shared_ptr< PolygonNode > allocWithTexture(const std::shared_ptr< Texture > &texture, const Rect &rect)
Definition: CUPolygonNode.h:300
Definition: CUSimpleTriangulator.h:66
Definition: CUPoly2.h:115
static std::shared_ptr< PolygonNode > alloc()
Definition: CUPolygonNode.h:121
static std::shared_ptr< PolygonNode > allocWithTexture(const std::shared_ptr< Texture > &texture)
Definition: CUPolygonNode.h:253
static std::shared_ptr< PolygonNode > alloc(const Poly2 &poly)
Definition: CUPolygonNode.h:157
static std::shared_ptr< PolygonNode > allocWithFile(const std::string &filename, const std::vector< Vec2 > &vertices)
Definition: CUPolygonNode.h:208
std::string _classname
Definition: CUTexturedNode.h:87
PolygonNode()
Definition: CUPolygonNode.h:96
static std::shared_ptr< PolygonNode > allocWithFile(const std::string &filename)
Definition: CUPolygonNode.h:191
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
Definition: CUPolygonNode.h:79
static std::shared_ptr< PolygonNode > allocWithTexture(const std::shared_ptr< Texture > &texture, const std::vector< Vec2 > &vertices)
Definition: CUPolygonNode.h:270
static std::shared_ptr< PolygonNode > allocWithFile(const std::string &filename, const Rect &rect)
Definition: CUPolygonNode.h:238
static std::shared_ptr< PolygonNode > allocWithFile(const std::string &filename, const Poly2 &poly)
Definition: CUPolygonNode.h:222
Definition: CURect.h:45
static SimpleTriangulator _triangulator
Definition: CUPolygonNode.h:83
static std::shared_ptr< PolygonNode > alloc(const std::vector< Vec2 > &vertices)
Definition: CUPolygonNode.h:141
std::string _name
Definition: CUNode.h:187
Definition: CUColor4.h:1104
Definition: CUAnimationNode.h:52
Definition: CUMat4.h:92
virtual ~PolygonNode()
Definition: CUPolygonNode.h:108
virtual void setPolygon(const std::vector< Vec2 > &vertices) override
static std::shared_ptr< PolygonNode > allocWithTexture(const std::shared_ptr< Texture > &texture, const Poly2 &poly)
Definition: CUPolygonNode.h:283