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