在不同平台下,cocos2d-x的入口是不一样的,在win32平台,程序是从_tWinMain()开始的,可以在每一个cocos2d-x的程序中的main.cpp中找到。
(PS: AppDelegate继承于CCApplication,在构造方法中,初始化CCApplication单例类的唯一实例)
代码如下:
<span style="font-size: 14px;">int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
return cocos2d::CCApplication::sharedApplication().run(); // 注意这里
}</span>
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 (!applicationDidFinishLaunching())
{
return 0;
}
CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView();
pMainWnd->centerWindow();
ShowWindow(pMainWnd->getHWnd(), SW_SHOW);
while (1)// 注意这里,主循环来了
{
if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Get current time tick.
QueryPerformanceCounter(&nNow);
// 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;
}
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 (!applicationDidFinishLaunching()) { return 0; } CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView(); pMainWnd->centerWindow(); ShowWindow(pMainWnd->getHWnd(), SW_SHOW); while (1)// 注意这里,主循环来了 { if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // Get current time tick. QueryPerformanceCounter(&nNow); // 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; }
CCDirector::sharedDirector()->mainLoop();
CCDirector::sharedDirector()->mainLoop();
这是神马东西啊!这个就是cocos2d-x的主循环了,由导演负责维护。从此就进入了cocos2d-x的世界,跟windows没有一毛钱关系了。
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine); // create the application instanceAppDelegate app; return cocos2d::CCApplication::sharedApplication().run();}
(PS:cocos2d-x中有很多这样的用法,即同一个头文件,在不同平台上实现不同)
win32平台下的CCEGLView是一个windows窗口,具体情况CCEGLView::sharedOpenGLView();
(PS:CCEGLView还用来处理鼠标点击模拟器触摸,按键等事件的处理,如果要自定义,请参看CCEGLView::WindowProc(),这个函数是Windows程序接收消息回调)
在完成CCEGLView初始化之后,将开启游戏渲染的主循环,即调用CCDirector::sharedDirector()->mainLoop()。这样,程序就又交给CCDirector类来处理,CCDirector主要处理两件事情:
<span style="font-size: 14px;">void CCDisplayLinkDirector::mainLoop(void)
{
if (m_bPurgeDirecotorInNextLoop)
{
purgeDirector();
m_bPurgeDirecotorInNextLoop = false;
}
elseif (! m_bInvalid)
{
drawScene();
// release the objects
CCPoolManager::getInstance()->pop();
}
}</span>
void CCDisplayLinkDirector::mainLoop(void){if (m_bPurgeDirecotorInNextLoop){purgeDirector(); m_bPurgeDirecotorInNextLoop = false;}else if (! m_bInvalid) { drawScene(); // release the objects CCPoolManager::getInstance()->pop(); }}
在cocos2d中渲染的根节点是CCScene(PS:CCScene也是CCNode的子类),但是在drawScene()中似乎没有类似draw()的方法,那是因为在渲染之前,要对游戏中的节点做一些处理,包括旋转,缩放等等,所以这个方法的名字叫visit(),而visit()是定义在CCNode中的虚方法,CCScene中并没有单独实现。
(PS:在每一次渲染的时候,都需要先将之前的矩阵入栈,在渲染完当前节点之后,矩阵出栈,恢复之前的矩阵)
通过CCNode::transform()对当前节点做矩阵处理(包括旋转,缩放等等)
绘制就很简单了,遍历所有的子节点,采用递归的方式,对每一个CCNode调用visit()方法
(PS:设置Z-Order的时候可以设置为负值,有时候是很必要的,比如我们以一个人物精灵的身体为位置参照物,绘制在Z-Order为0的位置上,如果要给这个人物精灵添加一个披风耍帅,就可以将披风的Z-Order设置为负值,并且可以继续以精灵身体作为参照物,因为cocos2d-x提供的访问顺序也是 Z-Order为负值的子节点->自身节点->Z-Order为正值的子节点)
<span style="font-size: 14px;">if(m_pChildren && m_pChildren->count() > 0)
{
// draw children zOrder < 0
ccArray *arrayData = m_pChildren->data;
for( ; i < arrayData->num; i++ )
{
pNode = (CCNode*) arrayData->arr[i];
if ( pNode && pNode->m_nZOrder < 0 )
{
pNode->visit();
}
else
{
break;
}
}
}
// self draw
this->draw();
// draw children zOrder >= 0
if (m_pChildren && m_pChildren->count() > 0)
{
ccArray *arrayData = m_pChildren->data;
for( ; i < arrayData->num; i++ )
{
pNode = (CCNode*) arrayData->arr[i];
if (pNode)
{
pNode->visit();
}
}
}</span>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。