Animations.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "StdInc.h"
  2. #include "Animations.h"
  3. #include "Images.h"
  4. #include "FilesHeaders.h"
  5. namespace Gfx
  6. {
  7. #define LE(x) SDL_SwapLE32(x)
  8. /*********** CAnimation ***********/
  9. CAnimation* CAnimation::makeFromDEF(const SH3DefFile& def, size_t fileSize)
  10. {
  11. if (def.totalBlocks == 0 || def.width == 0 || def.height == 0) return nullptr;
  12. if (def.fbEntrCount() == LE(1))
  13. {
  14. CImage* img = CImage::makeFromDEF(def, fileSize);
  15. if (img == nullptr) return nullptr;
  16. return new COneFrameAnimation(*img);
  17. }
  18. return new (LE(def.fbEntrCount())) CPalettedAnimation(def, fileSize);
  19. }
  20. CAnimation::~CAnimation() {}
  21. /*********** COneFrameAnimation ***********/
  22. COneFrameAnimation::COneFrameAnimation(CImage& img) :
  23. CAnimation(1, img.getWidth(), img.getHeight()),
  24. frame(img)
  25. {
  26. }
  27. COneFrameAnimation::~COneFrameAnimation()
  28. {
  29. delete &frame;
  30. }
  31. CImage* COneFrameAnimation::getFrame(ui32 fnr)
  32. {
  33. if (fnr == 0) return &frame;
  34. return nullptr;
  35. }
  36. /*********** CPalettedAnimation ***********/
  37. CPalettedAnimation::CPalettedAnimation(const SH3DefFile& def, size_t fileSize) :
  38. CAnimation(LE(def.fbEntrCount()), LE(def.width), LE(def.height)),
  39. palette(def.palette, def.type == LE(71) ? 1 : 10, true)
  40. //type == 71 - Buttons/buildings don't have shadows\semi-transparency
  41. {
  42. ua_ui32_ptr offsets = def.firstBlock.offsets();
  43. for (ui32 j=0; j<framesCount; ++j)
  44. {
  45. CPalettedBitmap* fr = CImage::makeFromDEFSprite(def.getSprite(offsets[j]), palette);
  46. if (fr == nullptr)
  47. {
  48. framesCount = j;
  49. break;
  50. }
  51. frames[j] = fr;
  52. }
  53. }
  54. CPalettedAnimation::~CPalettedAnimation()
  55. {
  56. for (ui32 j=0; j<framesCount; ++j)
  57. {
  58. delete frames[j];
  59. }
  60. }
  61. CImage* CPalettedAnimation::getFrame(ui32 fnr)
  62. {
  63. if (fnr >= framesCount) return nullptr;
  64. return frames[fnr];
  65. }
  66. }