cocos2d-x 3.x 之Console模块

环境: cocos3.10 Mac

注: 从个人博客园移植而来


简介

在看cocos2dx cpp-test项目中,无意之下发现了这样的一段代码:

bool AppDelegate::applicationDidFinishLaunching()
{
    // Enable Remote Console
    auto console = director->getConsole();
    console->listenOnTCP(5678);
}

该接口是Cocos3.x之后新增模块Console,可实现通过命令进行调试相关。 我们可以运行下cocos的cpp-test项目,然后打开终端命令,输入:

nc localhost 5678(Enter)
help(Enter)

会出现类似如下所示:
请添加图片描述

我们输入命令:

fps off

项目中的fps就会被关闭,如果输入命令: fps on就会重新打开。如上相关就是目前Console支持的命令相关。

类似于version,fps等命令的实现主要通过类Console接口实现。

// base/CCConsole.cpp
Console::Console()
: _commandSeparator(DEFAULT_COMMAND_SEPARATOR)
, _listenfd(-1)
, _running(false)
, _endThread(false)
, _isIpv6Server(false)
, _sendDebugStrings(false)
, _bindAddress("")
{
		// 
    createCommandFps();
    createCommandVersion();
}

// FPS 
void Console::createCommandFps()
{
    addCommand({"fps", "Turn on / off the FPS. Args: [-h | help | on | off | ]", CC_CALLBACK_2(Console::commandFps, this)});
    addSubCommand("fps", {"on", "Display the FPS on the bottom-left corner.", CC_CALLBACK_2(Console::commandFpsSubCommandOnOff, this)});
    addSubCommand("fps", {"off", "Hide the FPS on the bottom-left corner.", CC_CALLBACK_2(Console::commandFpsSubCommandOnOff, this)});
}

void Console::commandFpsSubCommandOnOff(int /*fd*/, const std::string& args)
{
    bool state = (args.compare("on") == 0);
    Director *dir = Director::getInstance();
    Scheduler *sched = dir->getScheduler();
    sched->performFunctionInCocosThread( std::bind(&Director::setDisplayStats, dir, state));
}

// version
void Console::createCommandVersion()
{
    addCommand({"version", "print version string ", CC_CALLBACK_2(Console::commandVersion, this)});
}

void Console::commandVersion(int fd, const std::string& /*args*/)
{
    Console::Utility::mydprintf(fd, "%s\n", cocos2dVersion());
}

示例

我们可以通过Console来增加新的调试命令相关, 以控制页面的显示隐藏等。新创建CppTest项目。

HelloWorldScene.h中:

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    virtual bool init();
    void menuCloseCallback(cocos2d::Ref* pSender);
  
private:
    // 标题命令相关
    void setTitleCommand(int fd, const std::string& args);
    // 图片命令相关
    void setSpriteCommond(int fd, const std::string& args);
    cocos2d::Label *m_title;
    cocos2d::Sprite *m_Sprite;
    
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

HelloWorldScene.cpp中:

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    // title
    m_title = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
    m_title->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - m_title->getContentSize().height));
    this->addChild(m_title, 1);

    // sprite
    m_Sprite = Sprite::create("HelloWorld.png");
    m_Sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(m_Sprite, 0);
    
    // AddConsole Test =======================
    Console *console = Director::getInstance()->getConsole();
    console->listenOnTCP(9999);
    
    static Console::Command commands[] = {
        {"Debug_Title", "set Title show/hide", CC_CALLBACK_2(HelloWorld::setTitleCommand, this)},
        {"Debug_Sprite", "set Sprite show/hide", CC_CALLBACK_2(HelloWorld::setSpriteCommond, this)},
    };
    for (int i = 0; i < sizeof(commands)/sizeof(commands[0]); ++i)
    {
        console->addCommand(commands[i]);
    }

    return true;
}

void HelloWorld::setTitleCommand(int fd, const std::string& args)
{
    CCLOG("setTitleCommand fd:%d", fd);
    CCLOG("setTitleCommand args: %s", args.c_str());
    bool isShow = m_title->isVisible();
    m_title->setVisible(!isShow);
}

void HelloWorld::setSpriteCommond(int fd, const std::string& args)
{
    bool isShow = m_Sprite->isVisible();
    m_Sprite->setVisible(!isShow);
}

项目成功运行后, 打开终端,输入命令:

nc localhost 9999
help

如下图所示:

请添加图片描述

输入命令Debug_Sprite就会消失,再输入就会显示出来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鹤九日

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值