author | Wuzzy <Wuzzy2@mail.ru> |
Sun, 07 Apr 2019 19:26:16 +0200 | |
changeset 14755 | ab7bf5036314 |
parent 14718 | cc6ab1e3f7d5 |
child 14859 | aed75d439027 |
permissions | -rw-r--r-- |
14299
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
1 |
pub mod instance; |
14709 | 2 |
pub mod ipc; |
14340 | 3 |
mod render; |
14158
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
4 |
mod world; |
14276
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14260
diff
changeset
|
5 |
|
14298 | 6 |
use std::{ |
14299
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
7 |
ffi::CString, |
14298 | 8 |
io::{Read, Write}, |
14299
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
9 |
mem::replace, |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
10 |
os::raw::{c_char, c_void}, |
14298 | 11 |
}; |
14276
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14260
diff
changeset
|
12 |
|
14718 | 13 |
use integral_geometry::Point; |
14 |
||
14710 | 15 |
use self::instance::EngineInstance; |
14193 | 16 |
|
14158
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
17 |
#[repr(C)] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
18 |
#[derive(Copy, Clone)] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
19 |
pub struct PreviewInfo { |
14148 | 20 |
width: u32, |
21 |
height: u32, |
|
22 |
hedgehogs_number: u8, |
|
23 |
land: *const u8, |
|
24 |
} |
|
25 |
||
26 |
#[no_mangle] |
|
14375
ef2fc0210362
Rename protocol_version to hedgewars_engine_protocol_version to ensure use of correct library
unC0Rr
parents:
14340
diff
changeset
|
27 |
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:
14340
diff
changeset
|
28 |
58 |
14148 | 29 |
} |
30 |
||
31 |
#[no_mangle] |
|
14158
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
32 |
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
|
33 |
let engine_state = Box::new(EngineInstance::new()); |
14158
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
34 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
35 |
Box::leak(engine_state) |
14148 | 36 |
} |
14158
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 |
#[no_mangle] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
39 |
pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) { |
14277 | 40 |
(*engine_state).process_ipc_queue(); |
41 |
||
14158
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
42 |
(*engine_state).world.generate_preview(); |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
43 |
|
14378
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
44 |
if let Some(land_preview) = (*engine_state).world.preview() { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
45 |
*preview = PreviewInfo { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
46 |
width: land_preview.width() as u32, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
47 |
height: land_preview.height() as u32, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
48 |
hedgehogs_number: 0, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
49 |
land: land_preview.raw_pixels().as_ptr(), |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
50 |
}; |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
51 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
52 |
} |
14158
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
53 |
|
14378
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
54 |
#[no_mangle] |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14375
diff
changeset
|
55 |
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:
14375
diff
changeset
|
56 |
(*engine_state).world.dispose_preview(); |
14158
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
57 |
} |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
58 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
59 |
#[no_mangle] |
14277 | 60 |
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
|
61 |
unsafe { |
14299
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
62 |
(*engine_state) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
63 |
.ipc |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
64 |
.write(std::slice::from_raw_parts(buf, size)) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
65 |
.unwrap(); |
14276
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14260
diff
changeset
|
66 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14260
diff
changeset
|
67 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14260
diff
changeset
|
68 |
|
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14260
diff
changeset
|
69 |
#[no_mangle] |
14299
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
70 |
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:
14298
diff
changeset
|
71 |
unsafe { |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
72 |
(*engine_state) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
73 |
.ipc |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
74 |
.read(std::slice::from_raw_parts_mut(buf, size)) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
75 |
.unwrap_or(0) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
76 |
} |
14276
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14260
diff
changeset
|
77 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14260
diff
changeset
|
78 |
|
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14260
diff
changeset
|
79 |
#[no_mangle] |
14298 | 80 |
pub extern "C" fn setup_current_gl_context( |
81 |
engine_state: &mut EngineInstance, |
|
82 |
width: u16, |
|
83 |
height: u16, |
|
14299
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
84 |
gl_loader: extern "C" fn(*const c_char) -> *const c_void, |
14298 | 85 |
) { |
14711 | 86 |
gl::load_with(|name| { |
87 |
let c_name = CString::new(name).unwrap(); |
|
88 |
gl_loader(c_name.as_ptr()) |
|
89 |
}); |
|
14712 | 90 |
unsafe { |
91 |
gl::Viewport(0, 0, width as i32, height as i32); |
|
92 |
} |
|
14710 | 93 |
engine_state.world.create_renderer(width, height); |
14298 | 94 |
} |
95 |
||
96 |
#[no_mangle] |
|
97 |
pub extern "C" fn render_frame(engine_state: &mut EngineInstance) { |
|
14712 | 98 |
engine_state.render() |
14298 | 99 |
} |
100 |
||
101 |
#[no_mangle] |
|
14299
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
102 |
pub extern "C" fn advance_simulation(engine_state: &mut EngineInstance, ticks: u32) -> bool { |
14300 | 103 |
engine_state.world.step(); |
104 |
true |
|
14299
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
105 |
} |
14718 | 106 |
|
107 |
#[no_mangle] |
|
108 |
pub extern "C" fn move_camera(engine_state: &mut EngineInstance, delta_x: i32, delta_y: i32) { |
|
109 |
engine_state |
|
110 |
.world |
|
111 |
.move_camera(Point::new(delta_x, delta_y), 0.0); |
|
112 |
} |
|
113 |
||
14299
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14298
diff
changeset
|
114 |
#[no_mangle] |
14158
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
115 |
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
|
116 |
unsafe { |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
117 |
Box::from_raw(engine_state); |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
118 |
} |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14148
diff
changeset
|
119 |
} |