author | sheepluva |
Fri, 25 Jun 2021 21:44:29 +0200 | |
changeset 15828 | 36816af30583 |
parent 15827 | 61da40b657fa |
child 15829 | 67f83b7dfc6c |
permissions | -rw-r--r-- |
14187 | 1 |
extern crate libloading; |
2 |
||
3 |
use libloading::{Library, Symbol}; |
|
4 |
use std::ops::Deref; |
|
15828
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
5 |
use std::cmp::{min,max}; |
15827
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
6 |
use std::env; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
7 |
use getopts::Options; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
8 |
use std::io::prelude::*; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
9 |
use std::io::{self, Read}; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
10 |
use std::net::{Shutdown, TcpStream}; |
14187 | 11 |
|
12 |
struct EngineInstance {} |
|
13 |
||
14 |
struct Engine<'a> { |
|
15 |
protocol_version: Symbol<'a, unsafe fn() -> u32>, |
|
16 |
start_engine: Symbol<'a, unsafe fn() -> *mut EngineInstance>, |
|
15827
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
17 |
generate_preview: Symbol<'a, unsafe fn(engine_state: &mut EngineInstance, preview: &mut PreviewInfo)>, |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
18 |
dispose_preview: Symbol<'a, unsafe fn(engine_state: &mut EngineInstance, preview: &mut PreviewInfo)>, |
14187 | 19 |
cleanup: Symbol<'a, unsafe fn(engine_state: *mut EngineInstance)>, |
15827
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
20 |
send_ipc: Symbol<'a, unsafe fn(engine_state: &mut EngineInstance, buf: *const u8, size: usize)>, |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
21 |
read_ipc: Symbol<'a, unsafe fn(engine_state: &mut EngineInstance, buf: *mut u8, size: usize) -> usize>, |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
22 |
} |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
23 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
24 |
#[repr(C)] |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
25 |
#[derive(Copy, Clone)] |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
26 |
struct PreviewInfo { |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
27 |
width: u32, |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
28 |
height: u32, |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
29 |
hedgehogs_number: u8, |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
30 |
land: *const u8, |
14187 | 31 |
} |
32 |
||
15828
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
33 |
const PREVIEW_WIDTH: u32 = 256; |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
34 |
const PREVIEW_HEIGHT: u32 = 128; |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
35 |
const PREVIEW_NPIXELS: usize = (PREVIEW_WIDTH * PREVIEW_HEIGHT) as usize; |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
36 |
const SCALE_FACTOR: u32 = 16; |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
37 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
38 |
fn resize_mono_preview(mono_pixels: &[u8], in_width: u32, in_height: u32, preview_pixels: &mut [u8]) { |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
39 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
40 |
assert!(mono_pixels.len() == (in_width * in_height) as usize); |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
41 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
42 |
let v_offset: u32 = max(0, PREVIEW_HEIGHT as i64 - (in_height / SCALE_FACTOR) as i64) as u32; |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
43 |
let h_offset: u32 = max(0, (PREVIEW_WIDTH as i64 / 2) - (in_width / SCALE_FACTOR / 2) as i64) as u32; |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
44 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
45 |
for y in v_offset..PREVIEW_HEIGHT { |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
46 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
47 |
let in_y = v_offset + (y * SCALE_FACTOR); |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
48 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
49 |
for x in h_offset..(PREVIEW_WIDTH - h_offset) { |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
50 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
51 |
let in_x = h_offset + (x * SCALE_FACTOR); |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
52 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
53 |
let out_px_address = (PREVIEW_WIDTH * y + x) as usize; |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
54 |
let in_px_address = (in_width * in_y + in_x) as usize; |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
55 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
56 |
preview_pixels[out_px_address] = mono_pixels[in_px_address]; |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
57 |
} |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
58 |
} |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
59 |
} |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
60 |
|
14187 | 61 |
fn main() { |
62 |
let hwlib = Library::new("libhedgewars_engine.so").unwrap(); |
|
63 |
||
64 |
unsafe { |
|
65 |
let engine = Engine { |
|
15824
d5c37e78ab83
hedgewars-engine (rust): fix "protocol_version" -> "hedgewars_engine_protocol_version"
sheepluva
parents:
14187
diff
changeset
|
66 |
protocol_version: hwlib.get(b"hedgewars_engine_protocol_version").unwrap(), |
14187 | 67 |
start_engine: hwlib.get(b"start_engine").unwrap(), |
15827
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
68 |
generate_preview: hwlib.get(b"generate_preview").unwrap(), |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
69 |
dispose_preview: hwlib.get(b"dispose_preview").unwrap(), |
14187 | 70 |
cleanup: hwlib.get(b"cleanup").unwrap(), |
15827
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
71 |
send_ipc: hwlib.get(b"send_ipc").unwrap(), |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
72 |
read_ipc: hwlib.get(b"read_ipc").unwrap(), |
14187 | 73 |
}; |
74 |
||
75 |
println!("Hedgewars engine, protocol version {}", engine.protocol_version.deref()()); |
|
15827
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
76 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
77 |
let args: Vec<String> = env::args().collect(); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
78 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
79 |
let mut opts = getopts::Options::new(); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
80 |
opts.optflag("", "internal", "[internal]"); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
81 |
opts.optflag("", "landpreview", "[internal]"); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
82 |
opts.optflag("", "recorder", "[internal]"); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
83 |
opts.optopt("", "port", "[internal]", "PORT"); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
84 |
opts.optopt("", "user-prefix", "Set the path to the custom data folder to find game content", "PATH_TO_FOLDER"); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
85 |
opts.optopt("", "prefix", "Set the path to the system game data folder", "PATH_TO_FOLDER"); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
86 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
87 |
let matches = match opts.parse(&args[1..]) { |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
88 |
Ok(m) => { m } |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
89 |
Err(f) => { panic!(f.to_string()) } |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
90 |
}; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
91 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
92 |
let engine_state = &mut *engine.start_engine.deref()(); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
93 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
94 |
let port: String = matches.opt_str("port").unwrap(); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
95 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
96 |
println!("PORT: {}", port); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
97 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
98 |
if matches.opt_present("landpreview") { |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
99 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
100 |
let mut stream = TcpStream::connect(format!("127.0.0.1:{}", port)).expect("Failed to connect to IPC port. Feelsbadman."); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
101 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
102 |
//stream.write(b"\x01C").unwrap(); // config |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
103 |
//stream.write(b"\x01?").unwrap(); // ping |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
104 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
105 |
let mut buf = [0;1]; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
106 |
loop { |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
107 |
let bytes_read = stream.read(&mut buf).unwrap(); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
108 |
if bytes_read == 0 { |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
109 |
break; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
110 |
} |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
111 |
engine.send_ipc.deref()(engine_state, &buf[0], buf.len()); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
112 |
// this looks like length 1 is being announced |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
113 |
if buf[0] == 1 { |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
114 |
let bytes_read = stream.read(&mut buf).unwrap(); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
115 |
if bytes_read == 0 { |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
116 |
break; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
117 |
} |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
118 |
if buf[0] == 33 { |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
119 |
println!("Ping? Pong!"); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
120 |
break; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
121 |
} |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
122 |
} |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
123 |
}; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
124 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
125 |
let preview_info = &mut PreviewInfo { |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
126 |
width: 0, |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
127 |
height: 0, |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
128 |
hedgehogs_number: 0, |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
129 |
land: std::ptr::null(), |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
130 |
}; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
131 |
|
15828
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
132 |
println!("Generating preview..."); |
15827
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
133 |
|
15828
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
134 |
engine.generate_preview.deref()(engine_state, preview_info); |
15827
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
135 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
136 |
//println!("Preview: w = {}, h = {}, n = {}", preview_info.width, preview_info.height, preview_info.hedgehogs_number); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
137 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
138 |
let land_size: usize = (preview_info.width * preview_info.height) as usize; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
139 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
140 |
let land_array: &[u8] = std::slice::from_raw_parts(preview_info.land, land_size); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
141 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
142 |
const PREVIEW_WIDTH: u32 = 256; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
143 |
const PREVIEW_HEIGHT: u32 = 128; |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
144 |
|
15828
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
145 |
println!("Resizing preview..."); |
15827
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
146 |
|
15828
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
147 |
let preview_image: &mut [u8] = &mut [0; PREVIEW_NPIXELS]; |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
148 |
resize_mono_preview(land_array, preview_info.width, preview_info.height, preview_image); |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
149 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
150 |
println!("Sending preview..."); |
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
151 |
|
36816af30583
hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents:
15827
diff
changeset
|
152 |
stream.write(preview_image).unwrap(); |
15827
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
153 |
stream.flush().unwrap(); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
154 |
stream.write(&[preview_info.hedgehogs_number]).unwrap(); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
155 |
stream.flush().unwrap(); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
156 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
157 |
println!("Preview sent, disconnect"); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
158 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
159 |
stream.shutdown(Shutdown::Both).expect("IPC shutdown call failed"); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
160 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
161 |
engine.dispose_preview.deref()(engine_state, preview_info); |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
162 |
} |
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
163 |
|
61da40b657fa
hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents:
15824
diff
changeset
|
164 |
engine.cleanup.deref()(engine_state); |
14187 | 165 |
} |
166 |
} |