CXCommon.h工具类

#ifndef __XCOMMON_H__
#define __XCOMMON_H__
/************************************************************************/
//XCommon.h 是通用类,提供一些功能
//1.字符串截断
//2.坐标转换
//3.文字乱码处理
// 
/************************************************************************/

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

#include "iconv.h"

#endif //#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

/************************************************************************/
/* 定义  define                                                         */
/************************************************************************/
#define FILENAME_LENGTH     64          //文件名长度
#define FILEDES_LENGTH   256

#define    CHAR_LENGTH 128            //字符串长度


/////////////////////application////////////////////////////////////

/************************************************************************/
/* 常用方法                                                              */
/************************************************************************/

class ENGINE_API CXCommon
{
public:
    static CCRect getNodeRect(CCNode * pNode)
    {
        CCRect rc;
        rc.origin = pNode->getPosition();
        rc.size = pNode->getContentSize();
        rc.origin.x -= rc.size.width / 2;
        rc.origin.y -= rc.size.height / 2;
        return rc;
    }

    //转换坐标
    //锚点为(x,x)是的坐标转换成锚点为(0,0)的坐标
    //例如:把锚点(0.5,0.5)坐标(0,0)转换成
    //锚点(0.5,0.5)坐标(0 - pNode.getContentSize.width * 0.5,0 - pNode.getContentSize.Height * 0.5)
    static void anchorToLocalPos(CCNode* pNode)
    {
        pNode->setPositionX(pNode->getPositionX() + pNode->getContentSize().width * pNode->getAnchorPoint().x);
        pNode->setPositionY(pNode->getPositionY() + pNode->getContentSize().height * pNode->getAnchorPoint().y);
    }
    /***************************
    *函数名称:invariantPosConvertAnchor
    *函数功能:改变锚点,并且让原坐标保持不变
    *函数参数:CCNode* pNode--要改变的节点
               CCPoint tAnchor--新的锚点
    *函数返回值:void
    *备注:
    ****************************/
    static void invariantPosConvertAnchor(CCNode* pNode, CCPoint tAnchor)
    {
        pNode->setPositionX(pNode->getPositionX() + pNode->getContentSize().width * (tAnchor.x - pNode->getAnchorPoint().x));
        pNode->setPositionY(pNode->getPositionY() + pNode->getContentSize().height * (tAnchor.y - pNode->getAnchorPoint().y));
        pNode->setAnchorPoint(tAnchor);
    }

    
    /***************************
    *函数名称:getAnchorCalibrationPos
    *函数功能:获取锚点(x,x)转换成锚点(0,0)后的坐标
    *函数参数:CCNode* pNode--要改变的节点
    *函数返回值:CCPoint通过新的锚点转换后的坐标
    *备注:这里getAnchorPoint所得到的锚点是新的锚点
    ****************************/
    static CCPoint getAnchorCalibrationPos(CCNode* pNode)
    {
        CCPoint tPoint;
        tPoint.x = pNode->getPositionX() + pNode->getContentSize().width * pNode->getAnchorPoint().x;
        tPoint.y = pNode->getPositionY() + pNode->getContentSize().height * pNode->getAnchorPoint().y;
        return tPoint;
    }

    
    /***************************
    *函数名称:distDiagonal
    *函数功能:求两个坐标点的距离
    *函数参数:const CCPoint& p1--端点坐标
               const CCPoint& p2--端点坐标
    *函数返回值:两个坐标点之间的距离
    *备注:
    ****************************/
    static float distDiagonal(const CCPoint& p1, const CCPoint& p2)
    {
        float fX = p2.x - p1.x;
        float fY = p2.y - p1.y;
        return  sqrtf(fabs((fX * fX) + (fY * fY)));
    }

    //勾股定理
    static float pythagorean(float a, float b)
    {
        ASSERT(a >= 0); 
        ASSERT(b >= 0);
        return sqrt( a * a + b * b);
    }


    ////分割字符串 C
    //static void c_split(const char * str,const char * deli, vector<string> *list) 
    //{ 
    //    char buff[1024]; 
    //    snprintf(buff,sizeof(buff),str); 
    //    char * gg; 
    //    char *p = strtok_r(buff, deli, &gg);
    //
    //    list->clear(); 
    //    while(p !=NULL) 
    //    { 
    //        list->push_back(p); 
    //        p = strtok_r(NULL, deli, &gg); 
    //    }; 
    //}

    
    /***************************
    *函数名称:split
    *函数功能:分割字符串
    *函数参数:const string& src        要分割的字符串
               const string& separator    分隔符,比如;或者空格等
               vector<string>& dest        分割后的各个子串保存到这个向量当中
    *函数返回值:void
    *备注:
    ****************************/
    static void split(const string& src, const string& separator, vector<string>& dest)
    {
        string str = src;
        string substring;
        string::size_type start = 0, index;

        do
        {
            index = str.find_first_of(separator,start);
            if (index != string::npos)
            {    
                substring = str.substr(start,index-start);
                dest.push_back(substring);
                start = str.find_first_not_of(separator,index);
                if (start == string::npos) return;
            }
        }while(index != string::npos);

        //the last token
        substring = str.substr(start);
        dest.push_back(substring);
    }


#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

    //编码转换
    //#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) 
    //    string title = "成绩";
    //    GBK2UTF8(title,"gb2312","utf-8"); 
    //#endif 
    static int GBK2UTF8(std::string & gbkStr, const char* toCode, const char* fromCode)  
    {  
        iconv_t iconvH;  
        iconvH = iconv_open(fromCode, toCode);  
        if (iconvH == 0)  
        {  
            return -1;  
        }  

        const char* strChar = gbkStr.c_str();  
        const char** pin = &strChar;  
        size_t strLength = gbkStr.length();  
        char* outbuf = (char*) malloc(strLength*4);  
        char* pBuff = outbuf;  

        memset( outbuf, 0, strLength*4);  
        size_t outLength = strLength*4;  
        if (-1 == iconv(iconvH, pin, &strLength, &outbuf, &outLength))  
        {  
            iconv_close(iconvH);  
            return -1;  
        }  
        gbkStr = pBuff;  
        iconv_close(iconvH);  
        return 0;  
    }
#endif //#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

    /***************************
    *函数名称:toString
    *函数功能:将int类型转化成string类型
    *函数参数:int nSrc--要转化的值
    *函数返回值:转化后的string值
    *备注:通过sprintf来转化,考虑到跨平台
    ****************************/
    static string toString(int nSrc)
    {
        char psStr[CHAR_LENGTH] = {0};
        sprintf(psStr, "%d", nSrc);
        return psStr;
    }
    /***************************
    *函数名称:toString
    *函数功能:将long long类型转化成string类型
    *函数参数:long long llSrc--要转化的值
    *函数返回值:转化后的string值
    *备注:通过sprintf来转化,考虑到跨平台
    ****************************/
    static string toString(long long llSrc)
    {
        char psStr[CHAR_LENGTH] = {0};
        sprintf(psStr, "%lld", llSrc);
        return psStr;
    }
    /***************************
    *函数名称:toString
    *函数功能:将float类型转化成string类型
    *函数参数:float fSrc--要转化的值
    *函数返回值:转化后的string值
    *备注:通过sprintf来转化,考虑到跨平台
    ****************************/
    static string toString(float fSrc)
    {
        char psStr[CHAR_LENGTH] = {0};
        sprintf(psStr, "%f", fSrc);
        return psStr;
    }
    /***************************
    *函数名称:toString
    *函数功能:将double类型转化成string类型
    *函数参数:double dSrc--要转化的值
    *函数返回值:转化后的string值
    *备注:通过sprintf来转化,考虑到跨平台
    ****************************/
    static string toString(double dSrc)
    {
        char psStr[CHAR_LENGTH] = {0};
        sprintf(psStr, "%f", dSrc);
        return psStr;
    }


};

#endif //__XCOMMON_H__
原文地址:https://www.cnblogs.com/newlist/p/3154543.html