Advertisement
Guest User

Loading Config/Map File

a guest
Jul 14th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. class Application
  6. {
  7. public:
  8.     bool load();
  9.  
  10. private:
  11.     std::vector<std::string>& split(const std::string &s, char delim, std::vector<std::string> &elems);
  12.     std::vector<std::string> split(const std::string &s, char delim);
  13.    
  14.     std::vector<std::unique_ptr<sf::Texture> > mTextures;
  15.     std::vector<sf::Text> mNames;
  16.     std::vector<sf::Sprite> mSprite;
  17.     std::vector<sf::Time> mTimestamps;
  18.    
  19. };
  20.  
  21. std::vector<std::string>& Application::split(const std::string &s, char delim, std::vector<std::string> &elems)
  22. {
  23.     std::stringstream ss(s);
  24.     std::string item;
  25.     while(std::getline(ss, item, delim))
  26.     {
  27.         elems.push_back(item);
  28.     }
  29.     return elems;
  30. }
  31.  
  32.  
  33. std::vector<std::string> Application::split(const std::string &s, char delim)
  34. {
  35.     std::vector<std::string> elems;
  36.     return split(s, delim, elems);
  37. }
  38.  
  39. template <class T>
  40. bool Application::fromString(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&))
  41. {
  42.     std::istringstream iss(s);
  43.     return !(iss >> f >> t).fail();
  44. }
  45.  
  46. bool Application::load()
  47. {
  48.     std::string line;
  49.     std::ifstream file("config.cfg");
  50.     if(file.fail())
  51.         return false;
  52.  
  53.     while(!file.eof())
  54.     {
  55.         std::getline(file, line);
  56.  
  57.         // Commented line
  58.         if(line.find_first_of("#") != line.npos)
  59.             continue;
  60.  
  61.         std::vector<std::string> splittedLine(split(line, ':'));
  62.         if(splittedLine.size() != 5)
  63.             continue;
  64.        
  65.         // Load texture
  66.         std::unique_ptr<sf::Texture> tex(new sf::Texture);
  67.         if(!tex->loadFromFile("img/games/" + splittedLine[0]))
  68.             continue;
  69.         mTextures.push_back(std::move(tex));
  70.        
  71.         // Sprites
  72.         sf::Sprite sprite(*mTextures.back());
  73.         float x = 0, y = 0;
  74.         if(!fromString<float>(x, splittedLine[2]) || !fromString<float>(y, splittedLine[3]))
  75.         {
  76.             mTextures.pop_back();
  77.             mNames.pop_back();
  78.             std::cout << "Positions not set..." << std::endl;
  79.             continue;
  80.         }
  81.         sprite.setPosition(x, y);
  82.         mSprite.push_back(std::move(sprite));
  83.  
  84.         // Move back the name
  85.         sf::Text text(splittedLine[1], sf::Font::getDefaultFont(), 14);
  86.         text.setOrigin(text.getLocalBounds().width/2.f, 0);
  87.         text.setPosition(x+32, y+78);
  88.         mNames.push_back(std::move(text));
  89.  
  90.         // Animation timestamp
  91.         int timestamp = 0;
  92.         if(!fromString<int>(timestamp, splittedLine[4]))
  93.         {
  94.             mTextures.pop_back();
  95.             mNames.pop_back();
  96.             mSprite.pop_back();
  97.             std::cout << "Timestep not set..." << std::endl;
  98.             continue;
  99.         }
  100.         mTimestamps.push_back(std::move(sf::milliseconds(timestamp)));
  101.  
  102.         mFirst.push_back(false);
  103.     }
  104.    
  105.     return true;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement