author | alfadur |
Fri, 22 Mar 2019 20:38:50 +0300 | |
changeset 14728 | 5237b4c44d11 |
parent 14727 | 88b0efba77b9 |
child 14734 | cc6ab1e3f7d5 |
permissions | -rw-r--r-- |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
1 |
pub mod instance; |
14725 | 2 |
pub mod ipc; |
14356 | 3 |
mod render; |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
4 |
mod world; |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
5 |
|
14314 | 6 |
use std::{ |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
7 |
ffi::CString, |
14314 | 8 |
io::{Read, Write}, |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
9 |
mem::replace, |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
10 |
os::raw::{c_char, c_void}, |
14314 | 11 |
}; |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
12 |
|
14726 | 13 |
use self::instance::EngineInstance; |
14209 | 14 |
|
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
15 |
#[repr(C)] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
16 |
#[derive(Copy, Clone)] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
17 |
pub struct PreviewInfo { |
14164 | 18 |
width: u32, |
19 |
height: u32, |
|
20 |
hedgehogs_number: u8, |
|
21 |
land: *const u8, |
|
22 |
} |
|
23 |
||
24 |
#[no_mangle] |
|
14391
ef2fc0210362
Rename protocol_version to hedgewars_engine_protocol_version to ensure use of correct library
unC0Rr
parents:
14356
diff
changeset
|
25 |
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:
14356
diff
changeset
|
26 |
58 |
14164 | 27 |
} |
28 |
||
29 |
#[no_mangle] |
|
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
30 |
pub extern "C" fn start_engine() -> *mut EngineInstance { |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
31 |
let engine_state = Box::new(EngineInstance::new()); |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
32 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
33 |
Box::leak(engine_state) |
14164 | 34 |
} |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
35 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
36 |
#[no_mangle] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
37 |
pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) { |
14293 | 38 |
(*engine_state).process_ipc_queue(); |
39 |
||
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
40 |
(*engine_state).world.generate_preview(); |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
41 |
|
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
42 |
if let Some(land_preview) = (*engine_state).world.preview() { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
43 |
*preview = PreviewInfo { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
44 |
width: land_preview.width() as u32, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
45 |
height: land_preview.height() as u32, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
46 |
hedgehogs_number: 0, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
47 |
land: land_preview.raw_pixels().as_ptr(), |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
48 |
}; |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
49 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
50 |
} |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
51 |
|
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
52 |
#[no_mangle] |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
53 |
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:
14391
diff
changeset
|
54 |
(*engine_state).world.dispose_preview(); |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
55 |
} |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
56 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
57 |
#[no_mangle] |
14293 | 58 |
pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const u8, size: usize) { |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
59 |
unsafe { |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
60 |
(*engine_state) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
61 |
.ipc |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
62 |
.write(std::slice::from_raw_parts(buf, size)) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
63 |
.unwrap(); |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
64 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
65 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
66 |
|
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
67 |
#[no_mangle] |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
68 |
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:
14314
diff
changeset
|
69 |
unsafe { |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
70 |
(*engine_state) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
71 |
.ipc |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
72 |
.read(std::slice::from_raw_parts_mut(buf, size)) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
73 |
.unwrap_or(0) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
74 |
} |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
75 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
76 |
|
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
77 |
#[no_mangle] |
14314 | 78 |
pub extern "C" fn setup_current_gl_context( |
79 |
engine_state: &mut EngineInstance, |
|
80 |
width: u16, |
|
81 |
height: u16, |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
82 |
gl_loader: extern "C" fn(*const c_char) -> *const c_void, |
14314 | 83 |
) { |
14727 | 84 |
gl::load_with(|name| { |
85 |
let c_name = CString::new(name).unwrap(); |
|
86 |
gl_loader(c_name.as_ptr()) |
|
87 |
}); |
|
14728 | 88 |
unsafe { |
89 |
gl::Viewport(0, 0, width as i32, height as i32); |
|
90 |
} |
|
14726 | 91 |
engine_state.world.create_renderer(width, height); |
14314 | 92 |
} |
93 |
||
94 |
#[no_mangle] |
|
95 |
pub extern "C" fn render_frame(engine_state: &mut EngineInstance) { |
|
14728 | 96 |
engine_state.render() |
14314 | 97 |
} |
98 |
||
99 |
#[no_mangle] |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
100 |
pub extern "C" fn advance_simulation(engine_state: &mut EngineInstance, ticks: u32) -> bool { |
14316 | 101 |
engine_state.world.step(); |
102 |
true |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
103 |
} |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
104 |
#[no_mangle] |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
105 |
pub extern "C" fn cleanup(engine_state: *mut EngineInstance) { |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
106 |
unsafe { |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
107 |
Box::from_raw(engine_state); |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
108 |
} |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
109 |
} |