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