author | unC0Rr |
Wed, 28 Aug 2024 15:34:49 +0200 | |
branch | transitional_engine |
changeset 16022 | 2003b466b279 |
parent 15924 | 9502611bffc1 |
parent 16017 | e8afb1bf2779 |
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::{ |
15924 | 8 |
ffi::{CStr, 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}, |
15891 | 12 |
path::Path, |
14293 | 13 |
}; |
14271
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14255
diff
changeset
|
14 |
|
14713 | 15 |
use integral_geometry::Point; |
16 |
||
14705 | 17 |
use self::instance::EngineInstance; |
14188 | 18 |
|
14153
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
19 |
#[repr(C)] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
20 |
#[derive(Copy, Clone)] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
21 |
pub struct PreviewInfo { |
14143 | 22 |
width: u32, |
23 |
height: u32, |
|
24 |
hedgehogs_number: u8, |
|
25 |
land: *const u8, |
|
26 |
} |
|
27 |
||
14854
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
28 |
#[repr(C)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
29 |
#[derive(Debug, PartialEq, Clone)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
30 |
pub enum SimpleEventType { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
31 |
SwitchHedgehog, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
32 |
Timer, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
33 |
LongJump, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
34 |
HighJump, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
35 |
Accept, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
36 |
Deny, |
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 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
39 |
#[repr(C)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
40 |
#[derive(Debug, PartialEq, Clone)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
41 |
pub enum LongEventType { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
42 |
ArrowUp, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
43 |
ArrowDown, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
44 |
ArrowLeft, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
45 |
ArrowRight, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
46 |
Precision, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
47 |
Attack, |
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 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
50 |
#[repr(C)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
51 |
#[derive(Debug, PartialEq, Clone)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
52 |
pub enum LongEventState { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
53 |
Set, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
54 |
Unset, |
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 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
57 |
#[repr(C)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
58 |
#[derive(Debug, PartialEq, Clone)] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
59 |
pub enum PositionedEventType { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
60 |
CursorMove, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
61 |
CursorClick, |
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 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
64 |
#[no_mangle] |
16016 | 65 |
pub extern "C" fn simple_event(engine_state: &mut EngineInstance, event_type: SimpleEventType) { |
66 |
println!("{:?}", event_type); |
|
67 |
} |
|
14854
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
68 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
69 |
#[no_mangle] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
70 |
pub extern "C" fn long_event( |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
71 |
engine_state: &mut EngineInstance, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
72 |
event_type: LongEventType, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
73 |
state: LongEventState, |
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 |
println!("{:?}: {:?}", event_type, state); |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
76 |
} |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
77 |
|
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
78 |
#[no_mangle] |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
79 |
pub extern "C" fn positioned_event( |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
80 |
engine_state: &mut EngineInstance, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
81 |
event_type: PositionedEventType, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
82 |
x: i32, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
83 |
y: i32, |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
84 |
) { |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
85 |
} |
aed75d439027
Implement external events approach to input user actions into engine.
unc0rr
parents:
14713
diff
changeset
|
86 |
|
14143 | 87 |
#[no_mangle] |
14370
ef2fc0210362
Rename protocol_version to hedgewars_engine_protocol_version to ensure use of correct library
unC0Rr
parents:
14335
diff
changeset
|
88 |
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
|
89 |
58 |
14143 | 90 |
} |
91 |
||
92 |
#[no_mangle] |
|
15891 | 93 |
pub extern "C" fn start_engine(data_path: *const i8) -> *mut EngineInstance { |
94 |
let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap(); |
|
95 |
||
96 |
let engine_state = Box::new(EngineInstance::new(Path::new(&data_path))); |
|
14153
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
97 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
98 |
Box::leak(engine_state) |
14143 | 99 |
} |
14153
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
100 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
101 |
#[no_mangle] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
102 |
pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) { |
14272 | 103 |
(*engine_state).process_ipc_queue(); |
104 |
||
14153
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
105 |
(*engine_state).world.generate_preview(); |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
106 |
|
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
107 |
if let Some(land_preview) = (*engine_state).world.preview() { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
108 |
*preview = PreviewInfo { |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
109 |
width: land_preview.width() as u32, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
110 |
height: land_preview.height() as u32, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
111 |
hedgehogs_number: 0, |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
112 |
land: land_preview.raw_pixels().as_ptr(), |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
113 |
}; |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
114 |
} |
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
115 |
} |
14153
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
116 |
|
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
117 |
#[no_mangle] |
15893
5b3beb90e1a6
Implement generation of c header from rust interface, adapt qmlfrontend
unC0Rr
parents:
15891
diff
changeset
|
118 |
pub extern "C" fn dispose_preview(engine_state: &mut EngineInstance) { |
14373
4409344db447
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
unC0Rr
parents:
14370
diff
changeset
|
119 |
(*engine_state).world.dispose_preview(); |
14153
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
120 |
} |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
121 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
122 |
#[no_mangle] |
14272 | 123 |
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
|
124 |
unsafe { |
14294
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14293
diff
changeset
|
125 |
(*engine_state) |
15265
07e909ba4203
Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents:
14854
diff
changeset
|
126 |
.ipc_channel |
14294
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14293
diff
changeset
|
127 |
.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
|
128 |
.unwrap(); |
14271
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14255
diff
changeset
|
129 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14255
diff
changeset
|
130 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14255
diff
changeset
|
131 |
|
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14255
diff
changeset
|
132 |
#[no_mangle] |
14294
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14293
diff
changeset
|
133 |
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
|
134 |
unsafe { |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14293
diff
changeset
|
135 |
(*engine_state) |
15265
07e909ba4203
Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents:
14854
diff
changeset
|
136 |
.ipc_channel |
14294
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14293
diff
changeset
|
137 |
.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
|
138 |
.unwrap_or(0) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14293
diff
changeset
|
139 |
} |
14271
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14255
diff
changeset
|
140 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14255
diff
changeset
|
141 |
|
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14255
diff
changeset
|
142 |
#[no_mangle] |
14293 | 143 |
pub extern "C" fn setup_current_gl_context( |
144 |
engine_state: &mut EngineInstance, |
|
145 |
width: u16, |
|
146 |
height: u16, |
|
15893
5b3beb90e1a6
Implement generation of c header from rust interface, adapt qmlfrontend
unC0Rr
parents:
15891
diff
changeset
|
147 |
gl_loader: extern "C" fn(*const c_char) -> *mut c_void, |
14293 | 148 |
) { |
14706 | 149 |
gl::load_with(|name| { |
150 |
let c_name = CString::new(name).unwrap(); |
|
151 |
gl_loader(c_name.as_ptr()) |
|
152 |
}); |
|
14707 | 153 |
unsafe { |
154 |
gl::Viewport(0, 0, width as i32, height as i32); |
|
155 |
} |
|
14705 | 156 |
engine_state.world.create_renderer(width, height); |
16017
e8afb1bf2779
Implement triggering advancement of simulation in qmlfrontend
unC0Rr
parents:
16016
diff
changeset
|
157 |
engine_state.world.init_renderer(); |
14293 | 158 |
} |
159 |
||
160 |
#[no_mangle] |
|
161 |
pub extern "C" fn render_frame(engine_state: &mut EngineInstance) { |
|
14707 | 162 |
engine_state.render() |
14293 | 163 |
} |
164 |
||
165 |
#[no_mangle] |
|
14294
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14293
diff
changeset
|
166 |
pub extern "C" fn advance_simulation(engine_state: &mut EngineInstance, ticks: u32) -> bool { |
14295 | 167 |
engine_state.world.step(); |
168 |
true |
|
14294
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14293
diff
changeset
|
169 |
} |
14713 | 170 |
|
171 |
#[no_mangle] |
|
172 |
pub extern "C" fn move_camera(engine_state: &mut EngineInstance, delta_x: i32, delta_y: i32) { |
|
173 |
engine_state |
|
174 |
.world |
|
175 |
.move_camera(Point::new(delta_x, delta_y), 0.0); |
|
176 |
} |
|
177 |
||
14294
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14293
diff
changeset
|
178 |
#[no_mangle] |
14153
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
179 |
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
|
180 |
unsafe { |
15893
5b3beb90e1a6
Implement generation of c header from rust interface, adapt qmlfrontend
unC0Rr
parents:
15891
diff
changeset
|
181 |
drop(Box::from_raw(engine_state)); |
14153
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
182 |
} |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14143
diff
changeset
|
183 |
} |