微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cocos2d-x 3.6 程序流程

  1. main.cpp
#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. run 方法:
int Application::run()
{
    PVRFrameEnableControlWindow(false);

    // Main message loop:
    LARGE_INTEGER nLast;
    LARGE_INTEGER nNow;

    QueryPerformanceCounter(&nLast);

    initGLContextAttrs();

    // Initialize instance and cocos2d.
    if (!applicationDidFinishLaunching())// 初始化,资源适配,屏幕适配,运行第一个场景等代码 
    {
        return 1;
    }

    auto director = Director::getInstance();//导演类对象获取
    auto glview = director->getopenGLView();

    // Retain glview to avoid glview being released in the while loop
    glview->retain();

    while(!glview->windowShouldClose())
    {
        QueryPerformanceCounter(&nNow);
        if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)
        {//控制每一帧所运行的 最小 时间,从而控制游戏的帧率
            nLast.QuadPart = nNow.QuadPart - (nNow.QuadPart % _animationInterval.QuadPart);

            director->mainLoop();//主循环逻辑代码!!!
            glview->pollEvents();
        }
        else
        {
            Sleep(1);
        }
    }
    //程序退出(清理资源)
    // Director should still do a cleanup if the window was closed manually.
    if (glview->isOpenglready())
    {
        director->end();
        director->mainLoop();
        director = nullptr;
    }
    glview->release();
    return 0;
}
  1. applicationDidFinishLaunching 方法
bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getopenGLView();
    if(!glview) {
        glview = GLViewImpl::create("My Game");
        director->setopenGLView(glview);
    }

    // turn on display FPS
    director->setdisplayStats(true);//显示FPS,设为false则关闭显示

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);

    register_all_packages();

    // create a scene. it's an autorelease object
    auto scene = HelloWorld::createScene(); //场景创建

    // run
    director->runWithScene(scene);

    return true;
}
  1. mainLoop 方法
void displayLinkDirector::mainLoop()
{
    if (_purgeDirectorInNextLoop)
    {
        _purgeDirectorInNextLoop = false;
        purgeDirector();
    }
    else if (_restartDirectorInNextLoop)
    {
        _restartDirectorInNextLoop = false;
        restartDirector();
    }
    else if (! _invalid)
    {
        drawScene();// 屏幕绘制及一些相应的逻辑处理

        // release the objects
        PoolManager::getInstance()->getCurrentPool()->clear();
    }
}

原文地址:https://www.jb51.cc/cocos2dx/342294.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐