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 
5 #include "CGUIImageList.h"
6 
7 
8 namespace irr
9 {
10 namespace gui
11 {
12 
13 //! constructor
CGUIImageList(video::IVideoDriver * driver)14 CGUIImageList::CGUIImageList( video::IVideoDriver* driver )
15  :	Driver( driver ),
16 	Texture( 0 ),
17 	ImageCount( 0 ),
18 	ImageSize( 0, 0 ),
19 	ImagesPerRow( 0 ),
20 	UseAlphaChannel( false )
21 {
22 	#ifdef _DEBUG
23 	setDebugName( "CGUIImageList" );
24 	#endif
25 
26 	if( Driver )
27 	{
28 		Driver->grab();
29 	}
30 }
31 
32 
33 
34 //! destructor
~CGUIImageList()35 CGUIImageList::~CGUIImageList()
36 {
37 	if( Driver )
38 	{
39 		Driver->drop();
40 	}
41 
42 	if( Texture )
43 	{
44 		Texture->drop();
45 	}
46 }
47 
48 
49 //! Creates the image list from texture.
createImageList(video::ITexture * texture,core::dimension2d<s32> imageSize,bool useAlphaChannel)50 bool CGUIImageList::createImageList(video::ITexture* texture,
51 				core::dimension2d<s32> imageSize,
52 				bool useAlphaChannel)
53 {
54 	if( !texture )
55 	{
56 		return false;
57 	}
58 
59 	Texture = texture;
60 	Texture->grab();
61 
62 	ImageSize = imageSize;
63 
64 	ImagesPerRow = Texture->getSize().Width / ImageSize.Width;
65 	ImageCount = ImagesPerRow * Texture->getSize().Height / ImageSize.Height;
66 
67 	UseAlphaChannel = useAlphaChannel;
68 
69 	return true;
70 }
71 
72 //! Draws an image and clips it to the specified rectangle if wanted
draw(s32 index,const core::position2d<s32> & destPos,const core::rect<s32> * clip)73 void CGUIImageList::draw( s32 index, const core::position2d<s32>& destPos,
74 		const core::rect<s32>* clip /*= 0*/ )
75 {
76 	core::rect<s32> sourceRect;
77 
78 	if( !Driver || index < 0 || index >= ImageCount )
79 	{
80 		return;
81 	}
82 
83 	sourceRect.UpperLeftCorner.X = ( index % ImagesPerRow ) * ImageSize.Width;
84 	sourceRect.UpperLeftCorner.Y = ( index / ImagesPerRow ) * ImageSize.Height;
85 	sourceRect.LowerRightCorner.X = sourceRect.UpperLeftCorner.X + ImageSize.Width;
86 	sourceRect.LowerRightCorner.Y = sourceRect.UpperLeftCorner.Y + ImageSize.Height;
87 
88 	Driver->draw2DImage( Texture, destPos, sourceRect, clip,
89 								video::SColor( 255, 255, 255, 255 ), UseAlphaChannel );
90 }
91 
92 } // end namespace gui
93 } // end namespace irr
94