(1) 首先,这里以win32平台下为例子.win32下游戏的启动都是从win32目录下main文件开始的,即是游戏的入口函数,如下:

#include "main.h"
#include "AppDelegate.h"
#include "cocos2d.h"USING_NS_CC;int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR    lpCmdLine,int       nCmdShow)
{UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine);// create the application instance
    AppDelegate app;   // 启动游戏return Application::getInstance()->run();
}

(1-1)这里可以看出,在入口函数中,首先创建了一个AppDelegate对象,AppDelegate继承 自CCApplication,在创建APPDelegate对象的时候就会隐式调用CCApplication构造函数,在这个构造函数里边会将AppDelegate的this指针传递给全局共享对象sm_pSharedApplication,如下:

Application::Application()//初始化win32应用程序对象
: _instance(nullptr)
, _accelTable(nullptr)
{_instance    = GetModuleHandle(nullptr);  // 用于控制帧数的计数值_animationInterval.QuadPart = 0;CC_ASSERT(! sm_pSharedApplication);// 全局共享对象sm_pSharedApplication = this;
}

(1-2) 接下来调用Application::getInstance()->run();启动游戏,如下:

int Application::run()
{PVRFrameEnableControlWindow(false);// Main message loop:
    LARGE_INTEGER nFreq;LARGE_INTEGER nLast;LARGE_INTEGER nNow;QueryPerformanceFrequency(&nFreq);QueryPerformanceCounter(&nLast);// Initialize instance and cocos2d.// 执行AppDeletegate重载的applicationDidFinishLaunching函数if (!applicationDidFinishLaunching()){return 0;}auto director = Director::getInstance();auto glview = director->getOpenGLView();// Retain glview to avoid glview being released in the while loopglview->retain();while(!glview->windowShouldClose()){QueryPerformanceCounter(&nNow);if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart){nLast.QuadPart = nNow.QuadPart;// 主循环,每帧调用director->mainLoop();glview->pollEvents();}else{Sleep(0);}}// Director should still do a cleanup if the window was closed manually.if (glview->isOpenGLReady()){// 结束,执行清理工作director->end();director->mainLoop();director = nullptr;}glview->release();return true;
}

(1-2-1) 我们进入到AppDelegate::applicationDidFinishLaunching(),看它究竟做了什么,我们以/cocos2d-x-3.2/templates/cpp-template-default/Classes/AppDelegate.cpp为例:

bool AppDelegate::applicationDidFinishLaunching() {// initialize directorauto director = Director::getInstance();auto glview = director->getOpenGLView();if(!glview) {// 创建glview对象, 这里采用默认的分辨率先创建出游戏窗口glview = GLView::create("My Game");// 这里设置了和OpenGL相关的一些信息director->setOpenGLView(glview);}// turn on display FPSdirector->setDisplayStats(true);// set FPS. the default value is 1.0/60 if you don't call thisdirector->setAnimationInterval(1.0 / 60);// create a scene. it's an autorelease object    // 创建场景auto scene = HelloWorld::createScene();// run 运行场景director->runWithScene(scene);return true;
}

(1-2-1-1) 可以看到applicationDidFinishLaunching函数里面设置了glview对象之后,就开始运行场景,可以进入GLView::create中看其究竟是如何创建GLView对象,同样,我们是win32下面看的, 所以找到cocos2d-x-3.2/cocos/platform/desktop/CCGLView.cpp文件:

GLView* GLView::create(const std::string& viewName)
{auto ret = new GLView;if(ret && ret->initWithRect(viewName, Rect(0, 0, 960, 640), 1)) {ret->autorelease();return ret;}return nullptr;
}

从代码可以看到只是简单的new一个GLView对象,我们进入/cocos2d-x-3.2/cocos/platform/desktop/CCGLView.h看一下它究竟是个什么东西:

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.http://www.cocos2d-x.org*/#ifndef __CC_EGLVIEW_DESKTOP_H__
#define __CC_EGLVIEW_DESKTOP_H__#include "base/CCRef.h"
#include "platform/CCCommon.h"
#include "platform/CCGLViewProtocol.h"
#include "glfw3.h"NS_CC_BEGINclass CC_DLL GLView : public GLViewProtocol, public Ref
{
public:static GLView* create(const std::string& viewName);static GLView* createWithRect(const std::string& viewName, Rect size, float frameZoomFactor = 1.0f);static GLView* createWithFullScreen(const std::string& viewName);static GLView* createWithFullScreen(const std::string& viewName, const GLFWvidmode &videoMode, GLFWmonitor *monitor);/**frameZoomFactor for frame. This method is for debugging big resolution (e.g.new ipad) app on desktop.*///void resize(int width, int height);float getFrameZoomFactor();//void centerWindow();virtual void setViewPortInPoints(float x , float y , float w , float h);virtual void setScissorInPoints(float x , float y , float w , float h);bool windowShouldClose();void pollEvents();GLFWwindow* getWindow() const { return _mainWindow; }/* override functions */virtual bool isOpenGLReady() override;// 删除窗口,做窗口清理工作virtual void end() override;// 交换buffervirtual void swapBuffers() override;// 设置窗口大小virtual void setFrameSize(float width, float height) override;// 设置输入法状态virtual void setIMEKeyboardState(bool bOpen) override;/** Set zoom factor for frame. This method is for debugging big resolution (e.g.new ipad) app on desktop.*/void setFrameZoomFactor(float zoomFactor);/** Retina support is disabled by default*  @note This method is only available on Mac.*/void enableRetina(bool enabled);/** Check whether retina display is enabled. */bool isRetinaEnabled() const { return _isRetinaEnabled; };/** Get retina factor */int getRetinaFactor() const { return _retinaFactor; }protected:GLView();virtual ~GLView();bool initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor);bool initWithFullScreen(const std::string& viewName);bool initWithFullscreen(const std::string& viewname, const GLFWvidmode &videoMode, GLFWmonitor *monitor);bool initGlew();void updateFrameSize();// GLFW callbacksvoid onGLFWError(int errorID, const char* errorDesc);void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify);void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y);void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y);void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);void onGLFWCharCallback(GLFWwindow* window, unsigned int character);void onGLFWWindowPosCallback(GLFWwindow* windows, int x, int y);void onGLFWframebuffersize(GLFWwindow* window, int w, int h);void onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height);bool _captured;bool _supportTouch;bool _isInRetinaMonitor;bool _isRetinaEnabled;int  _retinaFactor;  // Should be 1 or 2float _frameZoomFactor;GLFWwindow* _mainWindow;GLFWmonitor* _monitor;float _mouseX;float _mouseY;friend class GLFWEventHandler;private:CC_DISALLOW_COPY_AND_ASSIGN(GLView);
};NS_CC_END   // end of namespace   cocos2d#endif  // end of __CC_EGLVIEW_DESKTOP_H__

GLView继承自GLViewProtocol,我们也进入看一下:

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.http://www.cocos2d-x.org*******************************************************/#ifndef __CCGLVIEWPROTOCOL_H__
#define __CCGLVIEWPROTOCOL_H__#include "base/ccTypes.h"
#include "base/CCEventTouch.h"#include <vector>
// 5种屏幕适配策略
enum class ResolutionPolicy
{    EXACT_FIT,
    NO_BORDER,
    SHOW_ALL,
    FIXED_HEIGHT,
    FIXED_WIDTH,UNKNOWN,
};NS_CC_BEGIN

class CC_DLL GLViewProtocol
{
public:/*** @js ctor*/GLViewProtocol();/*** @js NA* @lua NA*/virtual ~GLViewProtocol();/** Force destroying EGL view, subclass must implement this method. */virtual void end() = 0;/** Get whether opengl render system is ready, subclass must implement this method. */virtual bool isOpenGLReady() = 0;/** Exchanges the front and back buffers, subclass must implement this method. */virtual void swapBuffers() = 0;/** Open or close IME keyboard , subclass must implement this method. */virtual void setIMEKeyboardState(bool open) = 0;/*** Polls input events. Subclass must implement methods if platform* does not provide event callbacks.*/virtual void pollInputEvents();/*** Get the frame size of EGL view.* In general, it returns the screen size since the EGL view is a fullscreen view.*/virtual const Size& getFrameSize() const;/*** Set the frame size of EGL view.*/virtual void setFrameSize(float width, float height);
    // 获取可见区域的原点和大小virtual Size getVisibleSize() const;virtual Vec2 getVisibleOrigin() const;virtual Rect getVisibleRect() const;

//设置设计的size,当需要适配多种设备时,可以用这个函数定义逻辑坐标,cocos2dx会自动将逻辑坐标转化成实际坐标,这样一样的代码可以适配各种设备分辨率virtual void setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy);/** Get design resolution size.*  Default resolution size is the same as 'getFrameSize'.*/virtual const Size&  getDesignResolutionSize() const;/*** Set opengl view port rectangle with points.*/virtual void setViewPortInPoints(float x , float y , float w , float h);/*** Set Scissor rectangle with points.*/virtual void setScissorInPoints(float x , float y , float w , float h);/*** Get whether GL_SCISSOR_TEST is enable*/virtual bool isScissorEnabled();/*** Get the current scissor rectangle*/virtual Rect getScissorRect() const;virtual void setViewName(const std::string& viewname);const std::string& getViewName() const;/** Touch events are handled by default; if you want to customize your handlers, please override these functions: */    // 触摸处理函数,可以重载virtual void handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[]);virtual void handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[]);virtual void handleTouchesEnd(int num, intptr_t ids[], float xs[], float ys[]);virtual void handleTouchesCancel(int num, intptr_t ids[], float xs[], float ys[]);/*** Get the opengl view port rectangle.*/const Rect& getViewPortRect() const;/*** Get scale factor of the horizontal direction.*/float getScaleX() const;/*** Get scale factor of the vertical direction.*/float getScaleY() const;/** returns the current Resolution policy */ResolutionPolicy getResolutionPolicy() const { return _resolutionPolicy; }protected:void updateDesignResolutionSize();void handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, intptr_t ids[], float xs[], float ys[]);// real screen size
    Size _screenSize;// resolution size, it is the size appropriate for the app resources.
    Size _designResolutionSize;// the view port size
    Rect _viewPortRect;// the view namestd::string _viewName;float _scaleX;float _scaleY;ResolutionPolicy _resolutionPolicy;
};// end of platform group
/// @}

NS_CC_END#endif /* __CCGLVIEWPROTOCOL_H__ */

以看到CCEGLView和GLViewProtocol是显示窗口,负责窗口级别的功能管理和实现, 包括:坐标和缩放管理, 画图工具,按键事件;

(1-2-1-2) 创建glview对象之后,导演类Director就把glview设置进游戏,其中包括很多配置信息, 如设置屏幕大小适配相关的函数getDesignResolutionSize, 如下:

void Director::setOpenGLView(GLView *openGLView)
{CCASSERT(openGLView, "opengl view should not be null");if (_openGLView != openGLView){// Configuration. Gather GPU infoConfiguration *conf = Configuration::getInstance();conf->gatherGPUInfo();CCLOG("%s\n",conf->getInfo().c_str());if(_openGLView)_openGLView->release();_openGLView = openGLView;_openGLView->retain();// set size 设置屏幕大小适配相关的函数_winSizeInPoints = _openGLView->getDesignResolutionSize();createStatsLabel();if (_openGLView){setGLDefaultValues();}// 完成初始化_renderer->initGLView();CHECK_GL_ERROR_DEBUG();if (_eventDispatcher){_eventDispatcher->setEnabled(true);}}
}

(1-2-1-2-1) 我们进入initGLView看看它都做了什么初始化工作,找到/cocos2d-x-3.2/cocos/renderer/CCRenderer.cpp:

void Renderer::initGLView()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA_cacheTextureListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){/** listen the event that renderer was recreated on Android/WP8 */this->setupBuffer();});Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_cacheTextureListener, -1);
#endif// 填充索引缓冲setupIndices();setupBuffer();_glViewAssigned = true;
}

(1-2-1-2-1-1) 进入setupIndices如下:

void Renderer::setupIndices()
{for( int i=0; i < VBO_SIZE; i++){    // 计算索引缓冲值_indices[i*6+0] = (GLushort) (i*4+0);_indices[i*6+1] = (GLushort) (i*4+1);_indices[i*6+2] = (GLushort) (i*4+2);_indices[i*6+3] = (GLushort) (i*4+3);_indices[i*6+4] = (GLushort) (i*4+2);_indices[i*6+5] = (GLushort) (i*4+1);}
}

(1-2-1-2-1-2) 进入setupBuffer如下:

void Renderer::setupBuffer()
{// 如果使用VAOif(Configuration::getInstance()->supportsShareableVAO()){// 初始化VAO和VBOsetupVBOAndVAO();}else{// 初始化VBOsetupVBO();}
}

(1-2-1-2-1-2-1) 进入setupVBOAndVAO和setupVBO, 开始调用OpenGL API进行顶点数据指定,具体意义参见基于Cocos2d-x学习OpenGL ES 2.0系列——编写自己的shader(2):

void Renderer::setupVBOAndVAO()
{glGenVertexArrays(1, &_quadVAO);GL::bindVAO(_quadVAO);glGenBuffers(2, &_buffersVBO[0]);glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * VBO_SIZE, _quads, GL_DYNAMIC_DRAW);// vertices
    glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, vertices));// colors
    glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, colors));// tex coords
    glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * VBO_SIZE * 6, _indices, GL_STATIC_DRAW);// Must unbind the VAO before changing the element buffer.GL::bindVAO(0);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);glBindBuffer(GL_ARRAY_BUFFER, 0);CHECK_GL_ERROR_DEBUG();
}void Renderer::setupVBO()
{glGenBuffers(2, &_buffersVBO[0]);mapBuffers();
}

(1-2-2) 在applicationDidFinishLaunching里面创建场景之后,就调用director->mainLoop();开始游戏主循环了.我们进入mainLoop看它做了什么, win32下我们找到cocos2d-x-3.2/cocos/base/CCDirector.cpp:

void DisplayLinkDirector::mainLoop()
{if (_purgeDirectorInNextLoop){_purgeDirectorInNextLoop = false;// 主循环结束,清除工作purgeDirector();}else if (! _invalid){     // 渲染场景drawScene();// release the objects        // 释放对象:内存池里之前通过autorelease加入的对象引用计数减 1.PoolManager::getInstance()->getCurrentPool()->clear();}
}

mainLoop主要完成三个动作:

  1 判断是否需要释放 CCDirector,如果需要,则删除 CCDirector 占用的资源。通常,游戏结束时才会执行这个步骤。  

  2 调用 drawScene()方法,绘制当前场景并进行其他必要的处理。

  3 弹出自动回收池,使得这一帧被放入自动回收池的对象全部释放。

(1-2-2-1) 由此可见,mainLoop()把内存管理以外的操作都交给了 drawScene()方法,因此关键的步骤都在 drawScene()方法之中。下面是 drawScene()方法的实现:

// Draw the Scene
void Director::drawScene()
{// calculate "global" dt    // 计算全局帧间时间差 dt
    calculateDeltaTime();// skip one flame when _deltaTime equal to zero.if(_deltaTime < FLT_EPSILON){return;}if (_openGLView){_openGLView->pollInputEvents();}//tick before glClear: issue #533if (! _paused){// 启动定时器_scheduler->update(_deltaTime);_eventDispatcher->dispatchEvent(_eventAfterUpdate);}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 (_nextScene){ // 如果有,设置下一个场景setNextScene();}
 // 保存原来的模型视图(ModelView)矩阵pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);// draw the sceneif (_runningScene){     // 开始绘制场景_runningScene->visit(_renderer, Mat4::IDENTITY, false); // 事件分发_eventDispatcher->dispatchEvent(_eventAfterVisit);}// draw the notifications nodeif (_notificationNode){        // 处理通知节点_notificationNode->visit(_renderer, Mat4::IDENTITY, false);}if (_displayStats){showStats();}// 开始渲染场景_renderer->render();_eventDispatcher->dispatchEvent(_eventAfterDraw);popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);_totalFrames++;// swap buffers 交换缓冲区if (_openGLView){_openGLView->swapBuffers();}if (_displayStats){calculateMPF();}
}

可以发现drawScene主要用于处理 OpenGL 和一些细节,如计算 FPS、帧间时间差等,这里我们主要进行了以下 3 个操作。
  1 调用了定时调度器的 update 方法,引发定时器事件。
  2 如果场景需要被切换,则调用 setNextScene 方法,在显示场景前切换场景。
  3 调用当前场景的 visit 方法,将当前场景加入渲染队列,并通过render统一渲染。

(1-2-2-1-1) 我们进入到visit方法里面,看它怎样把每一个节点添加到渲染队列, 这里我们找到/cocos2d-x-3.2/cocos/2d/CCNode.cpp:

void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags)
{// quick return if not visible. children won't be drawn.if (!_visible){return;}
// 设置_modelViewTransform矩阵uint32_t flags = processParentFlags(parentTransform, parentFlags);// IMPORTANT:// To ease the migration to v3.0, we still support the Mat4 stack,// but it is deprecated and your code should not rely on itDirector* director = Director::getInstance();director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);int i = 0;if(!_children.empty()){sortAllChildren();// draw children zOrder < 0for( ; i < _children.size(); i++ ){auto node = _children.at(i);if ( node && node->_localZOrder < 0 )node->visit(renderer, _modelViewTransform, flags);elsebreak;}// self drawthis->draw(renderer, _modelViewTransform, flags);for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)(*it)->visit(renderer, _modelViewTransform, flags);}else{this->draw(renderer, _modelViewTransform, flags);}director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);// FIX ME: Why need to set _orderOfArrival to 0??// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920// reset for next frame// _orderOfArrival = 0;
}

(1-2-2-1-1-1) 对节点的所有孩子排序,通过调用draw函数,首先绘制ZOrder<0的节点,在绘制自身,最后绘制ZOrder>0的节点. 我们进入draw看看它做些什么. 注意,visit和draw都是虚函数, 以sprite为例,我们进入到/cocos2d-x-3.2/cocos/2d/CCSprite.cpp:

void Sprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{// Don't do calculate the culling if the transform was not updated_insideBounds = (flags & FLAGS_TRANSFORM_DIRTY) ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;if(_insideBounds){_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, transform);renderer->addCommand(&_quadCommand);// 物理引擎相关绘制边界
#if CC_SPRITE_DEBUG_DRAW_customDebugDrawCommand.init(_globalZOrder);_customDebugDrawCommand.func = CC_CALLBACK_0(Sprite::drawDebugData, this);renderer->addCommand(&_customDebugDrawCommand);
#endif //CC_SPRITE_DEBUG_DRAW}
}

(1-2-2-1-1-1-1) 从代码中可以看出,Sprite的draw函数里面并没有做实际的渲染工作,而是用QuadCommand命令将渲染操作打包,加入到渲染队列里面,在drawscene最后通过调用render()进行统一渲染;我们可以看看_quadCommand.init里面究竟做了什么,找到/cocos2d-x-3.2/cocos/renderer/CCQuadCommand.cpp:

void QuadCommand::init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, ssize_t quadCount, const Mat4 &mv)
{CCASSERT(glProgramState, "Invalid GLProgramState");CCASSERT(glProgramState->getVertexAttribsFlags() == 0, "No custom attributes are supported in QuadCommand");_globalOrder = globalOrder;_quadsCount = quadCount;_quads = quad;// 设置MV矩阵_mv = mv;if( _textureID != textureID || _blendType.src != blendType.src || _blendType.dst != blendType.dst || _glProgramState != glProgramState) {// _textureID = textureID;// _blendType就是我们的BlendFunc混合函数_blendType = blendType;_glProgramState = glProgramState;// 生成材质ID
        generateMaterialID();}
}

(1-2-2-1-1-1-1-1) 我们在进入到generateMaterialID()函数里面看看:

void QuadCommand::generateMaterialID()
{if(_glProgramState->getUniformCount() > 0){_materialID = QuadCommand::MATERIAL_ID_DO_NOT_BATCH;}else{int glProgram = (int)_glProgramState->getGLProgram()->getProgram();int intArray[4] = { glProgram, (int)_textureID, (int)_blendType.src, (int)_blendType.dst};_materialID = XXH32((const void*)intArray, sizeof(intArray), 0);}
}

从这里我们可以看出, 我们的材质ID(_materialID)最终是要由shader(glProgram)、混合类型(_blendType)、纹理ID(_textureID)组成的, 所以这三样东西如果有谁不一样的话,那就无法生成相同的材质ID,也就无法在同一 个批次里进行渲染了。

(1-2-2-1-2) 现在,我们回到(1-2-2-1-1-1)的draw函数, 通过上面将渲染指令初始化之后,就是将打包好的渲染命令添加到渲染队列里面了.这里只需简单调用renderer->addCommand(&_quadCommand);即可. 这样,(1-2-2-1)处的drawscene函数中,visit通过调用派生类节点添加渲染指令到渲染队列的工作已经完成了.接下来要做的就是做实际的渲染工作了.3.x版本与之前版本不同,是在drawscene最后通过调用render()函数进行统一渲染的,我们进入render()看一下,找到cocos2d-x-3.2/cocos/renderer/CCRenderer.cpp:

void Renderer::render()
{//Uncomment this once everything is rendered by new renderer//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//TODO setup camera or MVP_isRendering = true;if (_glViewAssigned){// cleanup_drawnBatches = _drawnVertices = 0;//Process render commands//1. Sort render commands based on IDfor (auto &renderqueue : _renderGroups){renderqueue.sort();}visitRenderQueue(_renderGroups[0]);flush();}clean();_isRendering = false;
}

从代码可以看出,从Cocos2d-x3.0开始,Cocos2d-x引入了新的渲染流程,它不像2.x版本 直接在每一个node中的draw函数中直接调用OpenGL代码进行图形渲染,而是通过各种RenderCommand封装起来,然后添加到一个 CommandQueue队列里面去,而现在draw函数的作用就是在此函数中设置好相对应的RenderCommand参数,然后把此 RenderCommand添加到CommandQueue中。最后在每一帧结束时调用renderer函数进行渲染,在renderer函数中会根据 ID对RenderCommand进行排序,然后才进行渲染。

(1-2-2-1-2-1) 现在我们进入visitRenderQueue函数看看它做了什么动作:

void Renderer::visitRenderQueue(const RenderQueue& queue)
{ssize_t size = queue.size();for (ssize_t index = 0; index < size; ++index){auto command = queue[index];auto commandType = command->getType();    if(RenderCommand::Type::QUAD_COMMAND == commandType){flush3D();auto cmd = static_cast<QuadCommand*>(command);//Batch quads            // 如果Quad数据量超过VBO的大小,那么调用绘制,将缓存的命令全部绘制if(_numQuads + cmd->getQuadCount() > VBO_SIZE){CCASSERT(cmd->getQuadCount()>= 0 && cmd->getQuadCount() < VBO_SIZE, "VBO is not big enough for quad data, please break the quad data down or use customized render command");//Draw batched quads if VBO is full
                drawBatchedQuads();}       // 这个处理主要是把命令存入_batchedQuadCommands中,如果如果Quad数据量超过VBO的大小,那么调用绘制,将缓存的命令全部绘制.       // 如果一直没有超过VBO的大小,drawBatchedQuads绘制函数将在flush被调用时调用.// 将命令缓存起来,先不调用绘制_batchedQuadCommands.push_back(cmd);memcpy(_quads + _numQuads, cmd->getQuads(), sizeof(V3F_C4B_T2F_Quad) * cmd->getQuadCount()); // 通过MV矩阵, 转换成世界坐标convertToWorldCoordinates(_quads + _numQuads, cmd->getQuadCount(), cmd->getModelView());// 记录下四边形数量_numQuads += cmd->getQuadCount();}else if(RenderCommand::Type::GROUP_COMMAND == commandType){flush();int renderQueueID = ((GroupCommand*) command)->getRenderQueueID();visitRenderQueue(_renderGroups[renderQueueID]);}else if(RenderCommand::Type::CUSTOM_COMMAND == commandType){flush();auto cmd = static_cast<CustomCommand*>(command);cmd->execute();}else if(RenderCommand::Type::BATCH_COMMAND == commandType){flush();auto cmd = static_cast<BatchCommand*>(command);cmd->execute();}else if (RenderCommand::Type::MESH_COMMAND == commandType){flush2D();auto cmd = static_cast<MeshCommand*>(command);if (_lastBatchedMeshCommand == nullptr || _lastBatchedMeshCommand->getMaterialID() != cmd->getMaterialID()){flush3D();cmd->preBatchDraw();cmd->batchDraw();_lastBatchedMeshCommand = cmd;}else{cmd->batchDraw();}}else{CCLOGERROR("Unknown commands in renderQueue");}}
}

从代码中,我们看到RenderCommand类型有QUAD_COMMAND,CUSTOM_COMMAND,BATCH_COMMAND,GROUP_COMMAND,MESH_COMMAND五种,OpenGL的API调用是在Renderer::drawBatchedQuads()、BatchCommand::execute()中。通过上面代码的注释,可以看到最常用的QUAD_COMMAND类型的渲染命令的处理过程.

(1-2-2-1-2-1-1) 如果Quad数据量超过VBO的大小(VBO_SIZE = 65536 / 6;), 则会调用drawBatchedQuads进行批量渲染:

void Renderer::drawBatchedQuads()
{//TODO we can improve the draw performance by insert material switching command before hand.int quadsToDraw = 0;int startQuad = 0;//Upload buffer to VBOif(_numQuads <= 0 || _batchedQuadCommands.empty()){return;}// 是否支持VAOif (Configuration::getInstance()->supportsShareableVAO()){//Set VBO data 绑定VBO数据, 激活缓冲区对象glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);// option 1: subdata
//        glBufferSubData(GL_ARRAY_BUFFER, sizeof(_quads[0])*start, sizeof(_quads[0]) * n , &_quads[start] );// option 2: data
//        glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * (n-start), &quads_[start], GL_DYNAMIC_DRAW);// option 3: orphaning + glMapBuffer     // 用数据分配和初始化缓冲区对象glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * (_numQuads), nullptr, GL_DYNAMIC_DRAW);// OPENGL 缓冲区对象(buffer object),允许应用程序显式地指定把哪些数据存储在图形服务器或显存中     // 返回指向缓冲区的指针, 缓冲一经具体使用之后,只需要改变缓冲区的内容,即在glMapBuffer和glUnmapBuffer之间改变数据即可void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);memcpy(buf, _quads, sizeof(_quads[0])* (_numQuads));glUnmapBuffer(GL_ARRAY_BUFFER);// 解除绑定glBindBuffer(GL_ARRAY_BUFFER, 0);//Bind VAO 绑定VAO
        GL::bindVAO(_quadVAO);}else{
#define kQuadSize sizeof(_quads[0].bl)glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _numQuads , _quads, GL_DYNAMIC_DRAW);GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);// verticesglVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, vertices));// colorsglVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors));// tex coordsglVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords));glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);}//Start drawing verties in batchfor(const auto& cmd : _batchedQuadCommands){auto newMaterialID = cmd->getMaterialID();if(_lastMaterialID != newMaterialID || newMaterialID == QuadCommand::MATERIAL_ID_DO_NOT_BATCH){//Draw quadsif(quadsToDraw > 0){// 四边形都可以由2个三角形组合而成,指定6个索引点(画出2个GL_TRIANGLES)glDrawElements(GL_TRIANGLES, (GLsizei) quadsToDraw*6, GL_UNSIGNED_SHORT, (GLvoid*) (startQuad*6*sizeof(_indices[0])) );_drawnBatches++;_drawnVertices += quadsToDraw*6;startQuad += quadsToDraw;quadsToDraw = 0;}//Use new materialcmd->useMaterial();_lastMaterialID = newMaterialID;}quadsToDraw += cmd->getQuadCount();}//Draw any remaining quadif(quadsToDraw > 0){ // 画剩下的四边形glDrawElements(GL_TRIANGLES, (GLsizei) quadsToDraw*6, GL_UNSIGNED_SHORT, (GLvoid*) (startQuad*6*sizeof(_indices[0])) );_drawnBatches++;_drawnVertices += quadsToDraw*6;}if (Configuration::getInstance()->supportsShareableVAO()){//Unbind VAO 接除绑定VAOGL::bindVAO(0);}else{glBindBuffer(GL_ARRAY_BUFFER, 0);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);}_batchedQuadCommands.clear();_numQuads = 0;
}

附注:5种渲染类型:

1. QUAD_COMMAND:QuadCommand类绘制精灵等。所有绘制图片的命令都会调用到这里,处理这个类型命令的代码就是绘制贴图的openGL代码。
2. CUSTOM_COMMAND:CustomCommand类自定义绘制,自己定义绘制函数,在调用绘制时只需调用已经传进来的回调函数就可以,裁剪节点,绘制图形节点都采用这个绘制,把绘制函数定义在自己的类里。这种类型的绘制命令不会在处理命令的时候调用任何一句openGL代码,而是调用你写好并设置给func的绘制函数。
3. BATCH_COMMAND:BatchCommand类批处理绘制,批处理精灵和粒子,其实它类似于自定义绘制,也不会再render函数中出现任何一句openGL函数。
4. GROUP_COMMAND:GroupCommand类绘制组,一个节点包括两个以上绘制命令的时候,把这个绘制命令存储到另外一个_renderGroups中的元素中,并把这个元素的指针作为一个节点存储到_renderGroups[0]中。

5. MESH_COMMAND :

转载于:https://www.cnblogs.com/yyxt/p/5514412.html

cocos2d-x游戏引擎核心(3.x)----启动渲染流程相关推荐

  1. Cocos2d手机游戏引擎介绍

    Cocos2d 是一个 Python 用来开发2D 游戏和其他图形化交互应用的框架. 主要特性 界面流程控制: 非常容易地管理不同场景(scenes)之间的流程控制 精灵: 快速而方便的精灵 动作: ...

  2. opengl游戏引擎源码_跨平台渲染引擎之路:拨云见日

    前言 最近在工作中越来越多地接触到一些3D以及相比常见特性更酷炫的效果,因此萌发了想要自己从0开始打造一个渲染引擎的念头,一方面是为了更好地实现公司业务的需求,另一方面则是可以学到整个渲染流水线上的方 ...

  3. GPU、GPU驱动、OpenGL、游戏引擎之间的关系

    游戏引擎使用OpenGL渲染,OpenGL调用显卡驱动告诉GPU干活,GPU经过各种计算把最终的图像渲染出来. 游戏引擎:用于制作游戏或其它实时交互交互图形程序的软件,大多数游戏引擎都包含了渲染.UI ...

  4. 游戏开发技术——游戏引擎

    游戏开发技术--游戏引擎 是什么:游戏引擎是指一些已编写好的可编辑电脑游戏系统或者一些交互式实时图像应用程序的核心组件.这些系统为游戏设计者提供各种编写游戏所需的各种工具,其目的在于让游戏设计者能容易 ...

  5. 什么是 游戏引擎 ?各个主流引擎的区别

    在之前的元宇宙中有提及到游戏主播这个行业,那对应的就会有游戏开发者,对于开发最快捷的方式当然是使用游戏引擎进行开发了呀,那接下来我们就来说说这方面的内容吧 ! 本篇文章主要讲解,游戏开发中常用的5个游 ...

  6. 更高更快更强!“游戏引擎”发展漫谈

    作者:紫竹_Mydrivers [1.导读] 游戏引擎(Game Engine)是什么?大多数人给出的解释都是基于engine的英文翻译,将游戏引擎等同于汽车中的引擎(发动机),再好一些的解释成动力, ...

  7. 更高更快更强 游戏引擎发展漫谈

    [1.导读] 游戏引擎(Game Engine)是什么?大多数人给出的解释都是基于engine的英文翻译,将游戏引擎等同于汽车中的引擎(发动机),再好一些的解释成动力,这些解释都对,但是动力只说并不全 ...

  8. 【转载】游戏引擎发展史漫谈(资料整理)

    游戏引擎发展史漫谈 导读 游戏引擎(Game Engine)是什么?大多数人给出的解释都是基于engine的英文翻译,将游戏引擎等同于汽车中的引擎(发动机),再好一些的解释成动力,这些解释都对,但是动 ...

  9. 虚幻4引擎将至!从虚幻看游戏引擎发展

    3D游戏引擎是个什么玩意 泡泡网显卡频道5月23日  在某游戏中的一个场景中,玩家控制的角色躲藏在屋子里,敌人正在屋子外面搜索玩家.突然,玩家控制的是一个穿迷彩服的士兵,突然碰倒了桌子上的一个杯子,杯 ...

最新文章

  1. 【技术干货】如何进行低功耗设计和开发
  2. 投票系统web服务器,创建一个Web投票系统
  3. spark sql基本使用方法介绍(转载)
  4. linux欢迎信息打印本机ip,Linux shell 登录显示欢迎信息或机器信息(示例代码)
  5. django python3.6_Django+mysql+python3.6.5 Windows
  6. Android App 优化之 ANR 详解
  7. 计算机教师研修计划书,教师信息技术个人研修计划书
  8. Python 字典推导式 - Python零基础入门教程
  9. Mybatis(17)注解方式增删改查单表
  10. 知识蒸馏 | 知识回顾
  11. OmniConverter: Mac上的最简单好用的免费全能音视频转换器
  12. 计算机总线相关知识,计算机包括哪几种总线?
  13. SQL SERVER提示'Modify' 附近有语法错误
  14. 【爆牙游记】黄山归来不看岳-走进新时代。
  15. 关于matlab 画水平线
  16. 双鱼板mind+环境下设置
  17. 【纪中集训2019.3.30】星辰大海
  18. sklearn.metrics中的评估方法(MAE\MSE)
  19. 法制日报:要在法律上“悉心关照”百度
  20. 计算机录制语音所必需的硬件,录音电脑配置方面的硬件要求

热门文章

  1. SAP System Copy Guide, ECC 6.0, Oracle
  2. for循环 消耗时间计算
  3. Node.js之readline模块的使用
  4. centos平台cms系统
  5. 启动项目的时候,一直打印日志
  6. java 二进制,八进制,十进制,十六进制间相互转换的方法
  7. linux下ssh登录PIX防火墙
  8. SpringCloud 介绍
  9. concurrent(六)同步辅助器CyclicBarrier 源码分析
  10. oracle数据库访问order by不起作用分析