Make hedgewars-engine crate produce shared library, implement some basics
authorunC0Rr
Wed, 07 Nov 2018 15:55:32 +0100
changeset 14153 b273b43b16d2
parent 14152 5acfdf49742d
child 14154 8354b390f1a2
Make hedgewars-engine crate produce shared library, implement some basics
rust/hedgewars-engine/Cargo.toml
rust/hedgewars-engine/src/lib.rs
--- a/rust/hedgewars-engine/Cargo.toml	Wed Nov 07 15:55:00 2018 +0100
+++ b/rust/hedgewars-engine/Cargo.toml	Wed Nov 07 15:55:32 2018 +0100
@@ -5,3 +5,10 @@
 
 [dependencies]
 land2d = { path = "../land2d" }
+lfprng = { path = "../lfprng" }
+integral-geometry = { path = "../integral-geometry" }
+landgen = { path = "../landgen" }
+
+[lib]
+name = "hedgewars_engine"
+crate-type = ["dylib"]
--- a/rust/hedgewars-engine/src/lib.rs	Wed Nov 07 15:55:00 2018 +0100
+++ b/rust/hedgewars-engine/src/lib.rs	Wed Nov 07 15:55:32 2018 +0100
@@ -1,18 +1,55 @@
+extern crate integral_geometry;
+extern crate land2d;
+extern crate landgen;
+extern crate lfprng;
+
+mod world;
+
 #[repr(C)]
-pub struct Preview {
+pub struct EngineInstance {
+    world: world::World,
+}
+
+#[repr(C)]
+#[derive(Copy, Clone)]
+pub struct PreviewInfo {
     width: u32,
     height: u32,
     hedgehogs_number: u8,
     land: *const u8,
 }
 
-
 #[no_mangle]
 pub extern "C" fn protocol_version() -> u32 {
     56
 }
 
 #[no_mangle]
-pub extern "C" fn generate_preview () -> Preview  {
-    unimplemented!()
+pub extern "C" fn start_engine() -> *mut EngineInstance {
+    let engine_state = Box::new(EngineInstance {
+        world: world::World::new(),
+    });
+
+    Box::leak(engine_state)
 }
+
+#[no_mangle]
+pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) {
+    (*engine_state).world.generate_preview();
+
+    let land_preview = (*engine_state).world.preview();
+
+    *preview = PreviewInfo {
+        width: land_preview.width() as u32,
+        height: land_preview.height() as u32,
+        hedgehogs_number: 0,
+        land: land_preview.raw_pixels().as_ptr(),
+    };
+}
+
+#[no_mangle]
+pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
+    unsafe {
+        Box::from_raw(engine_state);
+    }
+}