學習cocos2d-x有一段時間了,現在來回顧一下所學的知識。回顧一下以前對cocos2d-x的初步探索:
首先從main函數入口:打開main.cpp,發現
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
遊戲入口函數;其代碼分析如下
//UNREFERENCED_PARAMETER宏定義,防止聲明變量未使用時編譯器出現警告
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
CCEGLView* eglView = CCEGLView::sharedOpenGLView();
eglView->setViewName("DownDownApple");
eglView->setFrameSize(480, 320);
return CCApplication::sharedApplication()->run();
還記得上面代碼創建了一個AppDelegate對象吧! AppDelegate繼承類CCApplication,在CCApplication的構造函數裏會賦值爲this,然後靜態成員函數sharedApplication返回該指針指向對象的引用,最後調用了app的run函數,進入整個程序的消息循環.接下來我們就來看看CCApplication的run函數;
代碼分析如下:
int CCApplication::run()
{
PVRFrameEnableControlWindow(false); //將窗口信息寫入註冊表之類,沒有仔細研究
// Main message loop:
MSG msg;
LARGE_INTEGER nFreq;
LARGE_INTEGER nLast;
LARGE_INTEGER nNow;
//高精度時間計數
QueryPerformanceFrequency(&nFreq); //獲得時鐘頻率
QueryPerformanceCounter(&nLast); //獲取當前時鐘精
//下面兩個都是純虛函數,子類實現,父類調用。
// Initialize instance and cocos2d.
if (! initInstance() || ! applicationDidFinishLaunching())
{
return 0;
}
CCEGLView& mainWnd = CCEGLView::sharedOpenGLView();
mainWnd.centerWindow();
ShowWindow(mainWnd.getHWnd(), SW_SHOW);
while (1)
{
if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
// Get current time tick.
QueryPerformanceCounter(&nNow);
//m_nAnimationInterval保存了幀頻率,可以通過CCDirector單例對象修改
// If it's the time to draw next frame, draw it, else sleep a while.
if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)
{
nLast.QuadPart = nNow.QuadPart;
CCDirector::sharedDirector()->mainLoop();
}
else
{
Sleep(0);
}
continue;
}
if (WM_QUIT == msg.message)
{
// Quit message loop.
break;
}
// Deal with windows message.
if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
有沒注意到if (! initInstance() || ! applicationDidFinishLaunching())這句代碼,其中的applicationDidFinishLaunching()在子類實現,也就是AppDelegate類.所以我們接下來打開AppDelegate.cpp的applicationDidFinishLaunching()函數看看.
在這裏可以實現CCDirector對象的一些初始化,代碼如下:
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector(); //創建單例對象
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); //創建一個EAGLView對象窗口
pDirector->setOpenGLView(pEGLView); //將EAGLView對象傳遞給導演
pEGLView->setDesignResolutionSize(800,480,kResolutionNoBorder); //設置窗口大小
pDirector->setDisplayStats(true); //是否開啓FPS顯示
pDirector->setAnimationInterval(1.0 / 60); //動畫更新速率
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene(); //創建場景對象(這裏可以用自己創建的場景類代替)
// run
pDirector->runWithScene(pScene); //運行一個場景
return true;
}
接下來我們可以看看HelloWorld::scene(),打開HelloWorld.cpp看看它是怎樣創建並初始化一個場景的;
代碼如下:
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create(); //創建一個場景類,此對象將會自動釋放(an autorelease object)
HelloWorld *layer = HelloWorld::create(); //創建一個層對象,也是會自動釋放
scene->addChild(layer); //將層添加到場景中
return scene; //返回場景對象
}
其中HelloWorld是 一個CCLayer類 實質上也是一個CCNode類,當使用create()時會調用Init()函數,所以我們可以在Init()函數中初始化我們的場景
bool HelloWorld::init()
{
//////////////////////////////
if ( !CCLayer::init() ) {
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); //獲取窗口大小
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); //獲取窗口方向
//添加一個圖片菜單層
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png", //缺省狀態
"CloseSelected.png",//選中狀態
this,//當前層
menu_selector(HelloWorld::menuCloseCallback));//按鈕按下的回調方法
//設置菜單按鈕的位置
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// 創建菜單
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
//添加HelloWorld文字
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
this->addChild(pLabel, 1);
//添加圖片
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(pSprite, 0);
return true;
}
這樣cocos2d-x的初步探索學習就到這裏了;