rust/lib-hedgewars-engine/src/lib.rs
changeset 14277 3152d9fdb499
parent 14276 1aac8a62be6f
child 14298 b8871dd33ec4
equal deleted inserted replaced
14276:1aac8a62be6f 14277:3152d9fdb499
     1 mod ipc;
     1 mod ipc;
     2 mod world;
     2 mod world;
       
     3 mod instance;
     3 
     4 
     4 use std::io::{Read, Write};
     5 use std::io::{Read, Write};
     5 
     6 
     6 use self::ipc::IPC;
     7 use self::instance::EngineInstance;
     7 
       
     8 #[repr(C)]
       
     9 pub struct EngineInstance {
       
    10     world: world::World,
       
    11     ipc: IPC,
       
    12 }
       
    13 
       
    14 impl EngineInstance {
       
    15     pub fn new() -> Self {
       
    16         let world = world::World::new();
       
    17         Self {
       
    18             world,
       
    19             ipc: IPC::new(),
       
    20         }
       
    21     }
       
    22 
       
    23     pub fn render<R, C>(
       
    24         &self,
       
    25         context: &mut gfx::Encoder<R, C>,
       
    26         target: &gfx::handle::RenderTargetView<R, gfx::format::Rgba8>,
       
    27     ) where
       
    28         R: gfx::Resources,
       
    29         C: gfx::CommandBuffer<R>,
       
    30     {
       
    31         context.clear(target, [0.0, 0.5, 0.0, 1.0]);
       
    32     }
       
    33 }
       
    34 
     8 
    35 #[repr(C)]
     9 #[repr(C)]
    36 #[derive(Copy, Clone)]
    10 #[derive(Copy, Clone)]
    37 pub struct PreviewInfo {
    11 pub struct PreviewInfo {
    38     width: u32,
    12     width: u32,
    53     Box::leak(engine_state)
    27     Box::leak(engine_state)
    54 }
    28 }
    55 
    29 
    56 #[no_mangle]
    30 #[no_mangle]
    57 pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) {
    31 pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) {
       
    32     (*engine_state).process_ipc_queue();
       
    33 
    58     (*engine_state).world.generate_preview();
    34     (*engine_state).world.generate_preview();
    59 
    35 
    60     let land_preview = (*engine_state).world.preview();
    36     let land_preview = (*engine_state).world.preview();
    61 
    37 
    62     *preview = PreviewInfo {
    38     *preview = PreviewInfo {
    66         land: land_preview.raw_pixels().as_ptr(),
    42         land: land_preview.raw_pixels().as_ptr(),
    67     };
    43     };
    68 }
    44 }
    69 
    45 
    70 #[no_mangle]
    46 #[no_mangle]
    71 pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const [u8], size: usize) {
    47 pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const u8, size: usize) {
    72     unsafe {
    48     unsafe {
    73         (*engine_state).ipc.write(&(*buf)[0..size]);
    49         (*engine_state).ipc.write(std::slice::from_raw_parts(buf, size)).unwrap();
    74     }
    50     }
    75 }
    51 }
    76 
    52 
    77 #[no_mangle]
    53 #[no_mangle]
    78 pub extern "C" fn read_ipc(
    54 pub extern "C" fn read_ipc(
    79     engine_state: &mut EngineInstance,
    55     engine_state: &mut EngineInstance,
    80     buf: *mut [u8],
    56     buf: *mut u8,
    81     size: usize,
    57     size: usize,
    82 ) -> usize {
    58 ) -> usize {
    83     unsafe { (*engine_state).ipc.read(&mut (*buf)[0..size]).unwrap_or(0) }
    59     unsafe { (*engine_state).ipc.read(std::slice::from_raw_parts_mut(buf, size)).unwrap_or(0) }
    84 }
    60 }
    85 
    61 
    86 #[no_mangle]
    62 #[no_mangle]
    87 pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
    63 pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
    88     unsafe {
    64     unsafe {