a) SpriteBank. error check on non existing textureNumber. Seen in broken Fonts. unified getFrameNr
b) CBillboardTextSceneNode. use getOriginalSize

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@6368 dfc29bdd-3216-0410-991c-e03cc46cb475
This commit is contained in:
engineer_apple 2022-05-01 09:50:32 +00:00
parent 61bc7d3a22
commit d19d6939d4
3 changed files with 38 additions and 34 deletions

View File

@ -32,10 +32,7 @@ CGUISpriteBank::CGUISpriteBank(IGUIEnvironment* env) :
CGUISpriteBank::~CGUISpriteBank() CGUISpriteBank::~CGUISpriteBank()
{ {
// drop textures clear();
for (u32 i=0; i<Textures.size(); ++i)
if (Textures[i])
Textures[i]->drop();
// drop video driver // drop video driver
if (Driver) if (Driver)
@ -133,15 +130,38 @@ s32 CGUISpriteBank::addTextureAsSprite(video::ITexture* texture)
return Sprites.size() - 1; return Sprites.size() - 1;
} }
// get FrameNr for time. return true on exisiting frame
inline bool CGUISpriteBank::getFrameNr(u32& frame,u32 index, u32 time, bool loop) const
{
frame = 0;
if (index >= Sprites.size())
return false;
const SGUISprite& sprite = Sprites[index];
const u32 frameSize = sprite.Frames.size();
if (frameSize < 1)
return false;
if (sprite.frameTime)
{
u32 f = (time / sprite.frameTime);
if (loop)
frame = f % frameSize;
else
frame = (f >= frameSize) ? frameSize - 1 : f;
}
return true;
}
//! draws a sprite in 2d with scale and color //! draws a sprite in 2d with scale and color
void CGUISpriteBank::draw2DSprite(u32 index, const core::position2di& pos, void CGUISpriteBank::draw2DSprite(u32 index, const core::position2di& pos,
const core::rect<s32>* clip, const video::SColor& color, const core::rect<s32>* clip, const video::SColor& color,
u32 starttime, u32 currenttime, bool loop, bool center) u32 starttime, u32 currenttime, bool loop, bool center)
{ {
if (index >= Sprites.size() || Sprites[index].Frames.empty() ) u32 frame = 0;
if (!getFrameNr(frame, index, currenttime - starttime, loop))
return; return;
u32 frame = getFrameNr(index, currenttime - starttime, loop);
const video::ITexture* tex = getTexture(Sprites[index].Frames[frame].textureNumber); const video::ITexture* tex = getTexture(Sprites[index].Frames[frame].textureNumber);
if (!tex) if (!tex)
return; return;
@ -163,10 +183,10 @@ void CGUISpriteBank::draw2DSprite(u32 index, const core::rect<s32>& destRect,
const core::rect<s32>* clip, const video::SColor * const colors, const core::rect<s32>* clip, const video::SColor * const colors,
u32 timeTicks, bool loop) u32 timeTicks, bool loop)
{ {
if (index >= Sprites.size() || Sprites[index].Frames.empty() ) u32 frame = 0;
if (!getFrameNr(frame,index, timeTicks, loop))
return; return;
u32 frame = getFrameNr(index, timeTicks, loop);
const video::ITexture* tex = getTexture(Sprites[index].Frames[frame].textureNumber); const video::ITexture* tex = getTexture(Sprites[index].Frames[frame].textureNumber);
if (!tex) if (!tex)
return; return;
@ -201,21 +221,16 @@ void CGUISpriteBank::draw2DSpriteBatch( const core::array<u32>& indices,
{ {
const u32 index = indices[i]; const u32 index = indices[i];
if (index >= Sprites.size() || Sprites[index].Frames.empty() )
continue;
// work out frame number // work out frame number
u32 frame = 0; u32 frame = 0;
if (Sprites[index].frameTime) if (!getFrameNr(frame, index, currenttime - starttime, loop))
{ return;
u32 f = ((currenttime - starttime) / Sprites[index].frameTime);
if (loop)
frame = f % Sprites[index].Frames.size();
else
frame = (f >= Sprites[index].Frames.size()) ? Sprites[index].Frames.size()-1 : f;
}
const u32 texNum = Sprites[index].Frames[frame].textureNumber; const u32 texNum = Sprites[index].Frames[frame].textureNumber;
if (texNum >= drawBatches.size())
{
continue;
}
SDrawBatch& currentBatch = drawBatches[texNum]; SDrawBatch& currentBatch = drawBatches[texNum];
const u32 rn = Sprites[index].Frames[frame].rectNumber; const u32 rn = Sprites[index].Frames[frame].rectNumber;

View File

@ -67,19 +67,7 @@ public:
protected: protected:
inline u32 getFrameNr(u32 index, u32 time, bool loop) const bool getFrameNr(u32& frameNr, u32 index, u32 time, bool loop) const;
{
u32 frame = 0;
if (Sprites[index].frameTime && Sprites[index].Frames.size() )
{
u32 f = (time / Sprites[index].frameTime);
if (loop)
frame = f % Sprites[index].Frames.size();
else
frame = (f >= Sprites[index].Frames.size()) ? Sprites[index].Frames.size()-1 : f;
}
return frame;
}
struct SDrawBatch struct SDrawBatch
{ {

View File

@ -215,8 +215,9 @@ void CBillboardTextSceneNode::setText(const wchar_t* text)
u32 rectno = sprites[spriteno].Frames[0].rectNumber; u32 rectno = sprites[spriteno].Frames[0].rectNumber;
u32 texno = sprites[spriteno].Frames[0].textureNumber; u32 texno = sprites[spriteno].Frames[0].textureNumber;
dim[0] = core::reciprocal ( (f32) Font->getSpriteBank()->getTexture(texno)->getSize().Width ); const core::dimension2d<u32>& texSize = Font->getSpriteBank()->getTexture(texno)->getOriginalSize();
dim[1] = core::reciprocal ( (f32) Font->getSpriteBank()->getTexture(texno)->getSize().Height ); dim[0] = core::reciprocal((f32)texSize.Width);
dim[1] = core::reciprocal((f32)texSize.Height);
const core::rect<s32>& s = sourceRects[rectno]; const core::rect<s32>& s = sourceRects[rectno];