public Sprite2DBatch( GraphicsContext graphics, int maxSpriteCount ) { int maxVertexCount = maxSpriteCount * 4 ; int maxIndexCount = maxSpriteCount * 6 ; graphicsContext = graphics ; #if !RESIZE_VERTEX_BUFFER vertexBuffer = new VertexBuffer( maxVertexCount, maxIndexCount, vertexFormats ) ; spriteCapacity = maxSpriteCount ; #endif // RESIZE_VERTEX_BUFFER vertexData = new Vertex[ maxVertexCount ] ; indexData = new ushort[ maxIndexCount ] ; spriteList = new Sprite2D[ maxSpriteCount ] ; sortedList = new Sprite2D[ maxSpriteCount ] ; #if ENABLE_SIN_TABLE if ( sinTable == null ) { sinTable = new float[ 4096 ] ; for ( int i = 0 ; i < 4096 ; i ++ ) { sinTable[ i ] = FMath.Sin( i * ( FMath.PI / 2048.0f ) ) ; } } #endif // ENABLE_SIN_TABLE }
public Sprite2DBatch(GraphicsContext graphics, int maxSpriteCount) { int maxVertexCount = maxSpriteCount * 4; int maxIndexCount = maxSpriteCount * 6; graphicsContext = graphics; #if !RESIZE_VERTEX_BUFFER vertexBuffer = new VertexBuffer(maxVertexCount, maxIndexCount, vertexFormats); spriteCapacity = maxSpriteCount; #endif // RESIZE_VERTEX_BUFFER vertexData = new Vertex[maxVertexCount]; indexData = new ushort[maxIndexCount]; spriteList = new Sprite2D[maxSpriteCount]; sortedList = new Sprite2D[maxSpriteCount]; #if ENABLE_SIN_TABLE if (sinTable == null) { sinTable = new float[4096]; for (int i = 0; i < 4096; i++) { sinTable[i] = FMath.Sin(i * (FMath.PI / 2048.0f)); } } #endif // ENABLE_SIN_TABLE }
internal void AddToSortedList(Sprite2D sprite) { int index = FindSortedList(sprite); if (index >= 0 && sortedList[index].sortKey == sprite.sortKey) { var head = sortedList[index]; var tail = head.sortPrev; head.sortPrev = sprite; tail.sortNext = sprite; sprite.sortPrev = tail; sprite.sortNext = head; } else { int count = (sortedCount++) - (++index); if (count > 0) { Array.Copy(sortedList, index, sortedList, index + 1, count); } sortedList[index] = sprite; sprite.sortPrev = sprite; sprite.sortNext = sprite; } sprite.indexID = -1; needUpdateIndexData = true; }
// Subroutines internal void AddToSpriteList(Sprite2D sprite) { sprite.spriteID = spriteCount; sprite.vertexID = vertexCount; spriteList[spriteCount++] = sprite; needUpdateVertexData = true; }
public void AddSprite( Sprite2D sprite ) { AddToSpriteList( sprite ) ; AddToSortedList( sprite ) ; sprite.batch = this ; vertexCount += 4 ; indexCount += 6 ; }
public void RemoveSprite( Sprite2D sprite ) { RemoveFromSpriteList( sprite ) ; RemoveFromSortedList( sprite ) ; sprite.batch = null ; vertexCount -= 4 ; indexCount -= 6 ; }
public void AddSprite(Sprite2D sprite) { AddToSpriteList(sprite); AddToSortedList(sprite); sprite.batch = this; vertexCount += 4; indexCount += 6; }
public void RemoveSprite(Sprite2D sprite) { RemoveFromSpriteList(sprite); RemoveFromSortedList(sprite); sprite.batch = null; vertexCount -= 4; indexCount -= 6; }
public void InitSprite(Sprite2D s) { s.Position.X = -100; s.Position.Y = -100; s.Direction = 0; s.Size = new Vector2(spriteSize, spriteSize); s.Center = new Vector2(0.5f, 0.5f); s.UVOffset = new Vector2(1, 1) * 0.25f; s.UVSize = new Vector2(0.25f, 0.25f); s.Color = new Rgba(255, 255, 255, 255); s.UpdateAll(); }
internal void UpdateIndexData() { if (!needUpdateIndexData) { return; } needUpdateIndexData = false; int indexID = 0; Sprite2DMaterial material = null; Sprite2D draw = null; drawList = null; drawCount = 0; for (int i = 0; i < sortedCount; i++) { var head = sortedList[i]; var sprite = head; if (material != sprite.Material) { material = sprite.Material; if (draw == null) { drawList = sprite; } if (draw != null) { draw.drawNext = sprite; } sprite.drawNext = null; draw = sprite; drawCount++; } do { if (sprite.indexID != indexID) { sprite.indexID = indexID; indexData[indexID + 0] = (ushort)(sprite.vertexID + 0); indexData[indexID + 1] = (ushort)(sprite.vertexID + 1); indexData[indexID + 2] = (ushort)(sprite.vertexID + 2); indexData[indexID + 3] = (ushort)(sprite.vertexID + 2); indexData[indexID + 4] = (ushort)(sprite.vertexID + 1); indexData[indexID + 5] = (ushort)(sprite.vertexID + 3); } indexID += 6; sprite = sprite.sortNext; } while (sprite != head); } vertexBuffer.SetIndices(indexData, 0, 0, indexCount); }
internal void RemoveFromSpriteList(Sprite2D sprite) { var tail = spriteList[--spriteCount]; if (sprite != tail) { spriteList[sprite.spriteID] = tail; for (int i = 0; i < 4; i++) { vertexData[sprite.vertexID + i] = vertexData[tail.vertexID + i]; } tail.spriteID = sprite.spriteID; tail.vertexID = sprite.vertexID; } needUpdateVertexData = true; }
internal int FindSortedList(Sprite2D sprite) { int lower = -1; int upper = sortedCount; while (lower + 1 < upper) { int middle = (lower + upper) / 2; if (sortedList[middle].sortKey > sprite.sortKey) { upper = middle; } else { lower = middle; } } return(lower); }
public void SetSpriteCount(int count) { count = Math.Min(Math.Max(count, 1), sprites.Length); if (count > spriteCount) { for (int i = spriteCount; i < count; i++) { sprites[i] = new Sprite2D(batch, material, 0); InitSprite(sprites[i]); } } else { for (int i = count; i < spriteCount; i++) { batch.RemoveSprite(sprites[i]); } } spriteCount = count; }
internal void RemoveFromSortedList(Sprite2D sprite) { int index = FindSortedList(sprite); if (sortedList[index] == sprite) { sortedList[index] = sprite.sortNext; if (sprite.sortNext == sprite) { int count = (--sortedCount) - index; if (count > 0) { Array.Copy(sortedList, index + 1, sortedList, index, count); } } } sprite.sortPrev.sortNext = sprite.sortNext; sprite.sortNext.sortPrev = sprite.sortPrev; needUpdateIndexData = true; }
public void InitSprite( Sprite2D s ) { s.Position.X = -100 ; s.Position.Y = -100 ; s.Direction = 0; s.Size = new Vector2( spriteSize, spriteSize ) ; s.Center = new Vector2( 0.5f, 0.5f ) ; s.UVOffset = new Vector2( 1, 1 ) * 0.25f ; s.UVSize = new Vector2( 0.25f, 0.25f ) ; s.Color = new Rgba( 255, 255, 255, 255 ) ; s.UpdateAll() ; }
internal int FindSortedList( Sprite2D sprite ) { int lower = -1 ; int upper = sortedCount ; while ( lower + 1 < upper ) { int middle = ( lower + upper ) / 2 ; if ( sortedList[ middle ].sortKey > sprite.sortKey ) { upper = middle ; } else { lower = middle ; } } return lower ; }
internal void RemoveFromSortedList( Sprite2D sprite ) { int index = FindSortedList( sprite ) ; if ( sortedList[ index ] == sprite ) { sortedList[ index ] = sprite.sortNext ; if ( sprite.sortNext == sprite ) { int count = ( -- sortedCount ) - index ; if ( count > 0 ) Array.Copy( sortedList, index + 1, sortedList, index, count ) ; } } sprite.sortPrev.sortNext = sprite.sortNext ; sprite.sortNext.sortPrev = sprite.sortPrev ; needUpdateIndexData = true ; }
internal void AddToSortedList( Sprite2D sprite ) { int index = FindSortedList( sprite ) ; if ( index >= 0 && sortedList[ index ].sortKey == sprite.sortKey ) { var head = sortedList[ index ] ; var tail = head.sortPrev ; head.sortPrev = sprite ; tail.sortNext = sprite ; sprite.sortPrev = tail ; sprite.sortNext = head ; } else { int count = ( sortedCount ++ ) - ( ++ index ) ; if ( count > 0 ) Array.Copy( sortedList, index, sortedList, index + 1, count ) ; sortedList[ index ] = sprite ; sprite.sortPrev = sprite ; sprite.sortNext = sprite ; } sprite.indexID = -1 ; needUpdateIndexData = true ; }
internal void RemoveFromSpriteList( Sprite2D sprite ) { var tail = spriteList[ -- spriteCount ] ; if ( sprite != tail ) { spriteList[ sprite.spriteID ] = tail ; for ( int i = 0 ; i < 4 ; i ++ ) { vertexData[ sprite.vertexID + i ] = vertexData[ tail.vertexID + i ] ; } tail.spriteID = sprite.spriteID ; tail.vertexID = sprite.vertexID ; } needUpdateVertexData = true ; }
// Subroutines internal void AddToSpriteList( Sprite2D sprite ) { sprite.spriteID = spriteCount ; sprite.vertexID = vertexCount ; spriteList[ spriteCount ++ ] = sprite ; needUpdateVertexData = true ; }
public void SetSpriteCount( int count ) { count = Math.Min( Math.Max( count, 1 ), sprites.Length ) ; if ( count > spriteCount ) { for ( int i = spriteCount ; i < count ; i ++ ) { sprites[ i ] = new Sprite2D( batch, material, 0 ) ; InitSprite( sprites[ i ] ) ; } } else { for ( int i = count ; i < spriteCount ; i ++ ) { batch.RemoveSprite( sprites[ i ] ) ; } } spriteCount = count ; }