Cocos2D-X provides a class to help you easily manage your Resources – CCFileUtils
You can add additional folders to your default search path.This is helpful to select resources according to device resolution.
1 2 3 4 5 6 7 8 9 10 11 12 |
vector<string> searchPath; searchPath.push_back("hd"); searchPath.push_back("sd"); //when looking for resources will search hd and sd folders CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath); //example CCSprite sprite = CCSprite::create("Player.png"); //can be either in hd,sd or under Resources |
1 2 3 4 5 6 7 8 9 |
string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("content.txt"); unsigned char* fileContent = NULL; unsigned long bufferSize = 0; fileContent = CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "r", &bufferSize); |
Note: for some reason in an Android project I have to put the ttf fonts under fonts folder or they wont be found.
1 2 3 4 5 6 7 |
CCLabelTTF *item1Text = CCLabelTTF::create("Info", "atarist.ttf", 10.0*scaleFactor, CCSizeMake(200*scaleFactor,40*scaleFactor), kCCTextAlignmentLeft); //if specifying only font name doesen't work add fonts to path, //create fonts folder under resources if not already there CCLabelTTF *item1Text = CCLabelTTF::create("Info", "fonts/atarist.ttf", 10.0*scaleFactor, CCSizeMake(200*scaleFactor,40*scaleFactor), kCCTextAlignmentLeft); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
//dont forget #include "support/tinyxml2/tinyxml2.h" string fullPath = CCFileUtils::sharedFileUtils()->getWritablePath() + "test.xml"; unsigned long bufferSize = 0; unsigned char *fileContent = CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "r", &bufferSize); if(bufferSize == 0){ CCLOG("debug cannot read from file %", fullPath.c_str()); return; } string content; content.append(reinterpret_cast<const char*>(fileContent)); XMLDocument tinyDoc; tinyDoc.Parse(content.c_str()); XMLElement *firstChild = tinyDoc.FirstChildElement()->FirstChildElement();; // root->firstChild while(firstChild != 0){ const XMLAttribute *att = firstChild->FirstAttribute(); const XMLAttribute *att2 = att->Next(); //next attribute const char *content = firstChild->GetText(); if(firstChild->NextSibling() == NULL){ break; }else { firstChild = firstChild->NextSibling()->ToElement(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
// dont forget #include "support/zip_support/unzip.h" string pathName = CCFileUtils::sharedFileUtils()->getWritablePath() + "filename.zip"; unzFile zipfile = unzOpen(pathName.c_str()); if ( zipfile == NULL ) { printf( "%s: not foundn" ,pathName.c_str()); return; } // Get info about the zip file unz_global_info global_info; if ( unzGetGlobalInfo( zipfile, &global_info ) != UNZ_OK ) { printf( "could not read file global infon" ); unzClose( zipfile ); return; } // Loop to extract all files uLong i; for ( i = 0; i < global_info.number_entry; ++i ) { // Get info about current file. unz_file_info file_info; char filename[ 100 ]; if ( unzGetCurrentFileInfo( zipfile, &file_info, filename, 100, NULL, 0, NULL, 0 ) != UNZ_OK ) { CCLog( "could not read file infon" ); unzClose( zipfile ); return; } // Buffer to hold data read from the zip file. char read_buffer[ READ_SIZE ]; string str(filename); //if(str.find(".png") != string::npos || str.find(".gif") != string::npos){ CCLog("debug extracting file %s",filename ); // Check if this entry is a directory or file. const size_t filename_length = strlen( filename ); if ( filename[ filename_length-1 ] == dir_delimter ) { // Entry is a directory, so create it. printf( "dir:%sn", filename ); } else { // Entry is a file, so extract it. CCLog( "file:%sn", filename ); if ( unzOpenCurrentFile( zipfile ) != UNZ_OK ) { CCLog( "could not open filen" ); unzClose( zipfile ); return ; } // Open a file to write out the data. string filepath = CCFileUtils::sharedFileUtils()->getWritablePath() + packageID + "_" + filename; CCLog("%s",filepath.c_str()); FILE *out = fopen( filepath.c_str(), "wb" ); if ( out == NULL ) { CCLog( "could not open destination filen" ); //unzCloseCurrentFile( zipfile ); //unzClose( zipfile ); //return; } else { int error = UNZ_OK; do { error = unzReadCurrentFile( zipfile, read_buffer, READ_SIZE ); if ( error < 0 ) { printf( "error %dn", error ); unzCloseCurrentFile( zipfile ); unzClose( zipfile ); return; } // Write data to file. if ( error > 0 ) { int dbuf = fwrite( read_buffer, error, 1, out ); // You should check return of fwrite... CCLog("debug bytes written %d %s",dbuf,filename); } } while ( error > 0 ); fclose( out ); } } unzCloseCurrentFile( zipfile ); // Go the the next entry listed in the zip file. if ( ( i+1 ) < global_info.number_entry ) { if ( unzGoToNextFile( zipfile ) != UNZ_OK ) { printf( "cound not read next filen" ); unzClose( zipfile ); return; } } } unzClose( zipfile ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
void PackageData::downloadPackage() { cocos2d::extension::CCHttpRequest* request = new cocos2d::extension::CCHttpRequest(); CCLog("Starting download ..."); request->setUrl("http://example.com/path/myfile.zip"); request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpGet); request->setResponseCallback(this, callfuncND_selector(PackageData::onHttpRequestCompleted)); request->setTag("GET download"); cocos2d::extension::CCHttpClient::getInstance()->send(request); request->release(); } void PackageData::onHttpRequestCompleted(cocos2d::extension::CCHttpClient *sender, cocos2d::extension::CCHttpResponse *response) { if (!response) { return; } // You can get original request type from: response->request->reqType if (0 != strlen(response->getHttpRequest()->getTag())) { CCLog("debug %s completed", response->getHttpRequest()->getTag()); } int statusCode = response->getResponseCode(); char statusString[64] = {}; CCLog(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag()); CCLog("debug response code: %d", statusCode); if (!response->isSucceed()) { CCLog("response failed"); CCLog("error buffer: %s", response->getErrorBuffer()); return; } // dump data vector<char> *responseData = response->getResponseData(); CCLog("debug Http Test, dump data: %d",responseData->size() ); string filePath = CCFileUtils::sharedFileUtils()->getWritablePath(); string fileName = "myfile.zip"; string pathName = filePath.c_str() + fileName; CCLog("debug filename %s",pathName.c_str()); FILE *fp = fopen(pathName.c_str(), "wb"); if (!fp) { CCLOG("debug can not create file %s", pathName.c_str()); return; } CCLog("debug written bytes %d",fwrite(responseData->data(), 1, responseData->size(), fp)); fclose(fp); } |