rust/lib-hwengine-future/src/lib.rs
branchtransitional_engine
changeset 15905 022ec6b916b7
parent 15904 33798b649d9c
child 15912 6e22f4390b7e
--- a/rust/lib-hwengine-future/src/lib.rs	Wed Jan 04 10:42:21 2023 +0100
+++ b/rust/lib-hwengine-future/src/lib.rs	Wed Jan 04 11:56:58 2023 +0100
@@ -10,6 +10,7 @@
 pub struct GameField {
     collision: land2d::Land2D<u16>,
     pixels: land2d::Land2D<u32>,
+    landgen_parameters: Option<LandGenerationParameters<u16>>,
 }
 
 #[no_mangle]
@@ -17,18 +18,24 @@
     game_field: &GameField,
     width: *mut i32,
     height: *mut i32,
+    play_width: *mut i32,
+    play_height: *mut i32,
 ) {
     unsafe {
         *width = game_field.collision.width() as i32;
         *height = game_field.collision.height() as i32;
+
+        *play_width = game_field.collision.play_width() as i32;
+        *play_height = game_field.collision.play_height() as i32;
     }
 }
 
 #[no_mangle]
 pub extern "C" fn create_empty_game_field(width: u32, height: u32) -> *mut GameField {
     let game_field = Box::new(GameField {
-        collision: land2d::Land2D::new(Size::new(width as usize, height as usize), 0),
-        pixels: land2d::Land2D::new(Size::new(width as usize, height as usize), 0),
+        collision: land2d::Land2D::new(&Size::new(width as usize, height as usize), 0),
+        pixels: land2d::Land2D::new(&Size::new(width as usize, height as usize), 0),
+        landgen_parameters: None,
     });
 
     Box::leak(game_field)
@@ -38,14 +45,14 @@
 pub extern "C" fn generate_templated_game_field(
     feature_size: u32,
     seed: *const i8,
+    template_type: *const i8,
     data_path: *const i8,
-    theme_name: *const i8,
 ) -> *mut GameField {
     let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap();
     let data_path = Path::new(&data_path);
 
     let seed: &str = unsafe { CStr::from_ptr(seed) }.to_str().unwrap();
-    let theme_name: &str = unsafe { CStr::from_ptr(theme_name) }.to_str().unwrap();
+    let template_type: &str = unsafe { CStr::from_ptr(template_type) }.to_str().unwrap();
 
     let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes());
 
@@ -58,11 +65,33 @@
     let distance_divisor = feature_size.pow(2) / 8 + 10;
     let params = LandGenerationParameters::new(0u16, 0x8000u16, distance_divisor, false, false);
     let template = map_gen
-        .get_template("medium", &mut random_numbers_gen)
+        .get_template(template_type, &mut random_numbers_gen)
         .expect("Error reading map templates file")
         .clone();
     let landgen = TemplatedLandGenerator::new(template);
     let collision = landgen.generate_land(&params, &mut random_numbers_gen);
+    let size = collision.size().size();
+
+    let game_field = Box::new(GameField {
+        collision,
+        pixels: land2d::Land2D::new(&size, 0),
+        landgen_parameters: Some(params),
+    });
+
+    Box::leak(game_field)
+}
+
+#[no_mangle]
+pub extern "C" fn apply_theme(
+    game_field: &mut GameField,
+    data_path: *const i8,
+    theme_name: *const i8,
+) {
+    let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap();
+    let data_path = Path::new(&data_path);
+
+    let theme_name: &str = unsafe { CStr::from_ptr(theme_name) }.to_str().unwrap();
+    let map_gen = MapGenerator::new();
 
     let theme = Theme::load(
         data_path
@@ -71,14 +100,13 @@
             .as_path(),
     )
     .unwrap();
-    let pixels = map_gen.make_texture(&collision, &params, &theme);
 
-    let game_field = Box::new(GameField {
-        collision,
-        pixels: pixels.into(),
-    });
+    let params = game_field
+        .landgen_parameters
+        .expect("Land generator parameters specified");
+    let pixels = map_gen.make_texture(&game_field.collision, &params, &theme);
 
-    Box::leak(game_field)
+    game_field.pixels = pixels.into();
 }
 
 #[no_mangle]