- Move hedgewars-engine into lib-hedgewars-engine
authorunc0rr
Wed, 07 Nov 2018 22:04:17 +0100
changeset 14187 7d3b94cdc3c4
parent 14186 8d9e3af8abce
child 14188 48b9da0178eb
- Move hedgewars-engine into lib-hedgewars-engine - Add command-line hedgewars-engine utility to use engine as standalone app
rust/hedgewars-engine/Cargo.toml
rust/hedgewars-engine/src/lib.rs
rust/hedgewars-engine/src/main.rs
rust/hedgewars-engine/src/world.rs
rust/lib-hedgewars-engine/Cargo.toml
rust/lib-hedgewars-engine/src/lib.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/rust/hedgewars-engine/Cargo.toml	Wed Nov 07 15:18:09 2018 -0500
+++ b/rust/hedgewars-engine/Cargo.toml	Wed Nov 07 22:04:17 2018 +0100
@@ -2,13 +2,9 @@
 name = "hedgewars-engine"
 version = "0.1.0"
 authors = ["Andrey Korotaev <a.korotaev@hedgewars.org>"]
+edition = "2018"
 
 [dependencies]
-land2d = { path = "../land2d" }
-lfprng = { path = "../lfprng" }
-integral-geometry = { path = "../integral-geometry" }
-landgen = { path = "../landgen" }
+lib-hedgewars-engine = { path = "../lib-hedgewars-engine" }
+libloading = "0.5.0"
 
-[lib]
-name = "hedgewars_engine"
-crate-type = ["dylib"]
--- a/rust/hedgewars-engine/src/lib.rs	Wed Nov 07 15:18:09 2018 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-extern crate integral_geometry;
-extern crate land2d;
-extern crate landgen;
-extern crate lfprng;
-
-mod world;
-
-#[repr(C)]
-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 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);
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/hedgewars-engine/src/main.rs	Wed Nov 07 22:04:17 2018 +0100
@@ -0,0 +1,26 @@
+extern crate libloading;
+
+use libloading::{Library, Symbol};
+use std::ops::Deref;
+
+struct EngineInstance {}
+
+struct Engine<'a> {
+    protocol_version: Symbol<'a, unsafe fn() -> u32>,
+    start_engine: Symbol<'a, unsafe fn() -> *mut EngineInstance>,
+    cleanup: Symbol<'a, unsafe fn(engine_state: *mut EngineInstance)>,
+}
+
+fn main() {
+    let hwlib = Library::new("libhedgewars_engine.so").unwrap();
+
+    unsafe {
+        let engine = Engine {
+            protocol_version: hwlib.get(b"protocol_version").unwrap(),
+            start_engine: hwlib.get(b"start_engine").unwrap(),
+            cleanup: hwlib.get(b"cleanup").unwrap(),
+        };
+
+        println!("Hedgewars engine, protocol version {}", engine.protocol_version.deref()());
+    }
+}
--- a/rust/hedgewars-engine/src/world.rs	Wed Nov 07 15:18:09 2018 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,44 +0,0 @@
-use integral_geometry::{Point, Rect, Size};
-use land2d::Land2D;
-use landgen::{
-    outline_template::OutlineTemplate, template_based::TemplatedLandGenerator,
-    LandGenerationParameters, LandGenerator,
-};
-use lfprng::LaggedFibonacciPRNG;
-
-pub struct World {
-    random_numbers_gen: LaggedFibonacciPRNG,
-    preview: Land2D<u8>,
-}
-
-impl World {
-    pub fn new() -> Self {
-        Self {
-            random_numbers_gen: LaggedFibonacciPRNG::new(&[]),
-            preview: Land2D::new(Size::new(0, 0), 0),
-        }
-    }
-
-    pub fn preview(&self) -> &Land2D<u8> {
-        &self.preview
-    }
-
-    pub fn generate_preview(&mut self) {
-        fn template() -> OutlineTemplate {
-            let mut template = OutlineTemplate::new(Size::new(4096, 2048));
-            template.islands = vec![vec![
-                Rect::from_size_coords(100, 2050, 1, 1),
-                Rect::from_size_coords(100, 500, 400, 1200),
-                Rect::from_size_coords(3600, 500, 400, 1200),
-                Rect::from_size_coords(3900, 2050, 1, 1),
-            ]];
-            template.fill_points = vec![Point::new(1, 0)];
-
-            template
-        }
-
-        let params = LandGenerationParameters::new(0 as u8, 255, 5, false, false);
-        let landgen = TemplatedLandGenerator::new(template());
-        self.preview = landgen.generate_land(&params, &mut self.random_numbers_gen);
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/lib-hedgewars-engine/Cargo.toml	Wed Nov 07 22:04:17 2018 +0100
@@ -0,0 +1,14 @@
+[package]
+name = "lib-hedgewars-engine"
+version = "0.1.0"
+authors = ["Andrey Korotaev <a.korotaev@hedgewars.org>"]
+
+[dependencies]
+land2d = { path = "../land2d" }
+lfprng = { path = "../lfprng" }
+integral-geometry = { path = "../integral-geometry" }
+landgen = { path = "../landgen" }
+
+[lib]
+name = "hedgewars_engine"
+crate-type = ["dylib"]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Wed Nov 07 22:04:17 2018 +0100
@@ -0,0 +1,55 @@
+extern crate integral_geometry;
+extern crate land2d;
+extern crate landgen;
+extern crate lfprng;
+
+mod world;
+
+#[repr(C)]
+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 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);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/lib-hedgewars-engine/src/world.rs	Wed Nov 07 22:04:17 2018 +0100
@@ -0,0 +1,44 @@
+use integral_geometry::{Point, Rect, Size};
+use land2d::Land2D;
+use landgen::{
+    outline_template::OutlineTemplate, template_based::TemplatedLandGenerator,
+    LandGenerationParameters, LandGenerator,
+};
+use lfprng::LaggedFibonacciPRNG;
+
+pub struct World {
+    random_numbers_gen: LaggedFibonacciPRNG,
+    preview: Land2D<u8>,
+}
+
+impl World {
+    pub fn new() -> Self {
+        Self {
+            random_numbers_gen: LaggedFibonacciPRNG::new(&[]),
+            preview: Land2D::new(Size::new(0, 0), 0),
+        }
+    }
+
+    pub fn preview(&self) -> &Land2D<u8> {
+        &self.preview
+    }
+
+    pub fn generate_preview(&mut self) {
+        fn template() -> OutlineTemplate {
+            let mut template = OutlineTemplate::new(Size::new(4096, 2048));
+            template.islands = vec![vec![
+                Rect::from_size_coords(100, 2050, 1, 1),
+                Rect::from_size_coords(100, 500, 400, 1200),
+                Rect::from_size_coords(3600, 500, 400, 1200),
+                Rect::from_size_coords(3900, 2050, 1, 1),
+            ]];
+            template.fill_points = vec![Point::new(1, 0)];
+
+            template
+        }
+
+        let params = LandGenerationParameters::new(0 as u8, 255, 5, false, false);
+        let landgen = TemplatedLandGenerator::new(template());
+        self.preview = landgen.generate_land(&params, &mut self.random_numbers_gen);
+    }
+}