Advertisement
Guest User

AutoResLoader.cpp

a guest
Oct 22nd, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. /* Corresponding header */
  2. #include "utils/tools/AutoResLoader.h"
  3.  
  4. /* C system icnludes */
  5.  
  6. /* C++ system icnludes */
  7. #include <sstream>
  8. #include <iostream>
  9. #include <cstdint>
  10.  
  11. /* Third-party icnludes */
  12.  
  13. /* Own icnludes */
  14. #include "sdl_utils/config/ImageContainerConfig.h"
  15. #include "utils/tools/ConfigValidator.h"
  16. #include "common/CommonDefines.h"
  17.  
  18. ImageConfig getWidthAndHeight(const std::string& file) {
  19.     // File name pattern [NAME][_][ID][_][WIDTH][_][HEIGHT]
  20.     ImageConfig info;
  21.     std::string tempStr;
  22.  
  23.     std::istringstream istr(file);
  24.     getline(istr, info.name, '_');
  25.    
  26.     getline(istr, tempStr, '_');
  27.     ConfigValidator::checkConfig(tempStr, INT);
  28.     info.id = std::stoi(tempStr);
  29.  
  30.     getline(istr, tempStr, '_');
  31.     ConfigValidator::checkConfig(tempStr, INT);
  32.     info.width = std::stoi(tempStr);
  33.    
  34.     getline(istr, tempStr, '.');
  35.     ConfigValidator::checkConfig(tempStr, INT);
  36.     info.height = std::stoi(tempStr);
  37.  
  38.     //getline(istr, config.type);
  39.  
  40.     return info;
  41. }
  42.  
  43. std::vector<ImageConfig> AutoResLoader::getFileConfigFromFolder() {
  44.     DIR* dir;
  45.     dirent* file;
  46.     if ((dir = opendir(RES_FOLDER_PATH.c_str())) == nullptr) {
  47.         std::cerr << "Could not open resource directory: "
  48.             << RES_FOLDER_PATH << std::endl;
  49.     }
  50.  
  51.     std::vector<ImageConfig> imageData(TextureId::RES_COUNT);
  52.     while ((file = readdir(dir)) != nullptr) {
  53.         const std::string currFile = file->d_name;
  54.         if (currFile == "." || currFile == "..") {
  55.             continue;
  56.         }
  57.         ImageConfig currImageConfig = getWidthAndHeight(currFile);
  58.         currImageConfig.location = RES_FOLDER_PATH + currFile;
  59.  
  60.         imageData[currImageConfig.id] = currImageConfig;
  61.     }
  62.  
  63.     closedir(dir);
  64.     return imageData;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement