summaryrefslogtreecommitdiff
path: root/src/Helpers/Skin/Skin.cpp
blob: 6b36295e13c9e5553e096a5ffb8f0a8fdde233cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "../Skin.hpp"



std::vector<sf::Color> skin::loadShader(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;

}
std::vector<sf::Color> skin::loadSkin(std::string skinName){
	sf::Image game;
	game.loadFromFile("bin/skin/"+skinName+"/game.png");

	std::vector<sf::Color> skin;



	//Background 
	skin.push_back(game.getPixel(0,0));	//Background MainWindow
	skin.push_back(game.getPixel(76,243));	//Background cells
	skin.push_back(game.getPixel(61,227)); 	//Background grid color
	skin.push_back(game.getPixel(326,58)); 	//Score bg
	skin.push_back(game.getPixel(441,58)); 	//Best score bg
	skin.push_back(sf::Color(143,122,102)); 	//Button bg 
	skin.push_back(sf::Color(238,228,218,186)); 		//Game over color bg
	
	//Font
	skin.push_back(game.getPixel(65,105)); 	//Title font color
	skin.push_back(game.getPixel(348,78)); 	//Score title fontcolor
	skin.push_back(game.getPixel(468,77)); 	//Best score title font color
	skin.push_back(game.getPixel(400,96)); 	//Score font color
	skin.push_back(game.getPixel(484,97)); 	//Best score font color
	skin.push_back(game.getPixel(128,660)); 	//2 and 4 font color
	skin.push_back(game.getPixel(359,661)); 	//other number font Color
	skin.push_back(sf::Color(143,122,102)); 	//game over font 

	//Skin 2 et le 4
	skin.push_back(game.getPixel(70,597)); //2
	skin.push_back(game.getPixel(190,597)); //4

	//Skin 8 à 64
	skin.push_back(game.getPixel(311,597)); //8
	skin.push_back(game.getPixel(431,597)); //16
	skin.push_back(game.getPixel(71,477)); //32
	skin.push_back(game.getPixel(191,477)); //64

	//Skin 128 à 2048
	skin.push_back(game.getPixel(312,477)); //128
	skin.push_back(game.getPixel(431,477)); //256
	skin.push_back(game.getPixel(71,358)); //512
	skin.push_back(game.getPixel(191,358)); //1024
	skin.push_back(game.getPixel(311,358)); //2048

	//Skin for other number
	skin.push_back(game.getPixel(432,358)); //More than 2048

	return skin;
}