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