use crate::client::marker::GuiMarker;
use crate::{Error, PhysicsClient};
use rubullet_sys as ffi;
pub enum ServerMode {
GraphicsMainThread {
tcp_port: Option<u16>,
},
Graphics {
tcp_port: Option<u16>,
},
}
pub struct PhysicsServer(pub(crate) PhysicsClient);
impl PhysicsServer {
pub fn new(mode: ServerMode) -> Result<PhysicsServer, Error> {
let (raw_handle, _gui_marker) = match mode {
ServerMode::GraphicsMainThread { tcp_port } => {
let tcp_port = tcp_port.unwrap_or(6667);
let gui_marker = GuiMarker::acquire()?;
unsafe {
let raw_handle =
ffi::b3CreateInProcessGraphicsServerAndConnectMainThreadSharedMemory(
tcp_port as i32,
);
(raw_handle, Some(gui_marker))
}
}
ServerMode::Graphics { tcp_port } => {
let tcp_port = tcp_port.unwrap_or(6667);
let gui_marker = GuiMarker::acquire()?;
let raw_handle = if cfg!(target_os = "macos") {
unsafe {
ffi::b3CreateInProcessGraphicsServerAndConnectMainThreadSharedMemory(
tcp_port as i32,
)
}
} else {
unsafe {
ffi::b3CreateInProcessGraphicsServerAndConnectSharedMemory(tcp_port as i32)
}
};
(raw_handle, Some(gui_marker))
}
};
let handle = raw_handle.expect("Bullet returned a null pointer");
let mut client = PhysicsClient {
handle,
_gui_marker,
_shared_memory_marker: None,
};
if !client.can_submit_command() {
return Err(Error::new("Physics server is not running"));
}
Ok(PhysicsServer(client))
}
pub fn is_connected(&mut self) -> bool {
self.0.is_connected()
}
}