CIndexBuffer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 IRR_C_INDEX_BUFFER_H_INCLUDED
  5. #define IRR_C_INDEX_BUFFER_H_INCLUDED
  6. #include "IIndexBuffer.h"
  7. namespace irr
  8. {
  9. namespace scene
  10. {
  11. class CIndexBuffer : public IIndexBuffer
  12. {
  13. // Virtual function wrapper around irr::core::array
  14. class IIndexList
  15. {
  16. public:
  17. virtual ~IIndexList(){};
  18. virtual u32 stride() const =0;
  19. virtual u32 size() const =0;
  20. virtual void push_back(u32 value) =0;
  21. virtual u32 operator [](u32 index) const =0;
  22. virtual u32 getLast() =0;
  23. virtual void setValue(u32 index, u32 value) =0;
  24. virtual void set_used(u32 usedNow) =0;
  25. virtual void reallocate(u32 new_size, bool canShrink=true) =0;
  26. virtual u32 allocated_size() const =0;
  27. virtual void* pointer() =0;
  28. virtual const void* const_pointer() const =0;
  29. virtual video::E_INDEX_TYPE getType() const =0;
  30. };
  31. template <class T>
  32. class CSpecificIndexList : public IIndexList
  33. {
  34. public:
  35. core::array<T> Indices;
  36. virtual u32 stride() const IRR_OVERRIDE {return sizeof(T);}
  37. virtual u32 size() const IRR_OVERRIDE {return Indices.size();}
  38. virtual void push_back(u32 value) IRR_OVERRIDE
  39. {
  40. Indices.push_back((T)value);
  41. }
  42. virtual u32 operator [](u32 index) const IRR_OVERRIDE
  43. {
  44. return (u32)(Indices[index]);
  45. }
  46. virtual u32 getLast() IRR_OVERRIDE {return (u32)Indices.getLast();}
  47. virtual void setValue(u32 index, u32 value) IRR_OVERRIDE
  48. {
  49. Indices[index]=(T)value;
  50. }
  51. virtual void set_used(u32 usedNow) IRR_OVERRIDE
  52. {
  53. Indices.set_used(usedNow);
  54. }
  55. virtual void reallocate(u32 new_size, bool canShrink) IRR_OVERRIDE
  56. {
  57. Indices.reallocate(new_size, canShrink);
  58. }
  59. virtual u32 allocated_size() const IRR_OVERRIDE
  60. {
  61. return Indices.allocated_size();
  62. }
  63. virtual void* pointer() IRR_OVERRIDE { return Indices.pointer(); }
  64. virtual const void* const_pointer() const IRR_OVERRIDE { return Indices.const_pointer(); }
  65. virtual video::E_INDEX_TYPE getType() const IRR_OVERRIDE
  66. {
  67. if (sizeof(T)==sizeof(u16))
  68. return video::EIT_16BIT;
  69. else
  70. return video::EIT_32BIT;
  71. }
  72. };
  73. public:
  74. IIndexList *Indices;
  75. CIndexBuffer(video::E_INDEX_TYPE IndexType) :Indices(0), MappingHint(EHM_NEVER), ChangedID(1)
  76. {
  77. setType(IndexType);
  78. }
  79. CIndexBuffer(const IIndexBuffer &IndexBufferCopy) :Indices(0), MappingHint(EHM_NEVER), ChangedID(1)
  80. {
  81. setType(IndexBufferCopy.getType());
  82. reallocate(IndexBufferCopy.size());
  83. for (u32 n=0;n<IndexBufferCopy.size();++n)
  84. push_back(IndexBufferCopy[n]);
  85. }
  86. virtual ~CIndexBuffer()
  87. {
  88. delete Indices;
  89. }
  90. virtual void setType(video::E_INDEX_TYPE indexType) IRR_OVERRIDE
  91. {
  92. if ( Indices && Indices->getType() == indexType )
  93. return;
  94. IIndexList *NewIndices=0;
  95. switch (indexType)
  96. {
  97. case video::EIT_16BIT:
  98. {
  99. NewIndices=new CSpecificIndexList<u16>;
  100. break;
  101. }
  102. case video::EIT_32BIT:
  103. {
  104. NewIndices=new CSpecificIndexList<u32>;
  105. break;
  106. }
  107. }
  108. if (Indices)
  109. {
  110. NewIndices->reallocate( Indices->size() );
  111. for(u32 n=0;n<Indices->size();++n)
  112. NewIndices->push_back((*Indices)[n]);
  113. delete Indices;
  114. }
  115. Indices=NewIndices;
  116. }
  117. virtual void* getData() IRR_OVERRIDE {return Indices->pointer();}
  118. virtual const void* getData() const IRR_OVERRIDE { return Indices->const_pointer(); }
  119. virtual video::E_INDEX_TYPE getType() const IRR_OVERRIDE {return Indices->getType();}
  120. virtual u32 stride() const IRR_OVERRIDE {return Indices->stride();}
  121. virtual u32 size() const IRR_OVERRIDE
  122. {
  123. return Indices->size();
  124. }
  125. virtual void push_back(u32 value) IRR_OVERRIDE
  126. {
  127. Indices->push_back(value);
  128. }
  129. virtual u32 operator [](u32 index) const IRR_OVERRIDE
  130. {
  131. return (*Indices)[index];
  132. }
  133. virtual u32 getLast() IRR_OVERRIDE
  134. {
  135. return Indices->getLast();
  136. }
  137. virtual void setValue(u32 index, u32 value) IRR_OVERRIDE
  138. {
  139. Indices->setValue(index, value);
  140. }
  141. virtual void set_used(u32 usedNow) IRR_OVERRIDE
  142. {
  143. Indices->set_used(usedNow);
  144. }
  145. virtual void reallocate(u32 new_size, bool canShrink=true) IRR_OVERRIDE
  146. {
  147. Indices->reallocate(new_size, canShrink);
  148. }
  149. virtual u32 allocated_size() const IRR_OVERRIDE
  150. {
  151. return Indices->allocated_size();
  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