]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/vertexformat.cpp
Use default member initializers for simple types
[libs/gl.git] / source / core / vertexformat.cpp
index 5d0b078fe06f154b2821ab176a2bf9310132c253..ff043f7d0466bef73b08e216836617ef8fe9200f 100644 (file)
@@ -1,8 +1,4 @@
-#include <algorithm>
-#include <msp/io/print.h>
 #include <msp/strings/format.h>
-#include <msp/strings/lexicalcast.h>
-#include <msp/strings/utils.h>
 #include "error.h"
 #include "vertexformat.h"
 
@@ -11,10 +7,6 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-VertexFormat::VertexFormat():
-       count(0)
-{ }
-
 VertexFormat::VertexFormat(VertexAttribute a):
        count(1)
 {
@@ -38,8 +30,8 @@ VertexFormat VertexFormat::operator,(DataType t) const
                throw invalid_operation("VertexFormat::operator,");
 
        VertexFormat r = *this;
-       UInt16 &a = r.attributes[r.count-1];
-       a = make_typed_attribute(static_cast<VertexAttribute>(a), t);
+       VertexAttribute &a = r.attributes[r.count-1];
+       a = make_typed_attribute(a, t);
 
        return r;
 }
@@ -50,8 +42,8 @@ VertexFormat VertexFormat::operator,(unsigned i) const
                throw invalid_operation("VertexFormat::operator,");
 
        VertexFormat r = *this;
-       UInt16 &a = r.attributes[r.count-1];
-       a = make_indexed_attribute(static_cast<VertexAttribute>(a), i);
+       VertexAttribute &a = r.attributes[r.count-1];
+       a = make_indexed_attribute(a, i);
 
        return r;
 }
@@ -66,8 +58,8 @@ bool VertexFormat::operator==(const VertexFormat &other) const
 unsigned VertexFormat::stride() const
 {
        unsigned s = 0;
-       for(const UInt16 *i=begin(); i!=end(); ++i)
-               s += get_attribute_size(*i);
+       for(VertexAttribute a: *this)
+               s += get_attribute_size(a);
        return s;
 }
 
@@ -75,18 +67,18 @@ int VertexFormat::offset(VertexAttribute attr) const
 {
        unsigned sem = get_attribute_semantic(attr);
        unsigned offs = 0;
-       for(const UInt16 *i=begin(); i!=end(); ++i)
+       for(VertexAttribute a: *this)
        {
-               if(get_attribute_semantic(*i)==sem)
+               if(get_attribute_semantic(a)==sem)
                {
-                       if(get_attribute_source_type(*i)==get_attribute_source_type(attr) &&
-                               get_attribute_component_count(*i)>=get_attribute_component_count(attr))
+                       if(get_attribute_source_type(a)==get_attribute_source_type(attr) &&
+                               get_attribute_component_count(a)>=get_attribute_component_count(attr))
                                return offs;
                        else
                                return -1;
                }
                else
-                       offs += get_attribute_size(*i);
+                       offs += get_attribute_size(a);
        }
 
        return -1;
@@ -97,20 +89,22 @@ VertexAttribute make_typed_attribute(VertexAttribute attr, DataType type)
 {
        if(is_matrix(type) || is_vector(type) || is_image(type))
                throw invalid_argument("make_typed_attribute");
+       if(is_integer_attribute(attr) && is_float(type))
+               throw invalid_argument("make_typed_attribute");
 
        return static_cast<VertexAttribute>((attr&0xFC0F) | (type&0x0F)<<4 | (type&0x300)>>1);
 }
 
 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
 {
-       unsigned base = attr;
+       VertexAttribute base = attr;
        if(get_attribute_semantic(attr)==get_attribute_semantic(TEXCOORD1))
        {
                if(index>=4)
                        throw out_of_range("make_indexed_attribute");
        }
        else if(get_attribute_semantic(attr)==get_attribute_semantic(RAW_ATTRIB1))
-               base &= 0x3FF;
+               base = static_cast<VertexAttribute>(base&0x3FF);
        else if(get_attribute_semantic(attr)!=get_attribute_semantic(GENERIC1))
                throw invalid_argument("make_indexed_attribute");
 
@@ -123,7 +117,7 @@ VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
 
 bool convert_attribute_type(string::const_iterator &i, string::const_iterator end, const char *name, VertexAttribute &attr, DataType type)
 {
-       string::const_iterator j = i;
+       auto j = i;
        for(; (j!=end && *j!='_' && *name); ++name, ++j)
                if(*j!=*name)
                        return false;
@@ -136,13 +130,23 @@ bool convert_attribute_type(string::const_iterator &i, string::const_iterator en
 
 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();
+       auto i = str.begin();
        for(; *name; ++name, ++i)
                if(*i!=*name)
                        return false;
+
+       VertexAttribute result = base_attr;
+
+       if(i!=str.end() && *i=='_')
+       {
+               if(*(++i)++!='I')
+                       return false;
+               result = static_cast<VertexAttribute>(result|8);
+       }
+
        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));
+       result = static_cast<VertexAttribute>(result+(*i++-'0'-min_size));
 
        while(i!=str.end())
        {