-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathColorConfiguration.cpp
118 lines (108 loc) · 4.8 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
#include <vector>
#include "ColorConfiguration.h"
#include "ContentLoader.h"
#include "StonesenseState.h"
using std::string;
using std::vector;
namespace {
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
size_t newIndex{ size_t(elemIndex) };
TiXmlElement* elemSubtype = elemMaterial->FirstChildElement("subtype");
if (elemSubtype == NULL) {
// add the configurations
if (configTable.size() <= newIndex) {
//increase size if needed
configTable.resize(newIndex+1);
}
if(configTable.at(newIndex).colorSet == false) {
configTable.at(newIndex).color = color;
configTable.at(newIndex).colorSet = true;
}
return;
}
for ( ; elemSubtype; elemSubtype = elemSubtype ->NextSiblingElement("subtype")) {
// get subtype
int subtypeId = lookupMaterialIndex( newIndex,elemSubtype->Attribute("value"));
if (subtypeId == INVALID_INDEX) {
contentError("Invalid or missing value attribute",elemSubtype);
continue;
}
size_t newSubtypeIndex{ size_t(subtypeId) };
// add the configurations
if (configTable.size() <= newSubtypeIndex) {
//increase size if needed
configTable.resize(newIndex+1);
}
if (configTable.at(newIndex).colorMaterials.size() <= newSubtypeIndex) {
//increase size if needed
configTable.at(newIndex).colorMaterials.resize(newSubtypeIndex+1);
}
if (configTable.at(newIndex).colorMaterials.at(newSubtypeIndex).colorSet == false) {
configTable.at(newIndex).colorMaterials.at(newSubtypeIndex).color = color;
configTable.at(newIndex).colorMaterials.at(newSubtypeIndex).colorSet = true;
}
}
}
}
}
bool addSingleColorConfig( TiXmlElement* elemRoot)
{
string elementType = elemRoot->Value();
if(elementType.compare( "colors" ) == 0) {
//parse colors
TiXmlElement* elemColor = elemRoot->FirstChildElement("color");
auto& contentLoader = stonesenseState.contentLoader;
while( elemColor ) {
parseColorElement( elemColor, contentLoader->colorConfigs, contentLoader->materialColorConfigs);
elemColor = elemColor->NextSiblingElement("color");
}
}
return true;
}