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