public SpriteBatch( 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 Sprite[ maxSpriteCount ] ; sortedList = new Sprite[ 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 void RemoveSprite( Sprite sprite ) { RemoveFromSpriteList( sprite ) ; RemoveFromSortedList( sprite ) ; sprite.batch = null ; vertexCount -= 4 ; indexCount -= 6 ; }
public void AddSprite( Sprite sprite ) { AddToSpriteList( sprite ) ; AddToSortedList( sprite ) ; sprite.batch = this ; vertexCount += 4 ; indexCount += 6 ; }
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 Sprite( batch, material, 0 ) ; InitSprite( sprites[ i ] ) ; } } else { for ( int i = count ; i < spriteCount ; i ++ ) { batch.RemoveSprite( sprites[ i ] ) ; } } spriteCount = count ; }
public void InitSprite( Sprite s ) { s.Position.X = 0; s.Position.Y = 0; s.Direction.X = 0; s.Direction.Y = 0; s.Size = new Vector2( spriteSize, spriteSize ) ; s.Center = new Vector2( 0.5f, 0.5f ) ; s.UVOffset = new Vector2( 0.25f, 0.25f ) ; s.UVSize = new Vector2( 0.25f, 0.25f ) ; s.Color = new Rgba( 255, 255, 255, 255 ) ; s.UpdateAll() ; }
internal int FindSortedList( Sprite 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( Sprite 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( Sprite 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( Sprite 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( Sprite sprite ) { sprite.spriteID = spriteCount ; sprite.vertexID = vertexCount ; spriteList[ spriteCount ++ ] = sprite ; needUpdateVertexData = true ; }