#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 / ;
rc.origin.y -= rc.size.height / ;
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 >= );
ASSERT(b >= );
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 = , 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 == )
{
return -;
} const char* strChar = gbkStr.c_str();
const char** pin = &strChar;
size_t strLength = gbkStr.length();
char* outbuf = (char*) malloc(strLength*);
char* pBuff = outbuf; memset( outbuf, , strLength*);
size_t outLength = strLength*;
if (- == iconv(iconvH, pin, &strLength, &outbuf, &outLength))
{
iconv_close(iconvH);
return -;
}
gbkStr = pBuff;
iconv_close(iconvH);
return ;
}
#endif //#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) /***************************
*函数名称:toString
*函数功能:将int类型转化成string类型
*函数参数:int nSrc--要转化的值
*函数返回值:转化后的string值
*备注:通过sprintf来转化,考虑到跨平台
****************************/
static string toString(int nSrc)
{
char psStr[CHAR_LENGTH] = {};
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] = {};
sprintf(psStr, "%lld", llSrc);
return psStr;
}
/***************************
*函数名称:toString
*函数功能:将float类型转化成string类型
*函数参数:float fSrc--要转化的值
*函数返回值:转化后的string值
*备注:通过sprintf来转化,考虑到跨平台
****************************/
static string toString(float fSrc)
{
char psStr[CHAR_LENGTH] = {};
sprintf(psStr, "%f", fSrc);
return psStr;
}
/***************************
*函数名称:toString
*函数功能:将double类型转化成string类型
*函数参数:double dSrc--要转化的值
*函数返回值:转化后的string值
*备注:通过sprintf来转化,考虑到跨平台
****************************/
static string toString(double dSrc)
{
char psStr[CHAR_LENGTH] = {};
sprintf(psStr, "%f", dSrc);
return psStr;
} }; #endif //__XCOMMON_H__

CXCommon.h工具类的更多相关文章

  1. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  2. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  3. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  4. JS 工具类

    之前工作用的JavaScript比较多,总结了一下工具类,和大家分享一下,有不足之处还请多多见谅!! 1. 数组工具类(arrayUtils) var arrayUtils = {}; (functi ...

  5. 随笔分类 - Android之工具类

    Android之文件搜索工具类 /** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 */ private void sear ...

  6. Android 系统工具类SystemUtils

    包含的功能有: 获取系统中所有APP应用.获取用户安装的APP应用.根据包名和Activity启动类查询应用信息.跳转到WIFI设置.WIFI网络开关.移动网络开关.GPS开关 当前若关则打开 当前若 ...

  7. App开发流程之加密工具类

    科技优家 2016-09-08 18:10 从这篇记录开始,记录的都算是干货了,都是一些编程日常的积累. 我建议先将基础的工具加入项目,后续的开发效率会呈指数增长.如果在专注功能开发过程中,才发现缺少 ...

  8. JAVA Collections工具类sort()排序方法

    主要分析内容: 一.Collections工具类两种sort()方法 二.示例 一.Collections工具类两种sort()方法 格式一: public static <T extends ...

  9. Java生成带小图标的二维码-google zxing 工具类

    近期一直忙于开发微信商城项目,应客户要求,要开发个有图标的二维码.经过两次改版,终于实现了该功能(第一次没有小图标,这次才整合好的),如下是完整代码 . 该代码使用Java7开发,另外使用 core- ...

随机推荐

  1. unity3D总结的一些细节,不注意有些要折腾非常多天!

    1. 注意!!ps保存图片时,若保存为ps格式,若关闭最大兼容将会导致unity导入失败!(n天) 2.switch 推断NGUI popuplist传来的value字符串时一定要trim一下去掉空格 ...

  2. MsChart<3> 饼状图

    MsChart,饼状图 1 <asp:Chart ID="Chart1" runat="server" BorderlineDashStyle=" ...

  3. Python性能优化:PyPy、Numba 与 Cython。PyPy的安装及对应pip的安装

    性能优化讨论见参考1:大概意思是,PyPy内置JIT,对纯Python项目兼容性极好,几乎可以直接运行并直接获得性能提升:缺点是对很多C语言库支持性不好.Numba是一个库,可以在运行时将Python ...

  4. Apache+Tomcat负载均衡问题集锦

    之前在windows 环境下搭建了下apache+tomcat负载均衡(不会的能够參考之前的文档,文档对于linux和windows都适用),一帆风顺.没有出现不论什么问题.今天尝试着在linux下搭 ...

  5. Nginx安装及配置文件解释

    安装nginx,还是在mac上面用brew比较方便. 首先,brew install nginx,提示改权限 sudo chown -R $(whoami) /usr/local 然后brew ins ...

  6. Oracle 乱码或则中文无法正确查询问题

    解决Oracle本身的字符集问题 oracle数据库的字符集更改 A.oracle server 端 字符集查询 select userenv('language') from dual 其中NLS_ ...

  7. 【中英】mac电脑清理软件 ToolWiz Mac Boost

    简单介绍: ToolWiz Mac Boost是一款适用于Mac电脑清理加速最好的终极应用, 使您的Mac电脑干净有条理, 执行飞速且稳定.始终保持最佳状态! ToolWiz Mac Boost 运用 ...

  8. [Functional Programming] Use Task/Async for Asynchronous Actions

    We refactor a standard node callback style workflow into a composed task-based workflow. Original Co ...

  9. reportservice报表单元格依据条件显示不同的颜色

    有时候.我们须要依据条件,让单元格显示不同的颜色: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveV9mMTIz/font/5a6L5L2T/fontsi ...

  10. 一致性哈希算法(Consistent Hashing) .

    应用场景 这里我先描述一个极其简单的业务场景:用4台Cache服务器缓存所有Object. 那么我将如何把一个Object映射至对应的Cache服务器呢?最简单的方法设置缓存规则:object.has ...