CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 MIT 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 
171  virtual bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue>& data) override;
172 
173 
174 #pragma mark -
175 #pragma mark Static Constructors
176 
192  static std::shared_ptr<AnimationNode> alloc(const std::shared_ptr<Texture>& texture,
193  int rows, int cols) {
194  std::shared_ptr<AnimationNode> node = std::make_shared<AnimationNode>();
195  return (node->initWithFilmstrip(texture,rows,cols) ? node : nullptr);
196 
197  }
198 
217  static std::shared_ptr<AnimationNode> alloc(const std::shared_ptr<Texture>& texture,
218  int rows, int cols, int size) {
219  std::shared_ptr<AnimationNode> node = std::make_shared<AnimationNode>();
220  return (node->initWithFilmstrip(texture,rows,cols,size) ? node : nullptr);
221  }
222 
244  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
245  const std::shared_ptr<JsonValue>& data) {
246  std::shared_ptr<AnimationNode> result = std::make_shared<AnimationNode>();
247  if (!result->initWithData(loader,data)) { result = nullptr; }
248  return std::dynamic_pointer_cast<Node>(result);
249  }
250 
251 
252 #pragma mark -
253 #pragma mark Attribute Accessors
254 
259  int getSize() const { return _size; }
260 
266  unsigned int getFrame() const { return _frame; }
267 
275  void setFrame(int frame);
276 
277 
278 };
279 
280 }
281 
282 #endif /* __CU_ANIMATION_NODE_H__ */
unsigned int getFrame() const
Definition: CUAnimationNode.h:266
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:259
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:244
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:192
static std::shared_ptr< AnimationNode > alloc(const std::shared_ptr< Texture > &texture, int rows, int cols, int size)
Definition: CUAnimationNode.h:217
void setFrame(int frame)
Definition: CUAction.h:51
int _size
Definition: CUAnimationNode.h:82
Definition: CUAnimationNode.h:77
~AnimationNode()
Definition: CUAnimationNode.h:108