Irrlicht 3D Engine
CIndexBuffer.h
Go to the documentation of this file.
1 // Copyright (C) 2008-2012 Nikolaus Gebhardt
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 
10 namespace irr
11 {
12 namespace scene
13 {
14 
15  class CIndexBuffer : public IIndexBuffer
16  {
17 
18  class IIndexList
19  {
20  public:
21  virtual ~IIndexList(){};
22 
23  virtual u32 stride() const =0;
24  virtual u32 size() const =0;
25  virtual void push_back(const u32 &element) =0;
26  virtual u32 operator [](u32 index) const =0;
27  virtual u32 getLast() =0;
28  virtual void setValue(u32 index, u32 value) =0;
29  virtual void set_used(u32 usedNow) =0;
30  virtual void reallocate(u32 new_size) =0;
31  virtual u32 allocated_size() const =0;
32  virtual void* pointer() =0;
33  virtual video::E_INDEX_TYPE getType() const =0;
34  };
35 
36  template <class T>
37  class CSpecificIndexList : public IIndexList
38  {
39  public:
41 
42  virtual u32 stride() const {return sizeof(T);}
43 
44  virtual u32 size() const {return Indices.size();}
45 
46  virtual void push_back(const u32 &element)
47  {
48  // push const ref due to compiler problem with gcc 4.6, big endian
49  Indices.push_back((const T&)element);
50  }
51 
52  virtual u32 operator [](u32 index) const
53  {
54  return (u32)(Indices[index]);
55  }
56 
57  virtual u32 getLast() {return (u32)Indices.getLast();}
58 
59  virtual void setValue(u32 index, u32 value)
60  {
61  Indices[index]=(T)value;
62  }
63 
64  virtual void set_used(u32 usedNow)
65  {
66  Indices.set_used(usedNow);
67  }
68 
69  virtual void reallocate(u32 new_size)
70  {
71  Indices.reallocate(new_size);
72  }
73 
74  virtual u32 allocated_size() const
75  {
76  return Indices.allocated_size();
77  }
78 
79  virtual void* pointer() {return Indices.pointer();}
80 
81  virtual video::E_INDEX_TYPE getType() const
82  {
83  if (sizeof(T)==sizeof(u16))
84  return video::EIT_16BIT;
85  else
86  return video::EIT_32BIT;
87  }
88  };
89 
90  public:
91  IIndexList *Indices;
92 
94  {
95  setType(IndexType);
96  }
97 
98  CIndexBuffer(const IIndexBuffer &IndexBufferCopy) :Indices(0), MappingHint(EHM_NEVER), ChangedID(1)
99  {
100  setType(IndexBufferCopy.getType());
101  reallocate(IndexBufferCopy.size());
102 
103  for (u32 n=0;n<IndexBufferCopy.size();++n)
104  push_back(IndexBufferCopy[n]);
105  }
106 
107  virtual ~CIndexBuffer()
108  {
109  delete Indices;
110  }
111 
112  //virtual void setType(video::E_INDEX_TYPE IndexType);
113  virtual void setType(video::E_INDEX_TYPE IndexType)
114  {
115  IIndexList *NewIndices=0;
116 
117  switch (IndexType)
118  {
119  case video::EIT_16BIT:
120  {
121  NewIndices=new CSpecificIndexList<u16>;
122  break;
123  }
124  case video::EIT_32BIT:
125  {
126  NewIndices=new CSpecificIndexList<u32>;
127  break;
128  }
129  }
130 
131  if (Indices)
132  {
133  NewIndices->reallocate( Indices->size() );
134 
135  for(u32 n=0;n<Indices->size();++n)
136  NewIndices->push_back((*Indices)[n]);
137 
138  delete Indices;
139  }
140 
141  Indices=NewIndices;
142  }
143 
144  virtual void* getData() {return Indices->pointer();}
145 
146  virtual video::E_INDEX_TYPE getType() const {return Indices->getType();}
147 
148  virtual u32 stride() const {return Indices->stride();}
149 
150  virtual u32 size() const
151  {
152  return Indices->size();
153  }
154 
155  virtual void push_back(const u32 &element)
156  {
157  Indices->push_back(element);
158  }
159 
160  virtual u32 operator [](u32 index) const
161  {
162  return (*Indices)[index];
163  }
164 
165  virtual u32 getLast()
166  {
167  return Indices->getLast();
168  }
169 
170  virtual void setValue(u32 index, u32 value)
171  {
172  Indices->setValue(index, value);
173  }
174 
175  virtual void set_used(u32 usedNow)
176  {
177  Indices->set_used(usedNow);
178  }
179 
180  virtual void reallocate(u32 new_size)
181  {
182  Indices->reallocate(new_size);
183  }
184 
185  virtual u32 allocated_size() const
186  {
187  return Indices->allocated_size();
188  }
189 
190  virtual void* pointer()
191  {
192  return Indices->pointer();
193  }
194 
197  {
198  return MappingHint;
199  }
200 
202  virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint )
203  {
204  MappingHint=NewMappingHint;
205  }
206 
208  virtual void setDirty()
209  {
210  ++ChangedID;
211  }
212 
214 
215  virtual u32 getChangedID() const {return ChangedID;}
216 
219  };
220 
221 
222 } // end namespace scene
223 } // end namespace irr
224 
225 #endif
226 
virtual void setDirty()
flags the mesh as changed, reloads hardware buffers
Definition: CIndexBuffer.h:208
void set_used(u32 usedNow)
Sets the size of the array and allocates new elements if necessary.
Definition: irrArray.h:257
virtual u32 stride() const
Definition: CIndexBuffer.h:148
void reallocate(u32 new_size, bool canShrink=true)
Reallocates the array, make it bigger or smaller.
Definition: irrArray.h:67
CIndexBuffer(video::E_INDEX_TYPE IndexType)
Definition: CIndexBuffer.h:93
virtual void * getData()
Definition: CIndexBuffer.h:144
virtual void push_back(const u32 &element)
Definition: CIndexBuffer.h:155
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:12
Don&#39;t store on the hardware.
u32 allocated_size() const
Get amount of memory allocated.
Definition: irrArray.h:377
T & getLast()
Gets last element.
Definition: irrArray.h:333
virtual E_HARDWARE_MAPPING getHardwareMappingHint() const
get the current hardware mapping hint
Definition: CIndexBuffer.h:196
virtual u32 allocated_size() const
Definition: CIndexBuffer.h:185
virtual u32 size() const =0
void push_back(const T &element)
Adds an element at back of array.
Definition: irrArray.h:112
unsigned short u16
16 bit unsigned variable.
Definition: irrTypes.h:40
virtual void setType(video::E_INDEX_TYPE IndexType)
Definition: CIndexBuffer.h:113
virtual u32 size() const
Definition: CIndexBuffer.h:150
virtual video::E_INDEX_TYPE getType() const =0
virtual void * pointer()
Definition: CIndexBuffer.h:190
virtual void setHardwareMappingHint(E_HARDWARE_MAPPING NewMappingHint)
set the hardware mapping hint, for driver
Definition: CIndexBuffer.h:202
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:58
virtual void reallocate(u32 new_size)
Definition: CIndexBuffer.h:180
u32 size() const
Get number of occupied elements of the array.
Definition: irrArray.h:368
virtual u32 getChangedID() const
Get the currently used ID for identification of changes.
Definition: CIndexBuffer.h:215
virtual void set_used(u32 usedNow)
Definition: CIndexBuffer.h:175
virtual void setValue(u32 index, u32 value)
Definition: CIndexBuffer.h:170
virtual u32 operator[](u32 index) const
Definition: CIndexBuffer.h:160
virtual video::E_INDEX_TYPE getType() const
Definition: CIndexBuffer.h:146
T * pointer()
Gets a pointer to the array.
Definition: irrArray.h:352
E_HARDWARE_MAPPING MappingHint
Definition: CIndexBuffer.h:217
CIndexBuffer(const IIndexBuffer &IndexBufferCopy)
Definition: CIndexBuffer.h:98