Add loadable skin and correct some things

This commit is contained in:
manzerbredes 2015-05-05 16:30:46 +02:00
parent f7610d669b
commit 1220e2d70f
15 changed files with 245 additions and 18 deletions

View file

@ -0,0 +1,3 @@
#Make Model lib
add_subdirectory(./Skin/)

17
src/Helpers/Skin.hpp Normal file
View file

@ -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);
}

View file

@ -0,0 +1 @@
add_library(Skin Skin.cpp)

118
src/Helpers/Skin/Skin.cpp Normal file
View file

@ -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;
}