#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. Guava API - FluentIterable Predicate Function Odering Range Splitter

    这写API可解决的问题 1. 集合元素的过滤 - FluentIterable Predicate Range Function 1) 先说Predicate<T>,这个相当与一个过滤原则 ...

  2. Openwrt WIFI探针开发【一】

    2017.9.26 公开源码(Apache2.0协议) https://github.com/769484623/WiFiProbe ————————————————————————————————— ...

  3. Computer Generated Angular Fisheye Projections [转]

    Computer GeneratedAngular Fisheye Projections Written by Paul Bourke May 2001 There are two main ide ...

  4. crm2013安装和部署语言包

    步骤 1:安装语言包 假设具有执行 Microsoft Dynamics CRM for Microsoft Office Outlook 的用户,除了在执行 Microsoft Dynamics C ...

  5. JS及JQuery对Html内容编码,Html转义

    1利用jquery /** JQuery Html Encoding.Decoding * 原理是利用JQuery自带的html()和text()函数可以转义Html字符 * 虚拟一个Div通过赋值和 ...

  6. (C++)C++类继承中的构造函数和析构函数

    思想: 在C++的类继承中, 建立对象时,首先调用基类的构造函数,然后在调用下一个派生类的构造函数,依次类推: 析构对象时,其顺序正好与构造相反: 例子: #include <iostream& ...

  7. Dubbo之旅--集群容错和负载均衡

    当我们的系统中用到Dubbo的集群环境,由于各种原因在集群调用失败时,Dubbo提供了多种容错方案,缺省为failover重试. Dubbo的集群容错在这里想说说他是由于我们实际的项目中出现了此类的问 ...

  8. 如何申请iOS开发者证书/发布app到手机

    申请iOS开发者证书 http://blog.csdn.net/htttw/article/details/7939405 如何向App Store提交应用 http://www.cocoachina ...

  9. 说说PHP中的命名空间相关概念

    说说PHP中的命名空间相关概念 1. PHP中的命名空间是什么? 什么是命名空间?"从广义上来说,命名空间是一种封装事物的方法.在非常多地方都能够见到这样的抽象概念. 比如.在操作系统中文件 ...

  10. 算法笔记_035:寻找最小的k个数(Java)

    目录 1 问题描述 2 解决方案 2.1 全部排序法 2.2 部分排序法 2.3 用堆代替数组法 2.4线性选择算法   1 问题描述 有n个整数,请找出其中最小的k个数,要求时间复杂度尽可能低. 2 ...