Add ZOrdering to SpriteBatches #1139
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently there is no way to set the draw order of elements in SpriteBatches that I know of. I believe this discourages the use of SpriteBatches in favour of bitmaps and anims even if SpriteBatches are superior in terms of performance. There also seems to be more tools for general drawables (eg Layers, Anim) rather than for SpriteBatches. The changes I've put here is the first step to remedy that if allowed.
What it does is it adds a z-order field to
BatchElement.SpriteBatchthen adds/inserts it into the right spot when adding to the sprite batch.It also re-inserts it to the right spot on setting the zorder so it should minimize the performance penalty when this is switched on. But I'm open to additional performance improvements you might notice.
Some notes
hasZOrderflag off so as to not break backwards compatibility.hasZOrderis switched on.before=trueflag to make it add from the start of the list instead but I think that might be over-engineering it a bit.ReversedElementsIteratorso we can iterate through the elements in reverse. I needed this so I could remove items easily and to add sprites at the end of the list if the zordering is the same.insertBeforeandinsertAfterto the SpriteBatch to easily add elements before or after a certain element. I'm a bit dubious on the logic here so we might have to test this.I've tested it on my end here and it seems fine but it's still largely untested not sure the procedure here.
Let me know if I've added too much into one merge request, or if you prefer it a bit more split up.
Here is an example below. The green sprite was added last. The bullets z-order is set to
0.0and one of the characters z-order is set to2.0here so the red character is always in front of the bullets. This is all one spritebatch and 1 draw call.hl_Laxoex9Srq.mp4