here is a CAnimSprite class for the users who love to animat 2D sprites with Irrlicht.
it allows you play the 2D sprite animation forward or backward,controling the speed of
animation, draw specific frame ,automatically set the colorkey,automatically adjest the
size of it's 3D coordenats relative to Screen dimantion so it will appears as it's orignal
size,and more .......,check out the class.
CAnimSprite.cpp
============
Code: Select all
class TAnimSprite : public ISceneNode
{
private:
core::aabbox3d<f32> Box;
video::S3DVertex Vertices[4];
u16 Indices[12];
video::SMaterial Material;
video::ITexture* Texture;
f32 fWidth,fHeight;
s32 crntFrm,TotalFrm;
s32 stepww,stephh;
BOOL forward;
DWORD time;
DWORD oldtick;
public:
TAnimSprite(ISceneNode* parent, ISceneManager* mgr, s32 id): ISceneNode(parent, mgr, id)
{
Material.Wireframe = false;
Material.Lighting = false;
u16 ind[] = { 0,1,3, 3,1,2, 1,0,2, 2,0,3 };
memcpy(Indices,ind,sizeof(u16)*12);
}
virtual void Load(char* filename,s32 frmWidth,s32 frmHeight)
{
IVideoDriver* driver = SceneManager->getVideoDriver();
dimension2d<s32> Screensize = driver->getScreenSize();
float x = (float)frmWidth/(float)Screensize.Width;
float y = (float)frmHeight/(float)Screensize.Height;
Vertices[0] = S3DVertex(-x,-y,0, 0,0,0,SColor(255,255,255,255),0,1);
Vertices[1] = S3DVertex( x,-y,0, 0,0,0,SColor(255,255,255,255),1,1);
Vertices[2] = S3DVertex( x, y,0, 0,0,0,SColor(255,255,255,255),1,0);
Vertices[3] = S3DVertex(-x, y,0, 0,0,0,SColor(255,255,255,255),0,0);
Box.reset(Vertices[0].Pos);
for (s32 i=1; i<4; ++i) Box.addInternalPoint(Vertices[i].Pos);
Texture = driver->getTexture(filename);
driver->makeColorKeyTexture(Texture,position2d<s32>(0,0));
Material.Texture1 = Texture;
Material.MaterialType = EMT_TRANSPARENT_ALPHA_CHANNEL;
dimension2d<s32> size = Texture->getOriginalSize();
fWidth = (float)frmWidth/(float)size.Width;
fHeight = (float)frmHeight/(float)size.Height;
crntFrm = 0;
stepww = size.Width / frmWidth;
stephh = size.Height / frmHeight;
TotalFrm = stepww * stephh;
forward = TRUE;
Vertices[0].TCoords.X = 0;
Vertices[0].TCoords.Y = fHeight;
Vertices[1].TCoords.X = fWidth;
Vertices[1].TCoords.Y = fHeight;
Vertices[2].TCoords.X = fWidth;
Vertices[2].TCoords.Y = 0;
Vertices[3].TCoords.X = 0;
Vertices[3].TCoords.Y = 0;
}
virtual void PlayForward() {forward = TRUE;}
virtual void PlayBackward() {forward = FALSE;}
virtual void SetSpeed(int spd) {time = spd;}
virtual void OnPreRender()
{
if (IsVisible) SceneManager->registerNodeForRendering(this);
ISceneNode::OnPreRender();
}
virtual void setFrame(int n)
{
float x = (n % stepww)*fWidth;
float y = (n / stepww)*fHeight;
Vertices[0].TCoords.X = x;
Vertices[0].TCoords.Y = y+fHeight;
Vertices[1].TCoords.X = x+fWidth;
Vertices[1].TCoords.Y = y+fHeight;
Vertices[2].TCoords.X = x+fWidth;
Vertices[2].TCoords.Y = y;
Vertices[3].TCoords.X = x;
Vertices[3].TCoords.Y = y;
}
virtual void Update()
{
if(GetTickCount()-oldtick > time)
{
oldtick = GetTickCount();
if (forward)
{
crntFrm++;
if (crntFrm > TotalFrm-1)crntFrm = 0;
}
else
{
crntFrm--;
if (crntFrm < 0 )crntFrm = TotalFrm-1;
}
float x = (crntFrm % stepww)*fWidth;
float y = (crntFrm / stepww)*fHeight;
Vertices[0].TCoords.X = x;
Vertices[0].TCoords.Y = y+fHeight;
Vertices[1].TCoords.X = x+fWidth;
Vertices[1].TCoords.Y = y+fHeight;
Vertices[2].TCoords.X = x+fWidth;
Vertices[2].TCoords.Y = y;
Vertices[3].TCoords.X = x;
Vertices[3].TCoords.Y = y;
}
}
virtual s32 GetMaxFrames() { return TotalFrm; }
virtual void render()
{
IVideoDriver* driver = SceneManager->getVideoDriver();
driver->setMaterial(Material);
driver->setTransform(ETS_WORLD, AbsoluteTransformation);
driver->drawIndexedTriangleList(&Vertices[0], 4, &Indices[0], 4);
}
virtual const aabbox3d<f32>& getBoundingBox() const
{
return Box;
}
virtual s32 getMaterialCount()
{
return 1;
}
virtual SMaterial& getMaterial(s32 i)
{
return Material;
}
};
Code: Select all
#include "irrlicht.h"
// Irrlicht Namespaces
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#include "CAnimSprite.cpp"
void main()
{
IrrlichtDevice* irrDevice = createDevice(EDT_DIRECTX9,dimension2d<s32>(640,480),32,false,false,false,0);
IVideoDriver* irrVideo = irrDevice->getVideoDriver();
ISceneManager* irrSceneMgr = irrDevice->getSceneManager();
TAnimSprite* myNode = new TAnimSprite(irrSceneMgr->getRootSceneNode(), irrSceneMgr, 666);
myNode->Load("sonwalk.bmp",54,54);
myNode->SetSpeed(100);
//myNode->PlayBackward();
While (irrDevice->run())
{
irrVideo->beginScene(true, true, SColor(0,200,200,200));
myNode->Update();
//myNode->setFrame(5);
irrSceneMgr->drawAll();
irrVideo->endScene();
}
irrDevice->drop();
return 0;
}