bool AppDelegate::applicationDidFinishLaunching() { // initialize director CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); // turn on display FPS pDirector->setDisplayStats(false); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60); pEGLView->setDesignResolutionSize(SCREEN_WIDTH, SCREEN_HEIGHT, kResolutionExactFit); CCFileUtils *fileUtils = CCFileUtils::sharedFileUtils(); fileUtils->addSearchPath("fonts"); fileUtils->addSearchPath("images"); fileUtils->addSearchPath("sounds"); CCScene* scene = CCScene::create(); GameScene* layer = GameScene::create(); scene->addChild(layer); pDirector->runWithScene(scene); return true; }
void Recipe76::doStep2() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) CCMessageBox("iOS Only", "RecipeBook"); return; #else std::string zipFilename = "archive.zip"; CCFileUtils* fileUtils = CCFileUtils::sharedFileUtils(); // フルパスを取得 std::string fullPath = fileUtils->fullPathForFilename(zipFilename.c_str()); // 書き込み可能なディレクトリを取得 std::string path = fileUtils->getWritablePath(); std::string unzipdPath = path + "unzipd/"; // zipの展開 unzipFileToDir(fullPath.c_str(), unzipdPath.c_str()); // 展開したzip中のテキストファイル取得 std::string txtPath = unzipdPath + "test-step2.txt"; unsigned long size; unsigned char* data = fileUtils->getFileData(txtPath.c_str(), "rb", &size); std::string text; if (data != NULL) { text.assign((const char*)data, size); delete [] data; } CCString *msg = CCString::createWithFormat("read from zip file'%s'", text.c_str()); CCLabelTTF *label = (CCLabelTTF*)this->getChildByTag(10); label->setString(msg->getCString()); #endif }
void initLuaGlobalVariables(const std::string& entry) { //GLOBAL_ROOT_DIR CCLuaEngine* pEngine = CCLuaEngine::defaultEngine(); CCLuaStack* pStack = pEngine->getLuaStack(); CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); using namespace std; string path = pFileUtils->fullPathForFilename(entry.c_str()); // replace "\" with "/", normalize the path int pos = string::npos; while ((pos = path.find_first_of("\\")) != string::npos) { path.replace(pos, 1, "/"); } string script_dir = path.substr(0, path.find_last_of("/")); string root_dir = script_dir.substr(0, script_dir.find_last_of("/")); CCLOG("RootDir: %s\nScriptDir: %s \n",root_dir.c_str(), script_dir.c_str()); std::string env = "GLOBAL_ROOT_DIR=\""; env.append(root_dir); env.append("\""); pEngine->executeString(env.c_str()); env = "__LUA_STARTUP_FILE__=\"";env.append(path);env.append("\""); pEngine->executeString(env.c_str()); pStack->addSearchPath(script_dir.c_str()); pFileUtils->addSearchPath(root_dir.c_str()); pFileUtils->addSearchPath(script_dir.c_str()); ScutExt::Init(root_dir+"/"); }
void Recipe76::doStep1() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) CCMessageBox("iOS Only", "RecipeBook"); return; #else std::string zipFilename = "archive.zip"; std::string filename = "test.txt"; CCFileUtils* fileUtils = CCFileUtils::sharedFileUtils(); // フルパスを取得 std::string fullPath = fileUtils->fullPathForFilename(zipFilename.c_str()); // archivezipファイルから test.txtデータをメモリに展開 unsigned long size; unsigned char *data = fileUtils->getFileDataFromZip(fullPath.c_str(), filename.c_str(), &size); CCLOG("data:%08X, fullPath:%s", data, fullPath.c_str()); std::string text; if (data!=NULL) { text.assign((const char*)data, size); //「delete [] data;」を忘れると、メモリーリークになるので注意 delete [] data; } CCString *msg = CCString::createWithFormat("read from zip file'%s'", text.c_str()); CCLabelTTF *label = (CCLabelTTF*)this->getChildByTag(10); label->setString(msg->getCString()); #endif }
bool ProjectConfig::validate(void) { CCFileUtils *utils = CCFileUtils::sharedFileUtils(); if (!utils->isDirectoryExist(m_projectDir)) return false; if (!utils->isDirectoryExist(getWritableRealPath())) return false; if (!utils->isFileExist(getScriptFileRealPath())) return false; return true; }
NS_CC_PURCHASE_BEGIN CCString* getDatabaseFilePath(CCString* prefix) { CCFileUtils* pUtils = CCFileUtils::sharedFileUtils(); const char* dbPath = prefix->length() == 0 ? DBPATH: CCString::createWithFormat("%s_%s", prefix->getCString(), DBPATH)->getCString(); std::string documentsDir = pUtils->getWriteablePath(); return CCString::createWithFormat("%s%s", documentsDir.c_str(), dbPath); }
void TestSearchPath::onExit() { CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); // reset search path sharedFileUtils->setSearchPaths(m_defaultSearchPathArray); sharedFileUtils->setSearchResolutionsOrder(m_defaultResolutionsOrderArray); FileUtilsDemo::onExit(); }
void TestIsFileExist::onExit() { CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); // reset filename lookup sharedFileUtils->setFilenameLookupDictionary(CCDictionary::create()); FileUtilsDemo::onExit(); }
void User::Save() { CCFileUtils* fileUtils = CCFileUtils::sharedFileUtils(); bugsCatched->writeToFile("BugsCatched.plist"); item->writeToFile("Item.plist"); string writablePath = fileUtils->getWritablePath() + "User.txt"; FILE *fp = fopen(writablePath.c_str(), "w"); string data = name + "\n" + ConvertInt(score) + "\n" + ConvertInt(level); fputs(data.c_str(), fp); fclose(fp); }
time_t XFileAssist::GetFileModifyTime(const char szFileName[]) { CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); // 注意有个坑: 安卓下面,fullPathForFilename返回的不见得是fullPath // 如果根据规则(searchPath + RelativePath + Resolution + FileName),在APK(Zip)包里面找到了这个文件 // 那么返回的就只是一个相对路径(相对于APK的root) std::string strFullPath = pFileUtils->fullPathForFilename(szFileName); return XFileHelper::GetFileModifyTime(strFullPath.c_str()); }
BOOL XFileAssist::WriteFileData(const char szFileName[], const void* pvData, size_t uDataLen) { CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); std::string strWritePath = pFileUtils->getWritablePath(); strWritePath.push_back(DIR_SPRIT); strWritePath += szFileName; return XFileHelper::WriteFileData(strWritePath.c_str(), pvData, uDataLen); }
XFileAssist::XFileAssist() { CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); std::string strWritePath = pFileUtils->getWritablePath(); std::vector<std::string> pathTable; pathTable.push_back(strWritePath); pFileUtils->setSearchPaths(pathTable); }
void CCApplication::setResourceRootPath(const std::string& rootResDir) { m_resourceRootPath = rootResDir; if (m_resourceRootPath[m_resourceRootPath.length() - 1] != '/') { m_resourceRootPath += '/'; } CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); std::vector<std::string> searchPaths = pFileUtils->getSearchPaths(); searchPaths.insert(searchPaths.begin(), m_resourceRootPath); pFileUtils->setSearchPaths(searchPaths); }
void TestSearchPath::onEnter() { FileUtilsDemo::onEnter(); CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); string ret; sharedFileUtils->purgeCachedEntries(); m_defaultSearchPathArray = sharedFileUtils->getSearchPaths(); vector<string> searchPaths = m_defaultSearchPathArray; string writablePath = sharedFileUtils->getWritablePath(); string fileName = writablePath+"external.txt"; char szBuf[100] = "Hello Cocos2d-x!"; FILE* fp = fopen(fileName.c_str(), "wb"); if (fp) { fwrite(szBuf, 1, strlen(szBuf), fp); fclose(fp); CCLog("Writing file to writable path succeed."); } searchPaths.insert(searchPaths.begin(), writablePath); searchPaths.insert(searchPaths.begin()+1, "Misc/searchpath1"); searchPaths.insert(searchPaths.begin()+2, "Misc/searchpath2"); sharedFileUtils->setSearchPaths(searchPaths); m_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); vector<string> resolutionsOrder = m_defaultResolutionsOrderArray; resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipad"); sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); for( int i=1; i<3; i++) { CCString *filename = CCString::createWithFormat("file%d.txt", i); ret = sharedFileUtils->fullPathForFilename(filename->getCString()); CCLog("%s -> %s", filename->getCString(), ret.c_str()); } // Gets external.txt from writable path string fullPath = sharedFileUtils->fullPathForFilename("external.txt"); CCLog("\nexternal file path = %s\n", fullPath.c_str()); if (fullPath.length() > 0) { fp = fopen(fullPath.c_str(), "rb"); if (fp) { char szReadBuf[100] = {0}; fread(szReadBuf, 1, strlen(szBuf), fp); CCLog("The content of file from writable path: %s", szReadBuf); fclose(fp); } } }
bool AppDelegate::applicationDidFinishLaunching() { // As an example, load config file // XXX: This should be loaded before the Director is initialized, // XXX: but at this point, the director is already initialized CCConfiguration::sharedConfiguration()->loadConfigFile("configs/config-example.plist"); // initialize director CCDirector *pDirector = CCDirector::sharedDirector(); pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); CCSize designSize = CCSizeMake(480, 320); CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); std::vector<std::string> searchPaths; if (screenSize.height > 320) { CCSize resourceSize = CCSizeMake(960, 640); searchPaths.push_back("hd"); searchPaths.push_back("hd/scenetest"); pDirector->setContentScaleFactor(resourceSize.height/designSize.height); } else { searchPaths.push_back("scenetest"); } pFileUtils->setSearchPaths(searchPaths); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionShowAll); #else CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder); #endif CCScene * pScene = CCScene::create(); CCLayer * pLayer = new TestController(); pLayer->autorelease(); pScene->addChild(pLayer); pDirector->runWithScene(pScene); return true; }
/** * 从数据包加载任务 */ bool MCTaskManager::loadTasks() { bool result = false; do { CCFileUtils *fileUtils = CCFileUtils::sharedFileUtils(); /* 加载任务 */ taskAccessor_ = new MCTaskAccessor; taskAccessor_->loadTasks(fileUtils->fullPathForFilename(kMCTaskPackageFilepath).c_str()); result = true; } while (0); return result; }
void TestResolutionDirectories::onEnter() { FileUtilsDemo::onEnter(); CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); string ret; sharedFileUtils->purgeCachedEntries(); m_defaultSearchPathArray = sharedFileUtils->getSearchPaths(); vector<string> searchPaths = m_defaultSearchPathArray; searchPaths.insert(searchPaths.begin(), "Misc"); sharedFileUtils->setSearchPaths(searchPaths); m_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); vector<string> resolutionsOrder = m_defaultResolutionsOrderArray; resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd"); resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad"); resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd"); resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide"); resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd"); resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone"); sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); for( int i=1; i<7; i++) { CCString *filename = CCString::createWithFormat("test%d.txt", i); ret = sharedFileUtils->fullPathForFilename(filename->getCString()); CCLog("%s -> %s", filename->getCString(), ret.c_str()); } }
bool AppDelegate::applicationDidFinishLaunching() { // initialize director CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); CCSize designSize = CCSizeMake(480, 320); CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); if (screenSize.height > 320) { CCSize resourceSize = CCSizeMake(960, 640); std::vector<std::string> searchPaths; searchPaths.push_back("hd"); pFileUtils->setSearchPaths(searchPaths); pDirector->setContentScaleFactor(resourceSize.height/designSize.height); } CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionShowAll); // turn on display FPS // pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object // CCScene *pScene = HelloWorld::scene(); CocosGUIExamplesRegisterScene *pScene = new CocosGUIExamplesRegisterScene(); pScene->autorelease(); // run pDirector->runWithScene(pScene); return true; }
void TestFilenameLookup::onEnter() { FileUtilsDemo::onEnter(); CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); CCDictionary *dict = CCDictionary::create(); dict->setObject(CCString::create("Images/grossini.png"), "grossini.bmp"); dict->setObject(CCString::create("Images/grossini.png"), "grossini.xcf"); sharedFileUtils->setFilenameLookupDictionary(dict); // Instead of loading carlitos.xcf, it will load grossini.png CCSprite *sprite = CCSprite::create("grossini.xcf"); this->addChild(sprite); CCSize s = CCDirector::sharedDirector()->getWinSize(); sprite->setPosition(ccp(s.width/2, s.height/2)); }
void TestIsFileExist::onEnter() { FileUtilsDemo::onEnter(); CCSize s = CCDirector::sharedDirector()->getWinSize(); CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); CCLabelTTF* pTTF = NULL; bool isExist = false; isExist = sharedFileUtils->isFileExist("Images/grossini.png"); pTTF = CCLabelTTF::create(isExist ? "Images/grossini.png exists" : "Images/grossini.png doesn't exist", "", 20); pTTF->setPosition(ccp(s.width/2, s.height/3)); this->addChild(pTTF); isExist = sharedFileUtils->isFileExist("Images/grossini.xcf"); pTTF = CCLabelTTF::create(isExist ? "Images/grossini.xcf exists" : "Images/grossini.xcf doesn't exist", "", 20); pTTF->setPosition(ccp(s.width/2, s.height/3*2)); this->addChild(pTTF); }
bool AppDelegate::applicationDidFinishLaunching() { // initialize director CCDirector *pDirector = CCDirector::sharedDirector(); pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); #ifdef TIZEN CCSize designSize = CCSizeMake(1280, 720); #else CCSize designSize = CCSizeMake(480, 320); #endif CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); if (screenSize.height > 320) { CCSize resourceSize = CCSizeMake(960, 640); std::vector<std::string> searchPaths; searchPaths.push_back("hd"); pFileUtils->setSearchPaths(searchPaths); pDirector->setContentScaleFactor(resourceSize.height/designSize.height); } CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder); // turn on display FPS pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60); CCScene * pScene = CCScene::create(); CCLayer * pLayer = new TestController(); pLayer->autorelease(); pScene->addChild(pLayer); pDirector->runWithScene(pScene); return true; }
BYTE* XFileAssist::ReadFileData(size_t* puSize, const char szFileName[], size_t uExtSize) { CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); // 注意有个坑: 安卓下面,fullPathForFilename返回的不见得是fullPath // 如果根据规则(searchPath + RelativePath + Resolution + FileName),在APK(Zip)包里面找到了这个文件 // 那么返回的就只是一个相对路径(相对于APK的root) //std::string strFullPath = pFileUtils->fullPathForFilename(szFileName); BYTE* pbyData = pFileUtils->getFileData(szFileName, "rb", (ssize_t*)puSize); if (pbyData == NULL || uExtSize == 0) return pbyData; BYTE* pbyBuffer = new BYTE[*puSize + uExtSize]; memcpy(pbyBuffer, pbyData, *puSize); CC_SAFE_DELETE_ARRAY(pbyData); return pbyBuffer; }
void Recipe76::doStep3() { #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) CCMessageBox("Android Only", "RecipeBook"); return; #else unsigned long size; unsigned char *data; std::string filename = "res/drawable-mdpi/icon.png"; std::string apkPath = getApkPath(); CCFileUtils* fileUtils = CCFileUtils::sharedFileUtils(); data = fileUtils->getFileDataFromZip(apkPath.c_str(), filename.c_str(), &size); if (data!=NULL) { // icon データ参照 CCLabelTTF *label = (CCLabelTTF*)this->getChildByTag(10); label->setString("apk からアイコンデータを取得しました"); delete [] data; } #endif }
void SQLHelper::init() { int result; //Check database if exist CCFileUtils *fileUtils = CCFileUtils::sharedFileUtils(); _dbPath = fileUtils->getWritablePath(); _dbPath.append("TrivialJokoa.db"); std::ifstream f(_dbPath.c_str()); if(!f) { //Database not exist result = sqlite3_open(_dbPath.c_str(),&_db); createDB(); } else { CCLOG("DB EXIST"); result = sqlite3_open(_dbPath.c_str(),&_db); } if (result != SQLITE_OK) CCLOG("OPENING WRONG, %d", result); }
void Recipe56::onEnter() { RecipeBase::onEnter(); CCFileUtils* fileUtils = CCFileUtils::sharedFileUtils(); std::string fullpath = fileUtils->fullPathForFilename("tweet_sample.json"); unsigned long size; unsigned char* data = fileUtils->getFileData(fullpath.c_str(), "rb", &size); if (data != NULL) { std::string err; // パースを実行し、picojson::valueにパース結果を格納する picojson::value v; picojson::parse(v, data, data+size, &err); delete [] data; if (err.empty()) { // JSONのルートのobject(std::map<std::string, value>) を取得する picojson::object &root = v.get<picojson::object>(); // 検索結果一覧を取得する picojson::array &results = root["results"].get<picojson::array>(); picojson::array::iterator it; for (it = results.begin(); it != results.end(); it++) { // Tweetのテキストとユーザを取得する picojson::object& tweet = it->get<picojson::object>(); std::string& from_user = tweet["from_user"].get<std::string>(); std::string& text = tweet["text"].get<std::string>(); CCLOG("%s:%s", from_user.c_str(), text.c_str()); } } } CCLabelTTF *label = (CCLabelTTF*)this->getChildByTag(10); label->setString("tweet データのパース処理が完了しました。\nログとソースを確認してください"); }
int CCLuaStack::lua_loadChunksFromZIP(lua_State *L) { if (lua_gettop(L) < 1) { CCLOG("lua_loadChunksFromZIP() - invalid arguments"); return 0; } const char *zipFilename = lua_tostring(L, -1); lua_settop(L, 0); CCFileUtils *utils = CCFileUtils::sharedFileUtils(); string zipFilePath = utils->fullPathForFilename(zipFilename); zipFilename = NULL; CCLuaStack *stack = CCLuaStack::stack(L); do { unsigned long size = 0; void *buffer = NULL; unsigned char *zipFileData = utils->getFileData(zipFilePath.c_str(), "rb", &size); CCZipFile *zip = NULL; bool isXXTEA = stack && stack->m_xxteaEnabled && zipFileData; for (unsigned int i = 0; isXXTEA && i < stack->m_xxteaSignLen && i < size; ++i) { isXXTEA = zipFileData[i] == stack->m_xxteaSign[i]; } if (isXXTEA) { // decrypt XXTEA xxtea_long len = 0; buffer = xxtea_decrypt(zipFileData + stack->m_xxteaSignLen, (xxtea_long)size - (xxtea_long)stack->m_xxteaSignLen, (unsigned char*)stack->m_xxteaKey, (xxtea_long)stack->m_xxteaKeyLen, &len); delete []zipFileData; zipFileData = NULL; zip = CCZipFile::createWithBuffer(buffer, len); } else { if (zipFileData) { zip = CCZipFile::createWithBuffer(zipFileData, size); } } if (zip) { CCLOG("lua_loadChunksFromZIP() - load zip file: %s%s", zipFilePath.c_str(), isXXTEA ? "*" : ""); lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); int count = 0; string filename = zip->getFirstFilename(); while (filename.length()) { unsigned long bufferSize = 0; unsigned char *buffer = zip->getFileData(filename.c_str(), &bufferSize); if (bufferSize) { if (lua_loadbuffer(L, (char*)buffer, (int)bufferSize, filename.c_str()) == 0) { lua_setfield(L, -2, filename.c_str()); ++count; } delete []buffer; } filename = zip->getNextFilename(); } CCLOG("lua_loadChunksFromZIP() - loaded chunks count: %d", count); lua_pop(L, 2); lua_pushboolean(L, 1); } else { CCLOG("lua_loadChunksFromZIP() - not found or invalid zip file: %s", zipFilePath.c_str()); lua_pushboolean(L, 0); } if (zipFileData) { delete []zipFileData; } if (buffer) { free(buffer); } } while (0); return 1; }
int loader_Android(lua_State *L) { std::string filename(luaL_checkstring(L, 1)); int pos = filename.rfind(".lua"); if (pos != std::string::npos) { filename = filename.substr(0, pos); } pos = filename.find_first_of("."); while (pos != std::string::npos) { filename.replace(pos, 1, "/"); pos = filename.find_first_of("."); } filename.append(".lua"); // search file in package.path unsigned char* codeBuffer = NULL; unsigned long codeBufferSize = 0; std::string codePath; CCFileUtils* utils = CCFileUtils::sharedFileUtils(); lua_getglobal(L, "package"); lua_getfield(L, -1, "path"); std::string searchpath(lua_tostring(L, -1)); lua_pop(L, 1); int begin = 0; int next = searchpath.find_first_of(";", 0); do { if (next == std::string::npos) next = searchpath.length(); std::string prefix = searchpath.substr(begin, next); if (prefix[0] == '.' && prefix[1] == '/') { prefix = prefix.substr(2); } pos = prefix.find("?.lua"); codePath = prefix.substr(0, pos).append(filename); codePath = utils->fullPathForFilename(codePath.c_str()); if (utils->isFileExist(codePath)) { codeBuffer = utils->getFileData(codePath.c_str(), "rb", &codeBufferSize); break; } begin = next + 1; next = searchpath.find_first_of(";", begin); } while (begin < (int)searchpath.length()); if (codeBuffer) { if (luaL_loadbuffer(L, (char*)codeBuffer, codeBufferSize, codePath.c_str()) != 0) { luaL_error(L, "error loading module %s from file %s :\n\t%s", lua_tostring(L, 1), filename.c_str(), lua_tostring(L, -1)); } delete []codeBuffer; } else { CCLog("can not get file data of %s", filename.c_str()); } return 1; }
bool AppDelegate::applicationDidFinishLaunching() { // initialize director CCDirector *pDirector = CCDirector::sharedDirector(); pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); CCFileUtils *pFileUtils = CCFileUtils::sharedFileUtils(); std::vector<std::string> searchPaths; CCSize designSize = CCSizeMake(480, 320); CCSize resourceSize; // if the device is iPad if (screenSize.height >= 768) { searchPaths.push_back("hd"); searchPaths.push_back("sd"); resourceSize = CCSizeMake(1024, 768); designSize = CCSizeMake(1024, 768); } // if the device is iPhone else{ // for retina iPhone if (screenSize.height > 320) { searchPaths.push_back("hd"); searchPaths.push_back("sd"); resourceSize = CCSizeMake(960, 640); } else{ searchPaths.push_back("sd"); resourceSize = CCSizeMake(480, 320); } } searchPaths.push_back("WhackAMoleSounds"); pFileUtils->setSearchPaths(searchPaths); pDirector->setContentScaleFactor(resourceSize.width / designSize.width); CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionFixedWidth); // load background and foreground CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("background.plist"); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("foreground.plist"); // turn on display FPS pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object CCScene *pScene = HelloWorld::scene(); // run pDirector->runWithScene(pScene); return true; }
int CCLuaStack::loadChunksFromZip(lua_State *L) { const char *zipFilename = lua_tostring(L, -1); CCFileUtils *utils = CCFileUtils::sharedFileUtils(); string zipFilePath = utils->fullPathForFilename(zipFilename); lua_pop(L, 1); zipFilename = NULL; do { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) string tmpFilePath = utils->getWriteablePath().append("cc_load_chunks.tmp"); unsigned long size = 0; unsigned char *buffer = utils->getFileData(zipFilePath.c_str(), "rb", &size); bool success = false; do { if (size == 0 || !buffer) { CCLOG("CCLoadChunksFromZip() - read source file %s failure", zipFilePath.c_str()); break; } FILE *tmp = fopen(tmpFilePath.c_str(), "wb"); if (!tmp) { CCLOG("CCLoadChunksFromZip() - create tmp file %s failure", tmpFilePath.c_str()); break; } success = fwrite(buffer, 1, size, tmp) > 0; fclose(tmp); if (success) { zipFilePath = tmpFilePath; CCLOG("CCLoadChunksFromZip() - copy zip file to %s ok", tmpFilePath.c_str()); } } while (0); if (buffer) { delete []buffer; } if (!success) { lua_pushboolean(L, 0); break; } #endif CCZipFile *zip = CCZipFile::create(zipFilePath.c_str()); if (zip) { CCLOG("CCLoadChunksFromZip() - load zip file: %s", zipFilePath.c_str()); lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); CCLOG("CCLoadChunksFromZip() - began"); int count = 0; string filename = zip->getFirstFilename(); while (filename.length()) { unsigned long bufferSize = 0; unsigned char *buffer = zip->getFileData(filename.c_str(), &bufferSize); if (bufferSize) { luaL_loadbuffer(L, (char*)buffer, bufferSize, filename.c_str()); lua_pushcclosure(L, &cc_lua_require, 1); lua_setfield(L, -2, filename.c_str()); delete []buffer; ++count; // CCLOG("CCLoadChunksFromZip() - chunk %s", filename.c_str()); } filename = zip->getNextFilename(); } CCLOG("CCLoadChunksFromZip() - ended, chunks count %d", count); lua_pop(L, 2); lua_pushboolean(L, 1); } else { CCLOG("CCLoadChunksFromZip() - not found zip file: %s", zipFilePath.c_str()); lua_pushboolean(L, 0); } #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) unlink(tmpFilePath.c_str()); #endif } while (0); return 1; }
void AppDelegate::initGameView() { // initialize director CCDirector *pDirector = CCDirector::sharedDirector(); pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); pDirector->setProjection(kCCDirectorProjection2D); CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); if(!isPortraitApp) { screenSize = CCSizeMake(screenSize.height, screenSize.width); } std::vector<std::string> resDirOrders; std::string res; TargetPlatform platform = CCApplication::sharedApplication()->getTargetPlatform(); if (platform == kTargetIphone || platform == kTargetIpad) { std::vector<std::string> searchPaths = CCFileUtils::sharedFileUtils()->getSearchPaths(); searchPaths.insert(searchPaths.begin(), "Published files iOS"); searchPaths.insert(searchPaths.begin(), getCCBDirectoryPath()); CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths); if (screenSize.height > 1136) { res = "iPad"; setResolutionSizes(true, true, isPortraitApp); resDirOrders.push_back("resources-ipadhd"); resDirOrders.push_back("resources-ipad"); resDirOrders.push_back("resources-iphonehd"); isIPhone = false; isRetina = true; cocos2d::extension::CCBReader::setResolutionScale(2); } else if(screenSize.height > 1024) { res = "iPhone"; setResolutionSizes(false, true, isPortraitApp); resDirOrders.push_back("resources-iphonehd"); resDirOrders.push_back("resources-iphone"); isIPhone = true; isRetina = true; } else if (screenSize.height > 960) { res = "iPad"; setResolutionSizes(true, false, isPortraitApp); resDirOrders.push_back("resources-ipad"); resDirOrders.push_back("resources-iphonehd"); isIPhone = false; isRetina = false; cocos2d::extension::CCBReader::setResolutionScale(2); } else if (screenSize.height > 480) { res = "iPhone"; setResolutionSizes(false, true, isPortraitApp); resDirOrders.push_back("resources-iphonehd"); resDirOrders.push_back("resources-iphone"); isIPhone = true; isRetina = true; } else { res = "iPhone"; setResolutionSizes(false, false, isPortraitApp); resDirOrders.push_back("resources-iphone"); isIPhone = true; isRetina = false; } } else if (platform == kTargetAndroid || platform == kTargetWindows) { int dpi = -1; dpi = CCDevice::getDPI(); if(dpi > 300) { // retina if (screenSize.height > 1920) { res = "xlarge"; setResolutionSizes(true, true, isPortraitApp); resDirOrders.push_back("resources-xlarge"); resDirOrders.push_back("resources-large"); resDirOrders.push_back("resources-medium"); resDirOrders.push_back("resources-small"); } else { res = "large"; setResolutionSizes(false, true, isPortraitApp); resDirOrders.push_back("resources-large"); resDirOrders.push_back("resources-medium"); resDirOrders.push_back("resources-small"); } } else { // non retina if (screenSize.height > 960) { res = "large"; setResolutionSizes(true, false, isPortraitApp); resDirOrders.push_back("resources-large"); resDirOrders.push_back("resources-medium"); resDirOrders.push_back("resources-small"); cocos2d::extension::CCBReader::setResolutionScale(2); } else if (screenSize.height > 768) { res = "medium"; setResolutionSizes(true, false, isPortraitApp); resDirOrders.push_back("resources-medium"); resDirOrders.push_back("resources-small"); } else if (screenSize.height > 480) { res = "small"; setResolutionSizes(false, false, isPortraitApp); resDirOrders.push_back("resources-small"); } else { setResolutionSizes(false, false, isPortraitApp); res = "xsmall"; resDirOrders.push_back("resources-xsmall"); } } } CCFileUtils *pFileUtils = CCFileUtils::sharedFileUtils(); pFileUtils->setSearchResolutionsOrder(resDirOrders); std::vector<std::string> searchPaths = pFileUtils->getSearchPaths(); searchPaths.insert(searchPaths.begin(), pFileUtils->getWritablePath()); pFileUtils->setSearchPaths(searchPaths); PlayerStatus::setDeviceResolution(res); // turn on display FPS pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60); }