个人见解我们进行cocos2dx开发时可以像进行Android开发的那样专门建立一些配置文件例如Android中value下的string,color,dimen等资源更好的利用mvc的设计模式,可以实现配置与程序相分离!
建立的配置文件如下
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<!-- string -->
<string name="app_name">AndroidBitmap</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<!-- color -->
<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 -->
<position name="position1">12,23</position>
<position name="position2">12,23</position>
<!-- colorint -->
<colorint name="ccc31">123,343,344</colorint>
<!-- colordouble -->
<colordouble name="ccc33">0.2,0.5,0.7</colordouble>
</resources>
创建读取配置文件的工具类,使用了rapidxml进行解析的
头文件
#ifndef _GLOBALPARAMETER_H_
#define _GLOBALPARAMETER_H_
#include "rapidxml.hpp"
//#include "rapidxml_iterators.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;
//load
void load();
//color
map<string, string> m_mapColor;
//dimen
map<string, string> m_mapDimen;
//string
map<string, string> m_mapString;
//position
map<string, PointXML<double>> m_mapPosition;
//colorint
map<string, PointColorXML<int>> m_mapColorint;
//colordouble
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
源文件
#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; // character type defaults to char
try {
doc.parse<0>(fdoc.data()); //会改变参数的内容,tmpbuf的生命周期必须到解析完
}
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;
}
测试案例
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!这样修改配置信息时就不用再去修改程序了!