rust/lib-hedgewars-engine/src/lib.rs
author unC0Rr
Thu, 22 Nov 2018 10:58:55 +0100
changeset 14276 1aac8a62be6f
parent 14260 a239e4243cf9
child 14277 3152d9fdb499
permissions -rw-r--r--
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
     1
mod ipc;
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     2
mod world;
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
     3
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
     4
use std::io::{Read, Write};
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
     5
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
     6
use self::ipc::IPC;
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     7
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     8
#[repr(C)]
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     9
pub struct EngineInstance {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    10
    world: world::World,
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    11
    ipc: IPC,
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    12
}
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    13
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    14
impl EngineInstance {
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    15
    pub fn new() -> Self {
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    16
        let world = world::World::new();
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    17
        Self {
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    18
            world,
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    19
            ipc: IPC::new(),
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    20
        }
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    21
    }
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    22
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    23
    pub fn render<R, C>(
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    24
        &self,
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    25
        context: &mut gfx::Encoder<R, C>,
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    26
        target: &gfx::handle::RenderTargetView<R, gfx::format::Rgba8>,
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    27
    ) where
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    28
        R: gfx::Resources,
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    29
        C: gfx::CommandBuffer<R>,
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    30
    {
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    31
        context.clear(target, [0.0, 0.5, 0.0, 1.0]);
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    32
    }
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    33
}
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    34
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    35
#[repr(C)]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    36
#[derive(Copy, Clone)]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    37
pub struct PreviewInfo {
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    38
    width: u32,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    39
    height: u32,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    40
    hedgehogs_number: u8,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    41
    land: *const u8,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    42
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    43
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    44
#[no_mangle]
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    45
pub extern "C" fn protocol_version() -> u32 {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    46
    56
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    47
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    48
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    49
#[no_mangle]
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    50
pub extern "C" fn start_engine() -> *mut EngineInstance {
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    51
    let engine_state = Box::new(EngineInstance::new());
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    52
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    53
    Box::leak(engine_state)
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    54
}
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    55
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    56
#[no_mangle]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    57
pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    58
    (*engine_state).world.generate_preview();
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    59
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    60
    let land_preview = (*engine_state).world.preview();
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    61
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    62
    *preview = PreviewInfo {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    63
        width: land_preview.width() as u32,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    64
        height: land_preview.height() as u32,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    65
        hedgehogs_number: 0,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    66
        land: land_preview.raw_pixels().as_ptr(),
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    67
    };
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    68
}
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    69
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    70
#[no_mangle]
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    71
pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const [u8], size: usize) {
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    72
    unsafe {
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    73
        (*engine_state).ipc.write(&(*buf)[0..size]);
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    74
    }
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    75
}
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    76
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    77
#[no_mangle]
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    78
pub extern "C" fn read_ipc(
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    79
    engine_state: &mut EngineInstance,
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    80
    buf: *mut [u8],
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    81
    size: usize,
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    82
) -> usize {
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    83
    unsafe { (*engine_state).ipc.read(&mut (*buf)[0..size]).unwrap_or(0) }
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    84
}
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    85
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    86
#[no_mangle]
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    87
pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    88
    unsafe {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    89
        Box::from_raw(engine_state);
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    90
    }
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    91
}