CUGL 1.1
Cornell University Game Library
CUAnimationNode.h
1 //
2 // CUAnimationNode.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a straight-forward filmstrip API, similar to what we
6 // use in the intro class. Note that this class extends PolygonNode, as it
7 // simply treats the node texture as a sprite sheet. Therefore, it is possible
8 // to animate the filmstrip over polygons. However, this can have undesirable
9 // effects if the polygon coordinates extend beyond a single animation frame.
10 //
11 // This class uses our standard shared-pointer architecture.
12 //
13 // 1. The constructor does not perform any initialization; it just sets all
14 // attributes to their defaults.
15 //
16 // 2. All initialization takes place via init methods, which can fail if an
17 // object is initialized more than once.
18 //
19 // 3. All allocation takes place via static constructors which return a shared
20 // pointer.
21 //
22 // CUGL zlib License:
23 // This software is provided 'as-is', without any express or implied
24 // warranty. In no event will the authors be held liable for any damages
25 // arising from the use of this software.
26 //
27 // Permission is granted to anyone to use this software for any purpose,
28 // including commercial applications, and to alter it and redistribute it
29 // freely, subject to the following restrictions:
30 //
31 // 1. The origin of this software must not be misrepresented; you must not
32 // claim that you wrote the original software. If you use this software
33 // in a product, an acknowledgment in the product documentation would be
34 // appreciated but is not required.
35 //
36 // 2. Altered source versions must be plainly marked as such, and must not
37 // be misrepresented as being the original software.
38 //
39 // 3. This notice may not be removed or altered from any source distribution.
40 //
41 // Author: Walker White
42 // Version: 12/1/16
43 //
44 #ifndef __CU_ANIMATION_NODE_H__
45 #define __CU_ANIMATION_NODE_H__
46 
47 #include <cugl/2d/CUPolygonNode.h>
48 #include <cugl/math/CURect.h>
49 #include <cugl/renderer/CUTexture.h>
50 
51 
52 namespace cugl {
53 
54 #pragma mark -
55 #pragma mark AnimationNode
56 
77 class AnimationNode : public PolygonNode {
78 protected:
80  int _cols;
82  int _size;
84  int _frame;
87 
88 #pragma mark -
89 #pragma mark Constructors
90 public:
99  AnimationNode();
100 
109 
126  bool initWithFilmstrip(const std::shared_ptr<Texture>& texture, int rows, int cols) {
127  return initWithFilmstrip(texture,rows,cols,rows*cols);
128  }
129 
148  bool initWithFilmstrip(const std::shared_ptr<Texture>& texture, int rows, int cols, int size);
149 
176  virtual bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue> data) override;
177 
178 
179 #pragma mark -
180 #pragma mark Static Constructors
181 
197  static std::shared_ptr<AnimationNode> alloc(const std::shared_ptr<Texture>& texture,
198  int rows, int cols) {
199  std::shared_ptr<AnimationNode> node = std::make_shared<AnimationNode>();
200  return (node->initWithFilmstrip(texture,rows,cols) ? node : nullptr);
201 
202  }
203 
222  static std::shared_ptr<AnimationNode> alloc(const std::shared_ptr<Texture>& texture,
223  int rows, int cols, int size) {
224  std::shared_ptr<AnimationNode> node = std::make_shared<AnimationNode>();
225  return (node->initWithFilmstrip(texture,rows,cols,size) ? node : nullptr);
226  }
227 
254  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
255  const std::shared_ptr<JsonValue> data) {
256  std::shared_ptr<AnimationNode> result = std::make_shared<AnimationNode>();
257  if (!result->initWithData(loader,data)) { result = nullptr; }
258  return std::dynamic_pointer_cast<Node>(result);
259  }
260 
261 
262 #pragma mark -
263 #pragma mark Attribute Accessors
264 
269  int getSize() const { return _size; }
270 
276  unsigned int getFrame() const { return _frame; }
277 
285  void setFrame(int frame);
286 
287 
288 };
289 
290 }
291 
292 #endif /* __CU_ANIMATION_NODE_H__ */
unsigned int getFrame() const
Definition: CUAnimationNode.h:276
int _frame
Definition: CUAnimationNode.h:84
Definition: CUNode.h:92
int _cols
Definition: CUAnimationNode.h:80
bool initWithFilmstrip(const std::shared_ptr< Texture > &texture, int rows, int cols)
Definition: CUAnimationNode.h:126
virtual bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data) override
Definition: CUPolygonNode.h:79
int getSize() const
Definition: CUAnimationNode.h:269
virtual void dispose() override
Rect _bounds
Definition: CUAnimationNode.h:86
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > data)
Definition: CUAnimationNode.h:254
Definition: CURect.h:45
Definition: CUSceneLoader.h:77
static std::shared_ptr< AnimationNode > alloc(const std::shared_ptr< Texture > &texture, int rows, int cols)
Definition: CUAnimationNode.h:197
static std::shared_ptr< AnimationNode > alloc(const std::shared_ptr< Texture > &texture, int rows, int cols, int size)
Definition: CUAnimationNode.h:222
void setFrame(int frame)
Definition: CUAction.h:51
int _size
Definition: CUAnimationNode.h:82
Definition: CUAnimationNode.h:77
~AnimationNode()
Definition: CUAnimationNode.h:108