author | alfadur |
Thu, 12 Nov 2020 00:24:58 +0300 | |
changeset 15781 | c929e25a7da2 |
parent 15286 | 07e909ba4203 |
child 15797 | 95402fa4e191 |
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 |
|
14734 | 13 |
use integral_geometry::Point; |
14 |
||
14726 | 15 |
use self::instance::EngineInstance; |
14209 | 16 |
|
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
17 |
#[repr(C)] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
18 |
#[derive(Copy, Clone)] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
19 |
pub struct PreviewInfo { |
14164 | 20 |
width: u32, |
21 |
height: u32, |
|
22 |
hedgehogs_number: u8, |
|
23 |
land: *const u8, |
|
24 |
} |
|
25 |
||
14875
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
26 |
#[repr(C)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
27 |
#[derive(Debug, PartialEq, Clone)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
28 |
pub enum SimpleEventType { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
29 |
SwitchHedgehog, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
30 |
Timer, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
31 |
LongJump, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
32 |
HighJump, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
33 |
Accept, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
34 |
Deny, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
35 |
} |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
36 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
37 |
#[repr(C)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
38 |
#[derive(Debug, PartialEq, Clone)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
39 |
pub enum LongEventType { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
40 |
ArrowUp, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
41 |
ArrowDown, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
42 |
ArrowLeft, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
43 |
ArrowRight, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
44 |
Precision, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
45 |
Attack, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
46 |
} |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
47 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
48 |
#[repr(C)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
49 |
#[derive(Debug, PartialEq, Clone)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
50 |
pub enum LongEventState { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
51 |
Set, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
52 |
Unset, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
53 |
} |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
54 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
55 |
#[repr(C)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
56 |
#[derive(Debug, PartialEq, Clone)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
57 |
pub enum PositionedEventType { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
58 |
CursorMove, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
59 |
CursorClick, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
60 |
} |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
61 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
62 |
#[no_mangle] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
63 |
pub extern "C" fn simple_event(engine_state: &mut EngineInstance, event_type: SimpleEventType) {} |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
64 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
65 |
#[no_mangle] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
66 |
pub extern "C" fn long_event( |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
67 |
engine_state: &mut EngineInstance, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
68 |
event_type: LongEventType, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
69 |
state: LongEventState, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
70 |
) { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
71 |
println!("{:?}: {:?}", event_type, state); |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
72 |
} |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
73 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
74 |
#[no_mangle] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
75 |
pub extern "C" fn positioned_event( |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
76 |
engine_state: &mut EngineInstance, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
77 |
event_type: PositionedEventType, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
78 |
x: i32, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
79 |
y: i32, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
80 |
) { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
81 |
} |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14734
diff
changeset
|
82 |
|
14164 | 83 |
#[no_mangle] |
14391
ef2fc0210362
Rename protocol_version to hedgewars_engine_protocol_version to ensure use of correct library
unC0Rr
parents:
14356
diff
changeset
|
84 |
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
|
85 |
58 |
14164 | 86 |
} |
87 |
||
88 |
#[no_mangle] |
|
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
89 |
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
|
90 |
let engine_state = Box::new(EngineInstance::new()); |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
91 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
92 |
Box::leak(engine_state) |
14164 | 93 |
} |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
94 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
95 |
#[no_mangle] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
96 |
pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) { |
14293 | 97 |
(*engine_state).process_ipc_queue(); |
98 |
||
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
99 |
(*engine_state).world.generate_preview(); |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
100 |
|
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
101 |
if let Some(land_preview) = (*engine_state).world.preview() { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
102 |
*preview = PreviewInfo { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
103 |
width: land_preview.width() as u32, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
104 |
height: land_preview.height() as u32, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
105 |
hedgehogs_number: 0, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
106 |
land: land_preview.raw_pixels().as_ptr(), |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
107 |
}; |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
108 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
109 |
} |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
110 |
|
14394
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
111 |
#[no_mangle] |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14391
diff
changeset
|
112 |
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
|
113 |
(*engine_state).world.dispose_preview(); |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
114 |
} |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
115 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
116 |
#[no_mangle] |
14293 | 117 |
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
|
118 |
unsafe { |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
119 |
(*engine_state) |
15286
07e909ba4203
Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents:
14875
diff
changeset
|
120 |
.ipc_channel |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
121 |
.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
|
122 |
.unwrap(); |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
123 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
124 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
125 |
|
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
126 |
#[no_mangle] |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
127 |
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
|
128 |
unsafe { |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
129 |
(*engine_state) |
15286
07e909ba4203
Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents:
14875
diff
changeset
|
130 |
.ipc_channel |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
131 |
.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
|
132 |
.unwrap_or(0) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
133 |
} |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
134 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
135 |
|
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
136 |
#[no_mangle] |
14314 | 137 |
pub extern "C" fn setup_current_gl_context( |
138 |
engine_state: &mut EngineInstance, |
|
139 |
width: u16, |
|
140 |
height: u16, |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
141 |
gl_loader: extern "C" fn(*const c_char) -> *const c_void, |
14314 | 142 |
) { |
14727 | 143 |
gl::load_with(|name| { |
144 |
let c_name = CString::new(name).unwrap(); |
|
145 |
gl_loader(c_name.as_ptr()) |
|
146 |
}); |
|
14728 | 147 |
unsafe { |
148 |
gl::Viewport(0, 0, width as i32, height as i32); |
|
149 |
} |
|
14726 | 150 |
engine_state.world.create_renderer(width, height); |
14314 | 151 |
} |
152 |
||
153 |
#[no_mangle] |
|
154 |
pub extern "C" fn render_frame(engine_state: &mut EngineInstance) { |
|
14728 | 155 |
engine_state.render() |
14314 | 156 |
} |
157 |
||
158 |
#[no_mangle] |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
159 |
pub extern "C" fn advance_simulation(engine_state: &mut EngineInstance, ticks: u32) -> bool { |
14316 | 160 |
engine_state.world.step(); |
161 |
true |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
162 |
} |
14734 | 163 |
|
164 |
#[no_mangle] |
|
165 |
pub extern "C" fn move_camera(engine_state: &mut EngineInstance, delta_x: i32, delta_y: i32) { |
|
166 |
engine_state |
|
167 |
.world |
|
168 |
.move_camera(Point::new(delta_x, delta_y), 0.0); |
|
169 |
} |
|
170 |
||
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
171 |
#[no_mangle] |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
172 |
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
|
173 |
unsafe { |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
174 |
Box::from_raw(engine_state); |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
175 |
} |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
176 |
} |