From 6f735eeefbbefb4a81314eadac1f3feffdbb7f8b Mon Sep 17 00:00:00 2001 From: jaby Date: Fri, 26 May 2023 22:20:43 +0200 Subject: [PATCH] Support transparency --- examples/PoolBox/application/src/main.cpp | 13 ++++++- examples/PoolBox/assets/Makefile | 2 +- .../src/images/reduced_tim/color_clut.rs | 12 +++--- .../src/images/reduced_tim/mod.rs | 37 +++++++++++++++++-- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/examples/PoolBox/application/src/main.cpp b/examples/PoolBox/application/src/main.cpp index e8de2b71..e42f1ed7 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -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,7 +116,8 @@ static void load_assets() { void main() { load_assets(); - + printf("Dino: 0x%02X\n", rectangle4.code.value); + while(true) { JabyEngine::GPU::render(triangle1); JabyEngine::GPU::render(triangle2); @@ -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); } diff --git a/examples/PoolBox/assets/Makefile b/examples/PoolBox/assets/Makefile index d898dbb8..15b5e4ca 100644 --- a/examples/PoolBox/assets/Makefile +++ b/examples/PoolBox/assets/Makefile @@ -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 diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs index 660e4387..63b3d9fa 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/color_clut.rs @@ -12,12 +12,12 @@ enum IndexPerByte { } pub struct IndexedImage { - palette: Vec, - raw_data: std::vec::IntoIter, - index_byte: IndexPerByte, - next_func: fn(&mut Self) -> Option, - width: u16, - height: u16, + pub palette: Vec, + raw_data: std::vec::IntoIter, + index_byte: IndexPerByte, + next_func: fn(&mut Self) -> Option, + width: u16, + height: u16, } impl IndexedImage { diff --git a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs index 6deb8357..d6745e6c 100644 --- a/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs +++ b/src/Tools/jaby_engine_fconv/src/images/reduced_tim/mod.rs @@ -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(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), } } \ No newline at end of file