Sprite Engine - 2D image drawing manager
Posted: Thu Mar 29, 2012 7:15 pm
:: Sprite Engine :: version 03_2012
(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:
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.
Okay, before we go on, I'll do a little explaining here. There are three object types in SpriteEngine: Image, Sprite and Particle(more on this later). An Image is just a container with an ITexture* to a spritesheet ITexture and a recti* to the rectangle of the part in the spritesheet you want. To draw an image, we create a Sprite and pass a Image* along with some drawing parameters into it. These Sprites are populated in a list (because there's a lot of them; one for each image drawn on screen) which is then simply passed to the engine to draw. You can do anything you want with it, like putting a Sprite for every player object and tell that object to manage it, or whatever, it's all up to you. Check out the documentation on _SSprite.hpp if you're wondering what you can do.
How to use Particles
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.
(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.