zoukankan      html  css  js  c++  java
  • cocos2dx一种简单的滚动文本日志

    在手机上调试,不能所见所得日志,这个是一个比较麻烦的事,于是想到了,用多行文本做日志。
    下面是日志的代码。从CCLabelTTF派生。这里主要提供了VLog和Log两个日志输出函数。
    定义如下
    void VLog(const char * paramFormat, va_list param_argptr);
    void Log(const char * paramFormat, ...);
    其中VLog主要应用于已经有va_list的情况下。

    #ifndef _X_LOG_VIEW_H_
    #define _X_LOG_VIEW_H_
    #include <cocos2d.h>
    #include <vector>
    #include <cstring>
    #include <cstdarg>
    USING_NS_CC;
    namespace zdh
    {
        using std::vector;
        using std::string;
        ///滚动文本日志
        /*
            这是一个模板类,模板参数N主要是设置一行日志缓冲的字节数。
        
    */
        template<int N = 128>
        class XLogView : public CCLabelTTF
        {
        public:
            XLogView()
                :m_LogMaxLine(10)
            {
            }
            //一组create,直接从CCLabelTTF复制过来
            static XLogView * create(const char *stringconst char *fontName, float fontSize)
            {
                return XLogView::create(string, fontName, fontSize,    CCSizeZero, kCCTextAlignmentCenter, kCCVerticalTextAlignmentTop);
            }

            static XLogView * create(const char *stringconst char *fontName, float fontSize, const CCSize& dimensions, CCTextAlignment hAlignment)
            {
                return XLogView::create(string, fontName, fontSize, dimensions, hAlignment, kCCVerticalTextAlignmentTop);
            }
            static XLogView * create(const char *stringconst char *fontName, float fontSize, const CCSize& dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment)
            {
                XLogView *pRet = new XLogView();
                if (pRet && pRet->initWithString(string, fontName, fontSize, dimensions, hAlignment, vAlignment))
                {
                    pRet->autorelease();
                    return pRet;
                }
                CC_SAFE_DELETE(pRet);
                return NULL;
            }
            static XLogView * createWithFontDefinition(const char *string, ccFontDefinition &textDefinition)
            {
                XLogView *pRet = new XLogView();
                if (pRet && pRet->initWithStringAndTextDefinition(string, textDefinition))
                {
                    pRet->autorelease();
                    return pRet;
                }
                CC_SAFE_DELETE(pRet);
                return NULL;
            }
            //设置滚动日志的最大行数
            void setLogMaxLine(int param_max_line)
            {
                if (param_max_line < 1)
                {
                    m_LogMaxLine = 1;
                }
                else if (param_max_line > 1024)
                {
                    m_LogMaxLine = 1024;
                }
                else m_LogMaxLine = param_max_line
            }
            //取滚动日志的最大行数
            int getLogMaxLine() const
            {
                return m_LogMaxLine;
            }
            //清除日志
            void Clear()
            {
                m_LogList.clear();
                setString("");
            }
            //生成日志
            void VLog(const char * paramFormat, va_list param_argptr)
            {
                char sTemp[N];
                vsnprintf(sTemp, sizeof(sTemp), paramFormat, param_argptr);
                //删除超出行数的日志
                while ((int)m_LogList.size() > m_LogMaxLine)
                {
                    m_LogList.erase(m_LogList.begin());
                }

                m_LogList.push_back(sTemp);
                //生成日志
                m_LogTemp = "";
                size_t iCount = m_LogList.size();
                for (size_t i = 0; i < iCount; i++)
                {
                    if (m_LogTemp.length() >0) m_LogTemp += " ";
                    m_LogTemp += m_LogList[i];
                }
                setString(m_LogTemp.c_str());
                //输出到控制台窗口
                #ifdef _MSC_VER
                    OutputDebugStringA(sTemp);
                #endif
            }
            void Log(const char * paramFormat, )
            {
                va_list argptr;
                va_start(argptr, paramFormat);
                VLog(sTemp, sizeof(sTemp), paramFormat, argptr);
                va_end(argptr);
            }
        private:
            string m_LogTemp;                //生成日志的临时变量
            int m_LogMaxLine;                //日志最大的行数
            vector<string> m_LogList;       //日志内容
        };
    }
    #endif
    在Scene里面,初始化的时候,调下面的函数void HelloWorld::InitLog()
    {
        auto visibleSize = CCDirector::sharedDirector()->getVisibleSize();
        auto pLog = THelloWorldLogView::create("", "宋体", 12, CCSizeMake(200,200), kCCTextAlignmentLeft);
        if (pLog != nullptr)
        {
            pLog->setAnchorPoint(ccp(0.5, 1));
            pLog->setPosition(ccp(visibleSize.width - 200, visibleSize.height - 10));
            this->addChild(pLog);
        }
        m_Log = pLog;
    }
    然后定义m_log 指针和一个PrintLog方法void HelloWorld::PrintLog(const char * paramFormat, )
    {
        if (m_Log != NULL)
        {
            va_list argptr;
            va_start(argptr, paramFormat);
            m_Log->VLog(paramFormat, argptr);
            va_end(argptr);
        }
    }
    这样,就可以在代码中,输出日志了
     
    原文链接:http://www.cppblog.com/zdhsoft/archive/2014/03/16/cocos2dxlogview.html
  • 相关阅读:
    IDEA快捷键
    nginx之epoll模型的详细介绍
    Liunx权限修改命令
    小技巧3
    小技巧2
    小技巧1
    Ajax的简单使用
    dubbo
    快速创建虚拟机
    登录和注册功能的实现
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/3617179.html
Copyright © 2011-2022 走看看