CSceneNodeAnimatorTexture.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "CSceneNodeAnimatorTexture.h"
  5. #include "ITexture.h"
  6. namespace irr
  7. {
  8. namespace scene
  9. {
  10. //! constructor
  11. CSceneNodeAnimatorTexture::CSceneNodeAnimatorTexture(const core::array<video::ITexture*>& textures,
  12. s32 timePerFrame, bool loop, u32 now)
  13. : ISceneNodeAnimatorFinishing(0),
  14. TimePerFrame(timePerFrame), StartTime(now), Loop(loop)
  15. {
  16. #ifdef _DEBUG
  17. setDebugName("CSceneNodeAnimatorTexture");
  18. #endif
  19. for (u32 i=0; i<textures.size(); ++i)
  20. {
  21. if (textures[i])
  22. textures[i]->grab();
  23. Textures.push_back(textures[i]);
  24. }
  25. FinishTime = now + (timePerFrame * Textures.size());
  26. }
  27. //! destructor
  28. CSceneNodeAnimatorTexture::~CSceneNodeAnimatorTexture()
  29. {
  30. clearTextures();
  31. }
  32. void CSceneNodeAnimatorTexture::clearTextures()
  33. {
  34. for (u32 i=0; i<Textures.size(); ++i)
  35. if (Textures[i])
  36. Textures[i]->drop();
  37. }
  38. //! animates a scene node
  39. void CSceneNodeAnimatorTexture::animateNode(ISceneNode* node, u32 timeMs)
  40. {
  41. if(!node)
  42. return;
  43. if (Textures.size())
  44. {
  45. const u32 t = (timeMs-StartTime);
  46. u32 idx = 0;
  47. if (!Loop && timeMs >= FinishTime)
  48. {
  49. idx = Textures.size() - 1;
  50. HasFinished = true;
  51. }
  52. else
  53. {
  54. idx = (t/TimePerFrame) % Textures.size();
  55. }
  56. if (idx < Textures.size())
  57. node->setMaterialTexture(0, Textures[idx]);
  58. }
  59. }
  60. //! Writes attributes of the scene node animator.
  61. void CSceneNodeAnimatorTexture::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
  62. {
  63. out->addInt("TimePerFrame", TimePerFrame);
  64. out->addBool("Loop", Loop);
  65. // add one texture in addition when serializing for editors
  66. // to make it easier to add textures quickly
  67. u32 count = Textures.size();
  68. if ( options && (options->Flags & io::EARWF_FOR_EDITOR))
  69. count += 1;
  70. for (u32 i=0; i<count; ++i)
  71. {
  72. core::stringc tname = "Texture";
  73. tname += (int)(i+1);
  74. out->addTexture(tname.c_str(), i<Textures.size() ? Textures[i] : 0);
  75. }
  76. }
  77. //! Reads attributes of the scene node animator.
  78. void CSceneNodeAnimatorTexture::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
  79. {
  80. TimePerFrame = in->getAttributeAsInt("TimePerFrame");
  81. Loop = in->getAttributeAsBool("Loop");
  82. clearTextures();
  83. for(u32 i=1; true; ++i)
  84. {
  85. core::stringc tname = "Texture";
  86. tname += (int)i;
  87. if (in->existsAttribute(tname.c_str()))
  88. {
  89. video::ITexture* tex = in->getAttributeAsTexture(tname.c_str());
  90. if (tex)
  91. {
  92. tex->grab();
  93. Textures.push_back(tex);
  94. }
  95. }
  96. else
  97. break;
  98. }
  99. }
  100. ISceneNodeAnimator* CSceneNodeAnimatorTexture::createClone(ISceneNode* node, ISceneManager* newManager)
  101. {
  102. CSceneNodeAnimatorTexture * newAnimator =
  103. new CSceneNodeAnimatorTexture(Textures, TimePerFrame, Loop, StartTime);
  104. return newAnimator;
  105. }
  106. } // end namespace scene
  107. } // end namespace irr