Skip to content

Commit 862153a

Browse files
author
jianglong0156
committed
issue cocos2d#1108: first step, the function of cc.game.restart
1 parent 7618126 commit 862153a

File tree

4 files changed

+80
-66
lines changed

4 files changed

+80
-66
lines changed

frameworks/js-bindings/bindings/manual/ScriptingCore.cpp

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@
5252
#include <vector>
5353
#include <map>
5454

55+
#include "jsb_cocos2dx_auto.hpp"
56+
#include "jsb_cocos2dx_extension_auto.hpp"
57+
#include "jsb_cocos2dx_builder_auto.hpp"
58+
#include "extension/jsb_cocos2dx_extension_manual.h"
59+
#include "cocos2d_specifics.hpp"
60+
#include "cocosbuilder/js_bindings_ccbreader.h"
61+
#include "localstorage/js_bindings_system_registration.h"
62+
#include "chipmunk/js_bindings_chipmunk_registration.h"
63+
#include "jsb_opengl_registration.h"
64+
#include "jsb_cocos2dx_ui_auto.hpp"
65+
#include "ui/jsb_cocos2dx_ui_manual.h"
66+
#include "cocostudio/jsb_cocos2dx_studio_manual.h"
67+
#include "jsb_cocos2dx_studio_auto.hpp"
68+
5569
#ifdef ANDROID
5670
#include <android/log.h>
5771
#include <jni/JniHelper.h>
@@ -372,14 +386,6 @@ bool JSB_cleanScript(JSContext *cx, uint32_t argc, jsval *vp)
372386
return true;
373387
};
374388

375-
bool JSB_restartGame(JSContext *cx, uint32_t argc, jsval *vp)
376-
{
377-
JS_SET_RVAL(cx, vp, JSVAL_VOID);
378-
JSB_PRECONDITION2(argc==0, cx, false, "Invalid number of arguments in executeScript");
379-
ScriptingCore::getInstance()->reset();
380-
return true;
381-
};
382-
383389
bool JSB_core_restartVM(JSContext *cx, uint32_t argc, jsval *vp)
384390
{
385391
JSB_PRECONDITION2(argc==0, cx, false, "Invalid number of arguments in executeScript");
@@ -426,7 +432,6 @@ void registerDefaultClasses(JSContext* cx, JSObject* global) {
426432
JS_DefineFunction(cx, global, "__getVersion", JSBCore_version, 0, JSPROP_READONLY | JSPROP_PERMANENT);
427433
JS_DefineFunction(cx, global, "__restartVM", JSB_core_restartVM, 0, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE );
428434
JS_DefineFunction(cx, global, "__cleanScript", JSB_cleanScript, 1, JSPROP_READONLY | JSPROP_PERMANENT);
429-
JS_DefineFunction(cx, global, "__restartGame", JSB_restartGame, 0, JSPROP_READONLY | JSPROP_PERMANENT);
430435
}
431436

432437
static void sc_finalize(JSFreeOp *freeOp, JSObject *obj) {
@@ -732,23 +737,19 @@ bool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* cx)
732737

733738
void ScriptingCore::reset()
734739
{
735-
auto director = Director::getInstance();
736-
FontFNT::purgeCachedData();
737-
if (director->getOpenGLView())
738-
{
739-
SpriteFrameCache::getInstance()->removeSpriteFrames();
740-
director->getTextureCache()->removeAllTextures();
741-
}
742-
FileUtils::getInstance()->purgeCachedEntries();
743-
director->getScheduler()->unscheduleAll();
744-
745-
cleanup();
746-
747-
this->addRegisterCallback(registerDefaultClasses);
748-
this->_runLoop = new SimpleRunLoop();
749-
750-
Application::getInstance()->run();
751-
//start();
740+
Director::getInstance()->end();
741+
}
742+
743+
void ScriptingCore::rebootVm()
744+
{
745+
Director::getInstance()->restartDirector();
746+
// release the objects
747+
PoolManager::getInstance()->getCurrentPool()->clear();
748+
cleanup();
749+
this->addRegisterCallback(registerDefaultClasses);
750+
this->_runLoop = new SimpleRunLoop();
751+
752+
runGame();
752753
}
753754

754755
ScriptingCore::~ScriptingCore()
@@ -1388,6 +1389,13 @@ int ScriptingCore::sendEvent(ScriptEvent* evt)
13881389
if (NULL == evt)
13891390
return 0;
13901391

1392+
// special type, can't use this code after JSAutoCompartment
1393+
if (evt->type == kRestartGame)
1394+
{
1395+
rebootVm();
1396+
return 0;
1397+
}
1398+
13911399
JSAutoCompartment ac(_cx, _global);
13921400

13931401
switch (evt->type)
@@ -1705,6 +1713,41 @@ void ScriptingCore::enableDebugger(unsigned int port)
17051713
}
17061714
}
17071715

1716+
void ScriptingCore::register_all()
1717+
{
1718+
addRegisterCallback(register_all_cocos2dx);
1719+
addRegisterCallback(register_all_cocos2dx_extension);
1720+
addRegisterCallback(register_cocos2dx_js_extensions);
1721+
addRegisterCallback(jsb_register_chipmunk);
1722+
addRegisterCallback(register_all_cocos2dx_extension_manual);
1723+
addRegisterCallback(register_all_cocos2dx_builder);
1724+
addRegisterCallback(register_CCBuilderReader);
1725+
addRegisterCallback(jsb_register_system);
1726+
addRegisterCallback(JSB_register_opengl);
1727+
addRegisterCallback(register_all_cocos2dx_ui);
1728+
addRegisterCallback(register_all_cocos2dx_ui_manual);
1729+
addRegisterCallback(register_all_cocos2dx_studio);
1730+
addRegisterCallback(register_all_cocos2dx_studio_manual);
1731+
}
1732+
1733+
void ScriptingCore::runGame()
1734+
{
1735+
register_all();
1736+
1737+
start();
1738+
1739+
runScript("script/jsb_boot.js");
1740+
1741+
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
1742+
enableDebugger();
1743+
#endif
1744+
1745+
auto pEngine = ScriptingCore::getInstance();
1746+
ScriptEngineManager::getInstance()->setScriptEngine(pEngine);
1747+
1748+
runScript("main.js");
1749+
}
1750+
17081751
JSObject* NewGlobalObject(JSContext* cx, bool debug)
17091752
{
17101753
JS::CompartmentOptions options;

frameworks/js-bindings/bindings/manual/ScriptingCore.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ class ScriptingCore : public cocos2d::ScriptEngineProtocol
174174
*/
175175
void reset();
176176

177+
/**
178+
* run cocos game
179+
*/
180+
void runGame();
181+
177182
/**
178183
* will add the register_sth callback to the list of functions that need to be called
179184
* after the creation of the context
@@ -247,7 +252,7 @@ class ScriptingCore : public cocos2d::ScriptEngineProtocol
247252

248253
private:
249254
void string_report(jsval val);
250-
255+
void register_all();
251256
public:
252257
int handleNodeEvent(void* data);
253258
int handleComponentEvent(void* data);
@@ -257,6 +262,8 @@ class ScriptingCore : public cocos2d::ScriptEngineProtocol
257262
bool handleTouchEvent(void* nativeObj, cocos2d::EventTouch::EventCode eventCode, cocos2d::Touch* touch, cocos2d::Event* event, jsval* jsvalRet = nullptr);
258263
bool handleMouseEvent(void* nativeObj, cocos2d::EventMouse::MouseEventType eventType, cocos2d::Event* event, jsval* jsvalRet = nullptr);
259264
bool handleKeybardEvent(void* nativeObj, cocos2d::EventKeyboard::KeyCode keyCode, bool isPressed, cocos2d::Event* event);
265+
266+
void rebootVm();
260267
};
261268

262269
JSObject* NewGlobalObject(JSContext* cx, bool debug = false);

frameworks/js-bindings/bindings/script/jsb_boot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ cc.game = {
15221522
*/
15231523
restart: function () {
15241524
//window.location.href = window.location.href;
1525-
__restartGame();
1525+
__restartVM();
15261526
},
15271527
/**
15281528
* Run game.

samples/js-moonwarriors/project/Classes/AppDelegate.cpp

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
#include "cocos2d.h"
44
#include "SimpleAudioEngine.h"
55
#include "ScriptingCore.h"
6-
#include "jsb_cocos2dx_auto.hpp"
7-
#include "jsb_cocos2dx_extension_auto.hpp"
8-
#include "jsb_cocos2dx_builder_auto.hpp"
9-
#include "extension/jsb_cocos2dx_extension_manual.h"
10-
#include "cocos2d_specifics.hpp"
11-
#include "cocosbuilder/js_bindings_ccbreader.h"
12-
#include "localstorage/js_bindings_system_registration.h"
13-
#include "chipmunk/js_bindings_chipmunk_registration.h"
14-
#include "jsb_opengl_registration.h"
15-
#include "jsb_cocos2dx_ui_auto.hpp"
16-
#include "ui/jsb_cocos2dx_ui_manual.h"
17-
#include "cocostudio/jsb_cocos2dx_studio_manual.h"
18-
#include "jsb_cocos2dx_studio_auto.hpp"
6+
197

208
USING_NS_CC;
219
using namespace CocosDenshion;
@@ -49,31 +37,7 @@ bool AppDelegate::applicationDidFinishLaunching()
4937
// set FPS. the default value is 1.0/60 if you don't call this
5038
director->setAnimationInterval(1.0 / 60);
5139

52-
ScriptingCore* sc = ScriptingCore::getInstance();
53-
sc->addRegisterCallback(register_all_cocos2dx);
54-
sc->addRegisterCallback(register_all_cocos2dx_extension);
55-
sc->addRegisterCallback(register_cocos2dx_js_extensions);
56-
sc->addRegisterCallback(jsb_register_chipmunk);
57-
sc->addRegisterCallback(register_all_cocos2dx_extension_manual);
58-
sc->addRegisterCallback(register_all_cocos2dx_builder);
59-
sc->addRegisterCallback(register_CCBuilderReader);
60-
sc->addRegisterCallback(jsb_register_system);
61-
sc->addRegisterCallback(JSB_register_opengl);
62-
sc->addRegisterCallback(register_all_cocos2dx_ui);
63-
sc->addRegisterCallback(register_all_cocos2dx_ui_manual);
64-
sc->addRegisterCallback(register_all_cocos2dx_studio);
65-
sc->addRegisterCallback(register_all_cocos2dx_studio_manual);
66-
67-
sc->start();
68-
sc->runScript("script/jsb_boot.js");
69-
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
70-
sc->enableDebugger();
71-
#endif
72-
73-
auto pEngine = ScriptingCore::getInstance();
74-
ScriptEngineManager::getInstance()->setScriptEngine(pEngine);
75-
76-
ScriptingCore::getInstance()->runScript("main.js");
40+
ScriptingCore::getInstance()->runGame();
7741

7842
return true;
7943
}

0 commit comments

Comments
 (0)