Sprite: Implement Y based Z draw ordering (RGSS2)

If two sprites have the same Z, their order is decided based
on their Y coordinate first, and only then (if equal) by creation
time.
This commit is contained in:
Jonas Kulla 2014-08-16 13:50:57 +02:00
parent 3887342439
commit e208bc49e8
5 changed files with 39 additions and 10 deletions

View File

@ -105,12 +105,14 @@ void Scene::composite()
}
SceneElement::SceneElement(Scene &scene, int z)
SceneElement::SceneElement(Scene &scene, int z, bool isSprite)
: link(this),
creationStamp(shState->genTimeStamp()),
z(z),
visible(true),
scene(&scene)
scene(&scene),
spriteY(0),
isSprite(isSprite)
{
scene.insert(*this);
}
@ -120,7 +122,9 @@ SceneElement::SceneElement(Scene &scene, int z, unsigned int cStamp)
creationStamp(cStamp),
z(z),
visible(true),
scene(&scene)
scene(&scene),
spriteY(0),
isSprite(false)
{
scene.insert(*this);
}
@ -167,11 +171,23 @@ void SceneElement::setVisible(bool value)
bool SceneElement::operator<(const SceneElement &o) const
{
/* Element draw order is decided by their Z value.
* If two Z values are equal, the later created object
* has priority */
if (z <= o.z)
{
if (z == o.z)
if (creationStamp > o.creationStamp)
return false;
{
#ifdef RGSS2
/* RGSS2: If two sprites' Z values collide,
* their Y coordinates decide draw order. Only
* on equal Y does the creation time take effect */
if (isSprite && o.isSprite && spriteY != o.spriteY)
return (spriteY < o.spriteY);
#endif
return (creationStamp <= o.creationStamp);
}
return true;
}
@ -179,6 +195,12 @@ bool SceneElement::operator<(const SceneElement &o) const
return false;
}
void SceneElement::setSpriteY(int value)
{
spriteY = value;
scene->reinsert(*this);
}
void SceneElement::unlink()
{
if (scene)

View File

@ -71,7 +71,7 @@ protected:
class SceneElement
{
public:
SceneElement(Scene &scene, int z = 0);
SceneElement(Scene &scene, int z = 0, bool isSprite = false);
SceneElement(Scene &scene, int z, unsigned int cStamp);
virtual ~SceneElement();
@ -108,6 +108,7 @@ protected:
* elements with lower priority are drawn earlier */
bool operator<(const SceneElement &o) const;
void setSpriteY(int value);
void unlink();
IntruListLink<SceneElement> link;
@ -119,6 +120,10 @@ protected:
friend class Scene;
friend class Viewport;
friend struct TilemapPrivate;
private:
int spriteY;
const bool isSprite;
};
#endif // SCENE_H

View File

@ -302,7 +302,7 @@ struct SpritePrivate
};
Sprite::Sprite(Viewport *viewport)
: ViewportElement(viewport)
: ViewportElement(viewport, 0, true)
{
p = new SpritePrivate;
onGeometryChange(scene->getGeometry());
@ -393,6 +393,8 @@ void Sprite::setY(int value)
#ifdef RGSS2
p->wave.dirty = true;
setSpriteY(value);
#endif
}

View File

@ -218,8 +218,8 @@ void Viewport::onGeometryChange(const Geometry &geo)
}
ViewportElement::ViewportElement(Viewport *viewport, int z)
: SceneElement(viewport ? *viewport : *shState->screen(), z),
ViewportElement::ViewportElement(Viewport *viewport, int z, bool isSprite)
: SceneElement(viewport ? *viewport : *shState->screen(), z, isSprite),
m_viewport(viewport)
{}

View File

@ -65,7 +65,7 @@ private:
class ViewportElement : public SceneElement
{
public:
ViewportElement(Viewport *viewport = 0, int z = 0);
ViewportElement(Viewport *viewport = 0, int z = 0, bool isSprite = false);
ViewportElement(Viewport *viewport, int z, unsigned int cStamp);
Viewport *getViewport() const;