CGUISpriteBank.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright (C) 2002-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. #include "CGUISpriteBank.h"
  5. #ifdef _IRR_COMPILE_WITH_GUI_
  6. #include "IGUIEnvironment.h"
  7. #include "IVideoDriver.h"
  8. #include "ITexture.h"
  9. namespace irr
  10. {
  11. namespace gui
  12. {
  13. CGUISpriteBank::CGUISpriteBank(IGUIEnvironment* env) :
  14. Environment(env), Driver(0)
  15. {
  16. #ifdef _DEBUG
  17. setDebugName("CGUISpriteBank");
  18. #endif
  19. if (Environment)
  20. {
  21. Driver = Environment->getVideoDriver();
  22. if (Driver)
  23. Driver->grab();
  24. }
  25. }
  26. CGUISpriteBank::~CGUISpriteBank()
  27. {
  28. // drop textures
  29. for (u32 i=0; i<Textures.size(); ++i)
  30. if (Textures[i])
  31. Textures[i]->drop();
  32. // drop video driver
  33. if (Driver)
  34. Driver->drop();
  35. }
  36. core::array< core::rect<s32> >& CGUISpriteBank::getPositions()
  37. {
  38. return Rectangles;
  39. }
  40. core::array< SGUISprite >& CGUISpriteBank::getSprites()
  41. {
  42. return Sprites;
  43. }
  44. u32 CGUISpriteBank::getTextureCount() const
  45. {
  46. return Textures.size();
  47. }
  48. video::ITexture* CGUISpriteBank::getTexture(u32 index) const
  49. {
  50. if (index < Textures.size())
  51. return Textures[index];
  52. else
  53. return 0;
  54. }
  55. void CGUISpriteBank::addTexture(video::ITexture* texture)
  56. {
  57. if (texture)
  58. texture->grab();
  59. Textures.push_back(texture);
  60. }
  61. void CGUISpriteBank::setTexture(u32 index, video::ITexture* texture)
  62. {
  63. while (index >= Textures.size())
  64. Textures.push_back(0);
  65. if (texture)
  66. texture->grab();
  67. if (Textures[index])
  68. Textures[index]->drop();
  69. Textures[index] = texture;
  70. }
  71. //! clear everything
  72. void CGUISpriteBank::clear()
  73. {
  74. // drop textures
  75. for (u32 i=0; i<Textures.size(); ++i)
  76. if (Textures[i])
  77. Textures[i]->drop();
  78. Textures.clear();
  79. Sprites.clear();
  80. Rectangles.clear();
  81. }
  82. //! Add the texture and use it for a single non-animated sprite.
  83. s32 CGUISpriteBank::addTextureAsSprite(video::ITexture* texture)
  84. {
  85. if ( !texture )
  86. return -1;
  87. addTexture(texture);
  88. u32 textureIndex = getTextureCount() - 1;
  89. u32 rectangleIndex = Rectangles.size();
  90. Rectangles.push_back( core::rect<s32>(0,0, texture->getOriginalSize().Width, texture->getOriginalSize().Height) );
  91. SGUISprite sprite;
  92. sprite.frameTime = 0;
  93. SGUISpriteFrame frame;
  94. frame.textureNumber = textureIndex;
  95. frame.rectNumber = rectangleIndex;
  96. sprite.Frames.push_back( frame );
  97. Sprites.push_back( sprite );
  98. return Sprites.size() - 1;
  99. }
  100. //! draws a sprite in 2d with scale and color
  101. void CGUISpriteBank::draw2DSprite(u32 index, const core::position2di& pos,
  102. const core::rect<s32>* clip, const video::SColor& color,
  103. u32 starttime, u32 currenttime, bool loop, bool center)
  104. {
  105. if (index >= Sprites.size() || Sprites[index].Frames.empty() )
  106. return;
  107. // work out frame number
  108. u32 frame = 0;
  109. if (Sprites[index].frameTime)
  110. {
  111. u32 f = ((currenttime - starttime) / Sprites[index].frameTime);
  112. if (loop)
  113. frame = f % Sprites[index].Frames.size();
  114. else
  115. frame = (f >= Sprites[index].Frames.size()) ? Sprites[index].Frames.size()-1 : f;
  116. }
  117. const video::ITexture* tex = Textures[Sprites[index].Frames[frame].textureNumber];
  118. if (!tex)
  119. return;
  120. const u32 rn = Sprites[index].Frames[frame].rectNumber;
  121. if (rn >= Rectangles.size())
  122. return;
  123. const core::rect<s32>& r = Rectangles[rn];
  124. if (center)
  125. {
  126. core::position2di p = pos;
  127. p -= r.getSize() / 2;
  128. Driver->draw2DImage(tex, p, r, clip, color, true);
  129. }
  130. else
  131. {
  132. Driver->draw2DImage(tex, pos, r, clip, color, true);
  133. }
  134. }
  135. void CGUISpriteBank::draw2DSpriteBatch( const core::array<u32>& indices,
  136. const core::array<core::position2di>& pos,
  137. const core::rect<s32>* clip,
  138. const video::SColor& color,
  139. u32 starttime, u32 currenttime,
  140. bool loop, bool center)
  141. {
  142. const irr::u32 drawCount = core::min_<u32>(indices.size(), pos.size());
  143. if( Textures.empty() )
  144. return;
  145. core::array<SDrawBatch> drawBatches(Textures.size());
  146. for(u32 i = 0;i < Textures.size();i++)
  147. {
  148. drawBatches.push_back(SDrawBatch());
  149. drawBatches[i].positions.reallocate(drawCount);
  150. drawBatches[i].sourceRects.reallocate(drawCount);
  151. }
  152. for(u32 i = 0;i < drawCount;i++)
  153. {
  154. const u32 index = indices[i];
  155. if (index >= Sprites.size() || Sprites[index].Frames.empty() )
  156. continue;
  157. // work out frame number
  158. u32 frame = 0;
  159. if (Sprites[index].frameTime)
  160. {
  161. u32 f = ((currenttime - starttime) / Sprites[index].frameTime);
  162. if (loop)
  163. frame = f % Sprites[index].Frames.size();
  164. else
  165. frame = (f >= Sprites[index].Frames.size()) ? Sprites[index].Frames.size()-1 : f;
  166. }
  167. const u32 texNum = Sprites[index].Frames[frame].textureNumber;
  168. SDrawBatch& currentBatch = drawBatches[texNum];
  169. const u32 rn = Sprites[index].Frames[frame].rectNumber;
  170. if (rn >= Rectangles.size())
  171. return;
  172. const core::rect<s32>& r = Rectangles[rn];
  173. if (center)
  174. {
  175. core::position2di p = pos[i];
  176. p -= r.getSize() / 2;
  177. currentBatch.positions.push_back(p);
  178. currentBatch.sourceRects.push_back(r);
  179. }
  180. else
  181. {
  182. currentBatch.positions.push_back(pos[i]);
  183. currentBatch.sourceRects.push_back(r);
  184. }
  185. }
  186. for(u32 i = 0;i < drawBatches.size();i++)
  187. {
  188. if(!drawBatches[i].positions.empty() && !drawBatches[i].sourceRects.empty())
  189. Driver->draw2DImageBatch(Textures[i], drawBatches[i].positions,
  190. drawBatches[i].sourceRects, clip, color, true);
  191. }
  192. }
  193. } // namespace gui
  194. } // namespace irr
  195. #endif // _IRR_COMPILE_WITH_GUI_