author | unC0Rr |
Wed, 04 Jan 2023 10:42:21 +0100 | |
branch | transitional_engine |
changeset 15904 | 33798b649d9c |
parent 15901 | f39f0f614dbf |
child 15905 | 022ec6b916b7 |
permissions | -rw-r--r-- |
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
1 |
use integral_geometry::{Point, Size}; |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
2 |
use land2d; |
15904 | 3 |
use landgen::{template_based::TemplatedLandGenerator, LandGenerationParameters, LandGenerator}; |
4 |
use lfprng::LaggedFibonacciPRNG; |
|
5 |
use mapgen::{theme::Theme, MapGenerator}; |
|
6 |
use std::fs; |
|
7 |
use std::{ffi::CStr, path::Path}; |
|
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
8 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
9 |
#[repr(C)] |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
10 |
pub struct GameField { |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
11 |
collision: land2d::Land2D<u16>, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
12 |
pixels: land2d::Land2D<u32>, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
13 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
14 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
15 |
#[no_mangle] |
15904 | 16 |
pub extern "C" fn get_game_field_parameters( |
17 |
game_field: &GameField, |
|
18 |
width: *mut i32, |
|
19 |
height: *mut i32, |
|
20 |
) { |
|
21 |
unsafe { |
|
22 |
*width = game_field.collision.width() as i32; |
|
23 |
*height = game_field.collision.height() as i32; |
|
24 |
} |
|
25 |
} |
|
26 |
||
27 |
#[no_mangle] |
|
28 |
pub extern "C" fn create_empty_game_field(width: u32, height: u32) -> *mut GameField { |
|
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
29 |
let game_field = Box::new(GameField { |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
30 |
collision: land2d::Land2D::new(Size::new(width as usize, height as usize), 0), |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
31 |
pixels: land2d::Land2D::new(Size::new(width as usize, height as usize), 0), |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
32 |
}); |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
33 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
34 |
Box::leak(game_field) |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
35 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
36 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
37 |
#[no_mangle] |
15904 | 38 |
pub extern "C" fn generate_templated_game_field( |
39 |
feature_size: u32, |
|
40 |
seed: *const i8, |
|
41 |
data_path: *const i8, |
|
42 |
theme_name: *const i8, |
|
43 |
) -> *mut GameField { |
|
44 |
let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap(); |
|
45 |
let data_path = Path::new(&data_path); |
|
46 |
||
47 |
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(); |
|
49 |
||
50 |
let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes()); |
|
51 |
||
52 |
let yaml_templates = |
|
53 |
fs::read_to_string(data_path.join(Path::new("map_templates.yaml")).as_path()) |
|
54 |
.expect("Error reading map templates file"); |
|
55 |
let mut map_gen = MapGenerator::new(); |
|
56 |
map_gen.import_yaml_templates(&yaml_templates); |
|
57 |
||
58 |
let distance_divisor = feature_size.pow(2) / 8 + 10; |
|
59 |
let params = LandGenerationParameters::new(0u16, 0x8000u16, distance_divisor, false, false); |
|
60 |
let template = map_gen |
|
61 |
.get_template("medium", &mut random_numbers_gen) |
|
62 |
.expect("Error reading map templates file") |
|
63 |
.clone(); |
|
64 |
let landgen = TemplatedLandGenerator::new(template); |
|
65 |
let collision = landgen.generate_land(¶ms, &mut random_numbers_gen); |
|
66 |
||
67 |
let theme = Theme::load( |
|
68 |
data_path |
|
69 |
.join(Path::new("Themes")) |
|
70 |
.join(Path::new(theme_name)) |
|
71 |
.as_path(), |
|
72 |
) |
|
73 |
.unwrap(); |
|
74 |
let pixels = map_gen.make_texture(&collision, ¶ms, &theme); |
|
75 |
||
76 |
let game_field = Box::new(GameField { |
|
77 |
collision, |
|
78 |
pixels: pixels.into(), |
|
79 |
}); |
|
80 |
||
81 |
Box::leak(game_field) |
|
82 |
} |
|
83 |
||
84 |
#[no_mangle] |
|
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
85 |
pub extern "C" fn land_get(game_field: &mut GameField, x: i32, y: i32) -> u16 { |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
86 |
game_field.collision.map(y, x, |p| *p) |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
87 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
88 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
89 |
#[no_mangle] |
15901
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
90 |
pub extern "C" fn land_set(game_field: &mut GameField, x: i32, y: i32, value: u16) { |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
91 |
game_field.collision.map(y, x, |p| *p = value); |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
92 |
} |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
93 |
|
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
94 |
#[no_mangle] |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
95 |
pub extern "C" fn land_row(game_field: &mut GameField, row: i32) -> *mut u16 { |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
96 |
game_field.collision[row as usize].as_mut_ptr() |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
97 |
} |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
98 |
|
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
99 |
#[no_mangle] |
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
100 |
pub extern "C" fn land_fill( |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
101 |
game_field: &mut GameField, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
102 |
x: i32, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
103 |
y: i32, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
104 |
border_value: u16, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
105 |
fill_value: u16, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
106 |
) { |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
107 |
game_field |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
108 |
.collision |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
109 |
.fill(Point::new(x, y), border_value, fill_value) |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
110 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
111 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
112 |
#[no_mangle] |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
113 |
pub extern "C" fn land_pixel_get(game_field: &mut GameField, x: i32, y: i32) -> u32 { |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
114 |
game_field.pixels.map(y, x, |p| *p) |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
115 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
116 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
117 |
#[no_mangle] |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
118 |
pub extern "C" fn land_pixel_set(game_field: &mut GameField, x: i32, y: i32, value: u32) { |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
119 |
game_field.pixels.map(y, x, |p| *p = value); |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
120 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
121 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
122 |
#[no_mangle] |
15901
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
123 |
pub extern "C" fn land_pixel_row(game_field: &mut GameField, row: i32) -> *mut u32 { |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
124 |
game_field.pixels[row as usize].as_mut_ptr() |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
125 |
} |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
126 |
|
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
127 |
#[no_mangle] |
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
128 |
pub extern "C" fn dispose_game_field(game_field: *mut GameField) { |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
129 |
unsafe { drop(Box::from_raw(game_field)) }; |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
130 |
} |