rust/lib-hedgewars-engine/src/world.rs
author alfadur
Fri, 22 Mar 2019 20:26:29 +0300
changeset 14705 19122a329774
parent 14704 12db7e435ea6
child 14708 19358c313ebb
permissions -rw-r--r--
make world renderer optional + fmt
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
     1
use fpnum::{fp, FPNum};
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
     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
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
     6
    outline_template::OutlineTemplate, template_based::TemplatedLandGenerator,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
     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
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    10
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    11
use crate::render::{camera::Camera, MapRenderer};
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    12
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    13
struct GameState {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    14
    land: Land2D<u32>,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    15
    physics: hwp::World,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    16
}
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    17
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    18
impl GameState {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    19
    fn new(land: Land2D<u32>, physics: hwp::World) -> Self {
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    20
        Self { land, physics }
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    21
    }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    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
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    27
    game_state: Option<GameState>,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    28
    renderer: Option<MapRenderer>,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    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
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    37
            game_state: None,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    38
            renderer: None,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    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
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    43
    pub fn create_renderer(&mut self, width: u16, height: u16) {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    44
        self.renderer = Some(MapRenderer::new(512, 512));
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    45
        self.camera = Camera::with_size(Size::new(width as usize, height as usize));
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    46
    }
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    47
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    48
    pub fn set_seed(&mut self, seed: &[u8]) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    49
        self.random_numbers_gen = LaggedFibonacciPRNG::new(seed);
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    50
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    51
14373
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
    52
    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
    53
        &self.preview
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    54
    }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    55
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    56
    pub fn generate_preview(&mut self) {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    57
        fn template() -> OutlineTemplate {
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    58
            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
    59
            template.islands = vec![vec![
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    60
                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
    61
                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
    62
                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
    63
                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
    64
            ]];
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    65
            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
    66
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    67
            template
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    68
        }
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    69
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    70
        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
    71
        let landgen = TemplatedLandGenerator::new(template());
14373
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
    72
        self.preview = Some(landgen.generate_land(&params, &mut self.random_numbers_gen));
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
    73
    }
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
    74
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
    75
    pub fn dispose_preview(&mut self) {
4409344db447 Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents: 14295
diff changeset
    76
        self.preview = None
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
    77
    }
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    78
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    79
    pub fn init(&mut self, template: OutlineTemplate) {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    80
        let physics = hwp::World::new(template.size);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    81
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    82
        let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    83
        let landgen = TemplatedLandGenerator::new(template);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    84
        let land = landgen.generate_land(&params, &mut self.random_numbers_gen);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    85
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    86
        use mapgen::{
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    87
            theme::{slice_u32_to_u8, Theme},
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    88
            MapGenerator,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    89
        };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    90
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    91
        use std::path::Path;
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    92
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    93
        let theme = Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    94
        let texture = MapGenerator::new().make_texture32(&land, &theme);
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    95
        if let Some(ref mut renderer) = self.renderer {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    96
            renderer.init(&texture);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    97
        }
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    98
        self.game_state = Some(GameState::new(land, physics));
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    99
    }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   100
14704
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   101
    pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) {
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   102
        self.camera.position += position_shift;
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   103
        self.camera.zoom += zoom_shift;
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   104
    }
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   105
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   106
    pub fn render(&mut self) {
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   107
        if let Some(ref mut renderer) = self.renderer {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   108
            unsafe {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   109
                gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   110
                gl::Clear(gl::COLOR_BUFFER_BIT);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   111
            }
14704
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   112
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   113
            renderer.render(self.camera.viewport());
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   114
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   115
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   116
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   117
    pub fn step(&mut self) {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   118
        if let Some(ref mut state) = self.game_state {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   119
            state.physics.step(fp!(1), &state.land);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   120
        }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   121
    }
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   122
}