[Cocos2d-x For WP8]Hello world

[Cocos2d-x For WP8]Hello world

Cocos2d-x For WP8使用C++开发,使用cocos2d-xv0.13同样的接口,Cocos2d-x For WP8的相关项目代码可以从下面的网址下载到:

https://github.com/cocos2d-x/cocos2dx-win8/tree/wp8

http://cocos2d-x.googlecode.com/files/cocos2dx-0.13.0-wp8-0.8.zip

打开了项目中的Hello World项目我们可以看到下面的项目结构,这里我把多余的东西删除掉了。

图片:

那么在WP8里面的Cocos2d-x框架其实还是要基于WP8的DirectX进行封装而成的,然后映射到了Cocos2d-x框架相关的接口。

(1)程序的入口

在这个项目里面我们先来关注下cocos2dorig.cpp文件,这里封装了程序的入口相关的信息,main方法就是程序的入口。在这个程序入口里面把原来的WP8的DirectX的初始化方法注释掉了,换成了Cocos2d-x所封装的程序实例AppDelegate类。

//游戏的入口
[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{
    //auto direct3DApplicationSource = ref new Direct3DApplicationSource();
    //CoreApplication::Run(direct3DApplicationSource);
    //创建程序的实例
    AppDelegate App;
    auto frameworkViewSource = ref new CCApplicationFrameworkViewSource();
    Windows::ApplicationModel::Core::CoreApplication::Run(frameworkViewSource);
    
    return 0;
}

ref class CCApplicationFrameworkViewSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource 
{
public:
    virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView()
    {
        //cocos2d::getSharedCCApplicationFrameworkView()封装了WP8 DirectX游戏的初始化逻辑
        return cocos2d::getSharedCCApplicationFrameworkView();
    }
};

(2)程序的实例

那么我们可以把AppDelegate类看作是程序的实例,因为程序的实例初始化,完成加载,程序进入休眠状态和程序回到前台的相关处理方法,类似于WP8应用程序App类的功能。

//程序初始化实例
bool AppDelegate::initInstance()
{
    bool bRet = false;
    do 
    {

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN8_METRO)

    // fix bug: 16bit aligned
    void* buff=_aligned_malloc(sizeof(CCEGLView),16);
    CCEGLView* mainView = new (buff) CCEGLView();
    mainView->Create();
    //mainView->setDesignResolution(480, 320);
    //mainView->setDesignResolution(640, 1066);
    CCLOG("Device Res:%d", m_deviceResolutionInPixels);
    //根据三种分辨率创建主视图
    switch (m_deviceResolutionInPixels) 
    {
    case DeviceResolutionInPixels_WVGA: 
        {
            mainView->setDesignResolution(480, 800);
            break;
        }
    case DeviceResolutionInPixels_720p: 
        {
            mainView->setDesignResolution(720, 1280);
            break;
        }    
    case DeviceResolutionInPixels_WXGA: 
        {
            mainView->setDesignResolution(768, 1280);
            break;
        }
    }
    

#endif // CC_PLATFORM_WIN8_METRO

        bRet = true;
    } while (0);
    return bRet;
}
//程序启动
bool AppDelegate::applicationDidFinishLaunching()
{
    // 初始化导演对象
    CCDirector *pDirector = CCDirector::sharedDirector();

    //设置主视图
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    // turn on display FPS
    //pDirector->setDisplayFPS(false);
    //设置屏幕方向
    pDirector->setDeviceOrientation(CCDeviceOrientationLandscapeLeft);
    // set FPS. the default value is 1.0/60 if you don't call this
    //pDirector->setAnimationInterval(1.0 / 60);

    // 创建scene
    CCScene *pScene = HelloWorld::scene();

    // 用导演对象运行scene run
    pDirector->runWithScene(pScene);

    return true;

}

//进入休眠
void AppDelegate::applicationDidEnterBackground()
{
    CCDirector::sharedDirector()->pause();
}

//从休眠回到程序
void AppDelegate::applicationWillEnterForeground()
{
    CCDirector::sharedDirector()->resume();
}

(3)游戏的界面和逻辑

那么HelloWorld类就是处理我们游戏的界面和逻辑的类了,话说你可以把它当做成是MainPage类。在这里定义了默认的游戏场景。Cocos2d的游戏结构可以简单地概括为场景、层、精灵,而这两个文件就是Hello World场景的实现文件。每个游戏组件都可以添加到另一个组件中,形成层次关系,例如场景中可以包含多个层,层中可以包含多个精灵。

HelloWorldScene中定义了一个HelloWorld类,该类继承自CCLayerColor,CCLayerColor也就是继承自 CCLayer,因此HelloWorld本身是一个层。

在AppDelegate::applicationDidFinishLaunching()方法里面我们看到了调用了HelloWorld::scene()的方法,那么这个就是创建游戏的场景。

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {   // 'scene'是一个可以自动释放的对象
        scene = CCScene::create();
        //创建失败跳出循环
        CC_BREAK_IF(! scene);
        // 'layer'是一个可以自动释放的对象
        HelloWorld *layer = HelloWorld::create();
        //创建失败跳出循环
        CC_BREAK_IF(! layer);
        // 添加layer到scene上面
        scene->addChild(layer);
    } while (0);

    // 返回scene
    return scene;
}

在上面的代码中我们看到了调用了HelloWorld::create()的方法,那么在HelloWorldScene.h头文件里面我们找到了该方法的定义,实际上就是调用了init()方法。

// implement the "static create()" method manually
     CREATE_FUNC(HelloWorld);
/**
 * define a create function for a specific type, such as CCLayer
 * @__TYPE__ class type to add create(), such as CCLayer
 */
#define CREATE_FUNC(__TYPE__) 
static __TYPE__* create() 
{ 
    __TYPE__ *pRet = new __TYPE__(); 
    if (pRet && pRet->init()) 
    { 
        pRet->autorelease(); 
        return pRet; 
    } 
    else 
    { 
        delete pRet; 
        pRet = NULL; 
        return NULL; 
    } 
}

下面我们再来看看init()方法是怎么对游戏进行初始化的。

//在init方法里面初始化当前的实例
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //CCLayer进行初始化,初始化失败跳出循环
        if ( !CCLayer::init() )
        {
            break;
        }
        //获取手机屏幕的大小
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        //创建文字Label
        CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Times New Roman", 24);
        //设置文字Label的位置
        pLabel->setPosition( ccp(size.width * 0.5, size.height * 0.5) );
         //设置文字Label的颜色
        pLabel->setColor(ccc3(160, 80, 5));
        //添加到当前的界面上
        this->addChild(pLabel, 1);
        bRet = true;
    } while (0);
    //返回成功
    return bRet;
}

游戏的运行如下图所示:

原文地址:https://www.cnblogs.com/linzheng/p/3267030.html