author | unC0Rr |
Mon, 13 Feb 2023 12:31:30 +0100 | |
branch | transitional_engine |
changeset 15922 | da6b67f13c12 |
parent 15921 | 5f00829c55ec |
child 15923 | d46ad15c6dec |
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; |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
3 |
use landgen::{ |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
4 |
outline_template_based::{ |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
5 |
outline_template::OutlineTemplate, template_based::TemplatedLandGenerator, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
6 |
}, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
7 |
LandGenerationParameters, LandGenerator, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
8 |
}; |
15904 | 9 |
use lfprng::LaggedFibonacciPRNG; |
10 |
use mapgen::{theme::Theme, MapGenerator}; |
|
11 |
use std::fs; |
|
12 |
use std::{ffi::CStr, path::Path}; |
|
15900
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 |
#[repr(C)] |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
15 |
pub struct GameField { |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
16 |
collision: land2d::Land2D<u16>, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
17 |
pixels: land2d::Land2D<u32>, |
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
18 |
landgen_parameters: Option<LandGenerationParameters<u16>>, |
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
19 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
20 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
21 |
#[no_mangle] |
15904 | 22 |
pub extern "C" fn get_game_field_parameters( |
23 |
game_field: &GameField, |
|
24 |
width: *mut i32, |
|
25 |
height: *mut i32, |
|
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
26 |
play_width: *mut i32, |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
27 |
play_height: *mut i32, |
15904 | 28 |
) { |
29 |
unsafe { |
|
30 |
*width = game_field.collision.width() as i32; |
|
31 |
*height = game_field.collision.height() as i32; |
|
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
32 |
|
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
33 |
*play_width = game_field.collision.play_width() as i32; |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
34 |
*play_height = game_field.collision.play_height() as i32; |
15904 | 35 |
} |
36 |
} |
|
37 |
||
38 |
#[no_mangle] |
|
39 |
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
|
40 |
let game_field = Box::new(GameField { |
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
41 |
collision: land2d::Land2D::new(&Size::new(width as usize, height as usize), 0), |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
42 |
pixels: land2d::Land2D::new(&Size::new(width as usize, height as usize), 0), |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
43 |
landgen_parameters: None, |
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
44 |
}); |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
45 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
46 |
Box::leak(game_field) |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
47 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
48 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
49 |
#[no_mangle] |
15904 | 50 |
pub extern "C" fn generate_templated_game_field( |
51 |
feature_size: u32, |
|
52 |
seed: *const i8, |
|
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
53 |
template_type: *const i8, |
15904 | 54 |
data_path: *const i8, |
55 |
) -> *mut GameField { |
|
56 |
let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap(); |
|
57 |
let data_path = Path::new(&data_path); |
|
58 |
||
59 |
let seed: &str = unsafe { CStr::from_ptr(seed) }.to_str().unwrap(); |
|
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
60 |
let template_type: &str = unsafe { CStr::from_ptr(template_type) }.to_str().unwrap(); |
15904 | 61 |
|
62 |
let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes()); |
|
63 |
||
64 |
let yaml_templates = |
|
65 |
fs::read_to_string(data_path.join(Path::new("map_templates.yaml")).as_path()) |
|
66 |
.expect("Error reading map templates file"); |
|
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
67 |
let mut map_gen = MapGenerator::<OutlineTemplate>::new(); |
15904 | 68 |
map_gen.import_yaml_templates(&yaml_templates); |
69 |
||
70 |
let distance_divisor = feature_size.pow(2) / 8 + 10; |
|
71 |
let params = LandGenerationParameters::new(0u16, 0x8000u16, distance_divisor, false, false); |
|
72 |
let template = map_gen |
|
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
73 |
.get_template(template_type, &mut random_numbers_gen) |
15904 | 74 |
.expect("Error reading map templates file") |
75 |
.clone(); |
|
76 |
let landgen = TemplatedLandGenerator::new(template); |
|
77 |
let collision = landgen.generate_land(¶ms, &mut random_numbers_gen); |
|
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
78 |
let size = collision.size().size(); |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
79 |
|
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
80 |
let game_field = Box::new(GameField { |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
81 |
collision, |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
82 |
pixels: land2d::Land2D::new(&size, 0), |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
83 |
landgen_parameters: Some(params), |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
84 |
}); |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
85 |
|
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
86 |
Box::leak(game_field) |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
87 |
} |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
88 |
|
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
89 |
#[no_mangle] |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
90 |
pub extern "C" fn apply_theme( |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
91 |
game_field: &mut GameField, |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
92 |
data_path: *const i8, |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
93 |
theme_name: *const i8, |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
94 |
) { |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
95 |
let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap(); |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
96 |
let data_path = Path::new(&data_path); |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
97 |
|
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
98 |
let theme_name: &str = unsafe { CStr::from_ptr(theme_name) }.to_str().unwrap(); |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
99 |
let map_gen = MapGenerator::<()>::new(); |
15904 | 100 |
|
101 |
let theme = Theme::load( |
|
102 |
data_path |
|
103 |
.join(Path::new("Themes")) |
|
104 |
.join(Path::new(theme_name)) |
|
105 |
.as_path(), |
|
106 |
) |
|
107 |
.unwrap(); |
|
108 |
||
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
109 |
let params = game_field |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
110 |
.landgen_parameters |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
111 |
.expect("Land generator parameters specified"); |
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
112 |
let pixels = map_gen.make_texture(&game_field.collision, ¶ms, &theme); |
15904 | 113 |
|
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
114 |
game_field.pixels = pixels.into(); |
15904 | 115 |
} |
116 |
||
117 |
#[no_mangle] |
|
15912 | 118 |
pub extern "C" fn land_get(game_field: &GameField, x: i32, y: i32) -> u16 { |
119 |
game_field.collision.get(y, x) |
|
15900
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_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
|
124 |
game_field.collision.map(y, x, |p| *p = value); |
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] |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
128 |
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
|
129 |
game_field.collision[row as usize].as_mut_ptr() |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
130 |
} |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
131 |
|
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
132 |
#[no_mangle] |
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
133 |
pub extern "C" fn land_fill( |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
134 |
game_field: &mut GameField, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
135 |
x: i32, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
136 |
y: i32, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
137 |
border_value: u16, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
138 |
fill_value: u16, |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
139 |
) { |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
140 |
game_field |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
141 |
.collision |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
142 |
.fill(Point::new(x, y), border_value, fill_value) |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
143 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
144 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
145 |
#[no_mangle] |
15912 | 146 |
pub extern "C" fn land_pixel_get(game_field: &GameField, x: i32, y: i32) -> u32 { |
147 |
game_field.pixels.get(y, x) |
|
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
148 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
149 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
150 |
#[no_mangle] |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
151 |
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
|
152 |
game_field.pixels.map(y, x, |p| *p = value); |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
153 |
} |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
154 |
|
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
155 |
#[no_mangle] |
15901
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
156 |
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
|
157 |
game_field.pixels[row as usize].as_mut_ptr() |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
158 |
} |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
159 |
|
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15900
diff
changeset
|
160 |
#[no_mangle] |
15900
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
161 |
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
|
162 |
unsafe { drop(Box::from_raw(game_field)) }; |
128ace913837
Introduce hwengine-future library, use Land allocated in it
unC0Rr
parents:
diff
changeset
|
163 |
} |