#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. FFmpeg YUV2RGB

    AVFrame* YUV2RGB( AVCodecContext * avctx, AVFrame * frame ) { AVFrame* pFrameRGB=NULL; pFrameRGB=avc ...

  2. Mahout构建图书推荐系统【一起学Mahout】

    阅读导读: 1.Mahout中推荐过滤算法支持哪两种算法? 2.用java代码怎样计算男性用户打分过的图书? 3.itemEuclidean.userEuclideanNoPref各自是什么算法? 1 ...

  3. ActiveMQ使用示例之Topic

    非持久的Topic消息示例  对于非持久的Topic消息的发送基本跟前面发送队列信息是一样的,只是把创建Destination的地方,由创建队列替换成创建Topic,例如: Destination d ...

  4. powershell 远程重启/关闭服务器

    powershell 远程重启/关闭服务器 #启动winrm PS C:\Windows\system32> winrm quickconfig -q #设置信任主机 PS C:\Windows ...

  5. [ES6] 04. The let keyword -- 2 Fiald case

    Fiald case 1: let can work in it's block { let a = 10; var b = 1; } a // ReferenceError: a is not de ...

  6. iphone系统更新 3002错误

    一:错误原因 与苹果服务器通信连接错误 二:解决 windows下:c:/windows/system32/drivers/etc 修改里面的hosts文件 去掉和apple.com相关的 三:ios ...

  7. webpack CommonsChunkPlugin 提取公共代码

    1.项目结构 2.部分代码 module.js console.log('module.js'); index文件夹下的index.js require('../module.js'); consol ...

  8. setTimeout模拟setInterval调用

    在开发环境下,尽量不用间歇调用,原因是后一个间歇调用可能会在前一个间歇调用结束之前启动. 尽量使用超时调用来模拟间歇调用. 示例代码: <!DOCTYPE html> <html l ...

  9. 基于 bootstrap 的数据展示--bootgrid 样式改动。

    bootgrid 的官网案例 http://www.jquery-bootgrid.com/Examples 官方demo 样式 基于项目须要,取消了一些不须要 的功能,改动了源代码 最后样式成了这样 ...

  10. android源码相关网站

    https://android.googlesource.com/ google的android源码网站 http://source.android.com/ android网站 git://code ...