author | fkaa |
Fri, 22 Mar 2019 18:01:08 +0200 | |
changeset 14702 | 29dbe9ce8b7d |
parent 14373 | 4409344db447 |
child 14704 | 12db7e435ea6 |
permissions | -rw-r--r-- |
14295 | 1 |
use fpnum::{FPNum, fp}; |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
2 |
use integral_geometry::{Point, Rect, Size}; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
3 |
use land2d::Land2D; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
4 |
use landgen::{ |
14295 | 5 |
outline_template::OutlineTemplate, |
6 |
template_based::TemplatedLandGenerator, |
|
7 |
LandGenerationParameters, |
|
8 |
LandGenerator, |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
9 |
}; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
10 |
use lfprng::LaggedFibonacciPRNG; |
14295 | 11 |
use hwphysics as hwp; |
12 |
||
14702 | 13 |
use crate::render::MapRenderer; |
14 |
||
14295 | 15 |
struct GameState { |
16 |
land: Land2D<u32>, |
|
17 |
physics: hwp::World, |
|
18 |
} |
|
19 |
||
20 |
impl GameState { |
|
21 |
fn new(land: Land2D<u32>, physics: hwp::World) -> Self { |
|
22 |
Self { |
|
23 |
land, |
|
24 |
physics, |
|
25 |
} |
|
26 |
} |
|
27 |
} |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
28 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
29 |
pub struct World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
30 |
random_numbers_gen: LaggedFibonacciPRNG, |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
31 |
preview: Option<Land2D<u8>>, |
14295 | 32 |
game_state: Option<GameState>, |
14702 | 33 |
renderer: MapRenderer, |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
34 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
35 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
36 |
impl World { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
37 |
pub fn new() -> Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
38 |
Self { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
39 |
random_numbers_gen: LaggedFibonacciPRNG::new(&[]), |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
40 |
preview: None, |
14295 | 41 |
game_state: None, |
14702 | 42 |
renderer: MapRenderer::new(512, 512), |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
43 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
44 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
45 |
|
14272 | 46 |
pub fn set_seed(&mut self, seed: &[u8]) { |
47 |
self.random_numbers_gen = LaggedFibonacciPRNG::new(seed); |
|
48 |
} |
|
49 |
||
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
50 |
pub fn preview(&self) -> &Option<Land2D<u8>> { |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
51 |
&self.preview |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
52 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
53 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
54 |
pub fn generate_preview(&mut self) { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
55 |
fn template() -> OutlineTemplate { |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
56 |
let mut template = OutlineTemplate::new(Size::new(4096, 2048)); |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
57 |
template.islands = vec![vec![ |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
58 |
Rect::from_size_coords(100, 2050, 1, 1), |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
59 |
Rect::from_size_coords(100, 500, 400, 1200), |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
60 |
Rect::from_size_coords(3600, 500, 400, 1200), |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
61 |
Rect::from_size_coords(3900, 2050, 1, 1), |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
62 |
]]; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
63 |
template.fill_points = vec![Point::new(1, 0)]; |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
64 |
|
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
65 |
template |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
66 |
} |
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
67 |
|
14295 | 68 |
let params = LandGenerationParameters::new(0u8, u8::max_value(), 5, false, false); |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
69 |
let landgen = TemplatedLandGenerator::new(template()); |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
70 |
self.preview = Some(landgen.generate_land(¶ms, &mut self.random_numbers_gen)); |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
71 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
72 |
|
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
73 |
pub fn dispose_preview(&mut self) { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14295
diff
changeset
|
74 |
self.preview = None |
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
75 |
} |
14295 | 76 |
|
77 |
pub fn init(&mut self, template: OutlineTemplate) { |
|
78 |
let physics = hwp::World::new(template.size); |
|
79 |
||
80 |
let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false); |
|
81 |
let landgen = TemplatedLandGenerator::new(template); |
|
82 |
let land = landgen.generate_land(¶ms, &mut self.random_numbers_gen); |
|
83 |
||
14702 | 84 |
use mapgen::{ |
85 |
MapGenerator, |
|
86 |
theme::{Theme, slice_u32_to_u8} |
|
87 |
}; |
|
88 |
||
89 |
use std::path::Path; |
|
90 |
||
91 |
let theme = Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap(); |
|
92 |
let texture = MapGenerator::new().make_texture32(&land, &theme); |
|
93 |
self.renderer.init(&texture); |
|
94 |
||
14295 | 95 |
self.game_state = Some(GameState::new(land, physics)); |
96 |
} |
|
97 |
||
14702 | 98 |
pub fn render(&mut self, x: f32, y: f32, w: f32, h: f32) { |
99 |
unsafe { |
|
100 |
gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32); |
|
101 |
gl::Clear(gl::COLOR_BUFFER_BIT); |
|
102 |
} |
|
103 |
||
104 |
self.renderer.render(Rect::new( |
|
105 |
Point::new(x as _, y as _), |
|
106 |
Point::new((x + w) as _, (y + h) as _), |
|
107 |
)); |
|
108 |
} |
|
109 |
||
14295 | 110 |
pub fn step(&mut self) { |
111 |
if let Some(ref mut state) = self.game_state { |
|
112 |
state.physics.step(fp!(1), &state.land); |
|
113 |
} |
|
114 |
} |
|
14154
8354b390f1a2
Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff
changeset
|
115 |
} |
14702 | 116 |
|
117 |
||
118 |