rust/lib-hedgewars-engine/src/world.rs
author fkaa
Fri, 22 Mar 2019 18:01:08 +0200
changeset 14702 29dbe9ce8b7d
parent 14373 4409344db447
child 14704 12db7e435ea6
permissions -rw-r--r--
add basic map rendering with gl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
     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
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
     5
    outline_template::OutlineTemplate,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
     6
    template_based::TemplatedLandGenerator,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
     7
    LandGenerationParameters,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
     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
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    11
use hwphysics as hwp;
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    12
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    13
use crate::render::MapRenderer;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    14
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    15
struct GameState {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    16
    land: Land2D<u32>,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    17
    physics: hwp::World,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    18
}
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    19
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    20
impl GameState {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    21
    fn new(land: Land2D<u32>, physics: hwp::World) -> Self {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    22
        Self {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    23
            land,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    24
            physics,
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    25
        }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    26
    }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    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
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    32
    game_state: Option<GameState>,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    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
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    41
            game_state: None,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    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
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    46
    pub fn set_seed(&mut self, seed: &[u8]) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    47
        self.random_numbers_gen = LaggedFibonacciPRNG::new(seed);
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    48
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14166
diff changeset
    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
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    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(&params, &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
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    76
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    77
    pub fn init(&mut self, template: OutlineTemplate) {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    78
        let physics = hwp::World::new(template.size);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    79
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    80
        let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    81
        let landgen = TemplatedLandGenerator::new(template);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    82
        let land = landgen.generate_land(&params, &mut self.random_numbers_gen);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    83
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    84
        use mapgen::{
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    85
            MapGenerator,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    86
            theme::{Theme, slice_u32_to_u8}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    87
        };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    88
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    89
        use std::path::Path;
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
        let theme = Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    92
        let texture = MapGenerator::new().make_texture32(&land, &theme);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    93
        self.renderer.init(&texture);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    94
        
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    95
        self.game_state = Some(GameState::new(land, physics));
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    96
    }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
    97
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    98
    pub fn render(&mut self, x: f32, y: f32, w: f32, h: f32) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
    99
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   100
            gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   101
            gl::Clear(gl::COLOR_BUFFER_BIT);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   102
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   103
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   104
        self.renderer.render(Rect::new(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   105
            Point::new(x as _, y as _),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   106
            Point::new((x + w) as _, (y + h) as _),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   107
        ));
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   108
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   109
14295
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   110
    pub fn step(&mut self) {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   111
        if let Some(ref mut state) = self.game_state {
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   112
            state.physics.step(fp!(1), &state.land);
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   113
        }
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14272
diff changeset
   114
    }
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents:
diff changeset
   115
}
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   116
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   117
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14373
diff changeset
   118