cocos2dx C++一些实用方法
来源:互联网 发布:linux有什么用途 编辑:程序博客网 时间:2024/06/06 19:35
1.获取GUID
const char* newGUID(){ static char buf[64] = { 0 }; GUID guid; if (S_OK == ::CoCreateGuid(&guid)) { _snprintf(buf, sizeof(buf) , "%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X" , guid.Data1 , guid.Data2 , guid.Data3 , guid.Data4[0], guid.Data4[1] , guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5] , guid.Data4[6], guid.Data4[7] ); } return (const char*)buf;}
2.获取当前时间戳
std::string getRawTime() { SYSTEMTIME sysTime; GetLocalTime(&sysTime); time_t unixTime; time(&unixTime); std::string rawTime = __String::createWithFormat("%ld%ld", unixTime, sysTime.wMilliseconds)->getCString();//输出UNIX时间戳字符串 rawTime.erase(10); return rawTime;}
3.获取文件时间戳
time_t tt;struct tm *tst;struct stat st;stat("D:\\simple.txt", &st);//必须使用两个'\\'tt = st.st_ctime;log("tt=%ld\n", tt);
4.压缩文件
zip库下载链接 http://download.csdn.net/detail/chinawallace/9584122HZIP hz = CreateZip(L"C:\\simple1.zip",0);ZipAdd(hz, L"simple.txt", L"C:\\simple.txt");CloseZip(hz);
5.获取网络状态
#include <Sensapi.h> #pragma comment(lib, "Sensapi.lib")DWORD flags;IsNetworkAlive(&flags);
6.cocos2dx获取本地数据流
ssize_t size = 0;unsigned char* titlech = FileUtils::getInstance()->getFileData("D:/test.png", "r", &size);
7.cocos2dx加载网络图片数据流
const char* pSrc = "网络图片数据流"; unsigned char *pData = (unsigned char*)(const_cast<char*>(pSrc)); auto pDataLen = strlen((const char*)pData); std::string load_str; load_str = std::string((const char*)pData, pDataLen); int len = 0; unsigned char *buffer1; len = base64Decode((unsigned char*)load_str.c_str(), (unsigned int)load_str.length(), &buffer1); Image* img = new Image(); bool ok = img->initWithImageData(buffer1, len); unsigned char *pngData = img->getData(); CCTexture2D *texture = new CCTexture2D(); texture->initWithData(pngData, img->getDataLen(), kCCTexture2DPixelFormat_RGB888, img->getWidth(), img->getHeight(), CCSizeMake((float)img->getWidth(), (float)img->getHeight())); sprite->initWithTexture(texture);
8.cocos2dx生成json
#include "json/document.h"#include "json/writer.h"#include "json/stringbuffer.h"#include <iostream>#include "data/HttpConstant.h"using namespace rapidjson;//生成jsonrapidjson::Document m_writedoc;m_writedoc.SetObject();rapidjson::Document::AllocatorType& allocator = m_writedoc.GetAllocator();rapidjson::Value object(rapidjson::kObjectType);m_writedoc.RemoveAllMembers();rapidjson::Value gradeId(rapidjson::kStringType);gradeId.SetString("45014d74cce3469b86ace39cf2025de6", allocator);object.AddMember("gradeId", gradeId, allocator);rapidjson::Value validation(rapidjson::kStringType);validation.SetString("123", allocator);object.AddMember("validation", validation, allocator);StringBuffer buffer;rapidjson::Writer<StringBuffer> writer(buffer);object.Accept(writer);std::string strSendMessage = buffer.GetString();//解析jsonrapidjson::Document writedoc;writedoc.SetObject();rapidjson::Value &value = writedoc.Parse<kParseDefaultFlags>(str.c_str());bool result = value["result"].GetBool();
9.cocos2dx的Http请求
#include "network/HttpClient.h"using namespace network;HttpRequest* request = new (std::nothrow) HttpRequest();request->setUrl("http://192.168.1.7:8080/lesiea/game/drumsData");request->setRequestType(HttpRequest::Type::POST);request->setResponseCallback(CC_CALLBACK_2(CourseSelectClassA1Layer::onHttpRequestCompleted, this));std::vector<std::string> headers;headers.push_back("Content-Type: application/json; charset=utf-8");request->setHeaders(headers);request->setTag("test");HttpClient::getInstance()->send(request);request->release();void onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response){ if (!response) { return; } long statusCode = response->getResponseCode(); log("response code: %ld", statusCode); if (statusCode != 200) { log("response failed"); log("error buffer: %s", response->getErrorBuffer()); auto layer = TipLayer::create("获取班级二维码失败"); this->addChild(layer, 100, 100); return; } else { std::string tag = response->getHttpRequest()->getTag(); std::vector<char> *buffer = response->getResponseData(); std::string str; for (unsigned int i = 0; i < buffer->size(); i++) { str += (*buffer)[i]; } log("%s", str); }}
10.随机列表数据
// random generator function:ptrdiff_t myrandom(ptrdiff_t i) { return rand() % i; }// pointer object to it:ptrdiff_t(*p_myrandom)(ptrdiff_t) = myrandom;srand(unsigned(time(NULL)));////s随机数种子// using myrandom:random_shuffle(m_userInfoVector.begin(), m_userInfoVector.end(), p_myrandom);
11.获取网络连接状态
#include <Sensapi.h> #pragma comment(lib, "Sensapi.lib")DWORD flags;//上网方式 BOOL m_bOnline=TRUE;//是否在线 m_bOnline=IsNetworkAlive(&flags); if(m_bOnline)//在线 { if ((flags & NETWORK_ALIVE_LAN) ==NETWORK_ALIVE_LAN) { cout<<"在线:NETWORK_ALIVE_LAN\n"; } if ((flags & NETWORK_ALIVE_WAN) ==NETWORK_ALIVE_WAN) { cout<<"在线:NETWORK_ALIVE_WAN\n"; } if ((flags & NETWORK_ALIVE_AOL) ==NETWORK_ALIVE_AOL) { cout<<"在线:NETWORK_ALIVE_AOL\n"; } } else cout<<"不在线\n"; }
12.列表随机排序
// random generator function:ptrdiff_t myrandom(ptrdiff_t i) { return rand() % i; }// pointer object to it:ptrdiff_t(*p_myrandom)(ptrdiff_t) = myrandom;srand(unsigned(time(NULL)));////s随机数种子 // using myrandom: random_shuffle(m_userInfoVector.begin(), m_userInfoVector.end(), p_myrandom); for (int i = 0; i < m_userInfoVector.size(); i++) { m_userInfoVector.at(i)->setIndex(i+1); m_pHeadVector.at(i)->initWithFile(m_userInfoVector.at(i)->getUserIcon()); }
13.c++分割子串
std::string tag = "save12";std::string str = "save";if (strstr(tag.c_str(), str.c_str())){ std::string s = tag.substr(4);}
14.读取本地数据流
ssize_t size = 0; unsigned char* titlech = FileUtils::getInstance()->getFileData(path, "rb+", &size); auto pDataLen = strlen((const char*)titlech); std::string load_str; load_str = std::string((const char*)titlech, pDataLen); log("data=%s", load_str.c_str()); std::string load_str; auto data = FileUtils::getInstance()->getDataFromFile(path); unsigned char* titlech = data.getBytes(); auto pDataLen = strlen((const char*)titlech); load_str = std::string((const char*)titlech, pDataLen); log("data=%s", load_str.c_str()); fstream fsIn; char s[65535]; fsIn.open(path.c_str(), ios::in | ios::binary); fsIn.read(s, 65535); fsIn.close(); char* c = s; std::string str = c; log("%s", str.c_str());
0 0
- cocos2dx C++一些实用方法
- 数据库一些实用方法
- linux一些实用方法
- cocos2dx-lua 一些公共方法
- 一些搜集的实用方法
- javaScript实用的一些方法
- QTreeWidget的一些实用方法
- JS一些实用的方法
- 一些实用的js方法
- jquery的一些实用方法
- Android开发一些实用方法
- JS一些实用的方法
- 4.Cocos2dx碰撞检测的一些方法。
- 一些C/C++的实用HOWTO备忘
- 收集javascript的一些实用方法!
- asp.net的一些实用小方法
- 一些实用的图形用户界面方法
- 一些QTP比较实用的方法
- 开启memcache扩展
- 伪静态配置开启
- windows下Qt Creator 调试器配置
- 6. Error opening trace file: Permission denied ;No such file or directory
- 从AIDL看Android跨进程通信
- cocos2dx C++一些实用方法
- 关于OC中的static关键字使用
- inotify+rsync安装配置
- 最大流最小割(概念)
- app:transformClassesWithDexForDebug异常处理
- curl模拟post请求提交
- hibernate的一对一映射-外键映射-双向-mysql数据库
- php 实现get,set方式读取属性值
- Yougth's Game[Ⅲ]