Archived
1
0
Fork 0
This repository has been archived on 2026-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
Danmaku/ffi-wrapper/src/ffi.rs

83 lines
No EOL
1.9 KiB
Rust

use shared::bullet::Bullet;
use shared::collision::Circle;
// Bullet routines
#[no_mangle]
pub extern "C" fn new_bullet(
class: u8, spawn_time: i64, spawn_x: f64, spawn_y: f64, radius: f64, param_x: f64, param_y: f64
) -> *mut Bullet {
let bullet = Bullet::new(
class,
spawn_time,
spawn_x,
spawn_y,
radius,
[param_x, param_y],
);
Box::into_raw(Box::new(bullet))
}
#[no_mangle]
pub extern "C" fn bullet_get_current_pos(bullet: *const Bullet, tick: i64, x: *mut f64, y: *mut f64) {
if let Some(b) = unsafe { bullet.as_ref() } {
let (bx, by) = b.get_current_pos(tick);
unsafe {
*x = bx;
*y = by;
}
}
}
#[no_mangle]
pub extern "C" fn bullet_beyond_kill_boundary(bullet: *const Bullet, tick: i64) -> bool {
if let Some(b) = unsafe { bullet.as_ref() } {
b.beyond_kill_boundary(tick)
} else {
false
}
}
#[no_mangle]
pub extern "C" fn destroy_bullet(bullet: *mut Bullet) {
if !bullet.is_null() {
unsafe {
drop(Box::from_raw(bullet));
}
}
}
#[no_mangle]
pub extern "C" fn bullet_collides_with(bullet: *mut Bullet, tick: i64, c: *const Circle) -> bool {
if bullet.is_null() || c.is_null() {
return false;
}
unsafe { (*bullet).collides_with(tick, &*c) }
}
// Collision
#[no_mangle]
pub extern "C" fn new_circle(x: f64, y: f64, radius: f64) -> *mut Circle {
let circle = Circle::new(x, y, radius);
Box::into_raw(Box::new(circle))
}
#[no_mangle]
pub extern "C" fn destroy_circle(circle: *mut Circle) {
if !circle.is_null() {
unsafe {
drop(Box::from_raw(circle));
}
}
}
#[no_mangle]
pub extern "C" fn circle_collides_with(c1: *const Circle, c2: *const Circle) -> bool {
if c1.is_null() || c2.is_null() {
return false;
}
unsafe { (*c1).collides_with(&*c2) }
}