本文共 3169 字,大约阅读时间需要 10 分钟。
IOS 下cocos2dx3.X 启动流程:
1、
入口函数:
int main(int argc,char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
// AppController类的实例,称为应用程序委托。
int retVal = UIApplicationMain(argc, argv,nil, @"AppController");
[pool release];
return retVal;
}
2、
接下来,主要就在AppController类中了:
2.1、
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// IMPORTANT: Setting the GLView should be done aftercreating the RootViewController
//进行一些初始化,然后进入游戏
cocos2d::GLView *glview =cocos2d::GLViewImpl::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
cocos2d::Application::getInstance()->run();
return YES;
}
GLViewImpl类,在分平台有不同的实现,继承于GLView类
----->>>
boolGLViewImpl::initWithEAGLView(void *eaglview)
{
_eaglview = eaglview;
CCEAGLView *glview = (CCEAGLView*)_eaglview;
//这里获取了屏幕大小
_screenSize.width =_designResolutionSize.width = [glview getWidth];
_screenSize.height = _designResolutionSize.height= [glview getHeight];
// _scaleX = _scaleY = [glviewcontentScaleFactor];
return true;
}
2.2、接着执行:
cocos2d::Director::getInstance()->setOpenGLView(glview);
voidDirector::setOpenGLView(GLView *openGLView)
{
if (_openGLView != openGLView)
{
// Configuration. Gather GPU info 获取依稀GPU的信息
Configuration *conf =Configuration::getInstance();
conf->gatherGPUInfo();
CCLOG("%s\n",conf->getInfo().c_str());
if(_openGLView)
_openGLView->release();
_openGLView = openGLView;
_openGLView->retain();
// set size,这个现在是屏幕大小
_winSizeInPoints =_openGLView->getDesignResolutionSize();
_isStatusLabelUpdated = true;
if (_openGLView)
{
setGLDefaultValues();
}
_renderer->initGLView();
CHECK_GL_ERROR_DEBUG();
if (_eventDispatcher)
{
_eventDispatcher->setEnabled(true);
}
}
}
2.3、接着执行: cocos2d::Application::getInstance()->run()
Application也是根据平台有不同的实现,继承于ApplicationProtocol
接下来就要运行到:
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
#if(CC_TARGET_PLATFORM== CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
glview =cocos2d::GLViewImpl::create("sample1");
#else
glview =cocos2d::GLViewImpl::createWithRect("sample1", Rect(0,0,900,640));
#endif
director->setOpenGLView(glview);
}
// set FPS. the default value is 1.0/60 ifyou don't call this
director->setAnimationInterval(1.0 /60);
//脚本初始化
ScriptingCore* sc =ScriptingCore::getInstance();
sc->addRegisterCallback(register_all_cocos2dx);
sc->addRegisterCallback(register_cocos2dx_js_core);
sc->addRegisterCallback(jsb_register_system);
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
sc->addRegisterCallback(JavascriptJavaBridge::_js_register);
#elif(CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM ==CC_PLATFORM_MAC)
sc->addRegisterCallback(JavaScriptObjCBridge::_js_register);
#endif
sc->start();
sc->runScript("script/jsb_boot.js");
#ifdefined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
sc->enableDebugger();
#endif
//执行main.js
ScriptEngineProtocol *engine =ScriptingCore::getInstance();
ScriptEngineManager::getInstance()->setScriptEngine(engine);
ScriptingCore::getInstance()->runScript("main.js");
return true;
}
转载地址:http://wesmi.baihongyu.com/