Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- class Application
- {
- public:
- bool load();
- private:
- std::vector<std::string>& split(const std::string &s, char delim, std::vector<std::string> &elems);
- std::vector<std::string> split(const std::string &s, char delim);
- std::vector<std::unique_ptr<sf::Texture> > mTextures;
- std::vector<sf::Text> mNames;
- std::vector<sf::Sprite> mSprite;
- std::vector<sf::Time> mTimestamps;
- };
- std::vector<std::string>& Application::split(const std::string &s, char delim, std::vector<std::string> &elems)
- {
- std::stringstream ss(s);
- std::string item;
- while(std::getline(ss, item, delim))
- {
- elems.push_back(item);
- }
- return elems;
- }
- std::vector<std::string> Application::split(const std::string &s, char delim)
- {
- std::vector<std::string> elems;
- return split(s, delim, elems);
- }
- template <class T>
- bool Application::fromString(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&))
- {
- std::istringstream iss(s);
- return !(iss >> f >> t).fail();
- }
- bool Application::load()
- {
- std::string line;
- std::ifstream file("config.cfg");
- if(file.fail())
- return false;
- while(!file.eof())
- {
- std::getline(file, line);
- // Commented line
- if(line.find_first_of("#") != line.npos)
- continue;
- std::vector<std::string> splittedLine(split(line, ':'));
- if(splittedLine.size() != 5)
- continue;
- // Load texture
- std::unique_ptr<sf::Texture> tex(new sf::Texture);
- if(!tex->loadFromFile("img/games/" + splittedLine[0]))
- continue;
- mTextures.push_back(std::move(tex));
- // Sprites
- sf::Sprite sprite(*mTextures.back());
- float x = 0, y = 0;
- if(!fromString<float>(x, splittedLine[2]) || !fromString<float>(y, splittedLine[3]))
- {
- mTextures.pop_back();
- mNames.pop_back();
- std::cout << "Positions not set..." << std::endl;
- continue;
- }
- sprite.setPosition(x, y);
- mSprite.push_back(std::move(sprite));
- // Move back the name
- sf::Text text(splittedLine[1], sf::Font::getDefaultFont(), 14);
- text.setOrigin(text.getLocalBounds().width/2.f, 0);
- text.setPosition(x+32, y+78);
- mNames.push_back(std::move(text));
- // Animation timestamp
- int timestamp = 0;
- if(!fromString<int>(timestamp, splittedLine[4]))
- {
- mTextures.pop_back();
- mNames.pop_back();
- mSprite.pop_back();
- std::cout << "Timestep not set..." << std::endl;
- continue;
- }
- mTimestamps.push_back(std::move(sf::milliseconds(timestamp)));
- mFirst.push_back(false);
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement