-
Notifications
You must be signed in to change notification settings - Fork 54
/
ColorConfiguration.cpp
130 lines (117 loc) · 4.59 KB
/
ColorConfiguration.cpp
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
#include "ColorConfiguration.h"
#include "ContentLoader.h"
#include <set>
using namespace std;
using namespace DFHack;
using namespace df::enums;
ColorMaterialConfiguration::ColorMaterialConfiguration()
{
color = al_map_rgb(255,255,255);
colorSet = 0;
}
ColorConfiguration::ColorConfiguration()
{
color = al_map_rgb(255,255,255);
colorSet = 0;
}
ColorConfiguration::~ColorConfiguration()
{
colorMaterials.clear();
}
void parseColorElement(TiXmlElement* elemColor, vector<ColorConfiguration> & configTable, MaterialMatcher<ALLEGRO_COLOR> & materialConfig)
{
const char* colorRedStr = elemColor->Attribute("red");
if(colorRedStr == NULL || colorRedStr[0] == 0) {
contentError("Invalid or missing color attribute",elemColor);
return; //nothing to work with
}
const char* colorGreenStr = elemColor->Attribute("green");
if(colorGreenStr == NULL || colorGreenStr[0] == 0) {
contentError("Invalid or missing color attribute",elemColor);
return; //nothing to work with
}
const char* colorBlueStr = elemColor->Attribute("blue");
if (colorBlueStr == NULL || colorBlueStr[0] == 0) {
contentError("Invalid or missing color attribute", elemColor);
return; //nothing to work with
}
int alpha = 255;
const char* colorAlphaStr = elemColor->Attribute("alpha");
if (colorAlphaStr != NULL && colorAlphaStr[0] != 0) {
alpha = atoi(colorAlphaStr);
}
int red = atoi(colorRedStr);
int green = atoi(colorGreenStr);
int blue = atoi(colorBlueStr);
ALLEGRO_COLOR color = al_map_rgba(red, green, blue, alpha);
//parse material elements
TiXmlElement* elemMaterial = elemColor->FirstChildElement("material");
if(elemMaterial == NULL) {
//if none, there's nothing to be done with this color.
contentError("Invalid or missing material attribute",elemColor);
return;
}
for( ; elemMaterial; elemMaterial = elemMaterial->NextSiblingElement("material")) {
//first try to match with a material token
const char* elemToken = elemMaterial->Attribute("token");
if (elemToken && elemToken[0])
{
materialConfig.set_material(color, elemToken);
continue;
}
// get material type
int elemIndex = lookupMaterialType(elemMaterial->Attribute("value"));
if (elemIndex == INVALID_INDEX) {
contentError("Invalid or missing value or token attribute",elemMaterial);
continue;
}
// parse subtype elements
TiXmlElement* elemSubtype = elemMaterial->FirstChildElement("subtype");
if (elemSubtype == NULL) {
// add the configurations
if (configTable.size() <= (uint32_t)elemIndex) {
//increase size if needed
configTable.resize(elemIndex+1);
}
if(configTable.at(elemIndex).colorSet == false) {
configTable.at(elemIndex).color = color;
configTable.at(elemIndex).colorSet = true;
}
return;
}
for ( ; elemSubtype; elemSubtype = elemSubtype ->NextSiblingElement("subtype")) {
// get subtype
int subtypeId = lookupMaterialIndex( elemIndex,elemSubtype->Attribute("value"));
if (subtypeId == INVALID_INDEX) {
contentError("Invalid or missing value attribute",elemSubtype);
continue;
}
// add the configurations
if (configTable.size() <= (uint32_t)elemIndex) {
//increase size if needed
configTable.resize(elemIndex+1);
}
if (configTable.at(elemIndex).colorMaterials.size() <= (uint32_t)subtypeId) {
//increase size if needed
configTable.at(elemIndex).colorMaterials.resize(subtypeId+1);
}
if (configTable.at(elemIndex).colorMaterials.at(subtypeId).colorSet == false) {
configTable.at(elemIndex).colorMaterials.at(subtypeId).color = color;
configTable.at(elemIndex).colorMaterials.at(subtypeId).colorSet = true;
}
}
}
}
bool addSingleColorConfig( TiXmlElement* elemRoot)
{
string elementType = elemRoot->Value();
if(elementType.compare( "colors" ) == 0) {
//parse colors
TiXmlElement* elemColor = elemRoot->FirstChildElement("color");
while( elemColor ) {
parseColorElement( elemColor, contentLoader->colorConfigs, contentLoader->materialColorConfigs);
elemColor = elemColor->NextSiblingElement("color");
}
}
return true;
}