]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor lexical conversion of vertex attributes
authorMikko Rasa <tdb@tdb.fi>
Sun, 28 Mar 2021 13:12:35 +0000 (16:12 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 28 Mar 2021 13:43:45 +0000 (16:43 +0300)
There was an error in the size of the base string for the GENERIC
attribute, causing it to never match.  Using a utility function makes
it less dependent on magic numbers.

Color is still handled specially because of the type suffix.

source/core/vertexformat.cpp

index 2ce02e2edc693189ee0a84f50426d40dc8c6b0ba..65bbea49887ad82f1cdf4097204861bba79e197b 100644 (file)
@@ -105,12 +105,40 @@ VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
        return static_cast<VertexAttribute>(base+index*8);
 }
 
+
+bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
+{
+       string::const_iterator i = str.begin();
+       for(; *name; ++name, ++i)
+               if(*i!=*name)
+                       return false;
+       if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
+               return false;
+       VertexAttribute result = static_cast<VertexAttribute>(base_attr+(*i-'0'-min_size));
+
+       if(++i!=str.end())
+       {
+               if(*i!='_' || ++i==str.end())
+                       return false;
+               try
+               {
+                       result = make_indexed_attribute(result, lexical_cast<unsigned>(string(i, str.end())));
+               }
+               catch(...)
+               {
+                       // The operator>> will throw a more appropriate exception
+                       return false;
+               }
+       }
+
+       attr = result;
+       return true;
+}
+
 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
 {
        const string &str = conv.get();
-       if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
-               a = static_cast<VertexAttribute>(VERTEX2+(str[6]-'2'));
-       else if(str=="NORMAL3")
+       if(str=="NORMAL3")
                a = NORMAL3;
        else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
                a = static_cast<VertexAttribute>(COLOR3_FLOAT+(str[5]-'3'));
@@ -120,33 +148,11 @@ void operator>>(const LexicalConverter &conv, VertexAttribute &a)
                a = TANGENT3;
        else if(str=="BINORMAL3")
                a = BINORMAL3;
-       else if(str.size()==6 && !str.compare(0, 5, "GROUP") && str[5]>='1' && str[5]<='4')
-               a = static_cast<VertexAttribute>(GROUP1+(str[5]-'1'));
-       else if(str.size()==7 && !str.compare(0, 6, "WEIGHT") && str[6]>='1' && str[6]<='4')
-               a = static_cast<VertexAttribute>(WEIGHT1+(str[6]-'1'));
-       else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
-       {
-               if(str.size()==9)
-                       a = static_cast<VertexAttribute>(TEXCOORD1+(str[8]-'1'));
-               else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='3')
-                       a = make_indexed_attribute(static_cast<VertexAttribute>(TEXCOORD1+(str[8]-'1')), str[10]-'0');
-               else
-                       throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
-       }
-       else if(str.size()>=10 && !str.compare(0, 6, "GENERIC") && str[7]>='1' && str[7]<='4' && str[8]=='_')
-       {
-               unsigned n;
-               try
-               {
-                       n = lexical_cast<unsigned>(str.substr(9));
-               }
-               catch(const lexical_error &)
-               {
-                       throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
-               }
-               a = make_indexed_attribute(static_cast<VertexAttribute>(GENERIC1+(str[7]-'1')), n);
-       }
-       else
+       else if(!convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) &&
+               !convert_attribute(str, "GROUP", 1, 4, a, GROUP1) &&
+               !convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) &&
+               !convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) &&
+               !convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1))
                throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
 }