/**
  The sprite abstraction class for parsing and rendering a sprite.
**/ import { SpriteParser } from './parsers/SpriteParser' ; import { SpriteRender } from './renderers/SpriteRender' ; export class Sprite { sprite : any ; spriteRender : any ; constructor ( public gl , public data ) { this . sprite = data . header === undefined ? SpriteParser . parse ( data ) : data ; } render ( ) { if ( ! this . spriteRender ) { this . spriteRender = new SpriteRender ( this . gl , this . sprite ) ; } this . spriteRender . render ( ) ; } subSprite ( x , y , w , h ) { const subsprite = { header : this . sprite . header , frames : Array ( this . sprite . frames . length ) } ; let width = this . sprite . header . maxWidth ; for ( let i = 0 ; i < this . sprite . frames . length ; ++ i ) { let frame = this . sprite . frames [ i ] ; let imageData = new Uint8Array ( 4 * w * h ) ; let start = 4 * ( width * y + x ) ; let n = start ; for ( let j = 0 ; j != h ; ++ j ) { let row = frame . imageData . subarray ( n , 4 * w + n ) ; imageData . set ( row , 4 * j * w ) ; n += 4 * width ; } subsprite . frames [ i ] = { group : frame . group , originX : frame . originX , originY : frame . originY , width : w , height : h , imageData : imageData } ; } return new Sprite ( this . gl , subsprite ) ; } }