CIndexBuffer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #ifndef __C_INDEX_BUFFER_H_INCLUDED__
  5. #define __C_INDEX_BUFFER_H_INCLUDED__
  6. #include "IIndexBuffer.h"
  7. namespace irr
  8. {
  9. namespace scene
  10. {
  11. class CIndexBuffer : public IIndexBuffer
  12. {
  13. class IIndexList
  14. {
  15. public:
  16. virtual ~IIndexList(){};
  17. virtual u32 stride() const =0;
  18. virtual u32 size() const =0;
  19. virtual void push_back(const u32 &element) =0;
  20. virtual u32 operator [](u32 index) const =0;
  21. virtual u32 getLast() =0;
  22. virtual void setValue(u32 index, u32 value) =0;
  23. virtual void set_used(u32 usedNow) =0;
  24. virtual void reallocate(u32 new_size) =0;
  25. virtual u32 allocated_size() const =0;
  26. virtual void* pointer() =0;
  27. virtual video::E_INDEX_TYPE getType() const =0;
  28. };
  29. template <class T>
  30. class CSpecificIndexList : public IIndexList
  31. {
  32. public:
  33. core::array<T> Indices;
  34. virtual u32 stride() const _IRR_OVERRIDE_ {return sizeof(T);}
  35. virtual u32 size() const _IRR_OVERRIDE_ {return Indices.size();}
  36. virtual void push_back(const u32 &element) _IRR_OVERRIDE_
  37. {
  38. // push const ref due to compiler problem with gcc 4.6, big endian
  39. Indices.push_back((const T&)element);
  40. }
  41. virtual u32 operator [](u32 index) const _IRR_OVERRIDE_
  42. {
  43. return (u32)(Indices[index]);
  44. }
  45. virtual u32 getLast() _IRR_OVERRIDE_ {return (u32)Indices.getLast();}
  46. virtual void setValue(u32 index, u32 value) _IRR_OVERRIDE_
  47. {
  48. Indices[index]=(T)value;
  49. }
  50. virtual void set_used(u32 usedNow) _IRR_OVERRIDE_
  51. {
  52. Indices.set_used(usedNow);
  53. }
  54. virtual void reallocate(u32 new_size) _IRR_OVERRIDE_
  55. {
  56. Indices.reallocate(new_size);
  57. }
  58. virtual u32 allocated_size() const _IRR_OVERRIDE_
  59. {
  60. return Indices.allocated_size();
  61. }
  62. virtual void* pointer() _IRR_OVERRIDE_ {return Indices.pointer();}
  63. virtual video::E_INDEX_TYPE getType() const _IRR_OVERRIDE_
  64. {
  65. if (sizeof(T)==sizeof(u16))
  66. return video::EIT_16BIT;
  67. else
  68. return video::EIT_32BIT;
  69. }
  70. };
  71. public:
  72. IIndexList *Indices;
  73. CIndexBuffer(video::E_INDEX_TYPE IndexType) :Indices(0), MappingHint(EHM_NEVER), ChangedID(1)
  74. {
  75. setType(IndexType);
  76. }
  77. CIndexBuffer(const IIndexBuffer &IndexBufferCopy) :Indices(0), MappingHint(EHM_NEVER), ChangedID(1)
  78. {
  79. setType(IndexBufferCopy.getType());
  80. reallocate(IndexBufferCopy.size());
  81. for (u32 n=0;n<IndexBufferCopy.size();++n)
  82. push_back(IndexBufferCopy[n]);
  83. }
  84. virtual ~CIndexBuffer()
  85. {
  86. delete Indices;
  87. }
  88. //virtual void setType(video::E_INDEX_TYPE IndexType);
  89. virtual void setType(video::E_INDEX_TYPE IndexType) _IRR_OVERRIDE_
  90. {
  91. IIndexList *NewIndices=0;
  92. switch (IndexType)
  93. {
  94. case video::EIT_16BIT:
  95. {
  96. NewIndices=new CSpecificIndexList<u16>;
  97. break;
  98. }
  99. case video::EIT_32BIT:
  100. {
  101. NewIndices=new CSpecificIndexList<u32>;
  102. break;
  103. }
  104. }
  105. if (Indices)
  106. {
  107. NewIndices->reallocate( Indices->size() );
  108. for(u32 n=0;n<Indices->size();++n)
  109. NewIndices->push_back((*Indices)[n]);
  110. delete Indices;
  111. }
  112. Indices=NewIndices;
  113. }
  114. virtual void* getData() _IRR_OVERRIDE_ {return Indices->pointer();}
  115. virtual video::E_INDEX_TYPE getType() const _IRR_OVERRIDE_ {return Indices->getType();}
  116. virtual u32 stride() const _IRR_OVERRIDE_ {return Indices->stride();}
  117. virtual u32 size() const _IRR_OVERRIDE_
  118. {
  119. return Indices->size();
  120. }
  121. virtual void push_back(const u32 &element) _IRR_OVERRIDE_
  122. {
  123. Indices->push_back(element);
  124. }
  125. virtual u32 operator [](u32 index) const _IRR_OVERRIDE_
  126. {
  127. return (*Indices)[index];
  128. }
  129. virtual u32 getLast() _IRR_OVERRIDE_
  130. {
  131. return Indices->getLast();
  132. }
  133. virtual void setValue(u32 index, u32 value) _IRR_OVERRIDE_
  134. {
  135. Indices->setValue(index, value);
  136. }
  137. virtual void set_used(u32 usedNow) _IRR_OVERRIDE_
  138. {
  139. Indices->set_used(usedNow);
  140. }
  141. virtual void reallocate(u32 new_size) _IRR_OVERRIDE_
  142. {
  143. Indices->reallocate(new_size);
  144. }
  145. virtual u32 allocated_size() const _IRR_OVERRIDE_
  146. {
  147. return Indices->allocated_size();
  148. }
  149. virtual void* pointer() _IRR_OVERRIDE_
  150. {
  151. return Indices->pointer();
  152. }
  153. //! get the current hardware mapping hint
  154. virtual E_HARDWARE_MAPPING getHardwareMappingHint() const _IRR_OVERRIDE_
  155. {
  156. return MappingHint;
  157. }
  158. //! set the hardware mapping hint, for driver
  159. virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint ) _IRR_OVERRIDE_
  160. {
  161. MappingHint=NewMappingHint;
  162. }
  163. //! flags the mesh as changed, reloads hardware buffers
  164. virtual void setDirty() _IRR_OVERRIDE_
  165. {
  166. ++ChangedID;
  167. }
  168. //! Get the currently used ID for identification of changes.
  169. /** This shouldn't be used for anything outside the VideoDriver. */
  170. virtual u32 getChangedID() const _IRR_OVERRIDE_ {return ChangedID;}
  171. E_HARDWARE_MAPPING MappingHint;
  172. u32 ChangedID;
  173. };
  174. } // end namespace scene
  175. } // end namespace irr
  176. #endif