]> git.tdb.fi Git - libs/gl.git/blob - source/vertexformat.cpp
Memory allocation changes
[libs/gl.git] / source / vertexformat.cpp
1 #include <algorithm>
2 #include <msp/strings/format.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
5 #include "error.h"
6 #include "vertexformat.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 VertexFormat::VertexFormat():
14         data(0)
15 { }
16
17 VertexFormat::VertexFormat(VertexComponent c):
18         data(new unsigned char[8])
19 {
20         data[0] = 1;
21         data[1] = c;
22 }
23
24 VertexFormat::VertexFormat(const VertexFormat &f):
25         data(0)
26 {
27         *this = f;
28 }
29
30 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
31 {
32         delete[] data;
33         if(f.data)
34         {
35                 data = new unsigned char[(f.data[0]&~7)+8];
36                 copy(f.data, f.data+f.data[0]+1, data);
37         }
38         else
39                 data = 0;
40
41         return *this;
42 }
43
44 VertexFormat::~VertexFormat()
45 {
46         delete[] data;
47 }
48
49 VertexFormat VertexFormat::operator,(VertexComponent c) const
50 {
51         VertexFormat r = *this;
52         if(r.data)
53         {
54                 const unsigned char n = ++r.data[0];
55                 if((n&7)==0)
56                 {
57                         unsigned char *newdt = new unsigned char[n+8];
58                         copy(r.data, r.data+n, newdt);
59                         delete r.data;
60                         r.data = newdt;
61                 }
62                 r.data[n] = c;
63         }
64         else
65         {
66                 r.data = new unsigned char[8];
67                 r.data[0] = 1;
68                 r.data[1] = c;
69         }
70
71         return r;
72 }
73
74 VertexFormat VertexFormat::operator,(unsigned i) const
75 {
76         if(!data)
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,");
82         *c += i*4;
83
84         return r;
85 }
86
87 unsigned VertexFormat::stride() const
88 {
89         unsigned s = 0;
90         for(const unsigned char *i=begin(); i!=end(); ++i)
91                 s += get_component_size(*i);
92         return s;
93 }
94
95 int VertexFormat::offset(VertexComponent comp, unsigned index) const
96 {
97         if((comp<TEXCOORD1 && index>0) || (comp<ATTRIB1 && index>=8) || index>=53)
98                 throw out_of_range("VertexFormat::offset");
99
100         unsigned type = get_component_type(comp)+index;
101         unsigned size = get_component_size(comp);
102         unsigned offs = 0;
103         for(const unsigned char *i=begin(); i!=end(); ++i)
104         {
105                 if(get_component_type(*i)==type)
106                 {
107                         if(get_component_size(*i)>=size)
108                                 return offs;
109                         else
110                                 return -1;
111                 }
112                 else
113                         offs += get_component_size(*i);
114         }
115
116         return -1;
117 }
118
119 void operator>>(const LexicalConverter &conv, VertexComponent &c)
120 {
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")
125                 c = 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")
129                 c = COLOR4_UBYTE;
130         else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
131         {
132                 if(str.size()==9)
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);
136                 else
137                         throw lexical_error(format("conversion of '%s' to VertexComponent", str));
138         }
139         else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
140         {
141                 unsigned n;
142                 try
143                 {
144                         n = lexical_cast<unsigned>(str.substr(8));
145                 }
146                 catch(const lexical_error &)
147                 {
148                         throw lexical_error(format("conversion of '%s' to VertexComponent", str));
149                 }
150                 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
151         }
152         else
153                 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
154 }
155
156 // XXX This will go away eventually
157 void operator>>(const LexicalConverter &conv, VertexFormat &f)
158 {
159         vector<string> parts = split(conv.get(), '_');
160         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
161         {
162                 if(*i=="COLOR4UB")
163                         *i = "COLOR4_UBYTE";
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);
170         }
171
172         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
173                 f = (f, lexical_cast<VertexComponent>(*i));
174 }
175
176 } // namespace GL
177 } // namespace Msp