|
- //use libc;
- use std::os::raw::c_char;
- use std::ffi::CStr;
- use std::convert::From;
- use std::time::Instant;
-
- use crate::text_parser::{FilmscriptFile, FILE_HANDLES};
- use crate::allocate_cstring;
- use crate::instruction::{CameraData, Vector3};
-
- #[repr(C)]
- pub struct CameraDataC {
- pub rotation: Vector3C,
- pub position: Vector3C,
- }
-
- impl From<CameraData> for CameraDataC {
- fn from(c: CameraData) -> Self {
- Self {
- rotation: Vector3C::from(c.rotation),
- position: Vector3C::from(c.position),
- }
- }
- }
-
- #[repr(C)]
- pub struct Vector3C {
- pub x: f64,
- pub y: f64,
- pub z: f64,
- }
-
- impl From<Vector3> for Vector3C {
- fn from(v: Vector3) -> Self {
- Self {
- x: v.x,
- y: v.y,
- z: v.z,
- }
- }
- }
-
- #[no_mangle]
- pub unsafe extern "C" fn filmscript_open(filepath: *const c_char) -> i64 {
- let c_str = CStr::from_ptr(filepath);
- if let Ok(path) = c_str.to_str() {
- if let Ok(fsf) = FilmscriptFile::from_path(std::path::Path::new(path)) {
- FILE_HANDLES.write().unwrap().push(fsf);
- return FILE_HANDLES.read().unwrap().len() as i64;
- } else {
- println!("Failed to open '{}' in filmscript_open(*char)", path);
- }
- } else {
- println!("Invalid *char parameter in filmscript_open(*char)");
- }
- return -1;
- }
-
- #[no_mangle]
- pub unsafe extern "C" fn filmscript_path(handle: i64) -> *const c_char {
- if let Some(path) = FILE_HANDLES.read().unwrap()[(handle-1) as usize].filepath().clone() {
- if let Some(s_path) = path.to_str() {
- return allocate_cstring(s_path);
- }
- }
- allocate_cstring("")
- }
-
- #[no_mangle]
- pub unsafe extern "C" fn filmscript_is_done(handle: i64) -> bool {
- FILE_HANDLES.read().unwrap()[(handle-1) as usize].is_done()
- }
-
- #[no_mangle]
- pub unsafe extern "C" fn filmscript_done_message(handle: i64) -> *const c_char {
- allocate_cstring(&FILE_HANDLES.read().unwrap()[(handle-1) as usize].done_msg())
- }
-
- #[no_mangle]
- pub unsafe extern "C" fn filmscript_poll(handle: i64) -> CameraDataC {
- let now = Instant::now().elapsed().as_secs_f64();
- let cdata = FILE_HANDLES.write().unwrap()[(handle-1) as usize].lerp(now);
- CameraDataC::from(cdata)
- }
|