SHARE
TWEET

path loading

a guest Mar 25th, 2014 395 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void GameConfig::loadFilePaths() {
  2.     // load file paths
  3.     unsigned long configDataSize = 0;
  4.     unsigned char *t = (FileUtils::getInstance()->getFileData("Res/config.json", "r", &configDataSize));
  5.    
  6.     char *configData = new char[configDataSize + 1];
  7.     memcpy(configData, t, configDataSize);
  8.     configData[configDataSize] = NULL;
  9.    
  10.     delete[] t;
  11.     t = NULL;
  12.    
  13.    
  14.     LanguageType currentLanguage = Application::getInstance()->getCurrentLanguage();
  15.    
  16.     if(!Localized::isLanguageSupported(currentLanguage)) {
  17.         CCLOG("Language is not supported!");
  18.         currentLanguage = LanguageType::ENGLISH;
  19.     }
  20.    
  21.     if(currentLanguageType != -1) {
  22.         currentLanguage = static_cast<LanguageType>(currentLanguageType);
  23.     } else {
  24.         currentLanguageType = static_cast<int>(currentLanguage);
  25.     }
  26.    
  27.     string languagePrefix = Localized::languageShortNameForType(currentLanguage);
  28.    
  29.     auto addResourcePath = [=](const string &path) {
  30.         FileUtils::getInstance()->addSearchPath((path + languagePrefix).c_str());
  31.         FileUtils::getInstance()->addSearchPath(path.c_str());
  32.     };
  33.    
  34.     Size frameSize = Director::getInstance()->getOpenGLView()->getFrameSize();
  35.    
  36.     rapidjson::Document configDoc;
  37.     configDoc.Parse<0>(configData);
  38.    
  39.     string layoutBaseDimensionType = configDoc["layoutBaseDimension"].GetString();
  40.    
  41.     float frameBaseDimension = frameSize.height;
  42.    
  43.     if(layoutBaseDimensionType.compare("width") == 0) {
  44.         frameBaseDimension = frameSize.width;
  45.     }
  46.  
  47.     const auto &designEntriesArray = configDoc["designResolutionEntries"];
  48.     if(designEntriesArray.IsArray()) {
  49.         for(int i = designEntriesArray.Size() - 1; i >= 0; --i) {
  50.             const auto &currentDeviceEntry = designEntriesArray[i];
  51.            
  52.             if(currentDeviceEntry.IsObject()) {
  53.                 double currentResolutionBaseDimension = currentDeviceEntry[layoutBaseDimensionType.c_str()].GetDouble();
  54.                
  55.                 if(frameBaseDimension > currentResolutionBaseDimension) {
  56.                     int requiredEntryIndex = MIN(i + 1, designEntriesArray.Size() - 1);
  57.                     const auto &requiredEntry = designEntriesArray[requiredEntryIndex];
  58.                     double requiredBaseDimension = requiredEntry[layoutBaseDimensionType.c_str()].GetDouble();
  59.                    
  60.                     //
  61.                     double layoutWidth = requiredEntry["designWidth"].GetDouble();
  62.                     double layoutHeight = requiredEntry["designHeight"].GetDouble();
  63.                    
  64.                     this->setDesignLayoutSize({layoutWidth, layoutHeight});
  65.                    
  66.                     float designLayoutBaseDimension = layoutHeight;
  67.                     ResolutionPolicy policy = ResolutionPolicy::FIXED_HEIGHT;
  68.                    
  69.                     if(layoutBaseDimensionType.compare("width") == 0) {
  70.                         designLayoutBaseDimension = layoutWidth;
  71.                         //frameBaseDimension = frameSize.width;
  72.                         policy = ResolutionPolicy::FIXED_WIDTH;
  73.                     }
  74.                    
  75.                     Director::getInstance()->getOpenGLView()->setDesignResolutionSize(layoutWidth, layoutHeight, policy);
  76.                     Director::getInstance()->setContentScaleFactor(requiredBaseDimension / designLayoutBaseDimension);
  77.                    
  78.                     Size visibleSize = Director::getInstance()->getVisibleSize();
  79.                     this->setVisibleOrigin({(visibleSize.width - layoutWidth) / 2.0, (visibleSize.height - layoutHeight) / 2.0});
  80.                     //
  81.  
  82.                     // load required entry and all the entries above
  83.                     for(; requiredEntryIndex >= 0; --requiredEntryIndex) {
  84.                         const auto &entryToLoad = designEntriesArray[requiredEntryIndex];
  85.                        
  86.                         if(entryToLoad.IsObject()) {
  87.                             for(auto it = entryToLoad["paths"].Begin(); it != entryToLoad["paths"].End(); ++it) {
  88.                                 string path = it->GetString();
  89.                                 addResourcePath(path);
  90.                             }
  91.                         }
  92.                     }
  93.                    
  94.                     // stop iterating all the rest entries
  95.                     break;
  96.                 }
  97.             }
  98.         }
  99.     }
  100.  
  101.     // load shared paths
  102.    
  103.     const auto &sharedPathsArray = configDoc["sharedPaths"];
  104.     if(sharedPathsArray.IsArray()) {
  105.         for(auto it = sharedPathsArray.Begin(); it != sharedPathsArray.End(); ++it) {
  106.             string path = it->GetString();
  107.             addResourcePath(path);
  108.         }
  109.     }
  110.    
  111.     delete[] configData;
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top