rust/lib-hedgewars-engine/src/instance.rs
author unC0Rr
Tue, 10 Sep 2024 13:56:51 +0200
branchtransitional_engine
changeset 16032 31cc1e450273
parent 16022 2003b466b279
permissions -rw-r--r--
Add maze land generator ported from pascal engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     1
use hedgewars_engine_messages::messages::{
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     2
    ConfigEngineMessage::*, EngineMessage::*, KeystrokeAction::*, SyncedEngineMessage::*,
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
     3
    UnorderedEngineMessage::*, UnsyncedEngineMessage::*, *,
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     4
};
15284
ae8e14d14596 Move messages queue to hedgewars-engine-messages lib
unc0rr
parents: 15265
diff changeset
     5
use hedgewars_engine_messages::queue::*;
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     6
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
     7
use integral_geometry::{Point, Rect, Size};
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15891
diff changeset
     8
use landgen::outline_template_based::outline_template::OutlineTemplate;
14293
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
     9
15891
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 15809
diff changeset
    10
use std::path::Path;
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 15809
diff changeset
    11
15265
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14705
diff changeset
    12
use super::{ipc::*, world::World};
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    13
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    14
pub struct EngineInstance {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    15
    pub world: World,
15265
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14705
diff changeset
    16
    pub ipc_channel: Channel,
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14705
diff changeset
    17
    ipc_queue: MessagesQueue,
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    18
}
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    19
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    20
impl EngineInstance {
15891
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 15809
diff changeset
    21
    pub fn new(data_path: &Path) -> Self {
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 15809
diff changeset
    22
        let mut world = World::new(data_path);
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    23
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    24
        fn template() -> OutlineTemplate {
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    25
            let mut template = OutlineTemplate::new(Size::new(4096 * 1, 2048 * 1));
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    26
            template.islands = vec![vec![
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    27
                Rect::from_size_coords(100, 2050, 1, 1),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    28
                Rect::from_size_coords(100, 500, 400, 1200),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    29
                Rect::from_size_coords(3600, 500, 400, 1200),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    30
                Rect::from_size_coords(3900, 2050, 1, 1),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    31
            ]];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    32
            template.fill_points = vec![Point::new(1, 0)];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    33
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    34
            template
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    35
        }
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    36
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    37
        world.init(template());
16017
e8afb1bf2779 Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents: 16011
diff changeset
    38
        world.init_renderer();
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    39
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    40
        Self {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    41
            world,
15265
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14705
diff changeset
    42
            ipc_channel: Channel::new(),
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14705
diff changeset
    43
            ipc_queue: MessagesQueue::new(QueueChatStrategy::LocalGame),
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    44
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    45
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    46
14704
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
    47
    pub fn render(&mut self) {
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
    48
        self.world.render();
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    49
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    50
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    51
    fn process_unordered_message(&mut self, message: &UnorderedEngineMessage) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    52
        match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    53
            Pong => println!("Pong!"),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    54
            _ => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    55
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    56
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    57
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    58
    fn process_config_message(&mut self, message: &ConfigEngineMessage) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    59
        match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    60
            SetSeed(seed) => self.world.set_seed(seed.as_bytes()),
15809
c3971b38bbfa hedgewars-engine (rust)/preview: recognise feature_size cmd and do something with it
sheepluva
parents: 15284
diff changeset
    61
            SetFeatureSize(feature_size) => self.world.set_feature_size(*feature_size),
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    62
            _ => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    63
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    64
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    65
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    66
    pub fn process_ipc_queue(&mut self) {
15265
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14705
diff changeset
    67
        for message in self.ipc_channel.iter() {
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14705
diff changeset
    68
            self.ipc_queue.push(message);
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14705
diff changeset
    69
        }
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14705
diff changeset
    70
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14705
diff changeset
    71
        let messages: Vec<EngineMessage> = self.ipc_queue.iter(0).collect();
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    72
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    73
        for message in messages {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    74
            println!("Processing message: {:?}", message);
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    75
            match message {
16011
52b51d92e88d Fix build
unC0Rr
parents: 15891
diff changeset
    76
                Unknown(data) => println!("Unknown message: {:?}", data),
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
    77
                Empty => println!("Empty message"),
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    78
                Synced(_, _) => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    79
                Unsynced(_) => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    80
                Unordered(unordered_message) => self.process_unordered_message(&unordered_message),
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
    81
                Config(config_message) => self.process_config_message(&config_message),
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    82
            }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    83
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    84
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    85
}