-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffi_lib.rs
79 lines (68 loc) · 1.77 KB
/
ffi_lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::cart_pole::*;
use piston_window::{PistonWindow, WindowSettings};
use plotters::prelude::*;
// #[no_mangle]
// pub extern fn callable_from_c(x: i32) -> i32 {
// x * 2 + 1
// }
#[no_mangle]
pub extern "C" fn new() -> *mut CartPole {
let t = CartPole::new();
Box::into_raw(Box::new(t))
}
#[no_mangle]
pub extern "C" fn free(ptr: *mut CartPole) {
if ptr.is_null() {
return;
}
unsafe {
Box::from_raw(ptr);
}
}
#[no_mangle]
pub extern "C" fn reset(ptr: *mut CartPole) {
assert!(!ptr.is_null());
let cp = unsafe { &mut *ptr };
cp.reset();
}
#[no_mangle]
pub extern "C" fn status(ptr: *const CartPole) {
assert!(!ptr.is_null());
let cp = unsafe { &*ptr };
println!("rusted_cart_pole status(): {}", cp);
}
#[no_mangle]
pub extern "C" fn step(ptr: *mut CartPole, force: f32) -> i32 {
assert!(!ptr.is_null());
let cp = unsafe { &mut *ptr };
let reward = cp.step(force);
reward
}
#[no_mangle]
pub extern "C" fn window_new() -> *mut PistonWindow {
let window: PistonWindow = WindowSettings::new("RustedCartPole", (800, 400))
.exit_on_esc(true)
// .controllers(false) // should they listen controller input
.samples(1)
.build()
.unwrap_or_else(|e| panic!("Failed to build PistonWindow: {}", e));
Box::into_raw(Box::new(window))
}
#[no_mangle]
pub extern "C" fn window_free(window: *mut PistonWindow) {
if window.is_null() {
return;
}
unsafe {
Box::from_raw(window);
}
}
#[no_mangle]
pub extern "C" fn window_draw(window: *mut PistonWindow, cp: *const CartPole) {
let window = unsafe { &mut *window };
let cp = unsafe { &*cp };
draw_piston_window(window, |b| {
cp.draw(b.into_drawing_area())?;
Ok(())
});
}