You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.2KB

  1. //use libc;
  2. use std::os::raw::c_char;
  3. use std::ffi::CStr;
  4. use std::convert::From;
  5. use std::time::Instant;
  6. use crate::text_parser::{FilmscriptFile, FILE_HANDLES};
  7. use crate::allocate_cstring;
  8. use crate::instruction::{CameraData, Vector3};
  9. #[repr(C)]
  10. pub struct CameraDataC {
  11. pub rotation: Vector3C,
  12. pub position: Vector3C,
  13. }
  14. impl From<CameraData> for CameraDataC {
  15. fn from(c: CameraData) -> Self {
  16. Self {
  17. rotation: Vector3C::from(c.rotation),
  18. position: Vector3C::from(c.position),
  19. }
  20. }
  21. }
  22. #[repr(C)]
  23. pub struct Vector3C {
  24. pub x: f64,
  25. pub y: f64,
  26. pub z: f64,
  27. }
  28. impl From<Vector3> for Vector3C {
  29. fn from(v: Vector3) -> Self {
  30. Self {
  31. x: v.x,
  32. y: v.y,
  33. z: v.z,
  34. }
  35. }
  36. }
  37. #[no_mangle]
  38. pub unsafe extern "C" fn filmscript_open(filepath: *const c_char) -> i64 {
  39. let c_str = CStr::from_ptr(filepath);
  40. if let Ok(path) = c_str.to_str() {
  41. if let Ok(fsf) = FilmscriptFile::from_path(std::path::Path::new(path)) {
  42. FILE_HANDLES.write().unwrap().push(fsf);
  43. return FILE_HANDLES.read().unwrap().len() as i64;
  44. } else {
  45. println!("Failed to open '{}' in filmscript_open(*char)", path);
  46. }
  47. } else {
  48. println!("Invalid *char parameter in filmscript_open(*char)");
  49. }
  50. return -1;
  51. }
  52. #[no_mangle]
  53. pub unsafe extern "C" fn filmscript_path(handle: i64) -> *const c_char {
  54. if let Some(path) = FILE_HANDLES.read().unwrap()[(handle-1) as usize].filepath().clone() {
  55. if let Some(s_path) = path.to_str() {
  56. return allocate_cstring(s_path);
  57. }
  58. }
  59. allocate_cstring("")
  60. }
  61. #[no_mangle]
  62. pub unsafe extern "C" fn filmscript_is_done(handle: i64) -> bool {
  63. FILE_HANDLES.read().unwrap()[(handle-1) as usize].is_done()
  64. }
  65. #[no_mangle]
  66. pub unsafe extern "C" fn filmscript_done_message(handle: i64) -> *const c_char {
  67. allocate_cstring(&FILE_HANDLES.read().unwrap()[(handle-1) as usize].done_msg())
  68. }
  69. #[no_mangle]
  70. pub unsafe extern "C" fn filmscript_poll(handle: i64) -> CameraDataC {
  71. let now = Instant::now().elapsed().as_secs_f64();
  72. let cdata = FILE_HANDLES.write().unwrap()[(handle-1) as usize].lerp(now);
  73. CameraDataC::from(cdata)
  74. }