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