🎉 Improve frame rendering

This commit is contained in:
AzazelN28 2024-11-20 13:49:45 +01:00
parent 59fdf64c66
commit 2db1740ce8
6 changed files with 59 additions and 31 deletions

View file

@ -2,6 +2,7 @@ pub mod render;
pub mod shapes;
pub mod state;
pub mod utils;
pub mod view;
use skia_safe as skia;
@ -43,9 +44,9 @@ pub unsafe extern "C" fn resize_surface(width: i32, height: i32) {
}
#[no_mangle]
pub unsafe extern "C" fn draw_all_shapes(zoom: f32, pan_x: f32, pan_y: f32) {
pub unsafe extern "C" fn render() {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
state.draw_all_shapes(zoom, pan_x, pan_y);
state.draw_all_shapes(state.view.zoom, state.view.x, state.view.y);
}
#[no_mangle]
@ -54,6 +55,14 @@ pub extern "C" fn reset_canvas() {
state.render_state().reset_canvas();
}
#[no_mangle]
pub extern "C" fn set_view(zoom: f32, x: f32, y: f32) {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
state.view.x = x;
state.view.y = y;
state.view.zoom = zoom;
}
#[no_mangle]
pub extern "C" fn use_shape(a: u32, b: u32, c: u32, d: u32) {
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");

View file

@ -3,6 +3,7 @@ use uuid::Uuid;
use crate::render::RenderState;
use crate::shapes::Shape;
use crate::view::View;
/// This struct holds the state of the Rust application between JS calls.
///
@ -14,6 +15,7 @@ pub(crate) struct State<'a> {
pub current_id: Option<Uuid>,
pub current_shape: Option<&'a mut Shape>,
pub shapes: HashMap<Uuid, Shape>,
pub view: View,
}
impl<'a> State<'a> {
@ -23,6 +25,11 @@ impl<'a> State<'a> {
current_id: None,
current_shape: None,
shapes: HashMap::with_capacity(capacity),
view: View {
x: 0.,
y: 0.,
zoom: 1.,
}
}
}

6
render-wasm/src/view.rs Normal file
View file

@ -0,0 +1,6 @@
pub(crate) struct View
{
pub x: f32,
pub y: f32,
pub zoom: f32,
}