rust/lib-hedgewars-engine/src/instance.rs
author alfadur <mail@none>
Mon, 04 Feb 2019 19:22:21 +0300
changeset 14672 6e6632068a33
parent 14294 21be7838a127
child 14702 29dbe9ce8b7d
permissions -rw-r--r--
Server action refactoring part 3 of N
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
};
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     5
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
     6
use self::gfx_gl::{CommandBuffer, Resources};
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
     7
use gfx::format::{Unorm, D24, R8_G8_B8_A8};
14293
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
     8
use gfx_device_gl as gfx_gl;
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
     9
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    10
use super::{ipc::IPC, world::World};
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    11
14293
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
    12
pub struct EngineGlContext {
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
    13
    pub device: gfx_gl::Device,
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
    14
    pub factory: gfx_gl::Factory,
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
    15
    pub render_target: gfx::handle::RenderTargetView<Resources, (R8_G8_B8_A8, Unorm)>,
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
    16
    pub depth_buffer: gfx::handle::DepthStencilView<Resources, (D24, Unorm)>,
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
    17
    pub command_buffer: gfx::Encoder<Resources, CommandBuffer>,
14293
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
    18
}
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
    19
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    20
pub struct EngineInstance {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    21
    pub world: World,
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    22
    pub ipc: IPC,
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
    23
    pub gl_context: Option<EngineGlContext>,
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    24
}
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    25
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    26
impl EngineInstance {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    27
    pub fn new() -> Self {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    28
        let world = World::new();
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    29
        Self {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    30
            world,
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    31
            ipc: IPC::new(),
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
    32
            gl_context: None,
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    33
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    34
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    35
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    36
    pub fn render<R, C>(
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    37
        &self,
14293
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
    38
        command_buffer: &mut gfx::Encoder<R, C>,
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
    39
        render_target: &gfx::handle::RenderTargetView<R, gfx::format::Rgba8>,
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    40
    ) where
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    41
        R: gfx::Resources,
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    42
        C: gfx::CommandBuffer<R>,
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    43
    {
14293
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
    44
        command_buffer.clear(render_target, [0.0, 0.5, 0.0, 1.0]);
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    45
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    46
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    47
    fn process_unordered_message(&mut self, message: &UnorderedEngineMessage) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    48
        match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    49
            Pong => println!("Pong!"),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    50
            _ => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    51
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    52
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    53
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    54
    fn process_config_message(&mut self, message: &ConfigEngineMessage) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    55
        match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    56
            SetSeed(seed) => self.world.set_seed(seed.as_bytes()),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    57
            _ => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    58
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    59
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    60
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    61
    pub fn process_ipc_queue(&mut self) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    62
        let messages: Vec<EngineMessage> = self.ipc.iter().collect();
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    63
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    64
        for message in messages {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    65
            println!("Processing message: {:?}", message);
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    66
            match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    67
                Unknown => println!("Unknown message"),
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
    68
                Empty => println!("Empty message"),
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    69
                Synced(_, _) => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    70
                Unsynced(_) => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    71
                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
    72
                Config(config_message) => self.process_config_message(&config_message),
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    73
            }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    74
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    75
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    76
}