Skip to content

Commit

Permalink
Make images top-to-bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldom-SE committed Jul 22, 2024
1 parent c54d8b9 commit b43db42
Show file tree
Hide file tree
Showing 22 changed files with 19 additions and 64 deletions.
Binary file modified assets/filter/click.px_filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/filter/dim.px_filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/filter/fade_to_black.px_filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/filter/hover.px_filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/filter/invert.px_filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/filter/invert_dim.px_filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/palette/palette_1.palette.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/palette/palette_2.palette.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/sprite/mage_cast.px_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/sprite/runner.px_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/sprite/snow_1.px_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/sprite/snow_2.px_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/tileset/tileset.px_tileset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/typeface/animated_typeface.px_typeface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/typeface/typeface.px_typeface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,12 @@ pub(crate) fn draw_spatial<'a, A: Animation + Spatial>(
PxCanvas::World => position - *camera,
PxCanvas::Camera => position,
};
let position = IVec2::new(position.x, image.size().y as i32 - position.y);
let size = size.as_ivec2();

let mut image = image.slice_mut(IRect {
min: position,
max: position + size.as_ivec2(),
min: position - IVec2::new(0, size.y),
max: position + IVec2::new(size.x, 0),
});

draw_animation(spatial, param, &mut image, animation, filters);
Expand Down
5 changes: 4 additions & 1 deletion src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ fn draw_cursor(
let mut image =
PxImageSliceMut::from_image_mut(images.get_mut(&screen.image).unwrap());

if let Some(pixel) = image.get_pixel_mut(cursor_pos.as_ivec2()) {
if let Some(pixel) = image.get_pixel_mut(IVec2::new(
cursor_pos.x as i32,
image.height() as i32 - 1 - cursor_pos.y as i32,
)) {
*pixel = filter
.get_pixel(IVec2::new(*pixel as i32, 0))
.expect("filter is incorrect size");
Expand Down
4 changes: 2 additions & 2 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl AssetLoader for PxFilterLoader {
/// and an image file. The image should have pixels in the same positions as the palette.
/// The position of each pixel describes the mapping of colors. The image must only contain colors
/// that are also in the palette. For animated filters, arrange a number of filters
/// from the bottom-left corner, moving rightwards, wrapping upwards when it gets to the edge
/// from the top-left corner, moving rightwards, wrapping downwards when it gets to the edge
/// of the image. For examples, see the `assets/` directory in this repository. `fade_to_black.png`
/// is an animated filter.
#[derive(Asset, Reflect, Debug)]
Expand All @@ -116,7 +116,7 @@ impl Animation for PxFilter {

fn draw(
&self,
_: Self::Param,
(): (),
image: &mut PxImageSliceMut<impl Pixel>,
frame: impl Fn(UVec2) -> usize,
_: impl Fn(u8) -> u8,
Expand Down
47 changes: 4 additions & 43 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,6 @@ impl<P: Pixel> PxImage<P> {
}
}

pub(crate) fn flip_vert(&self) -> Self {
PxImage {
image: self
.image
.chunks_exact(self.width)
.rev()
.flatten()
.copied()
.collect(),
width: self.width,
}
}

pub(crate) fn split_vert(self, chunk_height: usize) -> Vec<Self> {
self.image
.chunks_exact(chunk_height * self.width)
Expand Down Expand Up @@ -149,11 +136,6 @@ impl PxImage<Option<u8>> {
.convert(TextureFormat::Rgba8UnormSrgb)
.ok_or_else(|| anyhow!("could not convert image to `Rgba8UnormSrgb`"))?
.data
.chunks_exact(image.texture_descriptor.size.width as usize * 4)
.rev()
.flatten()
.copied()
.collect::<Vec<_>>()
.chunks_exact(4)
.map(|color| {
(color[3] != 0)
Expand All @@ -179,31 +161,6 @@ impl PxImage<Option<u8>> {
})
}

pub(crate) fn palette_indices_unaligned(palette: &Palette, image: &Image) -> Result<Self> {
Ok(Self {
image: image
.convert(TextureFormat::Rgba8UnormSrgb)
.unwrap()
.data
.chunks_exact(4)
.map(|color| {
(color[3] != 0)
.then(|| {
palette
.indices
.get(&[color[0], color[1], color[2]])
.copied()
.ok_or_else(|| {
anyhow!("a sprite contained a color that wasn't in the palette")
})
})
.transpose()
})
.collect::<Result<_>>()?,
width: image.texture_descriptor.size.width as usize,
})
}

pub(crate) fn trim_right(&mut self) {
while (0..self.height()).all(|row| self.image[self.width * (row + 1) - 1].is_none()) {
for row in (0..self.height()).rev() {
Expand Down Expand Up @@ -265,6 +222,10 @@ impl<'a, P: Pixel> PxImageSliceMut<'a, P> {
self.slice.size().x as u32
}

pub(crate) fn height(&self) -> u32 {
self.slice.size().y as u32
}

pub(crate) fn image_width(&self) -> usize {
self.width
}
Expand Down
4 changes: 1 addition & 3 deletions src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ impl Palette {
.convert(TextureFormat::Rgba8UnormSrgb)
.unwrap()
.data
.chunks_exact(palette.texture_descriptor.size.width as usize * 4)
.rev()
.flatten()
.iter()
.copied()
// TODO Should use chunks here
.fold(
Expand Down
5 changes: 1 addition & 4 deletions src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,13 @@ fn dither_slice<A: Algorithm<MAP_SIZE>, const MAP_SIZE: usize>(

// TODO Use more helpers
// TODO Feature gate
// TODO Immediate function version
fn image_to_sprite(
mut to_sprites: Query<(&ImageToSprite, &mut Handle<PxSprite>)>,
images: Res<Assets<Image>>,
palette: PaletteParam,
mut sprites: ResMut<Assets<PxSprite>>,
) {
let span = info_span!("init", name = "init").entered();
if to_sprites.iter().next().is_none() {
return;
}
Expand All @@ -413,10 +413,8 @@ fn image_to_sprite(
.map(|&color| color.into())
.collect::<Vec<[f32; 3]>>()[..],
);
drop(span);

to_sprites.iter_mut().for_each(|(image, mut sprite)| {
let span = info_span!("making_images", name = "making_images").entered();
let dither = &image.dither;
let image = images.get(&image.image).unwrap();

Expand Down Expand Up @@ -446,7 +444,6 @@ fn image_to_sprite(
.zip(sprite.data.iter_mut())
.enumerate()
.collect::<Vec<_>>();
drop(span);

pixels.par_chunk_map_mut(ComputeTaskPool::get(), 20, |_, pixels| {
use DitherAlgorithm::*;
Expand Down
12 changes: 3 additions & 9 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl AssetLoader for PxTypefaceLoader {
.load(reader, &settings.image_loader_settings, load_context)
.await?;
let palette = asset_palette().await;
let indices = PxImage::palette_indices_unaligned(palette, &image)?;
let indices = PxImage::palette_indices(palette, &image)?;
let height = indices.height();
let character_count = settings.characters.chars().count();

Expand All @@ -81,14 +81,8 @@ impl AssetLoader for PxTypefaceLoader {
settings
.characters
.chars()
.zip(
indices
.split_vert(height / character_count)
.into_iter()
.rev(),
)
.map(|(character, image)| {
let mut image = image.flip_vert();
.zip(indices.split_vert(height / character_count).into_iter())
.map(|(character, mut image)| {
image.trim_right();
let image_width = image.width();
let image_area = image.area();
Expand Down

0 comments on commit b43db42

Please sign in to comment.