rust/lib-hedgewars-engine/src/lib.rs
author unc0rr
Tue, 13 Nov 2018 07:43:36 +0100
changeset 14210 a8fe9cd51154
parent 14193 3b83ec44e10b
child 14212 bb2f301d4fe0
permissions -rw-r--r--
Move engine messages into separate lib for reuse
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
extern crate integral_geometry;
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     2
extern crate land2d;
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     3
extern crate landgen;
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
     4
extern crate lfprng;
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
     5
extern crate gfx;
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
     6
extern crate gfx_device_gl;
14158
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
mod world;
14174
e2c51c8e0b2e Start outlining some core engine stuff
unc0rr
parents: 14171
diff changeset
     9
mod command;
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    10
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    11
#[repr(C)]
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    12
pub struct EngineInstance {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    13
    world: world::World,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    14
}
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    15
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    16
impl EngineInstance {
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    17
    pub fn new() -> Self {
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    18
        let world = world::World::new();
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    19
        Self { world }
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    20
    }
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
    pub fn render<R, C>(
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    23
        &self,
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    24
        context: &mut gfx::Encoder<R, C>,
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    25
        target: &gfx::handle::RenderTargetView<R, gfx::format::Rgba8>)
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    26
        where R: gfx::Resources,
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    27
              C: gfx::CommandBuffer<R>
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    28
    {
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    29
        context.clear(target, [0.0, 0.5, 0.0, 1.0]);
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    30
    }
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    31
}
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14174
diff changeset
    32
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    33
#[repr(C)]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    34
#[derive(Copy, Clone)]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    35
pub struct PreviewInfo {
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    36
    width: u32,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    37
    height: u32,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    38
    hedgehogs_number: u8,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    39
    land: *const u8,
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    40
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    41
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    42
#[no_mangle]
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    43
pub extern "C" fn protocol_version() -> u32 {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    44
    56
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    45
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    46
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    47
#[no_mangle]
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    48
pub extern "C" fn start_engine() -> *mut EngineInstance {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    49
    let engine_state = Box::new(EngineInstance {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    50
        world: world::World::new(),
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    51
    });
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
    Box::leak(engine_state)
14148
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    54
}
14158
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    55
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    56
#[no_mangle]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    57
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
    58
    (*engine_state).world.generate_preview();
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
    let land_preview = (*engine_state).world.preview();
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
    *preview = PreviewInfo {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    63
        width: land_preview.width() as u32,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    64
        height: land_preview.height() as u32,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    65
        hedgehogs_number: 0,
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    66
        land: land_preview.raw_pixels().as_ptr(),
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
}
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    69
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    70
#[no_mangle]
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    71
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
    72
    unsafe {
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    73
        Box::from_raw(engine_state);
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    74
    }
b273b43b16d2 Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents: 14148
diff changeset
    75
}