zoukankan      html  css  js  c++  java
  • 2 cocos2dx 3.0 源码分析-Director


    Director 导演类, 这个类在整个引擎中担当着最重要的角色, 先看看它是如何初始化的,它共管理了哪些内容呢? 
     
    1初始化- 更新处理Scheduler
     
    Scheduler 这个类负责用户自定义的更新函数的调用, 或游戏对象的update(float dt)的调用。 像经常调用的schedule() 或scheduleUpdate() 就是这个类来负责处理的。
     
    见下面的代码, 就是程序中如何使用schedule。 具体如何处理, 在后面的还会进行展开。 这块可以单拿出一章来介绍一下了。 
     
      1 this->schedule(schedule_selector(HelloWorld::update), 10);  
     
    2初始化- 事件管理 EventDispatcher 
     
    EventDispatcher 这个类负责整个系统的事件处理,如用户自定义事件, 触摸事件,等都由这个类来负责。 
     
    现在我们知道,它是在这里申明即可, 具体在哪里进行调用, 事件的实现原理, 也会在一章内进行展开。 
     
    下面定义了几种不同的事件,为后面相应的事件触发时作为参数调用及触发事件。 
     
     _eventDispatcher = new (std::nothrow) EventDispatcher();
        _eventAfterDraw = new (std::nothrow) EventCustom(EVENT_AFTER_DRAW);
        _eventAfterDraw->setUserData(this);
        _eventAfterVisit = new (std::nothrow) EventCustom(EVENT_AFTER_VISIT);
        _eventAfterVisit->setUserData(this);
        _eventAfterUpdate = new (std::nothrow) EventCustom(EVENT_AFTER_UPDATE);
        _eventAfterUpdate->setUserData(this);
        _eventProjectionChanged = new (std::nothrow) EventCustom(EVENT_PROJECTION_CHANGED);
        _eventProjectionChanged->setUserData(this);
    3初始化纹理缓存TextureCache
     
    TextureCache 纹理缓存,进行纹理处理, 重量级类, 在后面展开。 
     
    initTextureCache();
     
    void Director::initTextureCache()
    {
    #ifdef EMSCRIPTEN
        _textureCache = new (std::nothrow) TextureCacheEmscripten();
    #else
        _textureCache = new (std::nothrow) TextureCache();
    #endif // EMSCRIPTEN
    }
     
    4 初始化矩阵OpenGLES用到initMatrixStack();
     
    矩阵,OpenGLES转换坐标时用到的,一个坐标的计算方式: 投影矩阵 × 视图矩阵 × 模型矩阵 × 3D位置
    1 _modelViewMatrixStack 模型矩阵
    2 _projectionMatrixStack 视图投影矩阵
    3 _textureMatrixStack 纹理矩阵, 这个做什么用的, 我还没深入看, 后面再说。  
     
     _modelViewMatrixStack.push(Mat4::IDENTITY);
     _projectionMatrixStack.push(Mat4::IDENTITY);
     _textureMatrixStack.push(Mat4::IDENTITY);
    5 初始化Render
     
    渲染, 这个渲染有的说, 现在我们知道通过它调用 我们真正的OpenGL 渲染命令。
     
    这个类设计了一个渲染队列RenderQueue(先简化点认为就一个吧),用户的渲染请求, 自定义的也好, 系统的也好, 都被封装成为了渲染命令加入到这个渲染队列队列中。 
     
     这个类设计的也很有意思,做了很多的优化设计。 后面详细说它。 
     
    _renderer = new (std::nothrow) Renderer;
     
    Renderer::Renderer()
    :_lastMaterialID(0)
    ,_lastBatchedMeshCommand(nullptr)
    ,_filledVertex(0)
    ,_filledIndex(0)
    ,_numberQuads(0)
    ,_glViewAssigned(false)
    ,_isRendering(false)
    #if CC_ENABLE_CACHE_TEXTURE_DATA
    ,_cacheTextureListener(nullptr)
    #endif
    {
        _groupCommandManager = new (std::nothrow) GroupCommandManager();
        _commandGroupStack.push(DEFAULT_RENDER_QUEUE);
       
        RenderQueue defaultRenderQueue;
        _renderGroups.push_back(defaultRenderQueue);
        _batchedCommands.reserve(BATCH_QUADCOMMAND_RESEVER_SIZE);
    }
     
     
    总结一下:
     
    可以看到,更新函数处理,事件处理,纹理缓存,渲染都已经在这个类中初始化了,也在这个类对外提供调用,Director还是一个单例类。 而runScene() 是如何处理的呢?后面跟上。 
     
     
  • 相关阅读:
    Block代码块中使用局部变量注意点
    This application's application-identifier entitlement does not match that of the installed application. These values must match for an upgrade to be allowed.
    iOS实现渐变颜色
    linker command failed with exit code 1 (use -v to see invocation)
    关于SVN的405错误Server sent unexpected return value (405 Method Not Allowed)的解决办法
    提交SVN Working copy locked解决
    FSCalendar使用和注意事项
    c/c++左值和右值
    mysql使用笔记(一)
    解决 ssh 登录到ubuntu server 慢的问题
  • 原文地址:https://www.cnblogs.com/liangzhimy/p/4425414.html
Copyright © 2011-2022 走看看