环境: 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就会消失,再输入就会显示出来。