您的位置:首页 > 移动开发

AppDelegate->Application->ApplicationProtocol

2017-07-12 16:13 369 查看
AppDelegate->Application->ApplicationProtocol;

ApplicationProtocol 里面都是虚函数 没什么有用的东西

class CC_DLL ApplicationProtocol
{
public:
enum class Platform
{
OS_WINDOWS,     /**< Windows */
OS_LINUX,       /**< Linux */
OS_MAC,         /**< Mac OS X*/
OS_ANDROID,     /**< Android */
OS_IPHONE,      /**< iPhone */
OS_IPAD,        /**< iPad */
OS_BLACKBERRY,  /**< BlackBerry */
OS_NACL,        /**< Native Client in Chrome */
OS_EMSCRIPTEN,  /**< Emscripten */
OS_TIZEN,       /**< Tizen */
OS_WINRT,       /**< Windows Runtime Applications */
OS_WP8          /**< Windows Phone 8 Applications */
};
virtual ~ApplicationProtocol(){
#if CC_ENABLE_SCRIPT_BINDING
ScriptEngineManager::destroyInstance();
#endif
/** clean auto release pool. */
PoolManager::destroyInstance();
}

/**
* 实例化导演类 场景初始化
*/
virtual bool applicationDidFinishLaunching() = 0;

/**
* 游戏到后台后 会调用这个方法
*/
virtual void applicationDidEnterBackground() = 0;

/**
* 从后台回到前台
*/
virtual void applicationWillEnterForeground() = 0;

/**
* FPS
*/
virtual void setAnimationInterval(float interval) = 0;
virtual void setAnimationInterval(float interval, SetIntervalReason reason) = 0;

/** Subclass override the function to set OpenGL context attribution instead of use default value.
* And now can only set six attributions:redBits,greenBits,blueBits,alphaBits,depthBits,stencilBits.
* Default value are(5,6,5,0,16,0), usually use as follows:
* void AppDelegate::initGLContextAttrs(){
*     GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
*     GLView::setGLContextAttrs(glContextAttrs);
* }
*/
virtual void initGLContextAttrs() {}

virtual LanguageType getCurrentLanguage() = 0;
virtual const char * getCurrentLanguageCode() = 0;

/**
@brief Get target platform.
* @js NA
* @lua NA
*/
virtual Platform getTargetPlatform() = 0;

virtual std::string getVersion() = 0;

/**
* Open url in default browser.
*/
virtual bool openURL(const std::string &url) = 0;
};


main.cpp Application::getInstance()->run() 开始启动应用。

那么在run里面有哪些呢?这是win平台下run的代码

int Application::run()
{
PVRFrameEnableControlWindow(false);

///////////////////////////////////////////////////////////////////////////
/////////////// changing timer resolution
///////////////////////////////////////////////////////////////////////////
UINT TARGET_RESOLUTION = 1; // 1 millisecond target resolution
TIMECAPS tc;
UINT wTimerRes = 0;
if (TIMERR_NOERROR == timeGetDevCaps(&tc, sizeof(TIMECAPS)))
{
wTimerRes = std::min(std::max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
timeBeginPeriod(wTimerRes);
}

// Main message loop:
LARGE_INTEGER nLast;
LARGE_INTEGER nNow;

QueryPerformanceCounter(&nLast);

initGLContextAttrs();

// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching())
{
return 1;
}

auto director = Director::getInstance();
auto glview = director->getOpenGLView();

glview->retain();

LONGLONG interval = 0LL;
LONG waitMS = 0L;

LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);

while(!glview->windowShouldClose())
{
QueryPerformanceCounter(&nNow);
interval = nNow.QuadPart - nLast.QuadPart;
if (interval >= _animationInterval.QuadPart)
{
nLast.QuadPart = nNow.QuadPart;
director->mainLoop();
glview->pollEvents();
}
else
{
waitMS = (_animationInterval.QuadPart - interval) * 1000LL / freq.QuadPart - 1L;
if (waitMS > 1L)
Sleep(waitMS);
}
}

if (glview->isOpenGLReady())
{
director->end();
director->mainLoop();
director = nullptr;
}
glview->release();
if (wTimerRes != 0)
{
timeEndPeriod(wTimerRes);
}
return 0;
}


安卓:

int Application::run()
{
// Initialize instance and cocos2d.
if (! applicationDidFinishLaunching())
{
return 0;
}

return -1;
}


ios:

int Application::run()
{
initGLContextAttrs();
if(!applicationDidFinishLaunching())
{
return 1;
}

long lastTime = 0L;
long curTime = 0L;

auto director = Director::getInstance();
auto glview = director->getOpenGLView();

// Retain glview to avoid glview being released in the while loop
glview->retain();

while (!glview->windowShouldClose())
{
lastTime = getCurrentMillSecond();

director->mainLoop();
glview->pollEvents();

curTime = getCurrentMillSecond();
if (curTime - lastTime < _animationInterval)
{
usleep(static_cast<useconds_t>((_animationInterval - curTime + lastTime)*1000));
}
}
if (glview->isOpenGLReady())
{
director->end();
director->mainLoop();
}

glview->release();

return 0;
}


对比发现 这里可以只关心 applicationDidFinishLaunching 这个方法 其他的代码应该和各个平台有关 貌似Application 这个类里面 其他内容没啥好关心的 主要的代码都在 AppDelegate 里面 绕了一圈 又回到了 AppDelegate。

补充:Director::getInstance()->mainLoop() 进入引擎的主循环

不同平台 进入主循环的地方不一样 这里就不一一介绍了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Applicatio
相关文章推荐