rust/lib-hedgewars-engine/src/lib.rs
changeset 14299 21be7838a127
parent 14298 b8871dd33ec4
child 14300 fd8e0e52d5bd
equal deleted inserted replaced
14298:b8871dd33ec4 14299:21be7838a127
       
     1 pub mod instance;
     1 mod ipc;
     2 mod ipc;
     2 mod world;
     3 mod world;
     3 pub mod instance;
       
     4 
     4 
       
     5 use gfx::{format::Formatted, Encoder};
     5 use std::{
     6 use std::{
       
     7     ffi::CString,
     6     io::{Read, Write},
     8     io::{Read, Write},
     7     ffi::{CString},
     9     mem::replace,
     8     os::raw::{c_void, c_char},
    10     os::raw::{c_char, c_void},
     9     mem::replace
       
    10 };
       
    11 use gfx::{
       
    12     Encoder,
       
    13     format::Formatted,
       
    14 };
    11 };
    15 
    12 
    16 use gfx_device_gl as gfx_gl;
    13 use gfx_device_gl as gfx_gl;
    17 
    14 
    18 use self::instance::{
    15 use self::instance::{EngineGlContext, EngineInstance};
    19     EngineInstance,
       
    20     EngineGlContext
       
    21 };
       
    22 
    16 
    23 #[repr(C)]
    17 #[repr(C)]
    24 #[derive(Copy, Clone)]
    18 #[derive(Copy, Clone)]
    25 pub struct PreviewInfo {
    19 pub struct PreviewInfo {
    26     width: u32,
    20     width: u32,
    58 }
    52 }
    59 
    53 
    60 #[no_mangle]
    54 #[no_mangle]
    61 pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const u8, size: usize) {
    55 pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const u8, size: usize) {
    62     unsafe {
    56     unsafe {
    63         (*engine_state).ipc.write(std::slice::from_raw_parts(buf, size)).unwrap();
    57         (*engine_state)
       
    58             .ipc
       
    59             .write(std::slice::from_raw_parts(buf, size))
       
    60             .unwrap();
    64     }
    61     }
    65 }
    62 }
    66 
    63 
    67 #[no_mangle]
    64 #[no_mangle]
    68 pub extern "C" fn read_ipc(
    65 pub extern "C" fn read_ipc(engine_state: &mut EngineInstance, buf: *mut u8, size: usize) -> usize {
    69     engine_state: &mut EngineInstance,
    66     unsafe {
    70     buf: *mut u8,
    67         (*engine_state)
    71     size: usize,
    68             .ipc
    72 ) -> usize {
    69             .read(std::slice::from_raw_parts_mut(buf, size))
    73     unsafe { (*engine_state).ipc.read(std::slice::from_raw_parts_mut(buf, size)).unwrap_or(0) }
    70             .unwrap_or(0)
       
    71     }
    74 }
    72 }
    75 
    73 
    76 #[no_mangle]
    74 #[no_mangle]
    77 pub extern "C" fn setup_current_gl_context(
    75 pub extern "C" fn setup_current_gl_context(
    78     engine_state: &mut EngineInstance,
    76     engine_state: &mut EngineInstance,
    79     width: u16,
    77     width: u16,
    80     height: u16,
    78     height: u16,
    81     gl_loader: extern "C" fn (*const c_char) -> *const c_void
    79     gl_loader: extern "C" fn(*const c_char) -> *const c_void,
    82 ) {
    80 ) {
    83     let (device, mut factory) = gfx_gl::create(|name| {
    81     let (device, mut factory) = gfx_gl::create(|name| {
    84         let c_name = CString::new(name).unwrap();
    82         let c_name = CString::new(name).unwrap();
    85         gl_loader(c_name.as_ptr())
    83         gl_loader(c_name.as_ptr())
    86     });
    84     });
    87 
    85 
    88     let dimensions = (width, height, 1u16, gfx::texture::AaMode::Single);
    86     let dimensions = (width, height, 1u16, gfx::texture::AaMode::Single);
    89     let (render_target, depth_buffer) = gfx_gl::create_main_targets_raw(
    87     let (render_target, depth_buffer) = gfx_gl::create_main_targets_raw(
    90         dimensions,
    88         dimensions,
    91         gfx::format::Rgba8::get_format().0,
    89         gfx::format::Rgba8::get_format().0,
    92         gfx::format::Depth::get_format().0
    90         gfx::format::Depth::get_format().0,
    93     );
    91     );
    94 
    92 
    95     let mut command_buffer: Encoder<_, _> = factory.create_command_buffer().into();
    93     let mut command_buffer: Encoder<_, _> = factory.create_command_buffer().into();
    96 
    94 
    97     engine_state.gl_context = Some(EngineGlContext {
    95     engine_state.gl_context = Some(EngineGlContext {
    98         device,
    96         device,
    99         factory,
    97         factory,
   100         render_target: gfx::memory::Typed::new(render_target),
    98         render_target: gfx::memory::Typed::new(render_target),
   101         depth_buffer: gfx::memory::Typed::new(depth_buffer),
    99         depth_buffer: gfx::memory::Typed::new(depth_buffer),
   102         command_buffer
   100         command_buffer,
   103     })
   101     })
   104 }
   102 }
   105 
   103 
   106 #[no_mangle]
   104 #[no_mangle]
   107 pub extern "C" fn render_frame(engine_state: &mut EngineInstance) {
   105 pub extern "C" fn render_frame(engine_state: &mut EngineInstance) {
   111     }
   109     }
   112     replace(&mut engine_state.gl_context, context);
   110     replace(&mut engine_state.gl_context, context);
   113 }
   111 }
   114 
   112 
   115 #[no_mangle]
   113 #[no_mangle]
       
   114 pub extern "C" fn advance_simulation(engine_state: &mut EngineInstance, ticks: u32) -> bool {
       
   115     unimplemented!()
       
   116 }
       
   117 #[no_mangle]
   116 pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
   118 pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
   117     unsafe {
   119     unsafe {
   118         Box::from_raw(engine_state);
   120         Box::from_raw(engine_state);
   119     }
   121     }
   120 }
   122 }