CGUIImageList.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // This file is part of the "Irrlicht Engine".
  2. // written by Reinhard Ostermeier, reinhard@nospam.r-ostermeier.de
  3. // modified by Thomas Alten
  4. #include "CGUIImageList.h"
  5. namespace irr
  6. {
  7. namespace gui
  8. {
  9. //! constructor
  10. CGUIImageList::CGUIImageList(video::IVideoDriver *driver) :
  11. Driver(driver),
  12. Texture(0),
  13. ImageCount(0),
  14. ImageSize(0, 0),
  15. ImagesPerRow(0),
  16. UseAlphaChannel(false)
  17. {
  18. #ifdef _DEBUG
  19. setDebugName("CGUIImageList");
  20. #endif
  21. if (Driver) {
  22. Driver->grab();
  23. }
  24. }
  25. //! destructor
  26. CGUIImageList::~CGUIImageList()
  27. {
  28. if (Driver) {
  29. Driver->drop();
  30. }
  31. if (Texture) {
  32. Texture->drop();
  33. }
  34. }
  35. //! Creates the image list from texture.
  36. bool CGUIImageList::createImageList(video::ITexture *texture,
  37. core::dimension2d<s32> imageSize,
  38. bool useAlphaChannel)
  39. {
  40. if (!texture) {
  41. return false;
  42. }
  43. Texture = texture;
  44. Texture->grab();
  45. ImageSize = imageSize;
  46. ImagesPerRow = Texture->getSize().Width / ImageSize.Width;
  47. ImageCount = ImagesPerRow * Texture->getSize().Height / ImageSize.Height;
  48. UseAlphaChannel = useAlphaChannel;
  49. return true;
  50. }
  51. //! Draws an image and clips it to the specified rectangle if wanted
  52. void CGUIImageList::draw(s32 index, const core::position2d<s32> &destPos,
  53. const core::rect<s32> *clip /*= 0*/)
  54. {
  55. core::rect<s32> sourceRect;
  56. if (!Driver || index < 0 || index >= ImageCount) {
  57. return;
  58. }
  59. sourceRect.UpperLeftCorner.X = (index % ImagesPerRow) * ImageSize.Width;
  60. sourceRect.UpperLeftCorner.Y = (index / ImagesPerRow) * ImageSize.Height;
  61. sourceRect.LowerRightCorner.X = sourceRect.UpperLeftCorner.X + ImageSize.Width;
  62. sourceRect.LowerRightCorner.Y = sourceRect.UpperLeftCorner.Y + ImageSize.Height;
  63. Driver->draw2DImage(Texture, destPos, sourceRect, clip,
  64. video::SColor(255, 255, 255, 255), UseAlphaChannel);
  65. }
  66. } // end namespace gui
  67. } // end namespace irr