(C) Keigen Shu 2011-2012. Released under the zlib License.
SpriteEngine(SE) is an Irrlicht wrapper I made to manage the process of drawing a very large number of 2D images, each with different transformations on the screen.I wrote it because I find Irrlicht's 2D drawing interface to be lacking some features up-front. This is also my first API-like code. The source code is on the next post because it's really long.
Features:
- Image transformations like colourMask, rotating and scaling.
- A very simple programmable particle object API.
- Documented code.
- Float-based image positioning.
- Image movement with rotation angle.
- Animated sprites.
- Image drawing filters.
Compiles on MSVC 2010 & GCC (try C++11 mode if it fails).
How to use
First, we need to setup the wrapper. Include "_SEngine.hpp" and then call SpriteEngine::setup(IrrlichtDevice*) right after you create your Irrlicht Device.
Code: Select all
#include "_SEngine.hxx"
IrrlichtDevice* pIrrlicht = createDevice(... blah ...);
if (pIrrlicht == 0) return 1;
SpriteEngine* pSpriteEngine = new SpriteEngine;
if (!(pSpriteEngine->setup(pIrrlicht)))
return 1;
Code: Select all
// To load ITextures; SpriteEngine will delete them when the engine shuts down
ITexture* JunkTest = pSpriteEngine->loadTextureFromFile ("./Theme/image/JunkTest.png");
// Constructing an Image using an entire ITexture.
Image* imgParticle = pSpriteEngine->loadImageFromFile("./Theme/image/Particle.png");
// Constructing an Image using only a part of an ITexture
Image* imgBG = pSpriteEngine->loadImageFromFile("./Theme/image/BigBGImage.png", recti(0,100,800,700));
// Constructing an Image using an existing ITexture.
Image* imgJunkTest = new Image (tJunk, recti(10,20,30,40));
// To delete images, just do:
// Note that the ITexture is still not unloaded by the manager.
// Read the documentation on _SEngine.hpp on how to do that.
delete imgJunkTest;
// Nyan Nyan Nyan (our first sprite)
Sprite* sprNyanCat = new Sprite (
imgNyanCat , // source image
position2df (80.0f,460.0f), // position (yes it's in float)
120.0f , // rotation in degrees
vector2df (1.5f) , // make it extra large
SColor(255,255,255,255) , // colour mask
true // tells the engine that the position value is based on the center of the image
);
// Let's put it in a list
irr::core::list<Sprite*>* sprList = new irr::core::list<Sprite*>;
sprList->push_back(sprNyanCat);
// Irrlicht's beginScene
pSpriteEngine->getVideoEngine()->beginScene(true, true, 0);
pSpriteEngine->getVideoEngine()->enableMaterial2D(true);
// Before doing SpriteEngine specific drawings, you need make this call.
// It was made for performance reasons.
pSpriteEngine->saveMatrix();
// Draw the list
pSpriteEngine->drawSpriteList(spriteList);
// Nyan-cat flies by 4.8px, rotating by 2 degrees.
sprNyanCat->move(4.8f, 2.0f);
pSpriteEngine->drawSprite(sprNyanCat);
// After doing SpriteEngine specific drawings, you need make this call.
// It was made for performance reasons.
pSpriteEngine->loadMatrix();
// end scene
pSpriteEngine->getVideoEngine()->endScene();
// to drop SpriteEngine...
pSpriteEngine->drop();
Particles is basically programmable Sprites. The reason I named it as Particle because it was originally made with the ability to draw 2D particles in mind and it was based on Touhou's scipt-based bullet system (that Japanese Bullet Curtain game). A particle contains a list of PObjects, each containing a pointer to a Sprite and an integer counter to keep track of time/state/whatever variables. Like Sprite, a Particle is put into an irr::core::list<Particle*> and then sent to the engine to draw.
To use this, you just make a new class derived from Particle(as the parent class). SpriteEngine will call the update() function in your particle class everytime it wants to draw. You can use your own functions, whatever; it's your own code.
Basically there are three steps:
1. Initialization - make sprites
2. Draw and Update - move/transform sprites around
3. Death - You have complete control of this, but generally you'll want to use the Age counter and Max limit variables in the PObjects.
If you want to know more, just look at the SimpParticle class example in _SParticles.hpp and _SParticles.cpp.