aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8.1/source/Irrlicht/CGUIImageList.cpp
blob: e02030d5938d928725139acdd738520a8bf1d4a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// This file is part of the "Irrlicht Engine".
// written by Reinhard Ostermeier, reinhard@nospam.r-ostermeier.de
// modified by Thomas Alten

#include "CGUIImageList.h"


namespace irr
{
namespace gui
{

//! constructor
CGUIImageList::CGUIImageList( video::IVideoDriver* driver )
 :	Driver( driver ), 
	Texture( 0 ),
	ImageCount( 0 ),
	ImageSize( 0, 0 ),
	ImagesPerRow( 0 ),
	UseAlphaChannel( false )
{
	#ifdef _DEBUG
	setDebugName( "CGUIImageList" );
	#endif

	if( Driver )
	{
		Driver->grab();
	}
}



//! destructor
CGUIImageList::~CGUIImageList()
{
	if( Driver )
	{
		Driver->drop();
	}

	if( Texture )
	{
		Texture->drop();
	}
}


//! Creates the image list from texture.
bool CGUIImageList::createImageList(video::ITexture* texture,
				core::dimension2d<s32> imageSize,
				bool useAlphaChannel)
{
	if( !texture )
	{
		return false;
	}

	Texture = texture;
	Texture->grab();

	ImageSize = imageSize;

	ImagesPerRow = Texture->getSize().Width / ImageSize.Width;
	ImageCount = ImagesPerRow * Texture->getSize().Height / ImageSize.Height;

	UseAlphaChannel = useAlphaChannel;

	return true;
}

//! Draws an image and clips it to the specified rectangle if wanted
void CGUIImageList::draw( s32 index, const core::position2d<s32>& destPos, 
		const core::rect<s32>* clip /*= 0*/ )
{
	core::rect<s32> sourceRect;

	if( !Driver || index < 0 || index >= ImageCount )
	{
		return;
	}

	sourceRect.UpperLeftCorner.X = ( index % ImagesPerRow ) * ImageSize.Width;
	sourceRect.UpperLeftCorner.Y = ( index / ImagesPerRow ) * ImageSize.Height;
	sourceRect.LowerRightCorner.X = sourceRect.UpperLeftCorner.X + ImageSize.Width;
	sourceRect.LowerRightCorner.Y = sourceRect.UpperLeftCorner.Y + ImageSize.Height;

	Driver->draw2DImage( Texture, destPos, sourceRect, clip, 
								video::SColor( 255, 255, 255, 255 ), UseAlphaChannel );
}

} // end namespace gui
} // end namespace irr