(C++) Scale Animator Patch

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

(C++) Scale Animator Patch

Post by Halifax »

Well there was talk about it on the forums, and I recently figured that it would actually be kind of useful. (e.g. for making a kaboom animaton or something) Anyways, here is the patch, and the files for scale animator, and an example. (I'm sorry, but I don't know if patches can create files, if they can't, then I will create another without the animator files, so tell me.)

Well here is the patch:

Code: Select all

Index: include/ESceneNodeAnimatorTypes.h
===================================================================
--- include/ESceneNodeAnimatorTypes.h	(revision 1344)
+++ include/ESceneNodeAnimatorTypes.h	(working copy)
@@ -39,6 +39,9 @@
 
 		//! Maya camera animator
 		ESNAT_CAMERA_MAYA,
+		
+		//! Scale animator
+		ESNAT_SCALE,
 
 		//! Amount of built-in scene node animators
 		ESNAT_COUNT,
Index: include/ISceneManager.h
===================================================================
--- include/ISceneManager.h	(revision 1344)
+++ include/ISceneManager.h	(working copy)
@@ -994,6 +994,14 @@
 		 See IReferenceCounted::drop() for more information. */
 		virtual ISceneNodeAnimator* createRotationAnimator(const core::vector3df& rotationPerSecond) = 0;
 
+		//! Creates a rotation animator, which rotates the attached scene node around itself.
+		/** \param rotationPerSecond: Specifies the speed of the animation
+		 \return Returns the animator. Attach it to a scene node with ISceneNode::addAnimator()
+		 and the animator will animate it.
+		 If you no longer need the animator, you should call ISceneNodeAnimator::drop().
+		 See IReferenceCounted::drop() for more information. */
+		virtual ISceneNodeAnimator* createScaleAnimator(const core::vector3df& scalePerSecond) = 0;
+
 		//! Creates a fly circle animator, which lets the attached scene node fly  around a center.
 		/** \param center: Center of the circle.
 		 \param radius: Radius of the circle.
Index: source/Irrlicht/CDefaultSceneNodeAnimatorFactory.cpp
===================================================================
--- source/Irrlicht/CDefaultSceneNodeAnimatorFactory.cpp	(revision 1344)
+++ source/Irrlicht/CDefaultSceneNodeAnimatorFactory.cpp	(working copy)
@@ -26,6 +26,7 @@
 	"collisionResponse",
 	"cameraFPS",
 	"cameraMaya",
+	"scale",
 	0
 };
 
@@ -90,6 +91,9 @@
 	case ESNAT_CAMERA_MAYA:
 		anim = new CSceneNodeAnimatorCameraMaya(CursorControl);
 		break;
+	case ESNAT_SCALE:
+		anim = Manager->createScaleAnimator(core::vector3df(2.0f, 2.0f, 2.0f));
+		break;
 	default:
 		break;
 	}
Index: source/Irrlicht/CSceneManager.cpp
===================================================================
--- source/Irrlicht/CSceneManager.cpp	(revision 1344)
+++ source/Irrlicht/CSceneManager.cpp	(working copy)
@@ -139,6 +139,7 @@
 #include "CSceneNodeAnimatorFollowSpline.h"
 #include "CSceneNodeAnimatorCameraFPS.h"
 #include "CSceneNodeAnimatorCameraMaya.h"
+#include "CSceneNodeAnimatorScale.h"
 #include "CDefaultSceneNodeAnimatorFactory.h"
 
 #include "CQuake3ShaderSceneNode.h"
@@ -1379,6 +1380,14 @@
 	return anim;
 }
 
+//! creates a scale animator, which scales the attach scene node.
+ISceneNodeAnimator* CSceneManager::createScaleAnimator(const core::vector3df& scalePerSecond)
+{
+	ISceneNodeAnimator* anim = new CSceneNodeAnimatorScale(os::Timer::getTime(),
+		scalePerSecond);
+	
+	return anim;
+}
 
 
 //! creates a fly circle animator, which lets the attached scene node fly around a center.
Index: source/Irrlicht/CSceneManager.h
===================================================================
--- source/Irrlicht/CSceneManager.h	(revision 1344)
+++ source/Irrlicht/CSceneManager.h	(working copy)
@@ -312,6 +312,12 @@
 		virtual ISceneNodeAnimator* createFollowSplineAnimator(s32 startTime,
 			const core::array< core::vector3df >& points,
 			f32 speed = 1.0f, f32 tightness = 0.5f);
+			
+		//! Creates a scale animator, which scales the attached scene node.
+		//! \param scalePerSecond: Specifies the speed of the animation
+		//! \return Returns the animator. Attach it to a scene node with ISceneNode::addAnimator()
+		//! and the animator will animate it.
+		virtual ISceneNodeAnimator* createScaleAnimator(const core::vector3df& scalePerSecond);
 
 
 		//! Creates a simple ITriangleSelector, based on a mesh.
Index: source/Irrlicht/CSceneNodeAnimatorScale.cpp
===================================================================
--- source/Irrlicht/CSceneNodeAnimatorScale.cpp	(revision 0)
+++ source/Irrlicht/CSceneNodeAnimatorScale.cpp	(revision 0)
@@ -0,0 +1,56 @@
+// Copyright (C) 2002-2007 Nikolaus Gebhardt
+// This file is part of the "Irrlicht Engine".
+// For conditions of distribution and use, see copyright notice in irrlicht.h
+
+#include "CSceneNodeAnimatorScale.h"
+
+namespace irr
+{
+namespace scene
+{
+
+//! constructor
+CSceneNodeAnimatorScale::CSceneNodeAnimatorScale(u32 time, const core::vector3df& scale)
+: Scale(scale), StartTime(time)
+{
+	#ifdef _DEBUG
+		setDebugName("CSceneNodeAnimatorScale");
+	#endif
+}
+
+//! destructor
+CSceneNodeAnimatorScale::~CSceneNodeAnimatorScale()
+{
+}
+
+//! animates a scene node
+void CSceneNodeAnimatorScale::animateNode(ISceneNode* node, u32 timeMs)
+{
+	if (node)
+	{
+		u32 diffTime = timeMs - StartTime;
+		
+		if (diffTime != 0)
+		{
+			core::vector3df NewScale = node->getScale();
+			NewScale += Scale * ((diffTime)/10.0f);
+			node->setScale(NewScale);
+			StartTime = timeMs;
+		}
+	}
+}
+
+//! Writes attributes of the scene node animator.
+void CSceneNodeAnimatorScale::serializeAttributes(io::IAttributes *out, io::SAttributeReadWriteOptions *options) const
+{
+	out->addVector3d("Scale", Scale);
+}
+
+//! Reads attributes of the scene node animator.
+void CSceneNodeAnimatorScale::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
+{
+	Scale = in->getAttributeAsVector3d("Scale");
+}
+
+} // end namespace scene
+} // end namespace irr
\ No newline at end of file
Index: source/Irrlicht/CSceneNodeAnimatorScale.h
===================================================================
--- source/Irrlicht/CSceneNodeAnimatorScale.h	(revision 0)
+++ source/Irrlicht/CSceneNodeAnimatorScale.h	(revision 0)
@@ -0,0 +1,45 @@
+// Copyright (C) 2002-2007 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_SCALE_H_INCLUDED__
+#define __C_SCENE_NODE_ANIMATOR_SCALE_H_INCLUDED__
+
+#include "ISceneNode.h"
+
+namespace irr
+{
+namespace scene
+{
+	class CSceneNodeAnimatorScale : public ISceneNodeAnimator
+	{
+	public:
+	
+		//! constructor
+		CSceneNodeAnimatorScale(u32 time, const core::vector3df& scale);
+		
+		//! destructor
+		virtual ~CSceneNodeAnimatorScale();
+		
+		//! 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* out, io::SAttributeReadWriteOptions* options = 0);
+		
+		//! Returns the type of the scene node animator.
+		virtual ESCENE_NODE_ANIMATOR_TYPE getType() const { return ESNAT_SCALE; }
+	
+	private:
+		
+		core::vector3df Scale;
+		u32 StartTime;
+	};
+
+} // end namespace scene
+} // end namespace irr
+
+#endif
\ No newline at end of file
CSceneNodeAnimatorScale.h

Code: Select all

// Copyright (C) 2002-2007 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_SCALE_H_INCLUDED__
#define __C_SCENE_NODE_ANIMATOR_SCALE_H_INCLUDED__

#include "ISceneNode.h"

namespace irr
{
namespace scene
{
	class CSceneNodeAnimatorScale : public ISceneNodeAnimator
	{
	public:
	
		//! constructor
		CSceneNodeAnimatorScale(u32 time, const core::vector3df& scale);
		
		//! destructor
		virtual ~CSceneNodeAnimatorScale();
		
		//! 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* out, io::SAttributeReadWriteOptions* options = 0);
		
		//! Returns the type of the scene node animator.
		virtual ESCENE_NODE_ANIMATOR_TYPE getType() const { return ESNAT_SCALE; }
	
	private:
		
		core::vector3df Scale;
		u32 StartTime;
	};

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

#endif
CSceneNodeAnimatorScale.cpp

Code: Select all

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

#include "CSceneNodeAnimatorScale.h"

namespace irr
{
namespace scene
{

//! constructor
CSceneNodeAnimatorScale::CSceneNodeAnimatorScale(u32 time, const core::vector3df& scale)
: Scale(scale), StartTime(time)
{
	#ifdef _DEBUG
		setDebugName("CSceneNodeAnimatorScale");
	#endif
}

//! destructor
CSceneNodeAnimatorScale::~CSceneNodeAnimatorScale()
{
}

//! animates a scene node
void CSceneNodeAnimatorScale::animateNode(ISceneNode* node, u32 timeMs)
{
	if (node)
	{
		u32 diffTime = timeMs - StartTime;
		
		if (diffTime != 0)
		{
			core::vector3df NewScale = node->getScale();
			NewScale += Scale * ((diffTime)/10.0f);
			node->setScale(NewScale);
			StartTime = timeMs;
		}
	}
}

//! Writes attributes of the scene node animator.
void CSceneNodeAnimatorScale::serializeAttributes(io::IAttributes *out, io::SAttributeReadWriteOptions *options) const
{
	out->addVector3d("Scale", Scale);
}

//! Reads attributes of the scene node animator.
void CSceneNodeAnimatorScale::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{
	Scale = in->getAttributeAsVector3d("Scale");
}

} // end namespace scene
} // end namespace irr
Here is the scale animator example. Basically it just creates a simple sphere, then adds a scale animator, and 5 second deletion animator. It would be cool if someone could do a better example, but whatever it works:

Code: Select all

#include <Irrlicht.h>
#include <iostream>

using namespace irr;

#pragma comment(lib, "Irrlicht.lib")

int main()
{
	video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
	
	printf("Please select the driver you want for this example:\n"\
		" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\
		" (d) Software Renderer\n (e) Burning's Software Renderer\n"\
		" (f) NullDevice\n (otherKey) exit\n\n");
		
	char i;
	std::cin >> i;

	switch(i)
	{
		case 'a': driverType = video::EDT_DIRECT3D9;break;
		case 'b': driverType = video::EDT_DIRECT3D8;break;
		case 'c': driverType = video::EDT_OPENGL;   break;
		case 'd': driverType = video::EDT_SOFTWARE; break;
		case 'e': driverType = video::EDT_BURNINGSVIDEO;break;
		case 'f': driverType = video::EDT_NULL;     break;
		default: return 1;
	}
	
	IrrlichtDevice* device = createDevice(driverType, core::dimension2d<s32>(640, 480));
	
	if (!device)
		return 1;
		
	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	
	scene::ISceneNode* node = smgr->addSphereSceneNode(10.0f, 32);
	node->setPosition(core::vector3df(0,0,0));
	node->setMaterialFlag(video::EMF_LIGHTING, false);
	
	scene::ISceneNodeAnimator* anim = smgr->createScaleAnimator(
		core::vector3df(0.01f, 0.01f, 0.01f));
	if (anim)
	{
		node->addAnimator(anim);
		anim->drop();
	}
	
	anim = smgr->createDeleteAnimator(5000);
	if (anim)
	{
		node->addAnimator(anim);
		anim->drop();
	}
	
	scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
	camera->setPosition(core::vector3df(0, 0, -100));
	device->getCursorControl()->setVisible(false);
	
	int lastFPS = -1;
	
	while (device->run())
	{
		if (device->isWindowActive())
		{
			driver->beginScene(true, true, video::SColor(255, 128, 128, 128));
				smgr->drawAll();
			driver->endScene();
			
			int fps = driver->getFPS();
			
			if (lastFPS != fps)
			{
				core::stringw str = L"Irrlicht Engine - Scale shader example [";
				str += driver->getName();
				str += "] FPS:";
				str += fps;
				
				device->setWindowCaption(str.c_str());
				lastFPS = fps;
			}
		}
	}
	
	device->drop();
	return 0;
}
There you go, I hope it's useful.
TheQuestion = 2B || !2B
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Nice work. One comment:
Copyright (C) 2002-2007 Nikolaus Gebhardt
Should be changed to
Copyright (C) 2002-2008 Nikolaus Gebhardt
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Virion
Competition winner
Posts: 2149
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

Nice observation :D
My company: https://kloena.com
My profile: https://zhieng.com
My co-working space: https://deskspace.info
My game engine: https://kemena3d.com
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Haha, true, that needs to be changed for all the files in the engine then, right? The only reason I did that is because I copied it out of the camera animators, which I thought were more recent than 2007. But eh, thanks, haha.
TheQuestion = 2B || !2B
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Check the logs, they already did it. :wink:
branches/ 1345 12 days hybrid Changed all copyrights to 2008. Updated comments, fixed indentations. Huge chang...
Edit:
Oh and they already merged it into trunk by the way..
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Ah, okay, I guess I should have updated then, haha. I'm working with revision 1344.
TheQuestion = 2B || !2B
Post Reply