import PixelImage from '../model/PixelImage' ; import C64Layout from './C64Layout' ; import IBinaryFormat from './IBinaryFormat' ; export default class SpritePad implements IBinaryFormat { public formatName = 'spritepad' ; public defaultExtension = 'spd' ; public supportedModes : string [ ] = [ 'c64HiresSprites' , 'c64MulticolorSprites' , 'c64ThreecolorSprites' , 'c64TwocolorSprites' ] ; private backgroundColor : number ; private multiColor1 : number ; private multiColor2 : number ; private nrOfSprites = 0 ; private sprites : Uint8Array [ ] = [ ] ; public fromPixelImage ( pixelImage ) { const bitmap = C64Layout . convertBitmap ( pixelImage ) ; const isMulticolor = pixelImage . mode . pixelWidth === 2 ; this . backgroundColor = pixelImage . colorMaps [ 0 ] . get ( 0 , 0 ) ; this . multiColor1 = isMulticolor ? pixelImage . colorMaps [ 1 ] . get ( 0 , 0 ) : 0 ; this . multiColor2 = isMulticolor ? pixelImage . colorMaps [ 2 ] . get ( 0 , 0 ) : 0 ; const spriteColorMapIndex = isMulticolor ? 3 : 1 ; let byteCounter = 0 ; pixelImage . mode . forEachCell ( 0 , ( x , y ) => { this . sprites [ this . nrOfSprites ] = new Uint8Array ( 64 ) ; for ( let i = 0 ; i < 63 ; i += 1 ) { this . sprites [ this . nrOfSprites ] [ i ] = bitmap [ byteCounter ] ; byteCounter += 1 ; } const modeFlag = isMulticolor ? 0x80 : 0 ; const spriteColor = pixelImage . colorMaps [ spriteColorMapIndex ] . get ( x , y ) & 0x0f ; this . sprites [ this . nrOfSprites ] [ 63 ] = modeFlag | spriteColor ; this . nrOfSprites += 1 ; } ) ; } public toMemoryMap ( ) { const result = [ new Uint8Array ( [ this . backgroundColor , this . multiColor1 , this . multiColor2 ] ) ] ; return result . concat ( this . sprites ) ; } }