nova_r8/
actor.rs

1/* SPDX-License-Identifier: GPL-3.0-only */
2
3use std::ffi::CString;
4use crate::Vec2;
5use crate::Material;
6
7use super::Actor;
8
9#[link(name = "nova-r8")]
10extern "C" {
11    fn New_Actor() -> *mut libc::c_void;
12    fn Free_Actor(a: *mut::libc::c_void);
13    fn Actor_Init(a: *mut libc::c_void, id: *const i8) -> i32;
14    fn Actor_Translate(a: *mut libc::c_void, x: f32, y: f32);
15    fn Actor_GetPosition(a: *mut libc::c_void) -> Vec2;
16    fn Actor_GetSize(a: *mut libc::c_void) -> Vec2;
17    fn Actor_SetPosition(a: *mut libc::c_void, x: f32, y: f32);
18    fn Actor_Rotate(a: *mut libc::c_void, angle: f32);
19    fn Actor_PlayAnimationClip(a: *mut libc::c_void, id: *const i8, mirrored: i32);
20    fn Actor_SetSprite(a: *mut libc::c_void, material: *mut libc::c_void, width: f32, height: f32);
21}
22
23impl Actor {
24    pub fn _new(actor: *mut libc::c_void) -> Actor {
25        Actor {
26            actor: actor,
27            shared: true,
28        }
29    }
30
31    pub fn new(id: &str) -> Actor {
32        let actor = unsafe { New_Actor() };
33        let c_str = CString::new(id).expect("CString::new failed");
34        unsafe {
35            Actor_Init(actor, c_str.as_ptr());
36        }
37
38        Actor {
39            actor: actor,
40            shared: false,
41        }
42    }
43
44    pub fn translate(&self, x: f32, y: f32) {
45        unsafe {
46            Actor_Translate(self.actor, x, y);
47        }
48    }
49
50    pub fn get_position(&self) -> Vec2 {
51        unsafe {
52            return Actor_GetPosition(self.actor);
53        }
54    }
55
56    pub fn get_size(&self) -> Vec2 {
57        unsafe {
58            return Actor_GetSize(self.actor);
59        }
60    }
61
62    pub fn set_position(&self, x: f32, y: f32) {
63        unsafe {
64            Actor_SetPosition(self.actor, x, y);
65        }
66    }
67
68    pub fn rotate(&self, angle: f32) {
69        unsafe {
70            Actor_Rotate(self.actor, angle);
71        }
72    }
73
74    pub fn set_sprite(&self, material: Material, width: f32, height: f32) {
75        unsafe {
76            Actor_SetSprite(self.actor, material.material, width, height)
77        }
78    }
79
80    pub fn play_animation_clip(&self, id: &str, mirrored: bool) {
81        unsafe {
82            let c_str = CString::new(id).expect("CString::new failed");
83            Actor_PlayAnimationClip(self.actor, c_str.as_ptr(), mirrored as std::os::raw::c_int)
84        }
85    }
86}
87
88impl Drop for Actor {
89    fn drop(&mut self) {
90        if !self.shared {
91            unsafe {
92                Free_Actor(self.actor);
93            }
94        }
95    }
96}