转载自:http://blog.csdn.net/u010154424/article/details/51260558
个人见解我们进行cocos2dx开发时可以像进行Android开发的那样专门建立一些配置文件例如Android中value下的string,color,
dimen等资源更好的利用mvc的设计模式,可以实现配置与程序相分离!建立的配置文件如下
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<string name="app_name">AndroidBitmap</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<color name="comm_head_back">#f7514b</color>
<color name="comm_title_text">#FFFFFF</color>
<color name="bottom_hong">#f85d58</color>
<color name="bottom_qian">#929292</color>
<color name="black">#000000</color>
<color name="comm_back_ground">#B2B2B2</color>
<color name="comm_back_shaixuan">#F4F4F4</color>
<color name="comm_line_color">#B2B2B2</color>
<color name="com_title_1">#b1b1b1</color>
<color name="com_title_2">#007aff</color>
<position name="position1">12,23</position>
<position name="position2">12,23</position>
<colorint name="ccc31">123,343,344</colorint>
<colordouble name="ccc33">0.2,0.5,0.7</colordouble>
</resources>
- 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
- 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
创建读取配置文件的工具类,使用了rapidxml进行解析的 ,cocos2dx没有自带rapidxml,这里下载:
rapidxml官网:http://rapidxml.sourceforge.net/
自己加入自己的程序即可
头文件
#ifndef _GLOBALPARAMETER_H_
#define _GLOBALPARAMETER_H_
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
#include "rapidxml_utils.hpp"
#include <map>
using namespace std;
using namespace rapidxml;
template <typename T>
class PointXML
{
public:
PointXML(){}
PointXML(const T x, const T y)
{
this->x = x;
this->y = y;
}
T x;
T y;
};
template <typename T>
class PointColorXML
{
public:
PointColorXML(){}
PointColorXML(const T r, const T g, const T b)
{
this->r = r;
this->g = g;
this->b = b;
}
T r;
T g;
T b;
};
class GlobalParameter
{
private :
GlobalParameter();
static GlobalParameter* m_GlobalParameter;
void load();
map<string, string> m_mapColor;
map<string, string> m_mapDimen;
map<string, string> m_mapString;
map<string, PointXML<double>> m_mapPosition;
map<string, PointColorXML<int>> m_mapColorint;
map<string, PointColorXML<double>> m_mapColordouble;
std::vector<std::string> split(std::string str, std::string pattern);
public:
static GlobalParameter* getInstance();
string getColor(string name);
string getDimen(string name);
string getString(string name);
PointXML<double> getPosition(string name);
PointColorXML<int> getColorint(string name);
PointColorXML<double> getColordouble(string name);
};
#endif
- 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
- 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
源文件
#include "GlobalParameter.h"
#include <stdlib.h>
GlobalParameter* GlobalParameter::m_GlobalParameter = NULL;
GlobalParameter::GlobalParameter()
{
load();
}
GlobalParameter* GlobalParameter::getInstance()
{
if (m_GlobalParameter == NULL)
{
m_GlobalParameter = new GlobalParameter();
}
return m_GlobalParameter;
}
string GlobalParameter::getColor(string name)
{
map<string, string >::iterator iteratorl= m_mapColor.find(name);
if (iteratorl == m_mapColor.end())
return "";
else
return m_mapColor.find(name)->second;
}
string GlobalParameter::getDimen(string name)
{
map<string, string >::iterator iteratorl = m_mapDimen.find(name);
if (iteratorl == m_mapDimen.end())
return "";
else
return m_mapDimen.find(name)->second;
}
string GlobalParameter::getString(string name)
{
map<string, string >::iterator iteratorl = m_mapString.find(name);
if (iteratorl == m_mapString.end())
return "";
else
return m_mapString.find(name)->second;
}
PointXML<double> GlobalParameter::getPosition(string name)
{
map<string, PointXML<double> >::iterator iteratorl = m_mapPosition.find(name);
if (iteratorl == m_mapPosition.end())
return PointXML<double>(0, 0);
else
return m_mapPosition.find(name)->second;
}
PointColorXML<int> GlobalParameter::getColorint(string name)
{
map<string, PointColorXML<int> >::iterator iteratorl = m_mapColorint.find(name);
if (iteratorl == m_mapColorint.end())
return PointColorXML<int>(0, 0,0);
else
return m_mapColorint.find(name)->second;
}
PointColorXML<double> GlobalParameter::getColordouble(string name)
{
map<string, PointColorXML<double> >::iterator iteratorl = m_mapColordouble.find(name);
if (iteratorl == m_mapColordouble.end())
return PointColorXML<double>(0, 0, 0);
else
return m_mapColordouble.find(name)->second;
}
void GlobalParameter::load()
{
file<> fdoc("E://demo.xml");
xml_document<> doc;
try {
doc.parse<0>(fdoc.data());
}
catch (rapidxml::parse_error &e) {
return;
}
xml_node<>* root = doc.first_node();
xml_node<>* message = root->first_node();
for (; message; message = message->next_sibling())
{
if (message->value_size() != NULL)
{
string s = message->name();
if ( s == "dimen")
{
xml_attribute<>* att = message->first_attribute("name");
m_mapDimen.insert(pair<string, string>(att->value(), message->value()));
}
else if (s == "string")
{
xml_attribute<>* att = message->first_attribute("name");
m_mapString.insert(pair<string, string>(att->value(), message->value()));
}
else if (s == "color")
{
xml_attribute<>* att = message->first_attribute("name");
m_mapColor.insert(pair<string, string>(att->value(), message->value()));
}
else if (s == "position")
{
xml_attribute<>* att = message->first_attribute("name");
string value = message->value();
std::vector<std::string> ve = split(value, ",");
PointXML<double> p;
if (ve.size() < 2)
{
if (ve.size() == 1)
{
p.x = atof(ve[0].data());
p.y = 0;
}
else
{
p.x = 0;
p.y = 0;
}
}
else
{
p.x = atof(ve[0].data());
p.y = atof(ve[1].data());
}
m_mapPosition.insert(pair<string, PointXML<double>>(att->value(), p));
}
else if (s == "colorint")
{
xml_attribute<>* att = message->first_attribute("name");
string value = message->value();
std::vector<std::string> ve = split(value, ",");
PointColorXML<int> p;
if (ve.size() < 3)
{
p.r = 0;
p.g = 0;
p.b = 0;
}
else
{
p.r = atoi(ve[0].data());
p.g = atoi(ve[1].data());
p.b = atoi(ve[2].data());
}
m_mapColorint.insert(pair<string, PointColorXML<int>>(att->value(), p));
}
else if (s == "colordouble")
{
xml_attribute<>* att = message->first_attribute("name");
string value = message->value();
std::vector<std::string> ve = split(value, ",");
PointColorXML<double> p;
if (ve.size() < 3)
{
p.r = 0;
p.g = 0;
p.b = 0;
}
else
{
p.r = atof(ve[0].data());
p.g = atof(ve[1].data());
p.b = atof(ve[2].data());
}
m_mapColordouble.insert(pair<string, PointColorXML<double>>(att->value(), p));
}
}
}
}
std::vector<std::string> GlobalParameter::split(std::string str, std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str += pattern;
int size=str.size();
for(int i=0; i<size; i++)
{
pos = str.find(pattern, i);
if(pos<size)
{
std::string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;
}
- 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
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 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
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
测试案例
GlobalParameter* m_Globalparameter = GlobalParameter::getInstance();
cout << m_Globalparameter->getColor("comm_head_back") << endl;
cout << m_Globalparameter->getString("app_name") << endl;
cout << m_Globalparameter->getDimen("activity_horizontal_margin") << endl;
PointXML<double> p = m_Globalparameter->getPosition("position1");
cout << p.x << "----" << p.y << endl;
PointColorXML<int> pi = m_Globalparameter->getColorint("ccc31");
cout << pi.r << "----" << pi.g << "---------" << pi.b << endl;
PointColorXML<double> pd = m_Globalparameter->getColordouble("ccc33");
cout << pd.r << "----" << pd.g << "---------" << pd.b << endl;
OK!这样修改配置信息时就不用再去修改程序了!