(原创)cocos2d-x 3.0 示例代码分析1:AppDelegate

星月最近在用3.0做类似刀塔游戏,第一次用3.0,之前一直只是查查资料,最近发现做一些特定行为需要对3.0一些新的特性了解。所以趁这个机会,把3.0的测试代码过一遍,同时增加注释,希望能对大家有帮助~

因为项目原因,所以不定期更新~~(小白:借口,继续找借口!)

 

星月倾心贡献~~~

 

// AppDelegate.cpp


/****************************************************************************
 Copyright (c) 2013      cocos2d-x.org
 Copyright (c) 2013-2014 Chukong Technologies Inc.

 http://www.cocos2d-x.org

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#include "AppDelegate.h"

#include "cocos2d.h"
#include "controller.h"
#include "cocostudio/CocoStudio.h"
#include "extensions/cocos-ext.h"

USING_NS_CC;

AppDelegate::AppDelegate()
:_curTest(nullptr)
{
}
// 
AppDelegate::~AppDelegate()
{
//    SimpleAudioEngine::end();
    // 释放armature
    cocostudio::ArmatureDataManager::destroyInstance();
}

// 程序初始化函数 
bool AppDelegate::applicationDidFinishLaunching()
{
    // As an example, load config file
    // XXX: This should be loaded before the Director is initialized,
    // XXX: but at this point, the director is already initialized
    Configuration::getInstance()->loadConfigFile("configs/config-example.plist");

    // initialize director
    // 取得设备 
    auto director = Director::getInstance();
    // 取得OpenGL窗口
    auto glview = director->getOpenGLView();
    if(!glview) {
        // 如果为空,则创建以"Cpp Tests"为窗口标题的窗口。
        glview = GLView::create("Cpp Tests");
        // 设置设备使用的窗口
        director->setOpenGLView(glview);
    }
    
    // 启用 FPS 显示
    director->setDisplayStats(true);
    
    // 设置 FPS 上限。如果不加设置,则默认 FPS 上限为 60
    director->setAnimationInterval(1.0 / 60);
    
    // 获取屏幕尺寸
    auto screenSize = glview->getFrameSize();

    auto designSize = Size(480, 320);

    auto fileUtils = FileUtils::getInstance();
    std::vector<std::string> searchPaths;
    
    // 这里是实现的重点,比较屏幕的高和设定的适配尺寸的高,选择合适的图片
    // 然后将对应图片的路径添加到搜索路径中,那么cocos2d-x就会到该目录去寻找图片
    if (screenSize.height > 320)
    {
        auto resourceSize = Size(960, 640);
        searchPaths.push_back("hd");
        searchPaths.push_back("hd/scenetest");
        searchPaths.push_back("hd/scenetest/ArmatureComponentTest");
        searchPaths.push_back("hd/scenetest/AttributeComponentTest");
        searchPaths.push_back("hd/scenetest/BackgroundComponentTest");
        searchPaths.push_back("hd/scenetest/EffectComponentTest");
        searchPaths.push_back("hd/scenetest/LoadSceneEdtiorFileTest");
        searchPaths.push_back("hd/scenetest/ParticleComponentTest");
        searchPaths.push_back("hd/scenetest/SpriteComponentTest");
        searchPaths.push_back("hd/scenetest/TmxMapComponentTest");
        searchPaths.push_back("hd/scenetest/UIComponentTest");
        searchPaths.push_back("hd/scenetest/TriggerTest");
        director->setContentScaleFactor(resourceSize.height/designSize.height);
    }
    else
    {
        searchPaths.push_back("scenetest/ArmatureComponentTest");
        searchPaths.push_back("scenetest/AttributeComponentTest");
        searchPaths.push_back("scenetest/BackgroundComponentTest");
        searchPaths.push_back("scenetest/EffectComponentTest");
        searchPaths.push_back("scenetest/LoadSceneEdtiorFileTest");
        searchPaths.push_back("scenetest/ParticleComponentTest");
        searchPaths.push_back("scenetest/SpriteComponentTest");
        searchPaths.push_back("scenetest/TmxMapComponentTest");
        searchPaths.push_back("scenetest/UIComponentTest");
        searchPaths.push_back("scenetest/TriggerTest");
    }
    
    fileUtils->setSearchPaths(searchPaths);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    // a bug in DirectX 11 level9-x on the device prevents ResolutionPolicy::NO_BORDER from working correctly
    // 应用程序可见,同时保持原始应用程序的宽高比
    glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::SHOW_ALL); 
#else
    // 铺满全屏,但是可能会失真,不保持原始应用程序的宽高比
    glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);
#endif

     // 创建一个场景,场景是一个 autorelease 对象
    auto scene = Scene::create();
    
    // 创建图层
    auto layer = new TestController();
    
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
    layer->addConsoleAutoTest();
#endif

    layer->autorelease();

    // 将图层加入到场景中
    scene->addChild(layer);
    
    // 运行场景
    director->runWithScene(scene);

    // Enable Remote Console
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
    // 参考资料http://www.tuicool.com/articles/yauuueM
    // 在Cocos2d-x 3.0 版新增了Console模块,可实现远程调试。在PC上连接5678端口,即可连接应用程序,输入对应的命令进行调试功能.
    auto console = director->getConsole();
    console->listenOnTCP(5678);
#endif

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
    Director::getInstance()->stopAnimation();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
    Director::getInstance()->startAnimation();
}

// 设置现在正在运行的测试例
void AppDelegate::setCurrentTest(BaseTest* curTest)
{
    _curTest = curTest;
}

// 获得现在正在运行的测试例
BaseTest* AppDelegate::getCurrentTest()
{
    return _curTest;
}


// (注:星月的英语水平有限,所以可能有错误的敌方,请大家谅解)(小白:你英语过四级了吗...)

 

AppDelegate到这里结束,敬请期待下一章节^_^(小白:期待毛线!@!)

作者使用 cocos2d-x 3.0 示例代码分析,未经作者允许,请勿转载!在此谢谢各位手下留情~~~

本文没有获得作者本人同意,不得转载,否则必追究相关责任。转载请注明出处!!~~

原文地址:http://www.cnblogs.com/wodehao0808/p/4019587.html

posted @ 2014-10-11 18:26  星月相随  阅读(870)  评论(0编辑  收藏  举报