Skip to content

Commit

Permalink
Full palette merging working across both spritesets and charsets + test
Browse files Browse the repository at this point in the history
  • Loading branch information
smnjameson committed Jul 25, 2022
1 parent 4c6385f commit a68ee6a
Show file tree
Hide file tree
Showing 26 changed files with 576 additions and 157 deletions.
87 changes: 61 additions & 26 deletions build/aseparse65/png65.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function setOutputPath(pathName) {

function getFCMData(png, palette) {
let data = []
let highestCol = -1
for(var y=0; y<png.height; y+=8) {
for(var x=0; x<png.width; x+=8) {
for(var r=0; r<8; r++) {
Expand All @@ -93,6 +94,7 @@ function getFCMData(png, palette) {
);
})
data.push(col)
if(col > highestCol) highestCol = col
}
}
}
Expand All @@ -102,6 +104,16 @@ function getFCMData(png, palette) {
}


function trimFCMPalette(charData, pal) {
let data = []
let highestCol = -1
for(var i=0; i<charData.length; i++) {
if(charData.data[i] > highestCol) highestCol = charData.data[i]
}
console.log(highestCol, pal.length)
return pal
}

function sortFCMDataForSprites(png, sw, sh, palette, charData, isSprite) {
let spriteData = {}
let charNewData = []
Expand Down Expand Up @@ -395,17 +407,17 @@ function getPaletteData(png) {
console.log("Palette size: " + palette.length + " colors");


if (palette.length <256 && !argv.nofill) {
console.log("Padding to 256 colors")
while(palette.length <256) {
palette.push({
red: 0,
green: 0,
blue: 0,
alpha: 0
});
}
}
// if (palette.length <256 && !argv.nofill) {
// console.log("Padding to 256 colors")
// while(palette.length <256) {
// palette.push({
// red: 0,
// green: 0,
// blue: 0,
// alpha: 0
// });
// }
// }


let pal = { r: [], g: [], b: [] }
Expand Down Expand Up @@ -584,7 +596,7 @@ function fillPalette (palette) {
}


function appendPaletteFCM(palette, charData) {
function appendPaletteFCM(palette, charData, isSprites) {
let existingPalette = []
let data = charData.data
let spriteData = charData.spriteData
Expand Down Expand Up @@ -645,10 +657,11 @@ function appendPaletteFCM(palette, charData) {
}


function appendPaletteNCM(palette, charData) {
function appendPaletteNCM(palette, charData, isSprites) {
let existingPalette = []
let data = charData.data
let spriteData = charData.spriteData
let sliceOffset = 0

existingPalette = fs.readFileSync(path.resolve(argv.palette), null)

Expand All @@ -657,7 +670,9 @@ function appendPaletteNCM(palette, charData) {
let startIndex = Math.ceil(numCols / 16) * 16

console.log(`Palette append: Existing ${numCols} from ${startIndex}`)

if(!isSprites) {
sliceOffset = startIndex
}
//put existing palette in place
for(var i=0; i<startIndex; i++) {
if(i<numCols) {
Expand All @@ -677,8 +692,10 @@ function appendPaletteNCM(palette, charData) {
pal.b.push(palette.b[i])
}

for(var i=0; i<spriteData.colors.length; i++) {
spriteData.colors[i] += startIndex/16
if(isSprites) {
for(var i=0; i<spriteData.colors.length; i++) {
spriteData.colors[i] += startIndex/16
}
}
//now check every char and add a new color offset
// let cnt = 0
Expand All @@ -688,7 +705,7 @@ function appendPaletteNCM(palette, charData) {
pal.r.splice(256,pal.r.length)
pal.g.splice(256,pal.g.length)
pal.b.splice(256,pal.b.length)
return {pal, data, spriteData}
return {pal, data, spriteData, sliceOffset}
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
Expand All @@ -713,6 +730,7 @@ async function runCharMapper(argv) {
if(argv.fcm) {
convertedPal = paletteData.pal
charData = getFCMData(png, paletteData.palette)
// convertedPal = trimFCMPalette(charData, convertedPal)
}

if(argv.ncm) {
Expand All @@ -721,12 +739,36 @@ async function runCharMapper(argv) {
paletteData = sortedPalette.paletteData

convertedPal = paletteData.pal
}

let appendPal = null
if(argv.palette) {

if(argv.ncm) {
appendPal = appendPaletteNCM(convertedPal, charData)
} else {
appendPal = appendPaletteFCM(convertedPal, charData)
}
if(!argv.nofill) {
convertedPal = fillPalette(appendPal.pal)
} else {
convertedPal = appendPal.pal
}
charData.data = appendPal.data
if(argv.ncm) charData.spriteData = appendPal.spriteData
}

if(argv.ncm){
//save NCM color table
if(argv.palette) {
sortedPalette.charColors = sortedPalette.charColors.map(a => a+appendPal.sliceOffset)
}
fs.writeFileSync(path.resolve(outputPath, inputName+"_ncm.bin"), Buffer.from(sortedPalette.charColors))
} else {
fs.writeFileSync(path.resolve(outputPath, inputName+"_ncm.bin"), Buffer.from([]))
}


//SAVE PALETTE
fs.writeFileSync(path.resolve(outputPath, inputName+"_palette.bin"), Buffer.from(convertedPal.r.concat(convertedPal.g).concat(convertedPal.b)))
//SAVE CHARS
Expand Down Expand Up @@ -791,17 +833,12 @@ async function runSpriteMapper(argv) {
}


if(argv.nofill) {
console.log("NOFILL")
}


if(argv.palette) {
let appendPal = null
if(argv.ncm) {
appendPal = appendPaletteNCM(convertedPal, charData)
appendPal = appendPaletteNCM(convertedPal, charData, true)
} else {
appendPal = appendPaletteFCM(convertedPal, charData)
appendPal = appendPaletteFCM(convertedPal, charData, true)
}
if(!argv.nofill) {
convertedPal = fillPalette(appendPal.pal)
Expand Down Expand Up @@ -834,8 +871,6 @@ async function runSpriteMapper(argv) {


fs.writeFileSync(path.resolve(outputPath, inputName+"_meta.bin"), Buffer.from(metaBuffer))




//SAVE PALETTE
Expand Down
118 changes: 90 additions & 28 deletions docs/S65.sublime-completions
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,72 @@
"completions": [
{
"trigger": "id",
"annotation": "Sprite_Spriteset",
"annotation": "Asset_Spriteset",
"kind": "snippet"
},
{
"trigger": "name",
"annotation": "Sprite_Spriteset",
"annotation": "Asset_Spriteset",
"kind": "snippet"
},
{
"trigger": "address",
"annotation": "Sprite_Spriteset",
"annotation": "Asset_Spriteset",
"kind": "snippet"
},
{
"trigger": "metaAddress",
"annotation": "Sprite_Spriteset",
"annotation": "Asset_Spriteset",
"kind": "snippet"
},
{
"trigger": "meta",
"annotation": "Sprite_Spriteset",
"annotation": "Asset_Spriteset",
"kind": "snippet"
},
{
"trigger": "palette",
"annotation": "Sprite_Spriteset",
"annotation": "Asset_Spriteset",
"kind": "snippet"
},
{
"trigger": "indices",
"annotation": "Sprite_Spriteset",
"annotation": "Asset_Spriteset",
"kind": "snippet"
},
{
"trigger": "id",
"annotation": "Asset_Charset",
"kind": "snippet"
},
{
"trigger": "name",
"annotation": "Asset_Charset",
"kind": "snippet"
},
{
"trigger": "address",
"annotation": "Asset_Charset",
"kind": "snippet"
},
{
"trigger": "colorAddress",
"annotation": "Asset_Charset",
"kind": "snippet"
},
{
"trigger": "palette",
"annotation": "Asset_Charset",
"kind": "snippet"
},
{
"trigger": "colors",
"annotation": "Asset_Charset",
"kind": "snippet"
},
{
"trigger": "indices",
"annotation": "Asset_Charset",
"kind": "snippet"
},
{
Expand Down Expand Up @@ -177,25 +212,64 @@
"details": "Width of the visible screen background layer in characters"
},
{
"trigger": "Asset_ImportSpritePalette",
"trigger": "Asset_ImportCharset",
"annotation": "name,charpath,address",
"contents": "Asset_ImportCharset(${1:name}, ${2:charpath}, ${3:address})",
"kind": "snippet",
"details": "Helper function to preload the _chars file generated by png65 into a given address and load the _ncm file <br> When loading using these imports you only need the first part of the path and file name. eg<br><br> Asset_ImportCharset(\"map\",\"assets/bin/tileset1\", $8000)<br><br> Will load both the \"assets/bin/tileset1_chars.bin\" and the \"assets/bin/tileset1_ncm.s\" files. NOTE: MUST be called before the Layer_InitScreen"
},
{
"trigger": "Asset_ImportCharsetPalette",
"annotation": "name",
"contents": "Asset_ImportSpritePalette(${1:name})",
"contents": "Asset_ImportCharsetPalette(${1:name})",
"kind": "snippet",
"details": "Data generation macro that imports the full 256 color palette for the given spriteset name. The spriteset must be imported already otherwise this will fail."
"details": "Data generation macro that imports the full 256 color palette for the given charset name. The charset must be imported already otherwise this will fail."
},
{
"trigger": "Asset_ImportSprites",
"trigger": "Asset_ImportSpriteset",
"annotation": "name,charpath,address",
"contents": "Asset_ImportSprites(${1:name}, ${2:charpath}, ${3:address})",
"contents": "Asset_ImportSpriteset(${1:name}, ${2:charpath}, ${3:address})",
"kind": "snippet",
"details": "Helper function to preload the sprites _chars file generated by png65 into a given address and load the _meta file to create the sprite to char index values for use in ...<br> S65 provides space for up to 16 sprite set imports at any one time<br> When loading using these imports you only need the first part of the path and file name. eg<br><br> S65_ImportSprites(\"player\",\"assets/bin/sprites1\", $8000)<br><br> Will load both the \"assets/bin/sprites1_ncm.bin\" (or _fcm) and the \"assets/bin/sprites1_meta.s\" files. NCM or FCM is determined from the metafile<br><br> NOTE: MUST be called before the Layer_InitScreen"
"details": "Helper function to preload the sprites _chars file generated by png65 into a given address and load the _meta file <br> S65 provides space for up to 16 sprite set imports at any one time<br> When loading using these imports you only need the first part of the path and file name. eg<br><br> Asset_ImportSpriteset(\"player\",\"assets/bin/sprites1\", $8000)<br><br> Will load both the \"assets/bin/sprites1_chars.bin\" and the \"assets/bin/sprites1_meta.s\" files. NCM or FCM is determined from the metafile<br><br> NOTE: MUST be called before the Layer_InitScreen"
},
{
"trigger": "Asset_ImportSpritesetPalette",
"annotation": "name",
"contents": "Asset_ImportSpritesetPalette(${1:name})",
"kind": "snippet",
"details": "Data generation macro that imports the full 256 color palette for the given spriteset name. The spriteset must be imported already otherwise this will fail."
},
{
"trigger": "Asset_GetCharset",
"annotation": "name",
"contents": "Asset_GetCharset(${1:name})",
"kind": "snippet",
"details": "This method returns the charset object containing all the vars needed to perform many operations.<br><br> id - The numerical id of the charset assigned by Asset_ImportCharset name - The name of the spriteset<br> address - The start address for the spriteset char data <br> colorAddress - The start address for the colro table if present<br> palette - A {List} of bytes containign the palette data <br> colors - A {List} of bytes containign the ncm color mapping data <br>"
},
{
"trigger": "Asset_GetSpriteset",
"annotation": "name",
"contents": "Asset_GetSpriteset(${1:name})",
"kind": "snippet",
"details": "Sprite sets are assigned numerical values from 0 to 15 in order as they are imported. This method returns the spriteset object containing all the vars needed to perform many operations, .id is used whenever referencing a spriteset in commands e.g. <a href=\"#Sprite_SetSpriteMeta\">Sprite_SetSpriteMeta</a><br><br> id - The numerical id of the spriteset assigned by Asset_ImportSprites name - The name of the spriteset<br> address - The start address for the spriteset char data <br> metaAddress - The start address for the spriteset metadata <br> meta - A {List} of bytes containing the metadata<br> palette - A {List} of bytes containign the palette data <br>"
"details": "Sprite sets are assigned numerical values from 0 to 15 in order as they are imported. This method returns the spriteset object containing all the vars needed to perform many operations, .id is used whenever referencing a spriteset in commands e.g. <a href=\"#Sprite_SetSpriteMeta\">Sprite_SetSpriteMeta</a><br><br> id - The numerical id of the spriteset assigned by Asset_ImportSpriteset name - The name of the spriteset<br> address - The start address for the spriteset char data <br> metaAddress - The start address for the spriteset metadata <br> meta - A {List} of bytes containing the metadata<br> palette - A {List} of bytes containign the palette data <br>"
},
{
"trigger": "Asset_Charset",
"annotation": "The object returned by GetCharset",
"kind": "snippet",
"details": "The object returned by GetCharset"
},
{
"trigger": "Asset_Spriteset",
"annotation": "The object returned by GetSpriteset",
"kind": "snippet",
"details": "The object returned by GetSpriteset"
},
{
"trigger": "Asset_Asset_SpriteListMetaTable",
"annotation": "Pointer to the lookup table for the spritesets meta data tables",
"kind": "snippet",
"details": "Pointer to the lookup table for the spritesets meta data tables"
},
{
"trigger": "DMA_CopyJob",
Expand Down Expand Up @@ -719,15 +793,9 @@
},
{
"trigger": "Sprite_MetaData",
"annotation": "This is a byte array used by the engine when Asset_ImportSprites and Asset_ImportSpritesMeta is used to store all the meta data for the sprites such as mappings from sprite to character numbers, colors etc.",
"annotation": "This is a byte array used by the engine when Asset_ImportSpriteset and Asset_ImportSpritesetMeta is used to store all the meta data for the sprites such as mappings from sprite to character numbers, colors etc.",
"kind": "snippet",
"details": "This is a byte array used by the engine when Asset_ImportSprites and Asset_ImportSpritesMeta is used to store all the meta data for the sprites such as mappings from sprite to character numbers, colors etc."
},
{
"trigger": "Sprite_Spriteset",
"annotation": "The object returned by GetSpriteset",
"kind": "snippet",
"details": "The object returned by GetSpriteset"
"details": "This is a byte array used by the engine when Asset_ImportSpriteset and Asset_ImportSpritesetMeta is used to store all the meta data for the sprites such as mappings from sprite to character numbers, colors etc."
},
{
"trigger": "Sprite_IOcolor",
Expand Down Expand Up @@ -813,12 +881,6 @@
"kind": "snippet",
"details": "The size in bytes of the IO registers for a single sprite"
},
{
"trigger": "Sprite_Sprite_SpritesetMetaTable",
"annotation": "Pointer to the lookup table for the spritesets meta data tables",
"kind": "snippet",
"details": "Pointer to the lookup table for the spritesets meta data tables"
},
{
"trigger": "System_GetRandom16",
"annotation": "",
Expand Down
Loading

0 comments on commit a68ee6a

Please sign in to comment.