I need to debug more often. My original palette swap scripts had one no-brainer bug and one script-killing typo. They were pretty easy to catch once I opened up a palswapped file and saw the wrong results. And just to make matters worse for me, I discovered Paintshop Pro switches the first two palette entries if I try to save an 8-bit GIF as a PNG without transparencies. In order to keep the palette the same, I have to make it transparent, which has no effect in GM, but makes browsing files harder.
Here is the code for palette swapping an NES-style (8-bit palette with 3 editable entries) GIF file:
var i,chr,col,pal;
chr = file_bin_open(temp_directory+"\"+argument0+".gif",1);
for(i=$10;i<$19;i+=1)
{
col = (i-$10) mod 3;
switch (i-$10) div 3
{
case 0:
pal = argument1;
break;
case 1:
pal = argument2;
break;
case 2:
pal = argument3;
break;
}
file_bin_seek(chr,i);
switch col
{
case 0:
file_bin_write_byte(chr,color_get_red(pal));
break;
case 1:
file_bin_write_byte(chr,color_get_green(pal));
break;
case 2:
file_bin_write_byte(chr,color_get_blue(pal));
break;
}
}
file_bin_close(chr);
For a PNG file, change $10 to $64 and $19 to $6D. As I said, Paintshop Pro messes with the palette if you save it without transparency, so it's best to use a GIF if you don't need more than 15 editable colors. And of course there are some speed considerations, which I haven't tested. This script may turn out to be useable only at the start of a room. We'll see.
After palswapping a file, you'll need to replace the resource in GM that uses it. Herein lies the real problem with palswapping. Conveniently once you replace a resource, it affects all resources that use it. In other words, background_replace() will affect a background image or tile set immediately. Sadly for me, this means my current plan of constructing tiles in-game is infeasible because it would require four background sets for each color combination, which defeats the purpose.
No comments:
Post a Comment