cocos 2d test-cpp 学习笔记之AppDelegate.cpp 代码分析

前端之家收集整理的这篇文章主要介绍了cocos 2d test-cpp 学习笔记之AppDelegate.cpp 代码分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码源码

“` c++
/******************************************************************
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 namespace cocos2d 也就是 使用命名空间
USING_NS_CC;

AppDelegate::AppDelegate()
:_curTest(nullptr)
{
}

AppDelegate::~AppDelegate() // 析构函数,在类被销毁之前执行
{
// SimpleAudioEngine::end();
cocostudio::ArmatureDataManager::destroyInstance();
//程序在此处被销毁
}

bool AppDelegate::applicationDidFinishLaunching()
{

Configuration::getInstance()->loadConfigFile("configs/config-example.plist");

// 初始化一个导演对象 ,  在cocos 2d中Director 只有一个,《单例模式》
auto director = Director::getInstance();
// 然后通过 导演 获得一个 OpenGL 视图, 这儿使用 [OpenGL] (https://www.opengl.org/) 进行渲染, 学习OpenGl 推荐[《OpenGL编程指南》](http://vdisk.weibo.com/s/BT-B_fs4lQPBs)
auto glview = director->getOpenGLView();
if(!glview) {
// 创建一个 名称为 “Cpp Tests”的 窗口
    glview = GLView::create("Cpp Tests");
    director->setOpenGLView(glview);
}
// 设置是否显示当前的显示 状态, 主要是当前显示的帧率
director->setDisplayStats(true);
// 设置动画的帧率 ,一般设置在 1.0/30 以下, 也就是 30帧每秒及以上 
director->setAnimationInterval(1.0 / 60);

auto screenSize = glview->getFrameSize();

auto designSize = Size(480,320);

auto fileUtils = FileUtils::getInstance();
std::vector<std::string> searchPaths;

if (screenSize.height > 320)
{
    auto resourceSize = Size(960,640);
    searchPaths.push_back("hd");
    searchPaths.push_back("ccs-res/hd");
    searchPaths.push_back("ccs-res/hd/scenetest");
    searchPaths.push_back("ccs-res/hd/scenetest/ArmatureComponentTest");
    searchPaths.push_back("ccs-res/hd/scenetest/AttributeComponentTest");
    searchPaths.push_back("ccs-res/hd/scenetest/BackgroundComponentTest");
    searchPaths.push_back("ccs-res/hd/scenetest/EffectComponentTest");
    searchPaths.push_back("ccs-res/hd/scenetest/LoadSceneEdtiorFileTest");
    searchPaths.push_back("ccs-res/hd/scenetest/ParticleComponentTest");
    searchPaths.push_back("ccs-res/hd/scenetest/SpriteComponentTest");
    searchPaths.push_back("ccs-res/hd/scenetest/TmxMapComponentTest");
    searchPaths.push_back("ccs-res/hd/scenetest/UIComponentTest");
    searchPaths.push_back("ccs-res/hd/scenetest/TriggerTest");
    searchPaths.push_back("ccs-res");
    director->setContentScaleFactor(resourceSize.height/designSize.height);
}
else
{
    searchPaths.push_back("ccs-res");
    searchPaths.push_back("ccs-res/scenetest/ArmatureComponentTest");
    searchPaths.push_back("ccs-res/scenetest/AttributeComponentTest");
    searchPaths.push_back("ccs-res/scenetest/BackgroundComponentTest");
    searchPaths.push_back("ccs-res/scenetest/EffectComponentTest");
    searchPaths.push_back("ccs-res/scenetest/LoadSceneEdtiorFileTest");
    searchPaths.push_back("ccs-res/scenetest/ParticleComponentTest");
    searchPaths.push_back("ccs-res/scenetest/SpriteComponentTest");
    searchPaths.push_back("ccs-res/scenetest/TmxMapComponentTest");
    searchPaths.push_back("ccs-res/scenetest/UIComponentTest");
    searchPaths.push_back("ccs-res/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,ResolutionPolicy::NO_BORDER);
`#endif

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();
layer->addConsoleAutoTest();
scene->addChild(layer);
director->runWithScene(scene);

// Enable Remote Console

#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
auto console = director->getConsole();
console->listenOnTCP(5678);
Configuration *conf = Configuration::getInstance();
bool isAutoRun = conf->getValue("cocos2d.x.testcpp.autorun",Value(false)).asBool();
if(isAutoRun)
{
layer->startAutoRun();
}
#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 构成了整个程序的生命过程 这个与 Android 中的 activity 有很大的相似之处

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