summaryrefslogtreecommitdiff
path: root/src/Helpers
diff options
context:
space:
mode:
Diffstat (limited to 'src/Helpers')
-rw-r--r--src/Helpers/CMakeLists.txt3
-rw-r--r--src/Helpers/Skin.hpp17
-rw-r--r--src/Helpers/Skin/CMakeLists.txt1
-rw-r--r--src/Helpers/Skin/Skin.cpp118
4 files changed, 139 insertions, 0 deletions
diff --git a/src/Helpers/CMakeLists.txt b/src/Helpers/CMakeLists.txt
new file mode 100644
index 0000000..bde671e
--- /dev/null
+++ b/src/Helpers/CMakeLists.txt
@@ -0,0 +1,3 @@
+#Make Model lib
+
+add_subdirectory(./Skin/)
diff --git a/src/Helpers/Skin.hpp b/src/Helpers/Skin.hpp
new file mode 100644
index 0000000..6f01f18
--- /dev/null
+++ b/src/Helpers/Skin.hpp
@@ -0,0 +1,17 @@
+#include <SFML/Graphics/Color.hpp>
+#include <string>
+#include <vector>
+#include <fstream>
+#include <iostream>
+
+
+namespace skin{
+
+ std::vector<sf::Color> loadSkin(std::string skinName);
+
+ std::vector<std::string> removeComments(std::vector<std::string> linesVector);
+
+ bool containOnlySpace(std::string line);
+
+ std::vector<int> extractRGBA(std::string line);
+}
diff --git a/src/Helpers/Skin/CMakeLists.txt b/src/Helpers/Skin/CMakeLists.txt
new file mode 100644
index 0000000..8ad7f17
--- /dev/null
+++ b/src/Helpers/Skin/CMakeLists.txt
@@ -0,0 +1 @@
+add_library(Skin Skin.cpp)
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;
+
+}