1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #include "CGUISpriteBank.h"
\r
7 #include "IGUIEnvironment.h"
\r
8 #include "IVideoDriver.h"
\r
9 #include "ITexture.h"
\r
16 CGUISpriteBank::CGUISpriteBank(IGUIEnvironment* env) :
\r
17 Environment(env), Driver(0)
\r
20 setDebugName("CGUISpriteBank");
\r
25 Driver = Environment->getVideoDriver();
\r
32 CGUISpriteBank::~CGUISpriteBank()
\r
36 // drop video driver
\r
42 core::array< core::rect<s32> >& CGUISpriteBank::getPositions()
\r
48 core::array< SGUISprite >& CGUISpriteBank::getSprites()
\r
54 u32 CGUISpriteBank::getTextureCount() const
\r
56 return Textures.size();
\r
60 video::ITexture* CGUISpriteBank::getTexture(u32 index) const
\r
62 if (index < Textures.size())
\r
63 return Textures[index];
\r
69 void CGUISpriteBank::addTexture(video::ITexture* texture)
\r
74 Textures.push_back(texture);
\r
78 void CGUISpriteBank::setTexture(u32 index, video::ITexture* texture)
\r
80 while (index >= Textures.size())
\r
81 Textures.push_back(0);
\r
86 if (Textures[index])
\r
87 Textures[index]->drop();
\r
89 Textures[index] = texture;
\r
93 //! clear everything
\r
94 void CGUISpriteBank::clear()
\r
97 for (u32 i=0; i<Textures.size(); ++i)
\r
100 Textures[i]->drop();
\r
104 Rectangles.clear();
\r
107 //! Add the texture and use it for a single non-animated sprite.
\r
108 s32 CGUISpriteBank::addTextureAsSprite(video::ITexture* texture)
\r
113 addTexture(texture);
\r
114 u32 textureIndex = getTextureCount() - 1;
\r
116 u32 rectangleIndex = Rectangles.size();
\r
117 Rectangles.push_back( core::rect<s32>(0,0, texture->getOriginalSize().Width, texture->getOriginalSize().Height) );
\r
120 sprite.frameTime = 0;
\r
122 SGUISpriteFrame frame;
\r
123 frame.textureNumber = textureIndex;
\r
124 frame.rectNumber = rectangleIndex;
\r
125 sprite.Frames.push_back( frame );
\r
127 Sprites.push_back( sprite );
\r
129 return Sprites.size() - 1;
\r
132 // get FrameNr for time. return true on exisiting frame
\r
133 inline bool CGUISpriteBank::getFrameNr(u32& frame,u32 index, u32 time, bool loop) const
\r
136 if (index >= Sprites.size())
\r
139 const SGUISprite& sprite = Sprites[index];
\r
140 const u32 frameSize = sprite.Frames.size();
\r
144 if (sprite.frameTime)
\r
146 u32 f = (time / sprite.frameTime);
\r
148 frame = f % frameSize;
\r
150 frame = (f >= frameSize) ? frameSize - 1 : f;
\r
155 //! draws a sprite in 2d with scale and color
\r
156 void CGUISpriteBank::draw2DSprite(u32 index, const core::position2di& pos,
\r
157 const core::rect<s32>* clip, const video::SColor& color,
\r
158 u32 starttime, u32 currenttime, bool loop, bool center)
\r
161 if (!getFrameNr(frame, index, currenttime - starttime, loop))
\r
164 const video::ITexture* tex = getTexture(Sprites[index].Frames[frame].textureNumber);
\r
168 const u32 rn = Sprites[index].Frames[frame].rectNumber;
\r
169 if (rn >= Rectangles.size())
\r
172 const core::rect<s32>& r = Rectangles[rn];
\r
173 core::position2di p(pos);
\r
176 p -= r.getSize() / 2;
\r
178 Driver->draw2DImage(tex, p, r, clip, color, true);
\r
181 void CGUISpriteBank::draw2DSprite(u32 index, const core::rect<s32>& destRect,
\r
182 const core::rect<s32>* clip, const video::SColor * const colors,
\r
183 u32 timeTicks, bool loop)
\r
186 if (!getFrameNr(frame,index, timeTicks, loop))
\r
189 const video::ITexture* tex = getTexture(Sprites[index].Frames[frame].textureNumber);
\r
193 const u32 rn = Sprites[index].Frames[frame].rectNumber;
\r
194 if (rn >= Rectangles.size())
\r
197 Driver->draw2DImage(tex, destRect, Rectangles[rn], clip, colors, true);
\r
200 void CGUISpriteBank::draw2DSpriteBatch( const core::array<u32>& indices,
\r
201 const core::array<core::position2di>& pos,
\r
202 const core::rect<s32>* clip,
\r
203 const video::SColor& color,
\r
204 u32 starttime, u32 currenttime,
\r
205 bool loop, bool center)
\r
207 const irr::u32 drawCount = core::min_<u32>(indices.size(), pos.size());
\r
209 if (!getTextureCount())
\r
211 core::array<SDrawBatch> drawBatches(getTextureCount());
\r
212 for (u32 i=0; i < Textures.size(); ++i)
\r
214 drawBatches.push_back(SDrawBatch());
\r
215 drawBatches[i].positions.reallocate(drawCount);
\r
216 drawBatches[i].sourceRects.reallocate(drawCount);
\r
219 for (u32 i = 0; i < drawCount; ++i)
\r
221 const u32 index = indices[i];
\r
223 // work out frame number
\r
225 if (!getFrameNr(frame, index, currenttime - starttime, loop))
\r
228 const u32 texNum = Sprites[index].Frames[frame].textureNumber;
\r
229 if (texNum >= drawBatches.size())
\r
233 SDrawBatch& currentBatch = drawBatches[texNum];
\r
235 const u32 rn = Sprites[index].Frames[frame].rectNumber;
\r
236 if (rn >= Rectangles.size())
\r
239 const core::rect<s32>& r = Rectangles[rn];
\r
243 core::position2di p = pos[i];
\r
244 p -= r.getSize() / 2;
\r
246 currentBatch.positions.push_back(p);
\r
247 currentBatch.sourceRects.push_back(r);
\r
251 currentBatch.positions.push_back(pos[i]);
\r
252 currentBatch.sourceRects.push_back(r);
\r
256 for(u32 i = 0;i < drawBatches.size();i++)
\r
258 if(!drawBatches[i].positions.empty() && !drawBatches[i].sourceRects.empty())
\r
259 Driver->draw2DImageBatch(getTexture(i), drawBatches[i].positions,
\r
260 drawBatches[i].sourceRects, clip, color, true);
\r