ImageScaled.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * ImageScaled.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ImageScaled.h"
  12. #include "SDLImage.h"
  13. #include "SDL_Extensions.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../render/IScreenHandler.h"
  16. #include "../render/Colors.h"
  17. #include "../../lib/constants/EntityIdentifiers.h"
  18. #include <SDL_surface.h>
  19. ImageScaled::ImageScaled(const ImageLocator & inputLocator, const std::shared_ptr<ISharedImage> & source, EImageBlitMode mode)
  20. : source(source)
  21. , locator(inputLocator)
  22. , colorMultiplier(Colors::WHITE_TRUE)
  23. , alphaValue(SDL_ALPHA_OPAQUE)
  24. , blitMode(mode)
  25. {
  26. locator.scalingFactor = GH.screenHandler().getScalingFactor();
  27. setBodyEnabled(true);
  28. }
  29. std::shared_ptr<ISharedImage> ImageScaled::getSharedImage() const
  30. {
  31. return body;
  32. }
  33. void ImageScaled::scaleFast(const Point &size)
  34. {
  35. if (body)
  36. body = body->scaleFast(size, nullptr); // FIXME: adjust for scaling
  37. }
  38. void ImageScaled::exportBitmap(const boost::filesystem::path &path) const
  39. {
  40. source->exportBitmap(path);
  41. }
  42. bool ImageScaled::isTransparent(const Point &coords) const
  43. {
  44. return source->isTransparent(coords);
  45. }
  46. Point ImageScaled::dimensions() const
  47. {
  48. return source->dimensions();
  49. }
  50. void ImageScaled::setAlpha(uint8_t value)
  51. {
  52. alphaValue = value;
  53. }
  54. void ImageScaled::setBlitMode(EImageBlitMode mode)
  55. {
  56. blitMode = mode;
  57. }
  58. void ImageScaled::draw(SDL_Surface *where, const Point &pos, const Rect *src) const
  59. {
  60. if (shadow)
  61. shadow->draw(where, nullptr, pos, src, Colors::WHITE_TRUE, alphaValue, blitMode);
  62. if (body)
  63. body->draw(where, nullptr, pos, src, Colors::WHITE_TRUE, alphaValue, blitMode);
  64. if (overlay)
  65. overlay->draw(where, nullptr, pos, src, colorMultiplier, colorMultiplier.a * alphaValue / 255, blitMode);
  66. }
  67. void ImageScaled::setOverlayColor(const ColorRGBA & color)
  68. {
  69. colorMultiplier = color;
  70. }
  71. void ImageScaled::playerColored(PlayerColor player)
  72. {
  73. playerColor = player;
  74. if (body)
  75. setBodyEnabled(true); // regenerate
  76. }
  77. void ImageScaled::shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove)
  78. {
  79. // TODO: implement
  80. }
  81. void ImageScaled::adjustPalette(const ColorFilter &shifter, uint32_t colorsToSkipMask)
  82. {
  83. // TODO: implement
  84. }
  85. void ImageScaled::setShadowEnabled(bool on)
  86. {
  87. if (on)
  88. {
  89. locator.layerBody = false;
  90. locator.layerShadow = true;
  91. locator.layerOverlay = false;
  92. locator.playerColored = PlayerColor::CANNOT_DETERMINE;
  93. shadow = GH.renderHandler().loadImage(locator, blitMode)->getSharedImage();
  94. }
  95. else
  96. shadow = nullptr;
  97. }
  98. void ImageScaled::setBodyEnabled(bool on)
  99. {
  100. if (on)
  101. {
  102. locator.layerBody = true;
  103. locator.layerShadow = false;
  104. locator.layerOverlay = false;
  105. locator.playerColored = playerColor;
  106. body = GH.renderHandler().loadImage(locator, blitMode)->getSharedImage();
  107. }
  108. else
  109. body = nullptr;
  110. }
  111. void ImageScaled::setOverlayEnabled(bool on)
  112. {
  113. if (on)
  114. {
  115. locator.layerBody = false;
  116. locator.layerShadow = false;
  117. locator.layerOverlay = true;
  118. locator.playerColored = PlayerColor::CANNOT_DETERMINE;
  119. overlay = GH.renderHandler().loadImage(locator, blitMode)->getSharedImage();
  120. }
  121. else
  122. overlay = nullptr;
  123. }