rust/lib-hedgewars-engine/src/lib.rs
author alfadur
Mon, 26 Nov 2018 02:50:54 +0300
changeset 14316 fd8e0e52d5bd
parent 14315 21be7838a127
child 14356 97be4f6bf264
permissions -rw-r--r--
add hwphysics to engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
     1
pub mod instance;
14292
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
     2
mod ipc;
14174
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
     3
mod world;
14292
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
     4
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
     5
use gfx::{format::Formatted, Encoder};
14314
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
     6
use std::{
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
     7
    ffi::CString,
14314
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
     8
    io::{Read, Write},
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
     9
    mem::replace,
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    10
    os::raw::{c_char, c_void},
14314
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    11
};
14292
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
    12
14314
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    13
use gfx_device_gl as gfx_gl;
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    14
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    15
use self::instance::{EngineGlContext, EngineInstance};
14209
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14190
diff changeset
    16
14174
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    17
#[repr(C)]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    18
#[derive(Copy, Clone)]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    19
pub struct PreviewInfo {
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    20
    width: u32,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    21
    height: u32,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    22
    hedgehogs_number: u8,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    23
    land: *const u8,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    24
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    25
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    26
#[no_mangle]
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    27
pub extern "C" fn protocol_version() -> u32 {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    28
    56
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    29
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    30
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    31
#[no_mangle]
14174
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    32
pub extern "C" fn start_engine() -> *mut EngineInstance {
14292
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
    33
    let engine_state = Box::new(EngineInstance::new());
14174
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    34
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    35
    Box::leak(engine_state)
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    36
}
14174
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    37
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    38
#[no_mangle]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    39
pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) {
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14292
diff changeset
    40
    (*engine_state).process_ipc_queue();
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14292
diff changeset
    41
14174
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    42
    (*engine_state).world.generate_preview();
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    43
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    44
    let land_preview = (*engine_state).world.preview();
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    45
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    46
    *preview = PreviewInfo {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    47
        width: land_preview.width() as u32,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    48
        height: land_preview.height() as u32,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    49
        hedgehogs_number: 0,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    50
        land: land_preview.raw_pixels().as_ptr(),
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    51
    };
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    52
}
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    53
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
    54
#[no_mangle]
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14292
diff changeset
    55
pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const u8, size: usize) {
14292
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
    56
    unsafe {
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    57
        (*engine_state)
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    58
            .ipc
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    59
            .write(std::slice::from_raw_parts(buf, size))
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    60
            .unwrap();
14292
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
    61
    }
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
    62
}
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
    63
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
    64
#[no_mangle]
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    65
pub extern "C" fn read_ipc(engine_state: &mut EngineInstance, buf: *mut u8, size: usize) -> usize {
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    66
    unsafe {
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    67
        (*engine_state)
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    68
            .ipc
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    69
            .read(std::slice::from_raw_parts_mut(buf, size))
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    70
            .unwrap_or(0)
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    71
    }
14292
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
    72
}
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
    73
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14276
diff changeset
    74
#[no_mangle]
14314
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    75
pub extern "C" fn setup_current_gl_context(
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    76
    engine_state: &mut EngineInstance,
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    77
    width: u16,
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    78
    height: u16,
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    79
    gl_loader: extern "C" fn(*const c_char) -> *const c_void,
14314
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    80
) {
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    81
    let (device, mut factory) = gfx_gl::create(|name| {
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    82
        let c_name = CString::new(name).unwrap();
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    83
        gl_loader(c_name.as_ptr())
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    84
    });
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    85
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    86
    let dimensions = (width, height, 1u16, gfx::texture::AaMode::Single);
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    87
    let (render_target, depth_buffer) = gfx_gl::create_main_targets_raw(
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    88
        dimensions,
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    89
        gfx::format::Rgba8::get_format().0,
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    90
        gfx::format::Depth::get_format().0,
14314
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    91
    );
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    92
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    93
    let mut command_buffer: Encoder<_, _> = factory.create_command_buffer().into();
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    94
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    95
    engine_state.gl_context = Some(EngineGlContext {
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    96
        device,
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    97
        factory,
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    98
        render_target: gfx::memory::Typed::new(render_target),
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
    99
        depth_buffer: gfx::memory::Typed::new(depth_buffer),
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
   100
        command_buffer,
14314
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   101
    })
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   102
}
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   103
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   104
#[no_mangle]
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   105
pub extern "C" fn render_frame(engine_state: &mut EngineInstance) {
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   106
    let mut context = replace(&mut engine_state.gl_context, None);
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   107
    if let Some(ref mut c) = context {
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   108
        engine_state.render(&mut c.command_buffer, &mut c.render_target)
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   109
    }
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   110
    replace(&mut engine_state.gl_context, context);
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   111
}
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   112
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
   113
#[no_mangle]
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
   114
pub extern "C" fn advance_simulation(engine_state: &mut EngineInstance, ticks: u32) -> bool {
14316
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14315
diff changeset
   115
    engine_state.world.step();
fd8e0e52d5bd add hwphysics to engine
alfadur
parents: 14315
diff changeset
   116
    true
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
   117
}
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
   118
#[no_mangle]
14174
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
   119
pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
   120
    unsafe {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
   121
        Box::from_raw(engine_state);
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
   122
    }
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14164
diff changeset
   123
}