Skylicht Engine
Loading...
Searching...
No Matches
CIndexBuffer.h
1// Copyright (C) 2012 Patryk Nadrowski
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#ifndef __C_INDEX_BUFFER_H_INCLUDED__
6#define __C_INDEX_BUFFER_H_INCLUDED__
7
8#include "IIndexBuffer.h"
9
10namespace irr
11{
12namespace scene
13{
14 class CIndexBuffer : public IIndexBuffer
15 {
16 public:
17 CIndexBuffer(video::E_INDEX_TYPE type = video::EIT_16BIT) : Type(type), HardwareMappingHint(EHM_NEVER), ChangedID(1), Indices(0)
18 {
19#ifdef _DEBUG
20 setDebugName("CIndexBuffer");
21#endif
22 if(Type == video::EIT_32BIT)
23 Indices = new CIndexList<u32>();
24 else // EIT_16BIT
25 Indices = new CIndexList<u16>();
26 }
27
28 CIndexBuffer(const CIndexBuffer &indexBuffer) : Type(video::EIT_16BIT), HardwareMappingHint(EHM_NEVER), ChangedID(1), Indices(0)
29 {
30 Type = indexBuffer.Type;
31
32 HardwareMappingHint = indexBuffer.HardwareMappingHint;
33
34 if (Type == video::EIT_32BIT)
35 Indices = new CIndexList<u32>();
36 else // EIT_16BIT
37 Indices = new CIndexList<u16>();
38
39 const u32 ibCount = indexBuffer.Indices->size();
40
41 Indices->reallocate(ibCount);
42
43 for (u32 i = 0; i < ibCount; ++i)
44 addIndex(indexBuffer.getIndex(i));
45 }
46
47 virtual ~CIndexBuffer()
48 {
49 delete Indices;
50 }
51
52 virtual void clear()
53 {
54 Indices->clear();
55 }
56
57 virtual u32 getLast()
58 {
59 return (u32)Indices->getLast();
60 }
61
62 virtual void set_used(u32 used)
63 {
64 Indices->set_used(used);
65 }
66
67 virtual void reallocate(u32 size)
68 {
69 Indices->reallocate(size);
70 }
71
72 virtual u32 allocated_size() const
73 {
74 return Indices->allocated_size();
75 }
76
77 virtual s32 linear_reverse_search(const u32& element) const
78 {
79 return Indices->linear_reverse_search(element);
80 }
81
82 virtual video::E_INDEX_TYPE getType() const
83 {
84 return Type;
85 }
86
87 virtual void setType(video::E_INDEX_TYPE type)
88 {
89 if (Type == type)
90 return;
91
92 Type = type;
93
94 IIndexList* IndicesList = 0;
95
96 switch (Type)
97 {
98 case video::EIT_16BIT:
99 {
100 IndicesList = new CIndexList<u16>();
101 break;
102 }
103 case video::EIT_32BIT:
104 {
105 IndicesList = new CIndexList<u32>();
106 break;
107 }
108 }
109
110 if (Indices)
111 {
112 IndicesList->reallocate(Indices->size());
113
114 for(u32 i = 0; i < Indices->size(); ++i)
115 IndicesList->addIndex(Indices->getIndex(i));
116
117 delete Indices;
118 }
119
120 Indices = IndicesList;
121 }
122
123 virtual E_HARDWARE_MAPPING getHardwareMappingHint() const
124 {
125 return HardwareMappingHint;
126 }
127
128 virtual void setHardwareMappingHint(E_HARDWARE_MAPPING hardwareMappingHint)
129 {
130 if (HardwareMappingHint != hardwareMappingHint)
131 setDirty();
132
133 HardwareMappingHint = hardwareMappingHint;
134 }
135
136 virtual void addIndex(const u32& index)
137 {
138 Indices->addIndex(index);
139 }
140
141 virtual u32 getIndex(u32 id) const
142 {
143 return Indices->getIndex(id);
144 }
145
146 virtual void* getIndices()
147 {
148 return Indices->pointer();
149 }
150
151 virtual u32 getIndexCount() const
152 {
153 return Indices->size();
154 }
155
156 virtual u32 getIndexSize() const
157 {
158 if(Type == video::EIT_32BIT)
159 return sizeof(u32);
160
161 return sizeof(u16);
162 }
163
164 virtual void setIndex(u32 id, u32 index)
165 {
166 Indices->setIndex(id, index);
167 }
168
169 virtual void setDirty()
170 {
171 if (HardwareBuffer)
172 HardwareBuffer->requestUpdate();
173
174 ++ChangedID;
175 }
176
177 virtual u32 getChangedID() const
178 {
179 return ChangedID;
180 }
181
182 protected:
184 {
185 public:
186 virtual ~IIndexList() {};
187 virtual void clear() = 0;
188 virtual void* pointer() = 0;
189 virtual u32 size() const = 0;
190 virtual u32 getLast() = 0;
191 virtual void set_used(u32 used) = 0;
192 virtual void reallocate(u32 size) = 0;
193 virtual u32 allocated_size() const = 0;
194 virtual s32 linear_reverse_search(const u32& element) const = 0;
195 virtual void addIndex(const u32& index) = 0;
196 virtual u32 getIndex(u32 id) const = 0;
197 virtual void setIndex(u32 id, u32 index) = 0;
198 };
199
200 template <class T>
201 class CIndexList : public IIndexList
202 {
203 public:
204 CIndexList() : Data(0)
205 {
206 }
207
208 CIndexList(const CIndexList &indexList) : Data(0)
209 {
210 const u32 ilCount = indexList.Data.size();
211
212 Data.reallocate(ilCount);
213
214 for (u32 i = 0; i < ilCount; ++i)
215 Data.push_back(indexList.Data[i]);
216 }
217
218 ~CIndexList()
219 {
220 }
221
222 virtual void clear()
223 {
224 Data.clear();
225 }
226
227 virtual void* pointer()
228 {
229 return Data.pointer();
230 }
231
232 virtual u32 size() const
233 {
234 return Data.size();
235 }
236
237 virtual u32 getLast()
238 {
239 return (u32)Data.getLast();
240 }
241
242 virtual void set_used(u32 used)
243 {
244 Data.set_used(used);
245 }
246
247 virtual void reallocate(u32 size)
248 {
249 Data.reallocate(size);
250 }
251
252 virtual u32 allocated_size() const
253 {
254 return Data.allocated_size();
255 }
256
257 virtual s32 linear_reverse_search(const u32& element) const
258 {
259 return Data.linear_reverse_search(element);
260 }
261
262 virtual void addIndex(const u32& index)
263 {
264 Data.push_back(index);
265 }
266
267 virtual u32 getIndex(u32 id) const
268 {
269 if (id < Data.size())
270 return Data[id];
271
272 return 0;
273 }
274
275 virtual void setIndex(u32 id, u32 index)
276 {
277 if (id < Data.size())
278 Data[id] = (T)index;
279 }
280
281 protected:
282 core::array<T> Data;
283 };
284
285 video::E_INDEX_TYPE Type;
286
287 E_HARDWARE_MAPPING HardwareMappingHint;
288
289 u32 ChangedID;
290
291 IIndexList* Indices;
292 };
293
294 //typedef CIndexList<u16> SIndexBuffer16;
295 //typedef CIndexList<u32> SIndexBuffer32;
296}
297}
298
299#endif
void setDebugName(const c8 *newName)
Sets the debug name of the object.
Definition IReferenceCounted.h:163
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.h:23
Definition CIndexBuffer.h:202
Definition CIndexBuffer.h:184
All scene management can be found in this namespace: Mesh loading, special scene nodes like octrees a...
Definition CIndexBuffer.h:13
E_HARDWARE_MAPPING
Definition EHardwareBufferFlags.h:14
@ EHM_NEVER
Don't store on the hardware.
Definition EHardwareBufferFlags.h:16
Everything in the Irrlicht Engine can be found in this namespace.
Definition Skylicht.h:33
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
signed int s32
32 bit signed variable.
Definition irrTypes.h:66
unsigned short u16
16 bit unsigned variable.
Definition irrTypes.h:40