rust/lib-hedgewars-engine/src/lib.rs
changeset 14276 1aac8a62be6f
parent 14260 a239e4243cf9
child 14277 3152d9fdb499
--- a/rust/lib-hedgewars-engine/src/lib.rs	Thu Nov 22 00:49:55 2018 +0300
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Thu Nov 22 10:58:55 2018 +0100
@@ -1,23 +1,32 @@
+mod ipc;
 mod world;
-mod ipc;
+
+use std::io::{Read, Write};
+
+use self::ipc::IPC;
 
 #[repr(C)]
 pub struct EngineInstance {
     world: world::World,
+    ipc: IPC,
 }
 
 impl EngineInstance {
     pub fn new() -> Self {
         let world = world::World::new();
-        Self { world }
+        Self {
+            world,
+            ipc: IPC::new(),
+        }
     }
 
     pub fn render<R, C>(
         &self,
         context: &mut gfx::Encoder<R, C>,
-        target: &gfx::handle::RenderTargetView<R, gfx::format::Rgba8>)
-        where R: gfx::Resources,
-              C: gfx::CommandBuffer<R>
+        target: &gfx::handle::RenderTargetView<R, gfx::format::Rgba8>,
+    ) where
+        R: gfx::Resources,
+        C: gfx::CommandBuffer<R>,
     {
         context.clear(target, [0.0, 0.5, 0.0, 1.0]);
     }
@@ -39,9 +48,7 @@
 
 #[no_mangle]
 pub extern "C" fn start_engine() -> *mut EngineInstance {
-    let engine_state = Box::new(EngineInstance {
-        world: world::World::new(),
-    });
+    let engine_state = Box::new(EngineInstance::new());
 
     Box::leak(engine_state)
 }
@@ -61,6 +68,22 @@
 }
 
 #[no_mangle]
+pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const [u8], size: usize) {
+    unsafe {
+        (*engine_state).ipc.write(&(*buf)[0..size]);
+    }
+}
+
+#[no_mangle]
+pub extern "C" fn read_ipc(
+    engine_state: &mut EngineInstance,
+    buf: *mut [u8],
+    size: usize,
+) -> usize {
+    unsafe { (*engine_state).ipc.read(&mut (*buf)[0..size]).unwrap_or(0) }
+}
+
+#[no_mangle]
 pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
     unsafe {
         Box::from_raw(engine_state);