Try this... I had written it for my 2d game.
But it is currently abandoned now
CSprite.h
Code: Select all
#ifndef __C_SPRITE_H_INCLUDED__
#define __C_SPRITE_H_INCLUDED__
#include "CStuff.h"
using namespace irr;
class CSprite : public CStuff
{
public:
//! Constructor.
CSprite(IrrlichtDevice *device);
//! Destructor.
~CSprite();
//! Create sprite from filename which automatically calculate size.
virtual void create(const c8* filename, s32 divX = 1, s32 dixY = 1);
//! Create sprite from ITexture which automatically calculate size.
virtual void create(video::ITexture *texture, s32 divX = 1, s32 divY = 1);
//! Make color key from upper left corner sprite.
virtual void makeColorKey();
//! Set positon X axis on screen.
virtual void setPosX(const s32 &posX);
//! Set position Y axis on screen.
virtual void setPosY(const s32 &posY);
//! Set position X,Y axis on screen.
virtual void setPos(const s32 &posX, const s32 &posY);
//! Set position X,Y axis on screen.
virtual void setPos(const core::position2di &pos);
//! Set current frame.
virtual void setFrame(const s32 &frame);
//! Get height of sprite.
virtual s32 getHeight();
//! Get width of sprite.
virtual s32 getWidth();
//! Get position X axis on screen.
virtual s32 getPosX();
//! Get position Y axis on screen.
virtual s32 getPosY();
//! Get position X,Y axis on screen.
virtual void getPos(s32 &outPosX, s32 &outPosY);
//! Get postion X,Y axis on screen.
virtual core::position2di getPos();
//! Get current frame.
virtual s32 getFrame();
//! return true if the sprite collides with another sprite.
virtual bool isCollided(CSprite *other);
//! Draw prite.
virtual void draw();
//! Draw prite on coordinate of the map.
virtual void draw(core::position2di &posMap);
//! Draw sprite with alpha channel.
virtual void drawAlpha();
//! Draw sprite with alpha channel on coordinate of the map.
virtual void drawAlpha(core::position2di &posMap);
protected:
video::ITexture *Texture;
core::array<core::rect<s32>> Frame;
core::position2di Position;
s32 CurrentFrame;
s32 Height;
s32 Width;
};
#endif //__C_SPRITE_H_INCLUDED__
CSprite.cpp
Code: Select all
#include "CSprite.h"
CSprite::CSprite(IrrlichtDevice *device) : CStuff(device)
{
Texture = NULL;
Position = core::position2di(0,0);
CurrentFrame = 0;
Height = 0;
Width = 0;
}
CSprite::~CSprite()
{
}
void CSprite::create(const c8 *filename, s32 divX, s32 divY)
{
//! \param filename get a filename of srpite.
//! \param divX number of sprites along colum(X).
//! \param divY number of sprites along row(Y).
Texture = Driver->getTexture(filename);
core::dimension2di spriteSize = Texture->getSize();
Width = spriteSize.Width / divX;
Height = spriteSize.Height / divY;
Frame.set_used(divX * divY);
s32 n = 0;
for( s32 j = 0; j < divY; ++j )
{
for( s32 i = 0; i < divX; ++i)
{
Frame[n] = core::rect<s32>(Width * i, Height * j,//Upper Left Corner
Width * (i+1),Height * (j+1));//Lower Right Corner
++n;
}
}
CurrentFrame = 0;
}
void CSprite::create(video::ITexture *texture, s32 divX, s32 divY)
{
//! \param texture get a texture(Sprite) which contrains in video card
//! \param divX number of sprites along colum(X).
//! \param divY number of sprites along row(Y).
Texture = texture;
core::dimension2di spriteSize = Texture->getSize();
Width = spriteSize.Width / divX;
Height = spriteSize.Height / divY;
Frame.set_used(divX * divY);
s32 n = 0;
for( s32 j = 0; j < divY; ++j )
{
for( s32 i = 0; i < divX; ++i)
{
Frame[n] = core::rect<s32>(Width * i,Height * j,//Upper Left Corner
Width * (i+1),Height * (j+1));//Lower Right Corner
++n;
}
}
CurrentFrame = 0;
}
void CSprite::makeColorKey()
{
Driver->makeColorKeyTexture(Texture,core::position2di(0,0));
}
void CSprite::setPosX(const s32 &posX)
{
Position.X = posX;
}
void CSprite::setPosY(const s32 &posY)
{
Position.Y = posY;
}
void CSprite::setPos(const s32 &posX, const s32 &posY)
{
Position.X = posX;
Position.Y = posY;
}
void CSprite::setPos(const core::position2di &pos)
{
Position = pos;
}
void CSprite::setFrame(const s32 &frame)
{
CurrentFrame = frame;
if( CurrentFrame > (s32)Frame.size() - 1 )
CurrentFrame = Frame.size() - 1;
else if( CurrentFrame < 0 )
CurrentFrame = 0;
}
s32 CSprite::getHeight()
{
return Height;
}
s32 CSprite::getWidth()
{
return Width;
}
s32 CSprite::getPosX()
{
return Position.X;
}
s32 CSprite::getPosY()
{
return Position.Y;
}
void CSprite::getPos(s32 &outPosX, s32 &outPosY)
{
outPosX = Position.X;
outPosY = Position.Y;
}
core::position2di CSprite::getPos()
{
return Position;
}
s32 CSprite::getFrame()
{
return CurrentFrame;
}
bool CSprite::isCollided(CSprite *other)
{
// \param other sprite which you want to check collision.
core::rect<s32> rect1(Position,//Upper Left
core::position2di(Position.X + Width, Position.Y + Height));//Lower Right
core::rect<s32> rect2(other->getPos(),//UL
core::position2di(other->getPos().X + other->getWidth(), other->getPos().Y + other->getHeight()));//LR
return rect1.isRectCollided(rect2);
}
void CSprite::draw()
{
Driver->draw2DImage(Texture,Position,Frame[CurrentFrame],0,video::SColor(255,255,255,255),false);
}
void CSprite::draw(core::position2di &posMap)
{
// \param posMap position of the map.
Driver->draw2DImage(Texture,Position,Frame[CurrentFrame],0,video::SColor(255,255,255,255),false);
}
void CSprite::drawAlpha()
{
Driver->draw2DImage(Texture,Position,Frame[CurrentFrame],0,video::SColor(255,255,255,255),true);
}
void CSprite::drawAlpha(core::position2di &posMap)
{
// \param posMap position of the map.
Driver->draw2DImage(Texture,Position - posMap,Frame[CurrentFrame],0,video::SColor(255,255,255,255),true);
}
You can add some methods which lack from the class.
I feel shy to share my noob class
because I'm the beginner,too
Good Luck!