博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2dx 2.x 每帧渲染分析
阅读量:4216 次
发布时间:2019-05-26

本文共 3540 字,大约阅读时间需要 11 分钟。

1、java端

1、Cocos2dxRenderer类	@Override	public void onDrawFrame(final GL10 gl) {		/*		 * No need to use algorithm in default(60 FPS) situation,		 * since onDrawFrame() was called by system 60 times per second by default.		 */		if (sAnimationInterval <= 1.0 / 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND) {			Cocos2dxRenderer.nativeRender();		} else {			final long now = System.nanoTime();			final long remain = mLastTickInNanoSeconds + Cocos2dxRenderer.sAnimationInterval - now;			if (remain > 0) {				try {					Thread.sleep(remain / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);				} catch (final Exception e) {				}			}			/*			 * Render time MUST be counted in, or the FPS will slower than appointed.			*/			mLastTickInNanoSeconds = System.nanoTime();			Cocos2dxRenderer.nativeRender(); //调用C++部分的函数		}	}
2、Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp C++端

JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender(JNIEnv* env) {        cocos2d::CCDirector::sharedDirector()->mainLoop();    }    -->> mainLoop函数    void CCDisplayLinkDirector::mainLoop(void){    if (m_bPurgeDirecotorInNextLoop)    {        m_bPurgeDirecotorInNextLoop = false;        purgeDirector();    }    else if (! m_bInvalid)     {         drawScene();              // release the objects         CCPoolManager::sharedPoolManager()->pop();             }}-->>drawScene渲染// Draw the Scenevoid CCDirector::drawScene(void){    // calculate "global" dt    calculateDeltaTime();    //tick before glClear: issue #533    if (! m_bPaused) //定时器相关,以后说    {        m_pScheduler->update(m_fDeltaTime);    }    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    /* to avoid flickr, nextScene MUST be here: after tick and before draw.     XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */    if (m_pNextScene)    {        setNextScene();    }    kmGLPushMatrix();    // draw the scene 渲染    if (m_pRunningScene)    {        m_pRunningScene->visit();    }    // draw the notifications node    if (m_pNotificationNode)    {        m_pNotificationNode->visit();    }        if (m_bDisplayStats)    {        showStats();    }        kmGLPopMatrix();    m_uTotalFrames++;    // swap buffers    if (m_pobOpenGLView)    {        m_pobOpenGLView->swapBuffers();    }        if (m_bDisplayStats)    {        calculateMPF();    }}
-->>visit函数,遍历UI树,调用visit和draw进行每个node的渲染void CCNode::visit(){    // quick return if not visible. children won't be drawn.    if (!m_bVisible)    {        return;    }    kmGLPushMatrix();     if (m_pGrid && m_pGrid->isActive())     {         m_pGrid->beforeDraw();     }    this->transform();    CCNode* pNode = NULL;    unsigned int i = 0;    if(m_pChildren && m_pChildren->count() > 0)    {        sortAllChildren();        // 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();        for( ; i < arrayData->num; i++ )        {            pNode = (CCNode*) arrayData->arr[i];            if (pNode)            {                pNode->visit();            }        }            }    else    {        this->draw();    }    // reset for next frame    m_uOrderOfArrival = 0;     if (m_pGrid && m_pGrid->isActive())     {         m_pGrid->afterDraw(this);    }     kmGLPopMatrix();}

转载地址:http://ptsmi.baihongyu.com/

你可能感兴趣的文章
Java并发编程从入门到精通 张振华.Jack --我的书
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第十二篇:世界上最快的捷径【张振华.Jack】
查看>>
Android中Java代码和XML布局效率问题
查看>>
android TextView属性大全(转)
查看>>
Conclusion for Resource Management
查看>>
Conclusion for Constructors,Destructors,and Assignment Operators
查看>>
Conclusion for Accustoming Yourself to C++
查看>>
面试题1:赋值运算函数(offer)
查看>>
Mark : MessagePack简介及使用
查看>>
Mark : hive文件存储格式
查看>>
mark : hadoop 四种压缩格式
查看>>
All Things OpenTSDB
查看>>
单例模式(singleton),工厂方法模式(factory),门面模式(facade)
查看>>
抽象模式,适配器模式(Adapter),模板方法模式(Template method)
查看>>
建造者模式(builder),桥梁模式(bridge mode),命令模式(Command mode)
查看>>
装饰模式(Decorator),迭代器模式(Iterator),组合模式(composite)
查看>>
观察者模式(Observer),责任链模式,访问者模式(Visitor)
查看>>
状态模式(State)
查看>>
快速排序
查看>>
插入算法
查看>>