From 0ef017cf4d312441160bb43b0ec14f6305fad550 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Wed, 12 Apr 2023 11:16:23 -0400 Subject: [PATCH] Do some cleanup and fix swap trait impls Oops! The argument to `gfxScreenSwapBuffers` is gfxScreen_t, not gfx3dSide_t ! Unfortunately they are both alias to libc::c_uint so we didn't get any help from the Rust compiler when passing the screen side into this function. --- ctru-rs/examples/gfx-3d-mode.rs | 3 +- ctru-rs/src/services/gfx.rs | 54 +++++++++++++++++---------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/ctru-rs/examples/gfx-3d-mode.rs b/ctru-rs/examples/gfx-3d-mode.rs index 596630e..582994e 100644 --- a/ctru-rs/examples/gfx-3d-mode.rs +++ b/ctru-rs/examples/gfx-3d-mode.rs @@ -62,8 +62,7 @@ fn main() { buf.copy_from(IMAGE.as_ptr(), IMAGE.len()); } - drop(left); - drop(right); + drop((left, right)); top_screen.flush_buffers(); top_screen.swap_buffers(); diff --git a/ctru-rs/src/services/gfx.rs b/ctru-rs/src/services/gfx.rs index 65c23c2..edc1607 100644 --- a/ctru-rs/src/services/gfx.rs +++ b/ctru-rs/src/services/gfx.rs @@ -81,20 +81,32 @@ pub struct TopScreen3D<'top_screen> { } pub trait Swap: private::Sealed { + /// Swaps the video buffers. + /// + /// This should be used even if double buffering is disabled. fn swap_buffers(&mut self); } -trait SwappableScreen: Screen {} -impl SwappableScreen for TopScreen {} -impl SwappableScreen for BottomScreen {} +impl Swap for TopScreen3D<'_> { + fn swap_buffers(&mut self) { + unsafe { + ctru_sys::gfxScreenSwapBuffers(ctru_sys::GFX_TOP, true); + } + } +} -impl Swap for S { - /// Swaps the video buffers. - /// - /// This should be used even if double buffering is disabled. +impl Swap for TopScreen { + fn swap_buffers(&mut self) { + unsafe { + ctru_sys::gfxScreenSwapBuffers(ctru_sys::GFX_TOP, false); + } + } +} + +impl Swap for BottomScreen { fn swap_buffers(&mut self) { unsafe { - ctru_sys::gfxScreenSwapBuffers(self.side().into(), false); + ctru_sys::gfxScreenSwapBuffers(ctru_sys::GFX_BOTTOM, false); } } } @@ -124,23 +136,6 @@ impl Flush for S { } } -impl Swap for TopScreen3D<'_> { - fn swap_buffers(&mut self) { - unsafe { - ctru_sys::gfxScreenSwapBuffers(Side::Left.into(), true); - } - } -} - -impl TopScreen3D<'_> { - /// Flush the buffers for both the left and right sides of the screen. - pub fn flush_buffers(&mut self) { - let (mut left, mut right) = self.split_mut(); - left.flush_buffer(); - right.flush_buffer(); - } -} - /// The left side of the top screen, when using 3D mode. #[derive(Debug)] #[non_exhaustive] @@ -253,6 +248,13 @@ impl TopScreen3D<'_> { (&mut screen.left as _, &mut screen.right as _) }) } + + /// Convenient helper to flush the buffers for both the left and right sides of the screen. + pub fn flush_buffers(&mut self) { + let (mut left, mut right) = self.split_mut(); + left.flush_buffer(); + right.flush_buffer(); + } } impl<'top_screen> From<&'top_screen RefCell> for TopScreen3D<'top_screen> { @@ -296,7 +298,7 @@ impl TopScreen { impl Screen for TopScreen { fn as_raw(&self) -> ctru_sys::gfxScreen_t { - self.left.as_raw() + ctru_sys::GFX_TOP } fn side(&self) -> Side {