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