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 / ;
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工具类的更多相关文章
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
- [转]Java常用工具类集合
转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- JS 工具类
之前工作用的JavaScript比较多,总结了一下工具类,和大家分享一下,有不足之处还请多多见谅!! 1. 数组工具类(arrayUtils) var arrayUtils = {}; (functi ...
- 随笔分类 - Android之工具类
Android之文件搜索工具类 /** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 */ private void sear ...
- Android 系统工具类SystemUtils
包含的功能有: 获取系统中所有APP应用.获取用户安装的APP应用.根据包名和Activity启动类查询应用信息.跳转到WIFI设置.WIFI网络开关.移动网络开关.GPS开关 当前若关则打开 当前若 ...
- App开发流程之加密工具类
科技优家 2016-09-08 18:10 从这篇记录开始,记录的都算是干货了,都是一些编程日常的积累. 我建议先将基础的工具加入项目,后续的开发效率会呈指数增长.如果在专注功能开发过程中,才发现缺少 ...
- JAVA Collections工具类sort()排序方法
主要分析内容: 一.Collections工具类两种sort()方法 二.示例 一.Collections工具类两种sort()方法 格式一: public static <T extends ...
- Java生成带小图标的二维码-google zxing 工具类
近期一直忙于开发微信商城项目,应客户要求,要开发个有图标的二维码.经过两次改版,终于实现了该功能(第一次没有小图标,这次才整合好的),如下是完整代码 . 该代码使用Java7开发,另外使用 core- ...
随机推荐
- Android获取cpu使用率,剩余内存和硬盘容量
1.内存信息 在proc/meminfo下有具体的内存使用情况,我这里获取的内存信息就是从这个文件中获取的.获取到具体的内存信息后依据我自己的需求,从bufferdreader中单独抽取出来了剩余的内 ...
- 论#include
1.#include " "与#include <> #include " "表示预编译命令源程序在当前项目下寻找头文件,如果找不到,再到标准头文件 ...
- 解决.NET 调用JAVA WEBService服务中文乱码问题
主要代码如下: String input ="中文" C#调用代码: String str = System.Text.Encoding.GetEncoding("UT ...
- Ubuntu简单搭建git私有服务
gitserver搭建过程 搭建gitserver过程记录 例如以下: 环境: serverUbuntu虚拟机(Boss),能通过网络訪问到(server地址:192.168.9.103). clie ...
- Google帮助IE浏览器实现对SVG支持
可缩放矢量图形(SVG)的意识就是一个用于描述二维矢量图形的一种开放图形格式. SVG现在已经能够广泛得应用到许多的项目当中,包括KDE和维基百科等.但是 Internet Explorer浏览器的内 ...
- 异类的Javascript处理和解析URL的方式
通常来说,我们使用Javascript处理和解析URL是使用location对象.在今天这个代码小技巧中,我们使用另外一个比较异类的方式处理和解析URL. 代码如下: function parseUR ...
- Google POI下载工具破解之路
我是GIS初学者,爱好二次开发,像初恋一样.最近对编译感兴趣,每当成功获取一点信息,就有一种快感,感觉马上就要成功了……其实,还早! 01.初次反编译 今天在微创业工作室找到了Google POI下载 ...
- android-文字的处理-随心
一.计算文字的大小 String timeStr = "00:00"; int textWidth = (int)Layout.getDesiredWidth(timeStr, 0 ...
- keytool命令总结
keytool 命令总结 一.创建数字证书 交互模式 使用默认的密钥库.keystore(目录是c: Documents and Setting用户名)和算法(DSA) keytool -genkey ...
- pselect 和 select
pselect函数是由POSIX发明的,如今许多Unix变种都支持它. #include <sys/select.h> #include <signal.h> #include ...