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):
29 data = new unsigned char[(f.data[0]&~7)+8];
30 memcpy(data, f.data, f.data[0]+1);
34 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
39 data = new unsigned char[(f.data[0]&~7)+8];
40 memcpy(data, f.data, f.data[0]+1);
48 VertexFormat::~VertexFormat()
53 unsigned VertexFormat::stride() const
56 for(const unsigned char *i=begin(); i!=end(); ++i)
61 int VertexFormat::offset(VertexComponent comp, unsigned index) const
63 if((comp<TEXCOORD1 && index>0) || (comp<ATTRIB1 && index>=8) || index>=53)
64 throw out_of_range("VertexFormat::offset");
66 unsigned type = (comp>>2)+index;
67 unsigned size = comp&3;
69 for(const unsigned char *i=begin(); i!=end(); ++i)
71 if(static_cast<unsigned>(*i>>2)==type)
85 VertexFormat operator,(const VertexFormat &f, VertexComponent c)
90 const unsigned char n = ++r.data[0];
93 unsigned char *newdt = new unsigned char[n+9];
94 memcpy(newdt, r.data, n);
102 r.data = new unsigned char[8];
110 VertexFormat operator,(const VertexFormat &f, unsigned i)
113 throw invalid_operation("VertexFormat::operator,");
115 unsigned char *c = r.data+r.data[0];
116 if((*c<TEXCOORD1 && i>0) || (*c<ATTRIB1 && i>=8) || i>=53)
117 throw out_of_range("VertexFormat::operator,");
123 void operator>>(const LexicalConverter &conv, VertexComponent &c)
125 const string &str = conv.get();
126 if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
127 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
128 else if(str=="NORMAL3")
130 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
131 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
132 else if(str=="COLOR4_UBYTE")
134 else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
137 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
138 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
139 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
141 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
143 else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
148 n = lexical_cast<unsigned>(str.substr(8));
150 catch(const lexical_error &)
152 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
154 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
157 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
160 // XXX This will go away eventually
161 void operator>>(const LexicalConverter &conv, VertexFormat &f)
163 vector<string> parts = split(conv.get(), '_');
164 for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
168 else if(i->size()==7 && !i->compare(0, 5, "COLOR") && (*i)[6]=='F')
169 *i = i->substr(0, 6)+"_FLOAT";
170 else if(i->size()>=10 && !i->compare(0, 8, "TEXCOORD"))
171 *i = i->substr(0, 9)+"_"+i->substr(9);
172 else if(i->size()>=8 && !i->compare(0, 6, "ATTRIB"))
173 *i = i->substr(0, 7)+"_"+i->substr(7);
176 for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
177 f = (f, lexical_cast<VertexComponent>(*i));