Cocos2d-x学习(2) - cocos2d运行分析

时间:2023-02-07 21:41:48

1、Cocos2d-x程序运行过程


Cocos2d-x工程组织形式如图所示:


Cocos2d-x学习(2) - cocos2d运行分析


Classes一般用来放用户自己编写的类。在Win32下,可以看到main.cpp和main.h两个文件,程序的入口就是在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;
CCEGLView* eglView = CCEGLView::sharedOpenGLView();
eglView->setViewName("block");
eglView->setFrameSize(320, 480);
return CCApplication::sharedApplication()->run();
}

从上述代码中可以看出,程序主要是通过一个CCApplication::sharedApplication()->run()来运行的,根据经验,CCApplication应该是一个类,sharedApplication()应该是该类的一个静态方法。

在CCAppliction.cpp中:


CCApplication* CCApplication::sharedApplication()
{
CC_ASSERT(sm_pSharedApplication);
return sm_pSharedApplication;
}

sm_pSharedApplication也是CCApplcation的一个静态成员。但是该程序初始化的时候:

// sharedApplication pointer
CCApplication * CCApplication::sm_pSharedApplication = 0;
 

那么该成员是在那创建的!? 首先需要了解下面这几个类:

AppDelegate –     app代理类,继承自CCApplication

class  AppDelegate:privatecocos2d::CCApplication


CCApplication – app应用类,继承自CCApplicationProtocol

class CC_DLLCCApplication:publicCCApplicationProtocol


CCApplicationProtocol是一个虚类,定义了一些接口函数


class CC_DLL CCApplicationProtocol
{
public:
virtual ~CCApplicationProtocol() {}
virtual bool applicationDidFinishLaunching() = 0;
virtual void applicationDidEnterBackground() = 0;
virtual void applicationWillEnterForeground() = 0;
virtual void setAnimationInterval(double interval) = 0;
virtual ccLanguageType getCurrentLanguage() = 0;
virtual TargetPlatform getTargetPlatform() = 0;
};

Cocos2d-x学习(2) - cocos2d运行分析


     上图就表示出了整个的过程,有个问题就是sm_pSharedApplication = this,那么这个this代表的究竟是什么意思呢?

上述中的this应该事项的是CCApplication的父类,也就是AppDelegate,因为在构造的过程中先去构造父类,父类构造后再构造子类。实际上就是使用AppDelegate类来进行设计。

在CCAplication::run()中通过调用applicationDidFinishLaunching()来启动程序,也就是网上很多资料都说的程序入口。


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;
}

 

对于cocos2d的用户来说:

在applicationDidFinishLaunching中调用自己设计的场景类就可以实现自己想要的功能。


bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

pDirector->setOpenGLView(pEGLView);
//pEGLView->setDesignResolutionSize(480, 320, kResolutionUnKnown);
pEGLView->setDesignResolutionSize(320, 480, kResolutionShowAll);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);

// create a scene. it's an autorelease object
CCScene *pScene = MainScene::CreateScene();

// run
pDirector->runWithScene(pScene);

return true;
}

2、Cocos2d的逻辑结构

    Cocos2d游戏是通过一系列的元素来进行定义的,其中包括:导演,场景,层,演员。元素具有以下特点:

一个导演同一时间只能运行一个场景;

一个场景当中,可以同时加载多个层;

一个层同可以加载多个精灵;

层中还可以加层。

Cocos2d-x学习(2) - cocos2d运行分析