From d39f803459e5c993617367e2343c2efb6116fecf Mon Sep 17 00:00:00 2001 From: halx99 Date: Tue, 25 Aug 2020 13:49:45 +0800 Subject: [PATCH] Add utils::createInstance to instead macro CREATE_FUNC --- cocos/base/ccUtils.h | 42 +++++++++++++++++-- .../Classes/AppDelegate.cpp | 2 +- .../Classes/HelloWorldScene.cpp | 5 --- .../Classes/HelloWorldScene.h | 5 --- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/cocos/base/ccUtils.h b/cocos/base/ccUtils.h index a6c31136a4..708c8866a6 100644 --- a/cocos/base/ccUtils.h +++ b/cocos/base/ccUtils.h @@ -153,8 +153,8 @@ namespace utils * @return Returns found node or nullptr with specified type 'T' */ - template inline - T findChild(Node* levelRoot, const std::string& name) + template + inline T findChild(Node* levelRoot, const std::string& name) { return dynamic_cast(findChild(levelRoot, name)); } @@ -164,12 +164,46 @@ namespace utils * @return Returns found node or nullptr with specified type 'T' */ - template inline - T findChild(Node* levelRoot, int tag) + template + inline T findChild(Node* levelRoot, int tag) { return dynamic_cast(findChild(levelRoot, tag)); } + /** + * Create a Game Object instance, like CREATE_FUNC, but more powerful + + * @return Returns a autorelease game object + * @limition: the init function finit must be public + */ + template + inline T* createInstance(F&& finit, Ts&&... args) + { + T* pRet = new(std::nothrow) T(); + if (pRet && std::mem_fn(finit)(pRet, std::forward<_Types>(args)...)) { + pRet->autorelease(); + return pRet; + } + else + { + delete pRet; + pRet = nullptr; + return nullptr; + } + } + + /** + * Create a Game Object instance with 'bool T::init()' function, like CREATE_FUNC, but more powerful + + * @return Returns a autorelease game object + * @limition: the init function finit must be public + */ + template + inline T* createInstance() + { + return ::cocos2d::utils::createInstance(&T::init); + } + /** * Gets the md5 hash for the given file. * @param filename The file to calculate md5 hash. diff --git a/templates/cpp-template-default/Classes/AppDelegate.cpp b/templates/cpp-template-default/Classes/AppDelegate.cpp index 8f9a485057..00d169397c 100644 --- a/templates/cpp-template-default/Classes/AppDelegate.cpp +++ b/templates/cpp-template-default/Classes/AppDelegate.cpp @@ -108,7 +108,7 @@ bool AppDelegate::applicationDidFinishLaunching() { register_all_packages(); // create a scene. it's an autorelease object - auto scene = HelloWorld::createScene(); + auto scene = utils::createInstance(); // run director->runWithScene(scene); diff --git a/templates/cpp-template-default/Classes/HelloWorldScene.cpp b/templates/cpp-template-default/Classes/HelloWorldScene.cpp index 6da19024a3..0169bf96f6 100644 --- a/templates/cpp-template-default/Classes/HelloWorldScene.cpp +++ b/templates/cpp-template-default/Classes/HelloWorldScene.cpp @@ -26,11 +26,6 @@ USING_NS_CC; -Scene* HelloWorld::createScene() -{ - return HelloWorld::create(); -} - // Print useful error message instead of segfaulting when files are not there. static void problemLoading(const char* filename) { diff --git a/templates/cpp-template-default/Classes/HelloWorldScene.h b/templates/cpp-template-default/Classes/HelloWorldScene.h index 00170938aa..2a0c4b6e80 100644 --- a/templates/cpp-template-default/Classes/HelloWorldScene.h +++ b/templates/cpp-template-default/Classes/HelloWorldScene.h @@ -30,15 +30,10 @@ class HelloWorld : public cocos2d::Scene { public: - static cocos2d::Scene* createScene(); - virtual bool init(); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender); - - // implement the "static create()" method manually - CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__