rust/lib-hedgewars-engine/src/lib.rs
author unC0Rr
Tue, 20 Nov 2018 16:56:24 +0100
changeset 14260 a239e4243cf9
parent 14212 bb2f301d4fe0
child 14276 1aac8a62be6f
permissions -rw-r--r--
Start outlining ipc subsystem
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     1
mod world;
14260
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents: 14212
diff changeset
     2
mod ipc;
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     3
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     4
#[repr(C)]
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     5
pub struct EngineInstance {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     6
    world: world::World,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     7
}
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     8
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
     9
impl EngineInstance {
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    10
    pub fn new() -> Self {
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    11
        let world = world::World::new();
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    12
        Self { world }
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    13
    }
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    14
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    15
    pub fn render<R, C>(
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    16
        &self,
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    17
        context: &mut gfx::Encoder<R, C>,
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    18
        target: &gfx::handle::RenderTargetView<R, gfx::format::Rgba8>)
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    19
        where R: gfx::Resources,
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    20
              C: gfx::CommandBuffer<R>
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    21
    {
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    22
        context.clear(target, [0.0, 0.5, 0.0, 1.0]);
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    23
    }
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    24
}
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    25
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    26
#[repr(C)]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    27
#[derive(Copy, Clone)]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    28
pub struct PreviewInfo {
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    29
    width: u32,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    30
    height: u32,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    31
    hedgehogs_number: u8,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    32
    land: *const u8,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    33
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    34
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    35
#[no_mangle]
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    36
pub extern "C" fn protocol_version() -> u32 {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    37
    56
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    38
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    39
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    40
#[no_mangle]
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    41
pub extern "C" fn start_engine() -> *mut EngineInstance {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    42
    let engine_state = Box::new(EngineInstance {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    43
        world: world::World::new(),
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
    Box::leak(engine_state)
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    47
}
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    48
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    49
#[no_mangle]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    50
pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    51
    (*engine_state).world.generate_preview();
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    52
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    53
    let land_preview = (*engine_state).world.preview();
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    54
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    55
    *preview = PreviewInfo {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    56
        width: land_preview.width() as u32,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    57
        height: land_preview.height() as u32,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    58
        hedgehogs_number: 0,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    59
        land: land_preview.raw_pixels().as_ptr(),
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    60
    };
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    61
}
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    62
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    63
#[no_mangle]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    64
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
    65
    unsafe {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    66
        Box::from_raw(engine_state);
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    67
    }
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    68
}