Code: Select all
#include <irrlicht.h>
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#pragma comment(lib, "Irrlicht.lib")
using namespace std;
using namespace irr;
using namespace video;
class Sprite2D
{
protected:
int m_iStartX, m_iStartY;
int m_iHeight;
int m_iWidth;
int m_iXPos;
int m_iYPos;
int m_iFrames;
int m_iFrame;
RECT m_rcBounds;
ITexture* m_tImage;
int m_iFrameDelay;
int m_iTimer;
int m_iLowFrame;
int m_iMaxFrame;
int m_iTransparency;
public:
Sprite2D(string szFilename, IVideoDriver *vDriver, int iStartX, int iStartY, int iWidth, int iHeight, bool bTransparent, COLORREF crTrans = RGB(0,0,0), int iTransparency = 255) {
m_tImage = vDriver->getTexture(szFilename.c_str());
if (bTransparent)
vDriver->makeColorKeyTexture(m_tImage, SColor(0,GetRValue(crTrans),GetGValue(crTrans),GetBValue(crTrans)));
m_iFrameDelay = 1000;
m_iFrames = 1;
m_iFrame = 0;
m_iStartX = iStartX;
m_iStartY = iStartY;
m_iWidth = iWidth;
m_iHeight = iHeight;
m_rcBounds.left = m_iXPos;
m_rcBounds.top = m_iYPos;
m_rcBounds.right = m_iXPos + m_iWidth;
m_rcBounds.bottom = m_iYPos + m_iHeight;
m_iLowFrame = 0;
m_iMaxFrame = 0;
m_iTimer = m_iFrameDelay;
m_iTransparency = iTransparency;
}
void SetNumFrames(int iFrames) {
m_iFrames = iFrames;
m_iMaxFrame = iFrames-1;
m_rcBounds.right = m_iXPos + (m_iWidth/iFrames);
}
void SetFrameDelay(int iFrameDelay){
m_iFrameDelay = iFrameDelay;
}
void Draw(IVideoDriver *vDriver) {
vDriver->draw2DImage(m_tImage, core::position2d<s32>(m_iXPos,m_iYPos), core::rect<s32>(m_iStartX+(m_iFrame*(m_iWidth/m_iFrames)),m_iStartY,m_iStartX+((m_iWidth/m_iFrames)*(m_iFrame+1)),m_iStartY+m_iHeight), 0, video::SColor(m_iTransparency,255,255,255), true);
}
void SetFrame(int iFrame) {
if (iFrame <= m_iFrames-1)
m_iFrame = iFrame;
}
int GetFrame() { return m_iFrame; };
void SetLowFrame(int iLowFrame) { m_iLowFrame = iLowFrame; };
void SetMaxFrame(int iMaxFrame) { m_iMaxFrame = iMaxFrame; };
void Update() {
m_rcBounds.left = m_iXPos;
m_rcBounds.top = m_iYPos;
m_rcBounds.right = m_iXPos + (m_iWidth/m_iFrames);
m_rcBounds.bottom = m_iYPos + m_iHeight;
if (GetTickCount() >= m_iTimer) {
m_iFrame++;
if (m_iFrame > m_iMaxFrame)
m_iFrame = m_iLowFrame;
m_iTimer = GetTickCount()+m_iFrameDelay;
}
}
void Move(int iXDir, int iYDir) {
m_iXPos += iXDir;
m_iYPos += iYDir;
}
bool IsPointInside(int iX, int iY) {
POINT ptPoint;
ptPoint.x = iX;
ptPoint.y = iY;
return PtInRect(&m_rcBounds, ptPoint);
}
int GetXPos() { return m_iXPos; };
int GetYPos() { return m_iYPos; };
int GetWidth() { return m_iWidth/m_iFrames; };
int GetHeight() { return m_iHeight; };
void SetPosition(int iX, int iY) { m_iXPos = iX; m_iYPos = iY;};
};
and here is an example, using the 2DGraphics project:
Code: Select all
#include <irrlicht.h>
#include <iostream>
#include <string>
#include <conio.h>
#include <stdio.h>
#include <irrKlang.h>
#include "Sprite2D.h"
#include <math.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#pragma comment(lib, "irrKlang.lib")
#pragma comment(lib, "Irrlicht.lib")
using namespace irr;
Sprite2D *Test;
int g_iFrameTimer = 1000/50;
int g_iFrameDelay = 1000/50;
int main()
{
video::E_DRIVER_TYPE driverType;
driverType = video::EDT_DIRECT3D8;
// create device
IrrlichtDevice *device = createDevice(driverType,
core::dimension2d<s32>(800, 600), 24);
if (device == 0)
return 1; // could not create selected driver.
device->setWindowCaption(L"");
video::IVideoDriver* driver = device->getVideoDriver();
Test = new Sprite2D("2ddemo.bmp",driver,349, 15, 74, 63, true,RGB(255,0,255));
Test->SetFrame(0);
Test->SetNumFrames(2);
Test->SetFrameDelay(1000);
while(device->run() && driver)
{
if (device->isWindowActive())
{
if (GetTickCount() > g_iFrameDelay) {
g_iFrameDelay = GetTickCount() + g_iFrameTimer;
u32 time = device->getTimer()->getTime();
driver->beginScene(true, true, video::SColor(0,0,0,0));
Test->Update();
Test->Draw(driver);
// draw transparent rect under cursor
core::position2d<s32> m = device->getCursorControl()->getPosition();
driver->draw2DRectangle(video::SColor(100,255,255,255),
core::rect<s32>(m.X-20, m.Y-20, m.X+20, m.Y+20));
driver->endScene();
}
}
}
device->drop();
return 0;
}