Cocos-2dx 3.X引擎生命周期
程序员文章站
2024-03-25 19:26:04
...
Cocos-2dx 3.X引擎生命周期
安卓中:
安卓层执行一个 AppActivity.java的Activity->static的so库
public class __PROJECT_PACKAGE_LAST_NAME_UF__ extends Cocos2dxActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
static {
System.loadLibrary("game");
}
}
执行 onCreate()之前会调用父类的Cocos2dxActivity的onCreate()
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CocosPlayClient.init(this, false);
onLoadNativeLibraries();
sContext = this;
this.mHandler = new Cocos2dxHandler(this);//处理安卓的弹窗等
Cocos2dxHelper.init(this);
this.mGLContextAttrs = getGLContextAttrs();//获取OpenGL ES的相关属性
this.init();
if (mVideoHelper == null) {
mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout);
}
if(mWebViewHelper == null){
mWebViewHelper = new Cocos2dxWebViewHelper(mFrameLayout);
}
}
运用JNI技术(java_c++交互协议)调用nativeInit(),实例化一个Director,调用Application的run()
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
{
auto director = cocos2d::Director::getInstance();
auto glview = director->getOpenGLView();
if (!glview)
{
glview = cocos2d::GLViewImpl::create("Android app");
glview->setFrameSize(w, h);
director->setOpenGLView(glview);
cocos2d::Application::getInstance()->run();
}
,,, ,,,, ,,,
}
run()调用Application 子类AppDelegate的applicationDidFinishLaunching(),并开启游戏主循环
int Application::run()
{
......
// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching())
{
return 1;
}
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
// Retain glview to avoid glview being released in the while loop
glview->retain();
......
//游戏刷新循环
while(!glview->windowShouldClose())
{
QueryPerformanceCounter(&nNow);
interval = nNow.QuadPart - nLast.QuadPart;
if (interval >= _animationInterval.QuadPart)
{
nLast.QuadPart = nNow.QuadPart;
director->mainLoop();
glview->pollEvents();
}
else
{
// The precision of timer on Windows is set to highest (1ms) by 'timeBeginPeriod' from above code,
// but it's still not precise enough. For example, if the precision of timer is 1ms,
// Sleep(3) may make a sleep of 2ms or 4ms. Therefore, we subtract 1ms here to make Sleep time shorter.
// If 'waitMS' is equal or less than 1ms, don't sleep and run into next loop to
// boost CPU to next frame accurately.
waitMS = (_animationInterval.QuadPart - interval) * 1000LL / freq.QuadPart - 1L;
if (waitMS > 1L)
Sleep(waitMS);
}
}
// Director should still do a cleanup if the window was closed manually.
if (glview->isOpenGLReady())
{
director->end();
director->mainLoop();
director = nullptr;
}
glview->release();
}
win_32中:
main主函数里面实例了一个Appdelegate对象,然后调用父类构造器,在父类构造器里面返回一个application对象指针,
有这个对象指针调用application类里面的run方法,run方法里面调用applicationDidFinishLaunching(),
applicationDidFinishLaunching()里面实现了游戏开始的场景。
游戏结束检测,游戏内部有个场景栈(后进后出原则),当我们调用 director->end()时,没有场景存在时,
执行purgeDirector(),结束游戏
void Director::purgeDirector()
{
reset();
CHECK_GL_ERROR_DEBUG();
// OpenGL view
if (_openGLView)
{
_openGLView->end();
_openGLView = nullptr;
}
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
EngineDataManager::destroy();
#endif
// delete Director
release();
}
上一篇: Cocos2d-x中动态纹理CCRenderTexture的使用
下一篇: restful API
推荐阅读
-
Cocos-2dx 3.X引擎生命周期
-
深入理解PHP内核(二)概览-PHP生命周期与Zend引擎,-phpzend
-
深入懂得PHP内核(二)概览-PHP生命周期与Zend引擎
-
深入理解PHP内核(二)概览-PHP生命周期与Zend引擎,-phpzend
-
深入理解PHP内核(二)概览-PHP生命周期与Zend引擎,-phpzend_PHP教程
-
深入理解PHP内核(二)概览-PHP生命周期与Zend引擎,-phpzend_PHP教程
-
深入理解PHP内核一概览-PHP生命周期与Zend引擎
-
深入理解PHP内核二概览-PHP生命周期与Zend引擎
-
深入理解PHP内核二概览-PHP生命周期与Zend引擎
-
深入理解PHP内核一概览-PHP生命周期与Zend引擎