Skip to content

Commit

Permalink
Fixed Image writing out upside down
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Mar 10, 2020
1 parent dd9f644 commit e7b1d29
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 11 additions & 3 deletions src/graphics/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,18 @@ impl Image {
let reader = gfx.factory.read_mapping(&dl_buffer)?;

// intermediary buffer to avoid casting.
// Apparently this also at one point made the screenshot upside-down,
// but no longer?
let mut data = Vec::with_capacity(self.width as usize * self.height as usize * 4);
data.extend(reader.into_iter().flatten());
// Assuming OpenGL backend whose typical readback option (glReadPixels) has origin at bottom left.
// Image formats on the other hand usually deal with top right.
for y in (0..self.height as usize).rev() {
data.extend(
reader
.iter()
.skip(y * self.width as usize)
.take(self.width as usize)
.flatten(),
);
}
Ok(data)
}

Expand Down
4 changes: 0 additions & 4 deletions src/graphics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,6 @@ pub fn present(ctx: &mut Context) -> GameResult<()> {
/// Take a screenshot by outputting the current render surface
/// (screen or selected canvas) to an `Image`.
pub fn screenshot(ctx: &mut Context) -> GameResult<Image> {
// TODO LATER: This makes the screenshot upside-down form some reason...
// Probably because all our images are upside down, for coordinate reasons!
// How can we fix it?
use gfx::memory::Bind;
let debug_id = DebugId::get(ctx);

Expand Down Expand Up @@ -1059,5 +1056,4 @@ mod tests {
assert_relative_eq!(real, expected);
}
}

}

0 comments on commit e7b1d29

Please sign in to comment.