本文主要讲解Cocos2d-x的整体启动过程:Cocos2d-x 在各个平台的实现代码是一样的,只要针对不同平台做相应的配置就可以了。
一、启动前奏
现在来看一下在iOS平台下的相关结构:
打开源代码自带工程,你会看到一个main文件,这里main里面有一个main函数,这是程序的入口函数。在这里它会加载AppController,进入这个类,这里有iOS平台初始化代码,但是最先执行的如下:
1
2
|
static
AppDelegates_sharedApplication;
|
在这里Cocos2d-x创建了一个appDelegare的对象,当然在创建的过程中会进行相应的初始化,通过代码可以看到:
2
3
4
5
/**
class
AppDelegate:
private
cocos2d::CCApplication
|
这里就会调用CCApplicaiton的构造函数:
5
CCApplication::CCApplication()
{
CC_ASSERT(!sm_pSharedApplication);
sm_pSharedApplication=
this
;
}
|
将this赋值给了sm_pSharedApplication, 这个this是什么? 实际上this是AppDeletegate, 因为这里的调用过程中并没有涉及到父类的对象,如果要涉及应该是CCApplication::CCApplication(); C++中,在创建派生类对象的时候不会创建父类对象,只会显示或者隐式的调用父类的构造函数。
但是这里为什么会有一个没有用到的全局变量呢? 答案在后面:
1
cocos2d::CCApplication::sharedApplication()->run();
|
一开始定义一个static 变量就是要定义一个CCApplication的实例,然后调用run函数。
进入run函数:
5
6
7
8
int
CCApplication::run()
{
if
(applicationDidFinishLaunching())
{
[[CCDirectorCallersharedDirectorCaller]startMainLoop];
}
return
0;
}
|
ok,可以看到,当applicationDidFinishLaunching执行成功后就会执行startMainLoop函数(这其实就启动了线程),分别看一下源代码:
applicationDidFinishLaunching 是CCApplication的抽象函数,在AppDelegate中实现:
8
9
10
11
12
13
14
15
bool
AppDelegate::applicationDidFinishLaunching()
CCDirector*pDirector=CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
//turnondisplayFPS
pDirector->setDisplayStats(
true
);
//setFPS.thedefaultvalueis1.0/60ifyoudon’tcallthis
pDirector->setAnimationInterval(1.0/60);
//createascene.it’sanautoreleaSEObject
CCScene*pScene=HelloWorld::scene();
//run
pDirector->runWithScene(pScene);
return
;
}
|
其实对于游戏运行来说,说白了就是一个死循环,就像win的消息那样。
来看一下startMainLoop函数:
9
-(
void
)startMainLoop
//CCDirector::setAnimationInterval()iscalled,weshouldinvalidateitfirst
[displayLinkinvalidate];
displayLink=nil;
displayLink=[NSClassFromString(@
"CADisplayLink"
)displayLinkWithTarget:selfselector:@selector(doCaller:)];
[displayLinksetFrameInterval:self.interval];
[displayLinkaddToRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSDefaultRunLoopMode];
}
|
什么?没有循环? 你错了,注意的是这里加载了一个类:CADisplayLink,就是这里循环起来的,这其实是一个定时器,默认情况是每秒运行60次。CADisplayLink是一个定时器类,他能以特定的模式注册到runloop,这样每当屏幕显示内容刷新结束,runloop就会向CADisplayLink指定的target发送一次执行的selector消息,对应的毁掉函数就会调用起来。
可以看到这里有一个回调函数:
4
)doCaller:(id)sender
cocos2d::CCDirector::sharedDirector()->mainLoop();
}
|
二、开始执行
在这里,我们就进入了mainLoop函数了。这里就是我们的头了:
14
CCDisplayLinkDirector::mainLoop(
)
(m_bPurgeDirecotorInNextLoop)
m_bPurgeDirecotorInNextLoop=
false
;
purgeDirector();
}
else
(!m_bInvalid)
{
drawScene();
//releasetheobjects
CCPoolManager::sharedPoolManager()->pop();
}
}
|
好吧,这里主要的函数drawScene(),来看看主要的实现点:
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//DrawtheScene
CCDirector::drawScene(
)
{
//calculate“global”dt
calculateDeltaTime();
//tickbeforeglClear:issue#533
(!m_bPaused)
{
m_pScheduler->update(m_fDeltaTime);
}
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
(m_pNextScene)
{
setNextScene();
}
kmGLPushMatrix();
//drawthescene
(m_pRunningScene)
{
m_pRunningScene->visit();
}
//drawthenotificationsnode
(m_pNotificationNode)
{
m_pNotificationNode->visit();
}
(m_bDisplayStats)
{
showStats();
}
kmGLPopMatrix();
m_uTotalFrames++;
//swapbuffers
(m_pobOpenGLView)
{
m_pobOpenGLView->swapBuffers();
}
(m_bDisplayStats)
{
calculateMPF();
}
}
|
我们先一步一步的看,在CCDirector中,mm文件里面有一个句:
CCDisplayLinkDirector*s_SharedDirector=NULL;
在这里CCSisplayLinkDirector是继承自CCDirector的,所以我们在运行的时候会调用:
9
CCDirector*CCDirector::sharedDirector(
(!s_SharedDirector)
s_SharedDirector=
new
CCDisplayLinkDirector();
s_SharedDirector->init();
s_SharedDirector;
}
|
而后:
11
CCDirector::init(
CCLOG(“cocos2d:%s”,cocos2dVersion());
…………………
//scheduler初始化CCScheduler定时调度器第二篇中会看到哟
m_pScheduler=
CCScheduler();
………………….
//createautoreleasepool这里CCPoolManager管理多个CCAutoreleasePool,将CCAutoreleasePool放到CCPoolManager中的m_pReleasePoolStack
CCPoolManager::sharedPoolManager()->push();
;
三、渲染开始
还记得刚刚我们看到的visit么? 他是在CCNode中实现的,我们来看一下:
45
46
47
48
49
50
51
52
53
54
55
CCNode::visit()
//quickreturnifnotvisible.childrenwon’tbedrawn.
(!m_bVisible)
{
;
//压入矩阵
(m_pGrid&&m_pGrid->isActive())
{
m_pGrid->beforeDraw();
}
->transform();
CCNode*pNode=NULL;
unsigned
i=0;
(m_pChildren&&m_pChildren->count()>0)
{
sortAllChildren();
//drawchildrenzOrder<0
ccArray*arrayData=m_pChildren->data;
for
(;i<arrayData->num;i++)
pNode=(CCNode*)arrayData->arr[i];
(pNode&&pNode->m_nZOrder<0)
{
pNode->visit();
}
else
{
break
;
}
}
//selfdraw
->draw();
(;i<arrayData->num;i++)
{
pNode=(CCNode*)arrayData->arr[i];
(pNode)
{
pNode->visit();
}
}
}
else
{
->draw();
}
//resetfornextframe
m_uOrderOfArrival=0;
(m_pGrid&&m_pGrid->isActive())
{
m_pGrid->afterDraw(
);
}
//出矩阵
}
|
刚刚看到了transform,这是做什么的呢?它是进行坐标系的变换,没有坐标系的变换,则无法在正确的位置绘制出纹理。变换矩阵等价于坐标系变换.变换矩阵是如何根据当前节点的位置、旋转角度和缩放比例等属性计算出来的了。形象地讲,transform 方法的任务就是根据当前节点的属性计算出如何把绘图坐标系变换为新坐标系的矩阵。
那就继续看transform吧:
20
CCNode::transform()
kmMat4transfrom4x4;
//Convert3×3into4×4matrix
CCAffineTransformtmpAffine=
->nodeToParentTransform();
CGAffineToGL(&tmpAffine,transfrom4x4.mat);
//UpdateZvertexmanually
transfrom4x4.mat[14]=m_fVertexZ;
kmGLMultMatrix(&transfrom4x4);
//XXX:Expensivecalls.Camerashouldbeintegratedintothecachedaffinematrix
(m_pCamera!=NULL&&!(m_pGrid!=NULL&&m_pGrid->isActive()))
{
translate=(m_obAnchorPointInPoints.x!=0.0f||m_obAnchorPointInPoints.y!=0.0f);
(translate)
kmGLTranslatef(RENDER_IN_SUBPIXEL(m_obAnchorPointInPoints.x),RENDER_IN_SUBPIXEL(m_obAnchorPointInPoints.y),0);
m_pCamera->locate();
(translate)
kmGLTranslatef(RENDER_IN_SUBPIXEL(-m_obAnchorPointInPoints.x),RENDER_IN_SUBPIXEL(-m_obAnchorPointInPoints.y),0);
}
}
|
一般来说游戏中会大量使用旋转,缩放,平移等仿射变换( 所谓仿射变换是指在线性变换的基础上加上平移,平移不是线性变换)。2D计算机图形学中的仿射变换通常是通过和3×3齐次矩阵相乘来实现的。Cocos2d中的仿射变换使用了Quartz 2D中的CGAffineTransform类来表示:
struct
CCAffineTransform{
float
a,b,c,d;
tx,ty;
};
typedef
CGAffineTransformCGAffineTransform;
在Cocos2d-x中,绘制是使用了openies的,所以CGAffineTransform只是用来表示2d仿射变换的,最终还是要转化成OpenglES的4*4变换矩阵的,因为OpenGL是3d的。这个转换工作是由 CGAffineToGL来完成的。
变换后的矩阵关系如下:
1
|m11m21m31m41||ac0tx||m12m22m32m42||bd0ty||m13m23m33m43|<=>|0010||m14m24m34m44||0001|
|
最后来看一个概念:
“节点坐标系”指的是以一个节点作为参考而产生的坐标系,换句话说,它的任何一个子节点的坐标值都是由这个坐标系确定的,通过以上方法,我们可以方便地处理触摸点,也可以方便地计算两个不同坐标系下点之间的方向关系。
这里是整个启动过程的一部分,后面我们还会根据Cocos2d-x的内存机制和回调机制来进行分析,也会有一些深层次的渲染知识。
来源网址:http://iqll.sinaapp.com/cocos2dx-启动过程详解一:渲染/
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|