# Current context to which `self` was sorted
private var context: nullable SpriteContext = null
+ # Index in `context`
+ private var context_index: Int = -1
+
# Current context to which `self` belongs
private var sprite_set: nullable SpriteSet = null
end
var usage: GLBufferUsage
# Sprites drawn by this context
- var sprites = new GroupedArray[Sprite]
+ var sprites = new GroupedSprites
# Sprites to update since last `draw`
var sprites_to_update = new Set[Sprite]
# Update GPU data of `sprite`
fun update_sprite(sprite: Sprite)
do
- var sprite_index = sprites.index_of(sprite)
- if sprite_index == -1 then return
+ var context = sprite.context
+ if context != self then return
+
+ var sprite_index = sprite.context_index
+ assert sprite_index != -1
# Vertices data
# Number of item slots in the array
fun capacity: Int do return items.length
- # Index of `item`
- fun index_of(item: E): Int do return items.index_of(item)
-
# List of available slots
var available = new MinHeap[Int].default
# Index of the spots after filled chunks
var ends = new List[Int]
- # Add `item` to the first available slot
- fun add(item: E)
+ # Add `item` to the first available slot and return its index
+ fun add(item: E): Int
do
length += 1
# at end of first chunk
ends.first += 1
end
- return
+ return i
end
items.add item
starts.add 0
ends.add 1
else ends.last += 1
+ return ends.last - 1
end
# Remove the first instance of `item`
fun remove(item: E)
do
- var i = items.index_of(item)
- assert i != -1
+ var index = items.index_of(item)
+ remove_at(item, index)
+ end
+
+ # Remove `item` at `index`
+ fun remove_at(item: E, index: Int)
+ do
+ var i = index
length -= 1
items[i] = null
while max > 0 and (starts.length > 1 or starts.first != 0) do
var i = ends.last - 1
var e = items[i]
+ assert e != null
remove e
add e
moved.add e
end
end
+# Optimized `GroupedArray` to use `Sprite::context_index` and avoid using `index_of`
+private class GroupedSprites
+ super GroupedArray[Sprite]
+
+ redef fun add(item)
+ do
+ var index = super
+ item.context_index = index
+ return index
+ end
+
+ redef fun remove(item) do remove_at(item, item.context_index)
+end
+
redef class GLfloatArray
private fun fill_from_matrix(matrix: Matrix, dst_offset: nullable Int)
do