rust/lib-hedgewars-engine/src/lib.rs
author unc0rr
Thu, 08 Nov 2018 21:57:05 +0100
changeset 14176 5ad911992460
parent 14169 e2c51c8e0b2e
child 14188 3b83ec44e10b
permissions -rw-r--r--
A bit of rethinking of game messages and command

extern crate integral_geometry;
extern crate land2d;
extern crate landgen;
extern crate lfprng;

mod world;
mod engine_message;
mod command;

#[repr(C)]
pub struct EngineInstance {
    world: world::World,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct PreviewInfo {
    width: u32,
    height: u32,
    hedgehogs_number: u8,
    land: *const u8,
}

#[no_mangle]
pub extern "C" fn protocol_version() -> u32 {
    56
}

#[no_mangle]
pub extern "C" fn start_engine() -> *mut EngineInstance {
    let engine_state = Box::new(EngineInstance {
        world: world::World::new(),
    });

    Box::leak(engine_state)
}

#[no_mangle]
pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) {
    (*engine_state).world.generate_preview();

    let land_preview = (*engine_state).world.preview();

    *preview = PreviewInfo {
        width: land_preview.width() as u32,
        height: land_preview.height() as u32,
        hedgehogs_number: 0,
        land: land_preview.raw_pixels().as_ptr(),
    };
}

#[no_mangle]
pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
    unsafe {
        Box::from_raw(engine_state);
    }
}