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 @@ @@ -2,7 +2,6 @@
name = "citro3d"
version = "0.1.0"
edition = "2021"
authors = [""]
[dependencies]
bitflags = "1.3.2"

22
citro3d/examples/triangle.rs

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

19
citro3d/src/lib.rs

@ -74,6 +74,25 @@ impl Instance { @@ -74,6 +74,25 @@ impl Instance {
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 {

Loading…
Cancel
Save