Irrlicht 3D Engine
CIndexBuffer.h
Go to the documentation of this file.
00001 // Copyright (C) 2008-2012 Nikolaus Gebhardt
00002 // This file is part of the "Irrlicht Engine".
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
00004 
00005 #ifndef __C_INDEX_BUFFER_H_INCLUDED__
00006 #define __C_INDEX_BUFFER_H_INCLUDED__
00007 
00008 #include "IIndexBuffer.h"
00009 
00010 namespace irr
00011 {
00012 namespace scene
00013 {
00014 
00015     class CIndexBuffer : public IIndexBuffer
00016     {
00017 
00018         class IIndexList
00019         {
00020         public:
00021             virtual ~IIndexList(){};
00022 
00023             virtual u32 stride() const =0;
00024             virtual u32 size() const =0;
00025             virtual void push_back(const u32 &element) =0;
00026             virtual u32 operator [](u32 index) const =0;
00027             virtual u32 getLast() =0;
00028             virtual void setValue(u32 index, u32 value) =0;
00029             virtual void set_used(u32 usedNow) =0;
00030             virtual void reallocate(u32 new_size) =0;
00031             virtual u32 allocated_size() const =0;
00032             virtual void* pointer() =0;
00033             virtual video::E_INDEX_TYPE getType() const =0;
00034         };
00035 
00036         template <class T>
00037         class CSpecificIndexList : public IIndexList
00038         {
00039         public:
00040             core::array<T> Indices;
00041 
00042             virtual u32 stride() const {return sizeof(T);}
00043 
00044             virtual u32 size() const {return Indices.size();}
00045 
00046             virtual void push_back(const u32 &element)
00047             {
00048                 // push const ref due to compiler problem with gcc 4.6, big endian
00049                 Indices.push_back((const T&)element);
00050             }
00051 
00052             virtual u32 operator [](u32 index) const
00053             {
00054                 return (u32)(Indices[index]);
00055             }
00056 
00057             virtual u32 getLast() {return (u32)Indices.getLast();}
00058 
00059             virtual void setValue(u32 index, u32 value)
00060             {
00061                 Indices[index]=(T)value;
00062             }
00063 
00064             virtual void set_used(u32 usedNow)
00065             {
00066                 Indices.set_used(usedNow);
00067             }
00068 
00069             virtual void reallocate(u32 new_size)
00070             {
00071                 Indices.reallocate(new_size);
00072             }
00073 
00074             virtual u32 allocated_size() const
00075             {
00076                 return Indices.allocated_size();
00077             }
00078 
00079             virtual void* pointer() {return Indices.pointer();}
00080 
00081             virtual video::E_INDEX_TYPE getType() const
00082             {
00083                 if (sizeof(T)==sizeof(u16))
00084                     return video::EIT_16BIT;
00085                 else
00086                     return video::EIT_32BIT;
00087             }
00088         };
00089 
00090     public:
00091         IIndexList *Indices;
00092 
00093         CIndexBuffer(video::E_INDEX_TYPE IndexType) :Indices(0), MappingHint(EHM_NEVER), ChangedID(1)
00094         {
00095             setType(IndexType);
00096         }
00097 
00098         CIndexBuffer(const IIndexBuffer &IndexBufferCopy) :Indices(0), MappingHint(EHM_NEVER), ChangedID(1)
00099         {
00100             setType(IndexBufferCopy.getType());
00101             reallocate(IndexBufferCopy.size());
00102 
00103             for (u32 n=0;n<IndexBufferCopy.size();++n)
00104                 push_back(IndexBufferCopy[n]);
00105         }
00106 
00107         virtual ~CIndexBuffer()
00108         {
00109             delete Indices;
00110         }
00111 
00112         //virtual void setType(video::E_INDEX_TYPE IndexType);
00113         virtual void setType(video::E_INDEX_TYPE IndexType)
00114         {
00115             IIndexList *NewIndices=0;
00116 
00117             switch (IndexType)
00118             {
00119                 case video::EIT_16BIT:
00120                 {
00121                     NewIndices=new CSpecificIndexList<u16>;
00122                     break;
00123                 }
00124                 case video::EIT_32BIT:
00125                 {
00126                     NewIndices=new CSpecificIndexList<u32>;
00127                     break;
00128                 }
00129             }
00130 
00131             if (Indices)
00132             {
00133                 NewIndices->reallocate( Indices->size() );
00134 
00135                 for(u32 n=0;n<Indices->size();++n)
00136                     NewIndices->push_back((*Indices)[n]);
00137 
00138                 delete Indices;
00139             }
00140 
00141             Indices=NewIndices;
00142         }
00143 
00144         virtual void* getData() {return Indices->pointer();}
00145 
00146         virtual video::E_INDEX_TYPE getType() const {return Indices->getType();}
00147 
00148         virtual u32 stride() const {return Indices->stride();}
00149 
00150         virtual u32 size() const
00151         {
00152             return Indices->size();
00153         }
00154 
00155         virtual void push_back(const u32 &element)
00156         {
00157             Indices->push_back(element);
00158         }
00159 
00160         virtual u32 operator [](u32 index) const
00161         {
00162             return (*Indices)[index];
00163         }
00164 
00165         virtual u32 getLast()
00166         {
00167             return Indices->getLast();
00168         }
00169 
00170         virtual void setValue(u32 index, u32 value)
00171         {
00172             Indices->setValue(index, value);
00173         }
00174 
00175         virtual void set_used(u32 usedNow)
00176         {
00177             Indices->set_used(usedNow);
00178         }
00179 
00180         virtual void reallocate(u32 new_size)
00181         {
00182             Indices->reallocate(new_size);
00183         }
00184 
00185         virtual u32 allocated_size() const
00186         {
00187             return Indices->allocated_size();
00188         }
00189 
00190         virtual void* pointer()
00191         {
00192             return Indices->pointer();
00193         }
00194 
00196         virtual E_HARDWARE_MAPPING getHardwareMappingHint() const
00197         {
00198             return MappingHint;
00199         }
00200 
00202         virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint )
00203         {
00204             MappingHint=NewMappingHint;
00205         }
00206 
00208         virtual void setDirty()
00209         {
00210             ++ChangedID;
00211         }
00212 
00214 
00215         virtual u32 getChangedID() const {return ChangedID;}
00216 
00217         E_HARDWARE_MAPPING MappingHint;
00218         u32 ChangedID;
00219     };
00220 
00221 
00222 } // end namespace scene
00223 } // end namespace irr
00224 
00225 #endif
00226