CSceneNodeAnimatorTexture and other Animators> more contr

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
random
Posts: 158
Joined: Wed Aug 11, 2010 6:01 am

CSceneNodeAnimatorTexture and other Animators> more contr

Post by random »

the CSceneNodeAnimatorTexture is a fine Node Animator Sample but i am missing a functionality

it can wether play in loop or once only also if you do removeAnimator() and lateron addAnimator it starts wether playing in loop (mostly it starts right in the middle of an animation whats not realy cool) or it even does not animated if loop is set to falso because it is not possible to reset the Starttime...

it is predefined to play synchronous animations of the same type with the same texture what gives the programmes an odd feeling of not controllable interaction. a simply funtionality added to it like:

Code: Select all

void CSceneNodeAnimatorTexture::resetAnimation()
{
StartTime = os::Timer::getTime();
EndTime = StartTime + (TimePerFrame * Textures.size());
}
would be realy fine and would give the programmer of games a more controllable code for interactions like axplosions and similar effects on attack mode of the player class.

i will create my own class for this functionality now because changing the code internal to the irrlicht code would cause in two things...

1) it would be needed to add an extra function to ISceneNodeAnimator what causes more changes to depending nodes

2) second option would be creating the texture animator by

Code: Select all

	ISceneNodeAnimator* anim = new CSceneNodeAnimatorTexture(textures,
		timePerFrame, loop, os::Timer::getTime());
instead for the option to use the resetAnimation() function, what if you have a look at it is allready the way you need to do when using some particleAffectors and related functions that can not be called from the IParticleAffector


is there anything bad that i have not seen yet regarding the resetAnimation() function?
random
Posts: 158
Joined: Wed Aug 11, 2010 6:01 am

Post by random »

well i tried to write my own class and it looks like this:

call from inside a custom class:

Code: Select all

glow = new CSceneNodeAnimatorTextureII(textures,60, false, timer->getTime());
(glow is predfined in the header file of the custom class):

Code: Select all

#include "../CSceneNodeAnimatorTextureII.h"
//---some code---
scene::ISceneNodeAnimator* glow;
CSceneNodeAnimatorTextureII.h :

Code: Select all

// Copyright (C) 2002-2005 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#ifndef __C_SCENE_NODE_ANIMATOR_TEXTUREII_H_INCLUDED__
#define __C_SCENE_NODE_ANIMATOR_TEXTUREII_H_INCLUDED__


#include <irrlicht.h>
#include "irrArray.h"
#include "ISceneNode.h"
#include "ISceneNodeAnimatorFinishingII.h"

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

    class CSceneNodeAnimatorTextureII : public ISceneNodeAnimatorFinishingII
    {
    public:

        //! constructor
        CSceneNodeAnimatorTextureII(const core::array<video::ITexture*>& textures,
            s32 timePerFrame, bool loop, u32 now);

        //! destructor
        virtual ~CSceneNodeAnimatorTextureII();

        //! animates a scene node
        virtual void animateNode(ISceneNode* node, u32 timeMs);

        //! Writes attributes of the scene node animator.
        virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const;

        //! Reads attributes of the scene node animator.
        virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0);

        //! Returns type of the scene node animator
        virtual ESCENE_NODE_ANIMATOR_TYPE getType() const { return ESNAT_TEXTURE; }

        //! Creates a clone of this animator.
        /** Please note that you will have to drop
        (IReferenceCounted::drop()) the returned pointer after calling
        this. */
        virtual ISceneNodeAnimator* createClone(ISceneNode* node, ISceneManager* newManager=0);

        virtual void resetAnimation();

    private:

        void clearTextures();

        core::array<video::ITexture*> Textures;
        u32 TimePerFrame;
        u32 StartTime;
        bool Loop;
    };


#endif


ISceneNodeAnimatorFinishingII.h:

Code: Select all

// Copyright (C) 2002-2008 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#ifndef __I_SCENE_NODE_ANIMATOR_FINISHING_H_INCLUDED__
#define __I_SCENE_NODE_ANIMATOR_FINISHING_H_INCLUDED__

#include "ISceneNode.h"

namespace irr
{
namespace scene
{
      //! This is an abstract base class for animators that have a discrete end time.
    class ISceneNodeAnimatorFinishingII : public ISceneNodeAnimator
      {
      public:

            //! constructor
    ISceneNodeAnimatorFinishingII(u32 finishTime) : FinishTime(finishTime), HasFinished(false) { }

    virtual bool hasFinished(void) const { return HasFinished; }

      protected:

            u32 FinishTime;
            bool HasFinished;
      };


} // end namespace scene
} // end namespace irr

#endif



CSceneNodeAnimatorTextureII.cpp :

Code: Select all

// Copyright (C) 2002-2005 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#include "CSceneNodeAnimatorTextureII.h"
#include "ITexture.h"


using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

//! constructor
CSceneNodeAnimatorTextureII::CSceneNodeAnimatorTextureII(const core::array<video::ITexture*>& textures,
					 s32 timePerFrame, bool loop, u32 now) : ISceneNodeAnimatorFinishingII(0),
	TimePerFrame(timePerFrame), StartTime(now), Loop(loop)
{
	#ifdef _DEBUG
	setDebugName("CSceneNodeAnimatorTextureII");
	#endif

	for (u32 i=0; i<textures.size(); ++i)
	{
		if (textures[i])
			textures[i]->grab();

		Textures.push_back(textures[i]);
	}

	FinishTime = now + (timePerFrame * Textures.size());
}

//! destructor
CSceneNodeAnimatorTextureII::~CSceneNodeAnimatorTextureII()
{
	clearTextures();
}


void CSceneNodeAnimatorTextureII::clearTextures()
{
	for (u32 i=0; i<Textures.size(); ++i)
		if (Textures[i])
			Textures[i]->drop();
}


//! animates a scene node
void CSceneNodeAnimatorTextureII::animateNode(ISceneNode* node, u32 timeMs)
{
	if(!node)
		return;

	if (Textures.size())
	{
		const u32 t = (timeMs-StartTime);

		u32 idx = 0;
		if (!Loop && timeMs >= FinishTime)
		{
			idx = Textures.size() - 1;
			HasFinished = true;
		}
		else
		{
			idx = (t/TimePerFrame) % Textures.size();
		}

		if (idx < Textures.size())
			node->setMaterialTexture(0, Textures[idx]);
	}
}


//! Writes attributes of the scene node animator.
void CSceneNodeAnimatorTextureII::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
{
	out->addInt("TimePerFrame", TimePerFrame);
	out->addBool("Loop", Loop);

	// add one texture in addition when serializing for editors
	// to make it easier to add textures quickly

	u32 count = Textures.size();
	if ( options && (options->Flags & io::EARWF_FOR_EDITOR))
		count += 1;

	for (u32 i=0; i<count; ++i)
	{
		core::stringc tname = "Texture";
		tname += (int)(i+1);

		out->addTexture(tname.c_str(), i<Textures.size() ? Textures[i] : 0);
	}
}


//! Reads attributes of the scene node animator.
void CSceneNodeAnimatorTextureII::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{
	TimePerFrame = in->getAttributeAsInt("TimePerFrame");
	Loop = in->getAttributeAsBool("Loop");

	clearTextures();

	for(u32 i=1; true; ++i)
	{
		core::stringc tname = "Texture";
		tname += (int)i;

		if (in->existsAttribute(tname.c_str()))
		{
			video::ITexture* tex = in->getAttributeAsTexture(tname.c_str());
			if (tex)
			{
				tex->grab();
				Textures.push_back(tex);
			}
		}
		else
			break;
	}
}


ISceneNodeAnimator* CSceneNodeAnimatorTextureII::createClone(ISceneNode* node, ISceneManager* newManager)
{
	CSceneNodeAnimatorTextureII * newAnimator =
		new CSceneNodeAnimatorTextureII(Textures, TimePerFrame, Loop, StartTime);

	return newAnimator;
}

void CSceneNodeAnimatorTextureII::resetAnimation()
{
irr::ITimer* timer;
StartTime = timer->getTime();
FinishTime = StartTime + (TimePerFrame * Textures.size());
}
as expected i receive an segmentation fault here when try to debug :)

do i have any other option to start playing an TexturAnimator(explosion) from the first image when a weapon node(laserBeam) hits a target?

because the TextureAnimator plays when:
loop == false:
starts once and finishes when all images in texture array have played once, starttime and depending on it finishing time is calculated once, also the serialze Attributes function does not fit any option to change this, even if it would IAtributes.h has no addU32 only an addInt
loop == true : also StartTime is calculated once No option to reset it at... it just plays in loop.

so what if i have syme player types just different weapons one wich is a bit faster, i could make the timePerFrame dynamic, would result in an explosion playing faster and look not realistic.

hmm can someone give me an option please or help me on the SIGSEGV (segmentation fault)?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: CSceneNodeAnimatorTexture and other Animators> more cont

Post by CuteAlien »

OK, digging up a very old post here *sorry*, but just found a link to this thread while going over my todo's. And so just for info in case anyone ever runs into this thread as well - Irrlicht 1.9 (aka svn trunk right now) has ISceneNodeAnimator::setStartTime and ISceneNodeAnimator::setEnabled by now. Which allow to pause and reset all (or most) animators.

Now let this thread rest again ;-)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply