Browse Source

port frame begin/end to safe rust

pull/18/head
Ian Chamberlain 3 years ago
parent
commit
e5e2e63586
No known key found for this signature in database
GPG Key ID: AE5484D09405AA60
  1. 1
      citro3d/Cargo.toml
  2. 22
      citro3d/examples/triangle.rs
  3. 19
      citro3d/src/lib.rs

1
citro3d/Cargo.toml

@ -2,7 +2,6 @@
name = "citro3d" name = "citro3d"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
authors = [""]
[dependencies] [dependencies]
bitflags = "1.3.2" bitflags = "1.3.2"

22
citro3d/examples/triangle.rs

@ -44,6 +44,9 @@ const VERTICES: &[Vertex] = &[
}, },
]; ];
static SHADER_BYTES: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/examples/assets/vshader.shbin"));
fn main() { fn main() {
ctru::init(); ctru::init();
@ -78,14 +81,7 @@ fn main() {
break; break;
} }
unsafe { instance.render_frame_with(|instance| {
citro3d_sys::C3D_FrameBegin(
citro3d_sys::C3D_FRAME_SYNCDRAW
.try_into()
.expect("const is valid u8"),
);
}
let clear_color: u32 = 0x7F_7F_7F_FF; let clear_color: u32 = 0x7F_7F_7F_FF;
render_target.clear(ClearFlags::ALL, clear_color, 0); render_target.clear(ClearFlags::ALL, clear_color, 0);
@ -94,22 +90,16 @@ fn main() {
.expect("failed to set render target"); .expect("failed to set render target");
scene_render(uloc_projection.into(), &projection); scene_render(uloc_projection.into(), &projection);
});
unsafe {
citro3d_sys::C3D_FrameEnd(0);
}
} }
scene_exit(vbo_data, program, vshader_dvlb); scene_exit(vbo_data, program, vshader_dvlb);
} }
static SHBIN_BYTES: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/examples/assets/vshader.shbin"));
fn scene_init() -> (shaderProgram_s, i8, C3D_Mtx, *mut libc::c_void, *mut DVLB_s) { fn scene_init() -> (shaderProgram_s, i8, C3D_Mtx, *mut libc::c_void, *mut DVLB_s) {
// Load the vertex shader, create a shader program and bind it // Load the vertex shader, create a shader program and bind it
unsafe { unsafe {
let mut shader_bytes = SHBIN_BYTES.to_owned(); let mut shader_bytes = SHADER_BYTES.to_owned();
// Assume the data is aligned properly... // Assume the data is aligned properly...
let vshader_dvlb = citro3d_sys::DVLB_ParseFile( let vshader_dvlb = citro3d_sys::DVLB_ParseFile(

19
citro3d/src/lib.rs

@ -74,6 +74,25 @@ impl Instance {
Err(Error::InvalidRenderTarget) Err(Error::InvalidRenderTarget)
} }
} }
/// Render a frame. The passed in function/closure can mutate the instance,
/// such as to [select a render target](Self::select_render_target).
pub fn render_frame_with(&mut self, f: impl FnOnce(&mut Self)) {
unsafe {
citro3d_sys::C3D_FrameBegin(
// TODO: begin + end flags should be configurable
citro3d_sys::C3D_FRAME_SYNCDRAW
.try_into()
.expect("const is valid u8"),
);
}
f(self);
unsafe {
citro3d_sys::C3D_FrameEnd(0);
}
}
} }
impl Drop for Instance { impl Drop for Instance {

Loading…
Cancel
Save