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
35 for (u32 i=0; i<Textures.size(); ++i)
\r
37 Textures[i]->drop();
\r
39 // drop video driver
\r
45 core::array< core::rect<s32> >& CGUISpriteBank::getPositions()
\r
51 core::array< SGUISprite >& CGUISpriteBank::getSprites()
\r
57 u32 CGUISpriteBank::getTextureCount() const
\r
59 return Textures.size();
\r
63 video::ITexture* CGUISpriteBank::getTexture(u32 index) const
\r
65 if (index < Textures.size())
\r
66 return Textures[index];
\r
72 void CGUISpriteBank::addTexture(video::ITexture* texture)
\r
77 Textures.push_back(texture);
\r
81 void CGUISpriteBank::setTexture(u32 index, video::ITexture* texture)
\r
83 while (index >= Textures.size())
\r
84 Textures.push_back(0);
\r
89 if (Textures[index])
\r
90 Textures[index]->drop();
\r
92 Textures[index] = texture;
\r
96 //! clear everything
\r
97 void CGUISpriteBank::clear()
\r
100 for (u32 i=0; i<Textures.size(); ++i)
\r
103 Textures[i]->drop();
\r
107 Rectangles.clear();
\r
110 //! Add the texture and use it for a single non-animated sprite.
\r
111 s32 CGUISpriteBank::addTextureAsSprite(video::ITexture* texture)
\r
116 addTexture(texture);
\r
117 u32 textureIndex = getTextureCount() - 1;
\r
119 u32 rectangleIndex = Rectangles.size();
\r
120 Rectangles.push_back( core::rect<s32>(0,0, texture->getOriginalSize().Width, texture->getOriginalSize().Height) );
\r
123 sprite.frameTime = 0;
\r
125 SGUISpriteFrame frame;
\r
126 frame.textureNumber = textureIndex;
\r
127 frame.rectNumber = rectangleIndex;
\r
128 sprite.Frames.push_back( frame );
\r
130 Sprites.push_back( sprite );
\r
132 return Sprites.size() - 1;
\r
135 //! draws a sprite in 2d with scale and color
\r
136 void CGUISpriteBank::draw2DSprite(u32 index, const core::position2di& pos,
\r
137 const core::rect<s32>* clip, const video::SColor& color,
\r
138 u32 starttime, u32 currenttime, bool loop, bool center)
\r
140 if (index >= Sprites.size() || Sprites[index].Frames.empty() )
\r
143 u32 frame = getFrameNr(index, currenttime - starttime, loop);
\r
144 const video::ITexture* tex = getTexture(Sprites[index].Frames[frame].textureNumber);
\r
148 const u32 rn = Sprites[index].Frames[frame].rectNumber;
\r
149 if (rn >= Rectangles.size())
\r
152 const core::rect<s32>& r = Rectangles[rn];
\r
153 core::position2di p(pos);
\r
156 p -= r.getSize() / 2;
\r
158 Driver->draw2DImage(tex, p, r, clip, color, true);
\r
161 void CGUISpriteBank::draw2DSprite(u32 index, const core::rect<s32>& destRect,
\r
162 const core::rect<s32>* clip, const video::SColor * const colors,
\r
163 u32 timeTicks, bool loop)
\r
165 if (index >= Sprites.size() || Sprites[index].Frames.empty() )
\r
168 u32 frame = getFrameNr(index, timeTicks, loop);
\r
169 const video::ITexture* tex = getTexture(Sprites[index].Frames[frame].textureNumber);
\r
173 const u32 rn = Sprites[index].Frames[frame].rectNumber;
\r
174 if (rn >= Rectangles.size())
\r
177 Driver->draw2DImage(tex, destRect, Rectangles[rn], clip, colors, true);
\r
180 void CGUISpriteBank::draw2DSpriteBatch( const core::array<u32>& indices,
\r
181 const core::array<core::position2di>& pos,
\r
182 const core::rect<s32>* clip,
\r
183 const video::SColor& color,
\r
184 u32 starttime, u32 currenttime,
\r
185 bool loop, bool center)
\r
187 const irr::u32 drawCount = core::min_<u32>(indices.size(), pos.size());
\r
189 if (!getTextureCount())
\r
191 core::array<SDrawBatch> drawBatches(getTextureCount());
\r
192 for (u32 i=0; i < Textures.size(); ++i)
\r
194 drawBatches.push_back(SDrawBatch());
\r
195 drawBatches[i].positions.reallocate(drawCount);
\r
196 drawBatches[i].sourceRects.reallocate(drawCount);
\r
199 for (u32 i = 0; i < drawCount; ++i)
\r
201 const u32 index = indices[i];
\r
203 if (index >= Sprites.size() || Sprites[index].Frames.empty() )
\r
206 // work out frame number
\r
208 if (Sprites[index].frameTime)
\r
210 u32 f = ((currenttime - starttime) / Sprites[index].frameTime);
\r
212 frame = f % Sprites[index].Frames.size();
\r
214 frame = (f >= Sprites[index].Frames.size()) ? Sprites[index].Frames.size()-1 : f;
\r
217 const u32 texNum = Sprites[index].Frames[frame].textureNumber;
\r
218 SDrawBatch& currentBatch = drawBatches[texNum];
\r
220 const u32 rn = Sprites[index].Frames[frame].rectNumber;
\r
221 if (rn >= Rectangles.size())
\r
224 const core::rect<s32>& r = Rectangles[rn];
\r
228 core::position2di p = pos[i];
\r
229 p -= r.getSize() / 2;
\r
231 currentBatch.positions.push_back(p);
\r
232 currentBatch.sourceRects.push_back(r);
\r
236 currentBatch.positions.push_back(pos[i]);
\r
237 currentBatch.sourceRects.push_back(r);
\r
241 for(u32 i = 0;i < drawBatches.size();i++)
\r
243 if(!drawBatches[i].positions.empty() && !drawBatches[i].sourceRects.empty())
\r
244 Driver->draw2DImageBatch(getTexture(i), drawBatches[i].positions,
\r
245 drawBatches[i].sourceRects, clip, color, true);
\r