cocos2d-x的Helloworld程序运行流程(win32)

前端之家收集整理的这篇文章主要介绍了cocos2d-x的Helloworld程序运行流程(win32)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

自学cocos2d难免有错误的理解,作为参考吧

创建好Helloworld工程后,进入VS2012可以看到main主函数

首先程序从主函数运行,代码如下:

#include "main.h"@H_403_12@
#include "AppDelegate.h"@H_403_12@
#include "cocos2d.h"@H_403_12@

USING_NS_CC;

int@H_403_12@ APIENTRY @H_403_12@_tWinMain(HINSTANCE hInstance, HINSTANCE @H_403_12@hPrevInstance, LPTSTR @H_403_12@   lpCmdLine,int@H_403_12@       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // create the application instance@H_403_12@
    AppDelegate app; //(第一步)创建对象@H_403_12@
    return@H_403_12@ Application::getInstance()->run();
}

创建完对象后的下一步才是最关键的。
(第二步)调用Application类里的getInstance()方法,这步我看了下源码基本上就是用一个指针指向app对象,然后通过该指针(第三步)调用run()方法
进入run()方法

int Application::run@H_403_12@()
{
    PVRFrameEnableControlWindow(false@H_403_12@);

    // Main message loop:@H_403_12@
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nLast;
    LARGE_INTEGER nNow;

    QueryPerformanceFrequency(&@H_403_12@nFreq);
    QueryPerformanceCounter(&@H_403_12@nLast);

    initGLContextAttrs();

    // Initialize instance and cocos2d.@H_403_12@
    if@H_403_12@ (!@H_403_12@applicationDidFinishLaunching())//(第四步)从这里进入AppDelegate类中初始化操作@H_403_12@
    {
        return@H_403_12@ 0@H_403_12@;
    }

    auto director =@H_403_12@ Director::getInstance@H_403_12@();
    auto glview =@H_403_12@ director->@H_403_12@getOpenGLView();

    // Retain glview to avoid glview being released in the while loop@H_403_12@
    glview->@H_403_12@retain();
    //(第十五步)游戏循环,如果没有触发停止操作会一直循环@H_403_12@
    while@H_403_12@(!@H_403_12@glview->@H_403_12@windowShouldClose())
    {
        QueryPerformanceCounter(&@H_403_12@nNow);
        if@H_403_12@ (nNow.@H_403_12@QuadPart -@H_403_12@ nLast.@H_403_12@QuadPart >@H_403_12@ _animationInterval.@H_403_12@QuadPart)
        {
            nLast.@H_403_12@QuadPart =@H_403_12@ nNow.@H_403_12@QuadPart -@H_403_12@ (nNow.@H_403_12@QuadPart %@H_403_12@ _animationInterval.@H_403_12@QuadPart);

            director->@H_403_12@mainLoop();
            glview->@H_403_12@pollEvents();
        }
        else@H_403_12@
        {
            Sleep(1@H_403_12@);
        }
    }

    // Director should still do a cleanup if the window was closed manually.@H_403_12@
    if@H_403_12@ (glview->@H_403_12@isOpenGLReady())
    {
        director->@H_403_12@end();
        director->@H_403_12@mainLoop();
        director =@H_403_12@ nullptr;
    }
    glview->@H_403_12@release();
    return@H_403_12@ true@H_403_12@;
}

在AppDelegate.cpp中查看applicationDidFinishLaunching()

bool AppDelegate::applicationDidFinishLaunching@H_403_12@() {
    // 初始化导演@H_403_12@
    auto director =@H_403_12@ Director::getInstance@H_403_12@();//(第五步)进入系统默认的init()初始化@H_403_12@
    auto glview =@H_403_12@ director->@H_403_12@getOpenGLView();//(第八步)创建OpenGL的对象@H_403_12@
    if@H_403_12@(!@H_403_12@glview) {
        glview =@H_403_12@ GLViewImpl::create@H_403_12@("My Game"@H_403_12@);
        director->@H_403_12@setOpenGLView(glview);
    }

    // (第九步)设置FPS的显示与否@H_403_12@
    director->@H_403_12@setDisplayStats(true@H_403_12@);

    // (第十步)设置FPS值@H_403_12@
    director->@H_403_12@setAnimationInterval(1.0@H_403_12@ /@H_403_12@ 60@H_403_12@);

    // (第十一步)创建一个背景,这里面有用户自己写的init()方法初始化@H_403_12@
    auto scene =@H_403_12@ HelloWorld::createScene@H_403_12@();

    // 运行@H_403_12@
    director->@H_403_12@runWithScene(scene);

    return@H_403_12@ true@H_403_12@;
}

第一句是创建导演对象以及初始化他,但是这个初始化init()不是Helloworld.cpp里面的init(),可以查看源码

Director*@H_403_12@ Director::getInstance@H_403_12@()
{
    if@H_403_12@ (!@H_403_12@s_SharedDirector)
    {
        s_SharedDirector =@H_403_12@ new@H_403_12@ (std::nothrow@H_403_12@) DisplayLinkDirector();
        CCASSERT(s_SharedDirector,"FATAL: Not enough memory"@H_403_12@);
        s_SharedDirector->@H_403_12@init();//(第六步)在这里做初始化操作@H_403_12@
    }

    return@H_403_12@ s_SharedDirector;
}

查看auto scene = HelloWorld::createScene();所用方法代码,可以看见

Scene*@H_403_12@ HelloWorld::createScene@H_403_12@()
{
    // 'scene' is an autorelease object@H_403_12@
    auto scene =@H_403_12@ Scene::create@H_403_12@();      //(第十二步)这里是调用Scene的init()方法@H_403_12@

    // 'layer' is an autorelease object@H_403_12@
    auto layer =@H_403_12@ HelloWorld::create@H_403_12@();  //(第十三步)这里是调用了Helloworld的init()方法@H_403_12@

    //(第十四步)将图层加到背景上@H_403_12@
    scene->@H_403_12@addChild(layer);

    // return the scene@H_403_12@
    return@H_403_12@ scene;
}

调用create()方法后程序会调用各自的init()方法

注意:第一次写博客语言表达肯能不太清晰,编写工具运用不太熟悉,谅解。

猜你在找的Cocos2d-x相关文章