Support transparency

This commit is contained in:
jaby 2023-05-26 22:20:43 +02:00
parent 1832f6b066
commit 6f735eeefb
4 changed files with 52 additions and 12 deletions

View File

@ -75,6 +75,15 @@ static constexpr const auto rectangle4 = JabyEngine::GPU::POLY_GT4(
JabyEngine::GPU::Color24::Green(),
JabyEngine::GPU::Color24::White()}
);
static constexpr const auto rectangle5 = JabyEngine::GPU::POLY_GT4(
{RectangleArea.position.move(0, RectangleArea.size.height), RectangleArea.size}, {0, 0},
RectangleTPage,
RectangleClut, {
JabyEngine::GPU::Color24::Red(),
JabyEngine::GPU::Color24::Blue(),
JabyEngine::GPU::Color24::Green(),
JabyEngine::GPU::Color24::White()}
).set_semi_transparent(true);
static void load_assets() {
static const JabyEngine::CDFile Assets[] = {
@ -107,6 +116,7 @@ static void load_assets() {
void main() {
load_assets();
printf("Dino: 0x%02X\n", rectangle4.code.value);
while(true) {
JabyEngine::GPU::render(triangle1);
@ -118,6 +128,7 @@ void main() {
JabyEngine::GPU::render(rectangle2);
JabyEngine::GPU::render(rectangle3);
JabyEngine::GPU::render(rectangle4);
JabyEngine::GPU::render(rectangle5);
JabyEngine::GPU::swap_buffers_vsync(2);
}

View File

@ -9,7 +9,7 @@ $(OUTPUT_DIR)/TexturePage.bin: TexturePage.png
$(OUTPUT_DIR)/IconTexture.bin: IconTexture.png
@mkdir -p $(OUTPUT_DIR)
jaby_engine_fconv --lz4 $< -o $@ simple-tim clut4
jaby_engine_fconv --lz4 $< -o $@ simple-tim clut4 --semi-trans --color-trans
all: $(OUTPUT_DIR)/TexturePage.bin $(OUTPUT_DIR)/IconTexture.bin

View File

@ -12,7 +12,7 @@ enum IndexPerByte {
}
pub struct IndexedImage {
palette: Vec<Color>,
pub palette: Vec<Color>,
raw_data: std::vec::IntoIter<u8>,
index_byte: IndexPerByte,
next_func: fn(&mut Self) -> Option<Color>,

View File

@ -30,7 +30,35 @@ pub struct Arguments {
color_depth: ColorType,
#[clap(value_enum, value_parser, default_value_t=ClutAlignment::None)]
clut_align: ClutAlignment
clut_align: ClutAlignment,
#[clap(long="semi-trans", default_value_t=false)]
semi_transparent: bool,
#[clap(long="color-trans", default_value_t=false)]
transparent_palette: bool
}
fn modify_palette(mut image: IndexedImage, clut_align: ClutAlignment, semi_transparent: bool, transparent_palette: bool) -> IndexedImage {
if semi_transparent {
for color in image.palette.iter_mut() {
*color = PSXColor::semi_transparent(color.get_red(), color.get_green(), color.get_blue());
}
}
if transparent_palette {
if clut_align == ClutAlignment::Block {
for color in image.palette.iter_mut().step_by(16) {
*color = PSXColor::transparent();
}
}
else {
if let Some(first_color) = image.palette.get_mut(0) {
*first_color = PSXColor::transparent();
}
}
}
image
}
fn encode<T: PSXImageConverter>(image: T, color_depth: ColorType, clut_align: ClutAlignment, output: &mut dyn Write) -> Result<(), Error> {
@ -95,7 +123,7 @@ fn convert_full16(input: Input, output: &mut dyn Write) -> Result<(), Error> {
}
}
fn convert_palette_based(input: Input, output: &mut dyn Write, color_type: ColorType, clut_align: ClutAlignment) -> Result<(), Error> {
fn convert_palette_based(input: Input, output: &mut dyn Write, color_type: ColorType, clut_align: ClutAlignment, semi_transparent: bool, transparent_palette: bool) -> Result<(), Error> {
match png::Decoder::new(input).read_info() {
Ok(reader) => {
let output_type = {
@ -105,7 +133,8 @@ fn convert_palette_based(input: Input, output: &mut dyn Write, color_type: Color
_ => return Err(Error::from_str("ColorType not supported"))
}
};
encode(IndexedImage::new(reader, output_type)?, color_type, clut_align, output)
encode(modify_palette(IndexedImage::new(reader, output_type)?, clut_align, semi_transparent, transparent_palette), color_type, clut_align, output)
},
Err(error) => Err(Error::from_error(error))
}
@ -114,6 +143,6 @@ fn convert_palette_based(input: Input, output: &mut dyn Write, color_type: Color
pub fn convert(args: Arguments, input: Input, output: &mut dyn Write) -> Result<(), Error> {
match args.color_depth {
ColorType::Full16 => convert_full16(input, output),
_ => convert_palette_based(input, output, args.color_depth, args.clut_align),
_ => convert_palette_based(input, output, args.color_depth, args.clut_align, args.semi_transparent, args.transparent_palette),
}
}