1
// Copyright (C) 2002-2011 Nikolaus Gebhardt
2
// This file is part of the "Irrlicht Engine".
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
5
#include "CGUISpriteBank.h"
6
#ifdef _IRR_COMPILE_WITH_GUI_
8
#include "IGUIEnvironment.h"
9
#include "IVideoDriver.h"
17
CGUISpriteBank::CGUISpriteBank(IGUIEnvironment* env) :
18
Environment(env), Driver(0)
21
setDebugName("CGUISpriteBank");
26
Driver = Environment->getVideoDriver();
33
CGUISpriteBank::~CGUISpriteBank()
36
for (u32 i=0; i<Textures.size(); ++i)
46
core::array< core::rect<s32> >& CGUISpriteBank::getPositions()
52
core::array< SGUISprite >& CGUISpriteBank::getSprites()
58
u32 CGUISpriteBank::getTextureCount() const
60
return Textures.size();
64
video::ITexture* CGUISpriteBank::getTexture(u32 index) const
66
if (index < Textures.size())
67
return Textures[index];
73
void CGUISpriteBank::addTexture(video::ITexture* texture)
78
Textures.push_back(texture);
82
void CGUISpriteBank::setTexture(u32 index, video::ITexture* texture)
84
while (index >= Textures.size())
85
Textures.push_back(0);
91
Textures[index]->drop();
93
Textures[index] = texture;
98
void CGUISpriteBank::clear()
101
for (u32 i=0; i<Textures.size(); ++i)
109
//! Add the texture and use it for a single non-animated sprite.
110
s32 CGUISpriteBank::addTextureAsSprite(video::ITexture* texture)
116
u32 textureIndex = getTextureCount() - 1;
118
u32 rectangleIndex = Rectangles.size();
119
Rectangles.push_back( core::rect<s32>(0,0, texture->getOriginalSize().Width, texture->getOriginalSize().Height) );
122
sprite.frameTime = 0;
124
SGUISpriteFrame frame;
125
frame.textureNumber = textureIndex;
126
frame.rectNumber = rectangleIndex;
127
sprite.Frames.push_back( frame );
129
Sprites.push_back( sprite );
131
return Sprites.size() - 1;
134
//! draws a sprite in 2d with scale and color
135
void CGUISpriteBank::draw2DSprite(u32 index, const core::position2di& pos,
136
const core::rect<s32>* clip, const video::SColor& color,
137
u32 starttime, u32 currenttime, bool loop, bool center)
139
if (index >= Sprites.size() || Sprites[index].Frames.empty() )
142
// work out frame number
144
if (Sprites[index].frameTime)
146
u32 f = ((currenttime - starttime) / Sprites[index].frameTime);
148
frame = f % Sprites[index].Frames.size();
150
frame = (f >= Sprites[index].Frames.size()) ? Sprites[index].Frames.size()-1 : f;
153
const video::ITexture* tex = Textures[Sprites[index].Frames[frame].textureNumber];
157
const u32 rn = Sprites[index].Frames[frame].rectNumber;
158
if (rn >= Rectangles.size())
161
const core::rect<s32>& r = Rectangles[rn];
165
core::position2di p = pos;
166
p -= r.getSize() / 2;
167
Driver->draw2DImage(tex, p, r, clip, color, true);
171
Driver->draw2DImage(tex, pos, r, clip, color, true);
176
void CGUISpriteBank::draw2DSpriteBatch( const core::array<u32>& indices,
177
const core::array<core::position2di>& pos,
178
const core::rect<s32>* clip,
179
const video::SColor& color,
180
u32 starttime, u32 currenttime,
181
bool loop, bool center)
183
const irr::u32 drawCount = core::min_<u32>(indices.size(), pos.size());
185
core::array<SDrawBatch> drawBatches(Textures.size());
186
for(u32 i = 0;i < Textures.size();i++)
188
drawBatches.push_back(SDrawBatch());
189
drawBatches[i].positions.reallocate(drawCount);
190
drawBatches[i].sourceRects.reallocate(drawCount);
193
for(u32 i = 0;i < drawCount;i++)
195
const u32 index = indices[i];
197
if (index >= Sprites.size() || Sprites[index].Frames.empty() )
200
// work out frame number
202
if (Sprites[index].frameTime)
204
u32 f = ((currenttime - starttime) / Sprites[index].frameTime);
206
frame = f % Sprites[index].Frames.size();
208
frame = (f >= Sprites[index].Frames.size()) ? Sprites[index].Frames.size()-1 : f;
211
const u32 texNum = Sprites[index].Frames[frame].textureNumber;
213
SDrawBatch& currentBatch = drawBatches[texNum];
215
const u32 rn = Sprites[index].Frames[frame].rectNumber;
216
if (rn >= Rectangles.size())
219
const core::rect<s32>& r = Rectangles[rn];
223
core::position2di p = pos[i];
224
p -= r.getSize() / 2;
226
currentBatch.positions.push_back(p);
227
currentBatch.sourceRects.push_back(r);
231
currentBatch.positions.push_back(pos[i]);
232
currentBatch.sourceRects.push_back(r);
236
for(u32 i = 0;i < drawBatches.size();i++)
238
if(!drawBatches[i].positions.empty() && !drawBatches[i].sourceRects.empty())
239
Driver->draw2DImageBatch(Textures[i], drawBatches[i].positions,
240
drawBatches[i].sourceRects, clip, color, true);
247
#endif // _IRR_COMPILE_WITH_GUI_