rust/lib-hedgewars-engine/src/lib.rs
author unC0Rr
Thu, 22 Nov 2018 13:39:51 +0100
changeset 14277 3152d9fdb499
parent 14276 1aac8a62be6f
child 14298 b8871dd33ec4
permissions -rw-r--r--
- Move EngineInstance into a separate module - Refactor ipc a bit and process messages
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;
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14276
diff changeset
     3
mod instance;
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
     4
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
     5
use std::io::{Read, Write};
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
     6
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14276
diff changeset
     7
use self::instance::EngineInstance;
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
     8
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     9
#[repr(C)]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    10
#[derive(Copy, Clone)]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    11
pub struct PreviewInfo {
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    12
    width: u32,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    13
    height: u32,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    14
    hedgehogs_number: u8,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    15
    land: *const u8,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    16
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    17
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    18
#[no_mangle]
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    19
pub extern "C" fn protocol_version() -> u32 {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    20
    56
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    21
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    22
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    23
#[no_mangle]
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    24
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
    25
    let engine_state = Box::new(EngineInstance::new());
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    26
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    27
    Box::leak(engine_state)
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    28
}
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    29
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    30
#[no_mangle]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    31
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
    32
    (*engine_state).process_ipc_queue();
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14276
diff changeset
    33
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    34
    (*engine_state).world.generate_preview();
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
    let land_preview = (*engine_state).world.preview();
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    37
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    38
    *preview = PreviewInfo {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    39
        width: land_preview.width() as u32,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    40
        height: land_preview.height() as u32,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    41
        hedgehogs_number: 0,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    42
        land: land_preview.raw_pixels().as_ptr(),
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    43
    };
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    44
}
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    45
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    46
#[no_mangle]
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14276
diff changeset
    47
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
    48
    unsafe {
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14276
diff changeset
    49
        (*engine_state).ipc.write(std::slice::from_raw_parts(buf, size)).unwrap();
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    50
    }
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    51
}
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    52
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    53
#[no_mangle]
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    54
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
    55
    engine_state: &mut EngineInstance,
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14276
diff changeset
    56
    buf: *mut u8,
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    57
    size: usize,
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    58
) -> usize {
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14276
diff changeset
    59
    unsafe { (*engine_state).ipc.read(std::slice::from_raw_parts_mut(buf, size)).unwrap_or(0) }
14276
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    60
}
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    61
1aac8a62be6f Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents: 14260
diff changeset
    62
#[no_mangle]
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    63
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
    64
    unsafe {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    65
        Box::from_raw(engine_state);
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    66
    }
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    67
}