summaryrefslogtreecommitdiff
path: root/src/Helpers/Skin/Skin.cpp
diff options
context:
space:
mode:
authormanzerbredes <loic.guegan_secondary@yahoo.fr>2015-05-05 16:30:46 +0200
committermanzerbredes <loic.guegan_secondary@yahoo.fr>2015-05-05 16:30:46 +0200
commit1220e2d70f40139bed69602041ba373297103573 (patch)
tree49a2443cdacee9c35abfa005a5d703ba7efa6e23 /src/Helpers/Skin/Skin.cpp
parentf7610d669bcf3986827a905fc4bc03946be0b0c6 (diff)
Add loadable skin and correct some things
Diffstat (limited to 'src/Helpers/Skin/Skin.cpp')
-rw-r--r--src/Helpers/Skin/Skin.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/Helpers/Skin/Skin.cpp b/src/Helpers/Skin/Skin.cpp
new file mode 100644
index 0000000..b421dae
--- /dev/null
+++ b/src/Helpers/Skin/Skin.cpp
@@ -0,0 +1,118 @@
+#include "../Skin.hpp"
+
+
+
+std::vector<sf::Color> skin::loadSkin(std::string skinName){
+
+ std::vector<sf::Color> skinLoaded;
+
+ std::ifstream skinFile("./bin/skin/"+skinName+"/skin.txt");
+
+
+ try{
+ if(!skinFile.is_open())
+ throw 1;
+
+ }
+ catch(int code){
+
+ std::cout << "Failed to open skin " + skinName + "check the location and the permissions !";
+ exit(code);
+ }
+
+
+ std::string line;
+ std::vector<std::string> linesVector;
+
+ while (std::getline(skinFile, line))
+ {
+ if(!line.empty() && !skin::containOnlySpace(line))
+ linesVector.push_back(line);
+ }
+
+ linesVector=skin::removeComments(linesVector);
+
+ for(int i=0;i<linesVector.size();i++){
+
+ std::vector<int> rgba;
+ rgba=skin::extractRGBA(linesVector.at(i));
+ if(rgba.size()==3)
+ skinLoaded.push_back(sf::Color(rgba.at(0), rgba.at(1), rgba.at(2)));
+ else
+ skinLoaded.push_back(sf::Color(rgba.at(0), rgba.at(1), rgba.at(2),rgba.at(3)));
+ }
+
+ return skinLoaded;
+}
+
+std::vector<std::string> skin::removeComments(std::vector<std::string> linesVector){
+
+ std::vector<std::string> linesWitouthCom;
+
+ for(int i=0;i<linesVector.size();i++){
+ std::string currentLine(linesVector.at(i));
+
+ if(currentLine[0]!='#'){
+ size_t posCom=currentLine.find("#");
+
+ if(posCom != std::string::npos)
+ linesWitouthCom.push_back(currentLine.erase(posCom,currentLine.size()));
+ else
+ linesWitouthCom.push_back(currentLine);
+
+ }
+ }
+
+ return linesWitouthCom;
+}
+
+
+
+bool skin::containOnlySpace(std::string line){
+ for(int i=0;i<line.size();i++){
+
+ if(line[i] != ' ')
+ return false;
+
+ }
+ return true;
+}
+
+
+std::vector<int> skin::extractRGBA(std::string line){
+ std::vector<int> rgba;
+
+ std::string numberFound;
+
+ int start=0;
+ try {
+ for(int j=0;j<3;j++){
+ for(int i=start;i<line.size();i++){
+ char ch=line.at(i);
+
+ if(ch>='0' && ch <='9'){
+ numberFound.push_back(ch);
+ }
+ else if(ch==' ' && numberFound.size() > 0){
+ start=i;
+ break;
+ }
+ }
+ rgba.push_back(std::stoi(numberFound));
+ numberFound.clear();
+ }
+
+
+ if(rgba.size()<3 || rgba.size()>4){
+ throw "Invalid thème format";
+ exit(1);
+ }
+ }
+ catch(std::string error){
+ std::cout << error;
+ exit(1);
+ }
+
+ return rgba;
+
+}