rust/lib-hwengine-future/src/lib.rs
branchtransitional_engine
changeset 15905 022ec6b916b7
parent 15904 33798b649d9c
child 15912 6e22f4390b7e
equal deleted inserted replaced
15904:33798b649d9c 15905:022ec6b916b7
     8 
     8 
     9 #[repr(C)]
     9 #[repr(C)]
    10 pub struct GameField {
    10 pub struct GameField {
    11     collision: land2d::Land2D<u16>,
    11     collision: land2d::Land2D<u16>,
    12     pixels: land2d::Land2D<u32>,
    12     pixels: land2d::Land2D<u32>,
       
    13     landgen_parameters: Option<LandGenerationParameters<u16>>,
    13 }
    14 }
    14 
    15 
    15 #[no_mangle]
    16 #[no_mangle]
    16 pub extern "C" fn get_game_field_parameters(
    17 pub extern "C" fn get_game_field_parameters(
    17     game_field: &GameField,
    18     game_field: &GameField,
    18     width: *mut i32,
    19     width: *mut i32,
    19     height: *mut i32,
    20     height: *mut i32,
       
    21     play_width: *mut i32,
       
    22     play_height: *mut i32,
    20 ) {
    23 ) {
    21     unsafe {
    24     unsafe {
    22         *width = game_field.collision.width() as i32;
    25         *width = game_field.collision.width() as i32;
    23         *height = game_field.collision.height() as i32;
    26         *height = game_field.collision.height() as i32;
       
    27 
       
    28         *play_width = game_field.collision.play_width() as i32;
       
    29         *play_height = game_field.collision.play_height() as i32;
    24     }
    30     }
    25 }
    31 }
    26 
    32 
    27 #[no_mangle]
    33 #[no_mangle]
    28 pub extern "C" fn create_empty_game_field(width: u32, height: u32) -> *mut GameField {
    34 pub extern "C" fn create_empty_game_field(width: u32, height: u32) -> *mut GameField {
    29     let game_field = Box::new(GameField {
    35     let game_field = Box::new(GameField {
    30         collision: land2d::Land2D::new(Size::new(width as usize, height as usize), 0),
    36         collision: land2d::Land2D::new(&Size::new(width as usize, height as usize), 0),
    31         pixels: land2d::Land2D::new(Size::new(width as usize, height as usize), 0),
    37         pixels: land2d::Land2D::new(&Size::new(width as usize, height as usize), 0),
       
    38         landgen_parameters: None,
    32     });
    39     });
    33 
    40 
    34     Box::leak(game_field)
    41     Box::leak(game_field)
    35 }
    42 }
    36 
    43 
    37 #[no_mangle]
    44 #[no_mangle]
    38 pub extern "C" fn generate_templated_game_field(
    45 pub extern "C" fn generate_templated_game_field(
    39     feature_size: u32,
    46     feature_size: u32,
    40     seed: *const i8,
    47     seed: *const i8,
       
    48     template_type: *const i8,
    41     data_path: *const i8,
    49     data_path: *const i8,
    42     theme_name: *const i8,
       
    43 ) -> *mut GameField {
    50 ) -> *mut GameField {
    44     let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap();
    51     let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap();
    45     let data_path = Path::new(&data_path);
    52     let data_path = Path::new(&data_path);
    46 
    53 
    47     let seed: &str = unsafe { CStr::from_ptr(seed) }.to_str().unwrap();
    54     let seed: &str = unsafe { CStr::from_ptr(seed) }.to_str().unwrap();
    48     let theme_name: &str = unsafe { CStr::from_ptr(theme_name) }.to_str().unwrap();
    55     let template_type: &str = unsafe { CStr::from_ptr(template_type) }.to_str().unwrap();
    49 
    56 
    50     let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes());
    57     let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes());
    51 
    58 
    52     let yaml_templates =
    59     let yaml_templates =
    53         fs::read_to_string(data_path.join(Path::new("map_templates.yaml")).as_path())
    60         fs::read_to_string(data_path.join(Path::new("map_templates.yaml")).as_path())
    56     map_gen.import_yaml_templates(&yaml_templates);
    63     map_gen.import_yaml_templates(&yaml_templates);
    57 
    64 
    58     let distance_divisor = feature_size.pow(2) / 8 + 10;
    65     let distance_divisor = feature_size.pow(2) / 8 + 10;
    59     let params = LandGenerationParameters::new(0u16, 0x8000u16, distance_divisor, false, false);
    66     let params = LandGenerationParameters::new(0u16, 0x8000u16, distance_divisor, false, false);
    60     let template = map_gen
    67     let template = map_gen
    61         .get_template("medium", &mut random_numbers_gen)
    68         .get_template(template_type, &mut random_numbers_gen)
    62         .expect("Error reading map templates file")
    69         .expect("Error reading map templates file")
    63         .clone();
    70         .clone();
    64     let landgen = TemplatedLandGenerator::new(template);
    71     let landgen = TemplatedLandGenerator::new(template);
    65     let collision = landgen.generate_land(&params, &mut random_numbers_gen);
    72     let collision = landgen.generate_land(&params, &mut random_numbers_gen);
       
    73     let size = collision.size().size();
       
    74 
       
    75     let game_field = Box::new(GameField {
       
    76         collision,
       
    77         pixels: land2d::Land2D::new(&size, 0),
       
    78         landgen_parameters: Some(params),
       
    79     });
       
    80 
       
    81     Box::leak(game_field)
       
    82 }
       
    83 
       
    84 #[no_mangle]
       
    85 pub extern "C" fn apply_theme(
       
    86     game_field: &mut GameField,
       
    87     data_path: *const i8,
       
    88     theme_name: *const i8,
       
    89 ) {
       
    90     let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap();
       
    91     let data_path = Path::new(&data_path);
       
    92 
       
    93     let theme_name: &str = unsafe { CStr::from_ptr(theme_name) }.to_str().unwrap();
       
    94     let map_gen = MapGenerator::new();
    66 
    95 
    67     let theme = Theme::load(
    96     let theme = Theme::load(
    68         data_path
    97         data_path
    69             .join(Path::new("Themes"))
    98             .join(Path::new("Themes"))
    70             .join(Path::new(theme_name))
    99             .join(Path::new(theme_name))
    71             .as_path(),
   100             .as_path(),
    72     )
   101     )
    73     .unwrap();
   102     .unwrap();
    74     let pixels = map_gen.make_texture(&collision, &params, &theme);
       
    75 
   103 
    76     let game_field = Box::new(GameField {
   104     let params = game_field
    77         collision,
   105         .landgen_parameters
    78         pixels: pixels.into(),
   106         .expect("Land generator parameters specified");
    79     });
   107     let pixels = map_gen.make_texture(&game_field.collision, &params, &theme);
    80 
   108 
    81     Box::leak(game_field)
   109     game_field.pixels = pixels.into();
    82 }
   110 }
    83 
   111 
    84 #[no_mangle]
   112 #[no_mangle]
    85 pub extern "C" fn land_get(game_field: &mut GameField, x: i32, y: i32) -> u16 {
   113 pub extern "C" fn land_get(game_field: &mut GameField, x: i32, y: i32) -> u16 {
    86     game_field.collision.map(y, x, |p| *p)
   114     game_field.collision.map(y, x, |p| *p)