先把上一篇忘記分析的autorelease說一下,在CCDirector.cpp的主循環有
void DisplayLinkDirector::mainLoop()
{
if (_purgeDirectorInNextLoop)
{
_purgeDirectorInNextLoop = false;
purgeDirector();
}
else if (! _invalid)
{
drawScene();
// release the objects
PoolManager::getInstance()->getCurrentPool()->clear();
}
}
顯然表明了在每一幀結束的時候會進行PoolManager的一個清理工作,而通過CCRef.cpp可知道當進行autorelease的時候會把對象放入AutoreleasePool的verctor裏面,所以調用了autorelease的對象會在一幀結束的時候調用了release的操作,即把引用計數減爲0,然後會進行delete的操作,把對象給釋放掉。
cocos是一個跨平臺的遊戲引擎,有必要分析一下cocos的啓動流程,這裏主要分析一下Win32的啓動流程,其他平臺的大同小異。
分析Win32的啓動流程,首先要找到win32的應用入口,即通常說的main函數。
在新建的工程裏面有一個main.cpp的文件,
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
return Application::getInstance()->run();
}
顯然這就是Win32的入口。顯然接下來要分析一下Application的內容:
先瀏覽一下頭文件的內容
//CCApplication.h
#ifndef __CC_APPLICATION_WIN32_H__
#define __CC_APPLICATION_WIN32_H__
#include "base/CCPlatformConfig.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
#include "CCStdC.h"
#include "platform/CCCommon.h"
#include "platform/CCApplicationProtocol.h"
#include <string>
NS_CC_BEGIN
class Rect;
class CC_DLL Application : public ApplicationProtocol
{
public:
/**
* @js ctor
*/
Application();
/**
* @js NA
* @lua NA
*/
virtual ~Application();
/**
@brief Run the message loop.
*/
int run();
/**
@brief Get current applicaiton instance.
@return Current application instance pointer.
*/
//說明是採用單例模式實現的
static Application* getInstance();
/** @deprecated Use getInstance() instead */
//被廢除的函數
CC_DEPRECATED_ATTRIBUTE static Application* sharedApplication();
/* override functions */
virtual void setAnimationInterval(double interval);
virtual LanguageType getCurrentLanguage();
virtual const char * getCurrentLanguageCode();
/**
@brief Get target platform
*/
virtual Platform getTargetPlatform();
/**
* Sets the Resource root path.
* @deprecated Please use FileUtils::getInstance()->setSearchPaths() instead.
*/
CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir);
/**
* Gets the Resource root path.
* @deprecated Please use FileUtils::getInstance()->getSearchPaths() instead.
*/
CC_DEPRECATED_ATTRIBUTE const std::string& getResourceRootPath(void);
void setStartupScriptFilename(const std::string& startupScriptFile);
const std::string& getStartupScriptFilename(void)
{
return _startupScriptFilename;
}
protected:
HINSTANCE _instance;
HACCEL _accelTable;
LARGE_INTEGER _animationInterval;
std::string _resourceRootPath;
std::string _startupScriptFilename;
static Application * sm_pSharedApplication;
};
NS_CC_END
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
#endif // __CC_APPLICATION_WIN32_H__
在來看一下具體的實現:
\\CCApplication.cpp
#include "base/CCPlatformConfig.h"
//限定是Win32平臺
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
#include "CCApplication.h"
#include "CCGLView.h"
#include "base/CCDirector.h"
#include <algorithm>
#include "platform/CCFileUtils.h"
/**
@brief This function change the PVRFrame show/hide setting in register.
@param bEnable If true show the PVRFrame window, otherwise hide.
*/
static void PVRFrameEnableControlWindow(bool bEnable);
NS_CC_BEGIN
// sharedApplication pointer
Application * Application::sm_pSharedApplication = 0;
//構造函數
Application::Application()
: _instance(nullptr)
, _accelTable(nullptr)
{
_instance = GetModuleHandle(nullptr);
_animationInterval.QuadPart = 0;
CC_ASSERT(! sm_pSharedApplication);
sm_pSharedApplication = this;
}
//析構函數,令sm_pSharedApplication的值爲Null,猜測是爲了防止野指針
Application::~Application()
{
CC_ASSERT(this == sm_pSharedApplication);
sm_pSharedApplication = NULL;
}
\\主要的內容,主要分析一下
int Application::run()
{
//這個函數是一些關於註冊表的操作,大致的操作就是向註冊表註冊一些內容
PVRFrameEnableControlWindow(false);
// Main message loop:
LARGE_INTEGER nFreq;
LARGE_INTEGER nLast;
LARGE_INTEGER nNow;
//這兩個也是Windows的接口函數,是精確到毫秒級的計時器函數
QueryPerformanceFrequency(&nFreq);
QueryPerformanceCounter(&nLast);
//這裏回調用工程裏面的Applidegrate.cpp的函數然後可以在裏面進入遊戲的場景
// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching())
{
return 0;
}
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
//對glview進行retain操作,防止被釋放掉
// Retain glview to avoid glview being released in the while loop
glview->retain();
//判斷窗口是否關掉
while(!glview->windowShouldClose())
{
//計時器判斷兩次執行的間隔是否大於設置的更新頻率,如果大於進入遊戲的主循環,如果小於則調用Sleep(0),查了相關的資料,知道如果當前的CPU有大於當前優先級的線程,則會調用優先級大的線程,否則繼續當前線程的操作,所以如果兩幀的間隔小於設置的函數,則會判斷如果有大於當前線程的優先級的線程,則調用優先級大的函數,否則繼續當前的線程
QueryPerformanceCounter(&nNow);
if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)
{
nLast.QuadPart = nNow.QuadPart;
//CCDirecotr的主循環,稍後分析
director->mainLoop();
glview->pollEvents();
}
else
{
Sleep(0);
}
}
//跳出循環後,判斷當前的opengl是否準備好,進行清理的工作
// Director should still do a cleanup if the window was closed manually.
if (glview->isOpenGLReady())
{
director->end();
director->mainLoop();
director = nullptr;
}
//釋放glview
glview->release();
return true;
}
//設置更新的間隔
void Application::setAnimationInterval(double interval)
{
LARGE_INTEGER nFreq;
QueryPerformanceFrequency(&nFreq);
_animationInterval.QuadPart = (LONGLONG)(interval * nFreq.QuadPart);
}
//////////////////////////////////////////////////////////////////////////
// static member function
//////////////////////////////////////////////////////////////////////////
//獲得Application的實例,採用了單例模式
Application* Application::getInstance()
{
CC_ASSERT(sm_pSharedApplication);
return sm_pSharedApplication;
}
//廢棄了的函數,此處不做分析了
// @deprecated Use getInstance() instead
Application* Application::sharedApplication()
{
return Application::getInstance();
}
//獲得當前使用的語言
LanguageType Application::getCurrentLanguage()
{
LanguageType ret = LanguageType::ENGLISH;
LCID localeID = GetUserDefaultLCID();
unsigned short primaryLanguageID = localeID & 0xFF;
switch (primaryLanguageID)
{
case LANG_CHINESE:
ret = LanguageType::CHINESE;
break;
case LANG_ENGLISH:
ret = LanguageType::ENGLISH;
break;
case LANG_FRENCH:
ret = LanguageType::FRENCH;
break;
case LANG_ITALIAN:
ret = LanguageType::ITALIAN;
break;
case LANG_GERMAN:
ret = LanguageType::GERMAN;
break;
case LANG_SPANISH:
ret = LanguageType::SPANISH;
break;
case LANG_DUTCH:
ret = LanguageType::DUTCH;
break;
case LANG_RUSSIAN:
ret = LanguageType::RUSSIAN;
break;
case LANG_KOREAN:
ret = LanguageType::KOREAN;
break;
case LANG_JAPANESE:
ret = LanguageType::JAPANESE;
break;
case LANG_HUNGARIAN:
ret = LanguageType::HUNGARIAN;
break;
case LANG_PORTUGUESE:
ret = LanguageType::PORTUGUESE;
break;
case LANG_ARABIC:
ret = LanguageType::ARABIC;
break;
case LANG_NORWEGIAN:
ret = LanguageType::NORWEGIAN;
break;
case LANG_POLISH:
ret = LanguageType::POLISH;
break;
}
return ret;
}
//獲得當前使用語言的代碼
const char * Application::getCurrentLanguageCode()
{
LANGID lid = GetUserDefaultUILanguage();
const LCID locale_id = MAKELCID(lid, SORT_DEFAULT);
static char code[3] = { 0 };
GetLocaleInfoA(locale_id, LOCALE_SISO639LANGNAME, code, sizeof(code));
code[2] = '\0';
return code;
}
//返回平臺
Application::Platform Application::getTargetPlatform()
{
return Platform::OS_WINDOWS;
}
//設置資源的路徑,以後分析CCFileUtils的時候在分析
void Application::setResourceRootPath(const std::string& rootResDir)
{
_resourceRootPath = rootResDir;
std::replace(_resourceRootPath.begin(), _resourceRootPath.end(), '\\', '/');
if (_resourceRootPath[_resourceRootPath.length() - 1] != '/')
{
_resourceRootPath += '/';
}
FileUtils* pFileUtils = FileUtils::getInstance();
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
searchPaths.insert(searchPaths.begin(), _resourceRootPath);
pFileUtils->setSearchPaths(searchPaths);
}
//獲得資源的根路徑
const std::string& Application::getResourceRootPath(void)
{
return _resourceRootPath;
}
//設置腳本的啓動名字,以後分析腳本的時候在分析
void Application::setStartupScriptFilename(const std::string& startupScriptFile)
{
_startupScriptFilename = startupScriptFile;
std::replace(_startupScriptFilename.begin(), _startupScriptFilename.end(), '\\', '/');
}
NS_CC_END
//////////////////////////////////////////////////////////////////////////
// Local function
//////////////////////////////////////////////////////////////////////////
//這個函數就是設置註冊表的實現函數,暫時對註冊表不太熟悉,不做具體分析,猜測就是對註冊表做一些寫入的操作
static void PVRFrameEnableControlWindow(bool bEnable)
{
HKEY hKey = 0;
// Open PVRFrame control key, if not exist create it.
if(ERROR_SUCCESS != RegCreateKeyExW(HKEY_CURRENT_USER,
L"Software\\Imagination Technologies\\PVRVFRame\\STARTUP\\",
0,
0,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
0,
&hKey,
NULL))
{
return;
}
const WCHAR* wszValue = L"hide_gui";
const WCHAR* wszNewData = (bEnable) ? L"NO" : L"YES";
WCHAR wszOldData[256] = {0};
DWORD dwSize = sizeof(wszOldData);
LSTATUS status = RegQueryValueExW(hKey, wszValue, 0, NULL, (LPBYTE)wszOldData, &dwSize);
if (ERROR_FILE_NOT_FOUND == status // the key not exist
|| (ERROR_SUCCESS == status // or the hide_gui value is exist
&& 0 != wcscmp(wszNewData, wszOldData))) // but new data and old data not equal
{
dwSize = sizeof(WCHAR) * (wcslen(wszNewData) + 1);
RegSetValueEx(hKey, wszValue, 0, REG_SZ, (const BYTE *)wszNewData, dwSize);
}
RegCloseKey(hKey);
}
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
applicationDidFinishLaunching這個函數是ApplicationProtocol的純虛函數,然後Appdelegate繼承Application並實現了這個函數,顯然調用此函數時,進入了Appdelegate.cpp裏面的applicationDidFinishLaunching的函數,這是面向對象的三大特性的多態的體現了。
從以上的分析可以知道,還有兩個文件比較重要,一個是Appdelegate.cpp和CCDirector.cpp
分別貼出這兩個文件
\\Appdelegate.h
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_
#include "cocos2d.h"
/**
@brief The cocos2d Application.
The reason for implement as private inheritance is to hide some interface call by Director.
*/
class AppDelegate : private cocos2d::Application
{
public:
AppDelegate();
virtual ~AppDelegate();
/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();
/**
@brief The function be called when the application enter background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();
/**
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};
#endif // _APP_DELEGATE_H_
\\標出紅色的函數就是實現了虛函數,這就是抽象的體現了,跨平臺抽象了接口,但是具體的實現就依據不同的平臺不同的實現。我們在applicationDidFinishLaunching裏面做的就是第一個場景的出現了,然後就進入了遊戲,很簡單,參考一下工程就明白了。接下來分析CCDirector.cpp文件,因爲我們這裏只用到了兩個函數,我們在這裏主要分析他們:end,mainLoop,
//實例化的時候返回了Director的子類,DisplayLinkDirector的實例,所以調用的是DisplayLinkDirector::mainLoop()
Director* Director::getInstance()
{
if (!s_SharedDirector)
{
s_SharedDirector = new DisplayLinkDirector();
s_SharedDirector->init();
}
return s_SharedDirector;
}
void DisplayLinkDirector::mainLoop()
{
//判斷是否清除導演類,即遊戲是否停止
if (_purgeDirectorInNextLoop)
{
_purgeDirectorInNextLoop = false;
purgeDirector();
}
else if (! _invalid)
{
//遊戲的渲染,具體下次分析遊戲的渲染的時候在分析
drawScene();
//這個就是本文首先提到的對於autorelease對象,會在此時進行清理的工作
// release the objects
PoolManager::getInstance()->getCurrentPool()->clear();
}
}
今天的分析暫時到這裏,下一次打算分析一下cocos的渲染。本文純屬個人分析學習,如果有不當之處,請大神指教一下!