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