CGUIImageList.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. {
  23. Driver->grab();
  24. }
  25. }
  26. //! destructor
  27. CGUIImageList::~CGUIImageList()
  28. {
  29. if( Driver )
  30. {
  31. Driver->drop();
  32. }
  33. if( Texture )
  34. {
  35. Texture->drop();
  36. }
  37. }
  38. //! Creates the image list from texture.
  39. bool CGUIImageList::createImageList(video::ITexture* texture,
  40. core::dimension2d<s32> imageSize,
  41. bool useAlphaChannel)
  42. {
  43. if( !texture )
  44. {
  45. return false;
  46. }
  47. Texture = texture;
  48. Texture->grab();
  49. ImageSize = imageSize;
  50. ImagesPerRow = Texture->getSize().Width / ImageSize.Width;
  51. ImageCount = ImagesPerRow * Texture->getSize().Height / ImageSize.Height;
  52. UseAlphaChannel = useAlphaChannel;
  53. return true;
  54. }
  55. //! Draws an image and clips it to the specified rectangle if wanted
  56. void CGUIImageList::draw( s32 index, const core::position2d<s32>& destPos,
  57. const core::rect<s32>* clip /*= 0*/ )
  58. {
  59. core::rect<s32> sourceRect;
  60. if( !Driver || index < 0 || index >= ImageCount )
  61. {
  62. return;
  63. }
  64. sourceRect.UpperLeftCorner.X = ( index % ImagesPerRow ) * ImageSize.Width;
  65. sourceRect.UpperLeftCorner.Y = ( index / ImagesPerRow ) * ImageSize.Height;
  66. sourceRect.LowerRightCorner.X = sourceRect.UpperLeftCorner.X + ImageSize.Width;
  67. sourceRect.LowerRightCorner.Y = sourceRect.UpperLeftCorner.Y + ImageSize.Height;
  68. Driver->draw2DImage( Texture, destPos, sourceRect, clip,
  69. video::SColor( 255, 255, 255, 255 ), UseAlphaChannel );
  70. }
  71. } // end namespace gui
  72. } // end namespace irr