I found the most flexible and the fastest way for my game is to use stlcontainer to store special RenState class that reference to surfaces and have some additional info(Y-point on screenmap Z and X points on screen map, and other info, some necessary for drawing object info). Before each render I search all the objects that should be drawn and push to this container(except ground tiles, shadows and some other floor objects - they do not need z-ordering I draw them 'on the fly'), after that I use stl::sort() for this container.
To make it work you should overload bool operator < for RenState. It sorts very fast using only < to compare.
This method is very flexible, because if I need to get backreference to the object that I draw later on(to the base class that controlls some NPC, for example) - I can get it fast. It is also flexible because the game is Isometrical but the physics can be in 3D(like in my game;)
It is also good for some additional blits, for example I added behind-the-wall-visibility to my engine in 10 minutes
One of my RenState class functions and overloaded operator:
- Code:
void RenState::Set(ui8 inType, ui16 inSubType,
bool inVisible, ui8 inAlpha, ui16 inPivotX, ui16
inPivotY, ui16 inPivotZ, Surface * inSurf, ui16
inSurfaceX, ui16 inSurfaceY, ui8 inSurfaceW, ui8
inSurfaceH, si8 inExtraY)
{
Type = inType;//e.g. 5==NPC or 10==fire spark
SubType = inSubType;//e.g. NPC number, or wall Tile(WallX&Y mixed in one variable)
Visible = inVisible;
Alpha = inAlpha;
PivotX = inPivotX;//X position on 'ScreenMap'
PivotY = inPivotY;//Y position on 'ScreenMap'
PivotZ = inPivotZ;//subtract from PivotY while
// blitting(tohave right z-order&3rdDimension)
Surf = inSurf;//Surface pointer
SurfaceX = inSurfaceX;//Surf rect to blit
SurfaceY = inSurfaceY;//Surf rect to blit
SurfaceW = inSurfaceW;//Surf rect to blit
SurfaceH = inSurfaceH;//Surf rect to blit
ExtraY = inExtraY;//This used in some specific cases
}
bool operator < (const RenState & inState1, const RenState & inState2)//Needed for stl::sort
{
return (inState1.PivotY < inState2.PivotY);
}