Browse Source

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.
pull/118/head
Ian Chamberlain 2 years ago
parent
commit
0ef017cf4d
No known key found for this signature in database
GPG Key ID: AE5484D09405AA60
  1. 3
      ctru-rs/examples/gfx-3d-mode.rs
  2. 54
      ctru-rs/src/services/gfx.rs

3
ctru-rs/examples/gfx-3d-mode.rs

@ -62,8 +62,7 @@ fn main() {
buf.copy_from(IMAGE.as_ptr(), IMAGE.len()); buf.copy_from(IMAGE.as_ptr(), IMAGE.len());
} }
drop(left); drop((left, right));
drop(right);
top_screen.flush_buffers(); top_screen.flush_buffers();
top_screen.swap_buffers(); top_screen.swap_buffers();

54
ctru-rs/src/services/gfx.rs

@ -81,20 +81,32 @@ pub struct TopScreen3D<'top_screen> {
} }
pub trait Swap: private::Sealed { pub trait Swap: private::Sealed {
/// Swaps the video buffers.
///
/// This should be used even if double buffering is disabled.
fn swap_buffers(&mut self); fn swap_buffers(&mut self);
} }
trait SwappableScreen: Screen {} impl Swap for TopScreen3D<'_> {
impl SwappableScreen for TopScreen {} fn swap_buffers(&mut self) {
impl SwappableScreen for BottomScreen {} unsafe {
ctru_sys::gfxScreenSwapBuffers(ctru_sys::GFX_TOP, true);
}
}
}
impl<S: SwappableScreen> Swap for S { impl Swap for TopScreen {
/// Swaps the video buffers. fn swap_buffers(&mut self) {
/// unsafe {
/// This should be used even if double buffering is disabled. ctru_sys::gfxScreenSwapBuffers(ctru_sys::GFX_TOP, false);
}
}
}
impl Swap for BottomScreen {
fn swap_buffers(&mut self) { fn swap_buffers(&mut self) {
unsafe { unsafe {
ctru_sys::gfxScreenSwapBuffers(self.side().into(), false); ctru_sys::gfxScreenSwapBuffers(ctru_sys::GFX_BOTTOM, false);
} }
} }
} }
@ -124,23 +136,6 @@ impl<S: FlushableScreen> 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. /// The left side of the top screen, when using 3D mode.
#[derive(Debug)] #[derive(Debug)]
#[non_exhaustive] #[non_exhaustive]
@ -253,6 +248,13 @@ impl TopScreen3D<'_> {
(&mut screen.left as _, &mut screen.right as _) (&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<TopScreen>> for TopScreen3D<'top_screen> { impl<'top_screen> From<&'top_screen RefCell<TopScreen>> for TopScreen3D<'top_screen> {
@ -296,7 +298,7 @@ impl TopScreen {
impl Screen for TopScreen { impl Screen for TopScreen {
fn as_raw(&self) -> ctru_sys::gfxScreen_t { fn as_raw(&self) -> ctru_sys::gfxScreen_t {
self.left.as_raw() ctru_sys::GFX_TOP
} }
fn side(&self) -> Side { fn side(&self) -> Side {

Loading…
Cancel
Save