cocos2dx 3.2 HelloWorld简析

我们知道所有的C++程序都是从main函数开始运行的,cocos2dx也一样。打开VS解决方案中的win32目录下的main.cpp类。

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();
}

发现除了一些宏定义外,就实例化了个AppDelegate的对象app和运行单例Application的run方法,接着追踪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.
    if (!applicationDidFinishLaunching())
    {
        return 0;
    }

    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;

            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;
}

这一步大概是做了实例化cocos2d的实例,接着渲染。来看这一行 if (!applicationDidFinishLaunching()),执行的是Application的子类AppDelegate的虚函数applicationDidFinishLaunching。

bool AppDelegate::applicationDidFinishLaunching() {
    // 初始化导演类
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLView::create("My Game");
        director->setOpenGLView(glview);
    }

    // 开启显示FPS的信息
    director->setDisplayStats(true);

    // 设置帧率FPS
    director->setAnimationInterval(1.0 / 60);

    // 创建一个场景
    auto scene = HelloWorld::createScene();

    // 用导演类的单例来启动场景
    director->runWithScene(scene);

    director->getActionManager();

    return true;
}

其中通过HelloWorld::createScene()的方法来创建一个场景,然后用导演类的单例来启动该场景。再回到Application::run()里面来执行OpenGL的渲染。来看看HelloWorld::createScene()。

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

再来看HelloWorld的头文件的这一句class HelloWorld : public cocos2d::Layer。HelloWorld继承于Layer,所以要用Scene类的create方法来创建一个场景,把然后HelloWorld的create出来的layer层添加到scene场景中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值