2 #include <msp/strings/format.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
6 #include "vertexformat.h"
13 VertexFormat::VertexFormat():
17 VertexFormat::VertexFormat(VertexComponent c):
18 data(new unsigned char[8])
24 VertexFormat::VertexFormat(const VertexFormat &f):
30 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
35 data = new unsigned char[(f.data[0]&~7)+8];
36 copy(f.data, f.data+f.data[0]+1, data);
44 VertexFormat::~VertexFormat()
49 VertexFormat VertexFormat::operator,(VertexComponent c) const
51 VertexFormat r = *this;
54 const unsigned char n = ++r.data[0];
57 unsigned char *newdt = new unsigned char[n+8];
58 copy(r.data, r.data+n, newdt);
66 r.data = new unsigned char[8];
74 VertexFormat VertexFormat::operator,(unsigned i) const
77 throw invalid_operation("VertexFormat::operator,");
78 VertexFormat r = *this;
79 unsigned char *c = r.data+r.data[0];
80 if((*c<TEXCOORD1 && i>0) || (*c<ATTRIB1 && i>=8) || i>=53)
81 throw invalid_argument("VertexFormat::operator,");
87 unsigned VertexFormat::stride() const
90 for(const unsigned char *i=begin(); i!=end(); ++i)
91 s += get_component_size(*i);
95 int VertexFormat::offset(VertexComponent comp, unsigned index) const
97 if((comp<TEXCOORD1 && index>0) || (comp<ATTRIB1 && index>=8) || index>=53)
98 throw out_of_range("VertexFormat::offset");
100 unsigned type = get_component_type(comp)+index;
101 unsigned size = get_component_size(comp);
103 for(const unsigned char *i=begin(); i!=end(); ++i)
105 if(get_component_type(*i)==type)
107 if(get_component_size(*i)>=size)
113 offs += get_component_size(*i);
119 void operator>>(const LexicalConverter &conv, VertexComponent &c)
121 const string &str = conv.get();
122 if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
123 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
124 else if(str=="NORMAL3")
126 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
127 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
128 else if(str=="COLOR4_UBYTE")
130 else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
133 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
134 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
135 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
137 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
139 else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
144 n = lexical_cast<unsigned>(str.substr(8));
146 catch(const lexical_error &)
148 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
150 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
153 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
156 // XXX This will go away eventually
157 void operator>>(const LexicalConverter &conv, VertexFormat &f)
159 vector<string> parts = split(conv.get(), '_');
160 for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
164 else if(i->size()==7 && !i->compare(0, 5, "COLOR") && (*i)[6]=='F')
165 *i = i->substr(0, 6)+"_FLOAT";
166 else if(i->size()>=10 && !i->compare(0, 8, "TEXCOORD"))
167 *i = i->substr(0, 9)+"_"+i->substr(9);
168 else if(i->size()>=8 && !i->compare(0, 6, "ATTRIB"))
169 *i = i->substr(0, 7)+"_"+i->substr(7);
172 for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
173 f = (f, lexical_cast<VertexComponent>(*i));