author | alfadur |
Wed, 28 Nov 2018 21:35:54 +0300 | |
changeset 14356 | 97be4f6bf264 |
parent 14316 | fd8e0e52d5bd |
child 14391 | ef2fc0210362 |
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; |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
2 |
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 |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
6 |
use gfx::{format::Formatted, Encoder}; |
14314 | 7 |
use std::{ |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
8 |
ffi::CString, |
14314 | 9 |
io::{Read, Write}, |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
10 |
mem::replace, |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
11 |
os::raw::{c_char, c_void}, |
14314 | 12 |
}; |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
13 |
|
14314 | 14 |
use gfx_device_gl as gfx_gl; |
15 |
||
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
16 |
use self::instance::{EngineGlContext, EngineInstance}; |
14209 | 17 |
|
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
18 |
#[repr(C)] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
19 |
#[derive(Copy, Clone)] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
20 |
pub struct PreviewInfo { |
14164 | 21 |
width: u32, |
22 |
height: u32, |
|
23 |
hedgehogs_number: u8, |
|
24 |
land: *const u8, |
|
25 |
} |
|
26 |
||
27 |
#[no_mangle] |
|
28 |
pub extern "C" fn protocol_version() -> u32 { |
|
29 |
56 |
|
30 |
} |
|
31 |
||
32 |
#[no_mangle] |
|
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
33 |
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
|
34 |
let engine_state = Box::new(EngineInstance::new()); |
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 |
Box::leak(engine_state) |
14164 | 37 |
} |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
38 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
39 |
#[no_mangle] |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
40 |
pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) { |
14293 | 41 |
(*engine_state).process_ipc_queue(); |
42 |
||
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
43 |
(*engine_state).world.generate_preview(); |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
44 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
45 |
let land_preview = (*engine_state).world.preview(); |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
46 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
47 |
*preview = PreviewInfo { |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
48 |
width: land_preview.width() as u32, |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
49 |
height: land_preview.height() as u32, |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
50 |
hedgehogs_number: 0, |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
51 |
land: land_preview.raw_pixels().as_ptr(), |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
52 |
}; |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
53 |
} |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
54 |
|
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
55 |
#[no_mangle] |
14293 | 56 |
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
|
57 |
unsafe { |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
58 |
(*engine_state) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
59 |
.ipc |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
60 |
.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
|
61 |
.unwrap(); |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
62 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
63 |
} |
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 |
#[no_mangle] |
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
66 |
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
|
67 |
unsafe { |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
68 |
(*engine_state) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
69 |
.ipc |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
70 |
.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
|
71 |
.unwrap_or(0) |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
72 |
} |
14292
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
73 |
} |
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
74 |
|
1aac8a62be6f
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
unC0Rr
parents:
14276
diff
changeset
|
75 |
#[no_mangle] |
14314 | 76 |
pub extern "C" fn setup_current_gl_context( |
77 |
engine_state: &mut EngineInstance, |
|
78 |
width: u16, |
|
79 |
height: u16, |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
80 |
gl_loader: extern "C" fn(*const c_char) -> *const c_void, |
14314 | 81 |
) { |
82 |
let (device, mut factory) = gfx_gl::create(|name| { |
|
83 |
let c_name = CString::new(name).unwrap(); |
|
84 |
gl_loader(c_name.as_ptr()) |
|
85 |
}); |
|
86 |
||
87 |
let dimensions = (width, height, 1u16, gfx::texture::AaMode::Single); |
|
88 |
let (render_target, depth_buffer) = gfx_gl::create_main_targets_raw( |
|
89 |
dimensions, |
|
90 |
gfx::format::Rgba8::get_format().0, |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
91 |
gfx::format::Depth::get_format().0, |
14314 | 92 |
); |
93 |
||
94 |
let mut command_buffer: Encoder<_, _> = factory.create_command_buffer().into(); |
|
95 |
||
96 |
engine_state.gl_context = Some(EngineGlContext { |
|
97 |
device, |
|
98 |
factory, |
|
99 |
render_target: gfx::memory::Typed::new(render_target), |
|
100 |
depth_buffer: gfx::memory::Typed::new(depth_buffer), |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
101 |
command_buffer, |
14314 | 102 |
}) |
103 |
} |
|
104 |
||
105 |
#[no_mangle] |
|
106 |
pub extern "C" fn render_frame(engine_state: &mut EngineInstance) { |
|
107 |
let mut context = replace(&mut engine_state.gl_context, None); |
|
108 |
if let Some(ref mut c) = context { |
|
109 |
engine_state.render(&mut c.command_buffer, &mut c.render_target) |
|
110 |
} |
|
111 |
replace(&mut engine_state.gl_context, context); |
|
112 |
} |
|
113 |
||
114 |
#[no_mangle] |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
115 |
pub extern "C" fn advance_simulation(engine_state: &mut EngineInstance, ticks: u32) -> bool { |
14316 | 116 |
engine_state.world.step(); |
117 |
true |
|
14315
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
118 |
} |
21be7838a127
Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents:
14314
diff
changeset
|
119 |
#[no_mangle] |
14174
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
120 |
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
|
121 |
unsafe { |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
122 |
Box::from_raw(engine_state); |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
123 |
} |
b273b43b16d2
Make hedgewars-engine crate produce shared library, implement some basics
unC0Rr
parents:
14164
diff
changeset
|
124 |
} |