Cornell Cocos
Cornell Extensions to Cocos2d
CURootLayer.h
1 //
2 // CURootLayer.h
3 // Cornell Extensions to Cocos2D
4 //
5 // A module for the top layer for a Cocos2d Scene. This class automates a lot of
6 // of the start-up requirements for working in Cocos2d. To create a Cocos2d
7 // game, you just need to subclass the class and implement three methods:
8 // start, stop, and update.
9 //
10 // This class is based on the Cocos2d class LayerColor, which is used by their
11 // HelloWorld application. However, it's rendering is much more efficient, as
12 // it does not inject a custom drawing command into the pipeline.
13 //
14 // Author: Walker White
15 // Version: 12/5/15
16 //
17 #ifndef __CU_ROOT_LAYER_H__
18 #define __CU_ROOT_LAYER_H__
19 
20 #include <cocos2d.h>
21 #include <renderer/CCTrianglesCommand.h>
22 
23 NS_CC_BEGIN
24 
25 #pragma mark -
26 #pragma mark Static Constructor Template
27 
28 
29 namespace GameRoot {
42  template<class T>
43  Scene* createScene() {
44  T* layer = new (std::nothrow)T();
45  if (layer && layer->init()) {
46  layer->autorelease();
47 
48  // 'scene' is an autorelease object
49  auto scene = Scene::create();
50  // add layer as a child to scene
51  scene->addChild(layer);
52  layer->start();
53  // return the scene
54  return scene;
55  }
56  CC_SAFE_DELETE(layer);
57  return nullptr;
58  }
59 
74  template<class T>
75  Scene* createScene(const Size& size) {
76  T* layer = new (std::nothrow)T();
77  if (layer && layer->init(size)) {
78  layer->autorelease();
79 
80  // 'scene' is an autorelease object
81  auto scene = Scene::create();
82  // add layer as a child to scene
83  scene->addChild(layer);
84  layer->start();
85  // return the scene
86  return scene;
87  }
88  CC_SAFE_DELETE(layer);
89  return nullptr;
90  }
91 }
92 
93 #pragma mark -
94 #pragma mark Root Layer
95 
98 class CC_DLL RootLayer: public Layer, public BlendProtocol {
99 
100 #pragma mark Internal Helpers
101 private:
103  CC_DISALLOW_COPY_AND_ASSIGN(RootLayer);
104 
105 protected:
107  Texture2D* _texture;
109  BlendFunc _blendFunc;
110 
112  TrianglesCommand _command;
114  TrianglesCommand::Triangles _triangles;
115 
117  bool _active;
118 
122  void clearRenderData();
123 
127  void updateColor(void) override;
128 
132  void updateBlendFunc(void);
133 
134 
135 public:
136 #pragma mark Attribute Accessors
137 
145  virtual void setContentSize(const Size & size) override;
146 
154  void setBlendFunc(const BlendFunc &blendFunc) override { _blendFunc = blendFunc; }
155 
163  const BlendFunc& getBlendFunc() const override { return _blendFunc; }
164 
176  virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags) override;
177 
181  virtual void cleanup() override;
182 
183 
184 #pragma mark Primary Designer Methods
185 
192  bool isActive() const { return _active; }
193 
200  virtual void start() {
201  _active = true;
202  scheduleUpdate();
203  }
204 
212  virtual void stop() {
213  unscheduleUpdate();
214  _active = false;
215  }
216 
226  virtual void update(float dt) override {
227  CCASSERT(_active, "Update called on inactive root layer");
228  Node::update(dt);
229  }
230 
236  void shutdown();
237 
238 
239 CC_CONSTRUCTOR_ACCESS:
240 #pragma mark Initializers
241 
247  RootLayer(void);
248 
252  virtual ~RootLayer(void);
253 
261  virtual bool init() override;
262 
272  virtual bool initWithSize(const Size& size);
273 
282  virtual bool initWithColor(const Color4B& color, const Size& size);
283 
296  template<class T>
297  friend Scene* GameRoot::createScene();
298 
313  template<class T>
314  friend Scene* GameRoot::createScene(const Size& size);
315 };
316 
317 NS_CC_END
318 
319 #endif /* defined(__CU_ROOT_LAYER_H__) */
Definition: CURootLayer.h:98
Texture2D * _texture
Definition: CURootLayer.h:107
bool isActive() const
Definition: CURootLayer.h:192
virtual void start()
Definition: CURootLayer.h:200
const BlendFunc & getBlendFunc() const override
Definition: CURootLayer.h:163
virtual void stop()
Definition: CURootLayer.h:212
BlendFunc _blendFunc
Definition: CURootLayer.h:109
virtual void update(float dt) override
Definition: CURootLayer.h:226
TrianglesCommand _command
Definition: CURootLayer.h:112
bool _active
Definition: CURootLayer.h:117
Definition: CURootLayer.h:29
TrianglesCommand::Triangles _triangles
Definition: CURootLayer.h:114
void setBlendFunc(const BlendFunc &blendFunc) override
Definition: CURootLayer.h:154