rust/hedgewars-engine/src/main.rs
author sheepluva
Fri, 25 Jun 2021 22:16:41 +0200
changeset 15829 67f83b7dfc6c
parent 15828 36816af30583
child 15830 1fa2f38c5700
permissions -rw-r--r--
hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
     1
extern crate libloading;
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
     2
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
     3
use libloading::{Library, Symbol};
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
     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
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    11
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    12
struct EngineInstance {}
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    13
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    14
struct Engine<'a> {
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    15
    protocol_version: Symbol<'a, unsafe fn() -> u32>,
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    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
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    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
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    31
}
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    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;
15829
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    37
const VALUE_PER_INPIXEL: u8 = 1;
15828
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    38
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    39
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
    40
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    41
    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
    42
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 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
    44
    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
    45
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    46
    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
    47
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    48
        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
    49
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    50
        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
    51
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    52
            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
    53
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 out_px_address = (PREVIEW_WIDTH * y + x) as usize;
15829
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    55
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    56
            let mut in_px_address = (in_width * in_y + in_x) as usize;
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    57
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    58
            let mut value = 0;
15828
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    59
15829
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    60
            for i in 0..SCALE_FACTOR as usize {
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    61
                for j in 0..SCALE_FACTOR as usize {
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    62
                    if (value < 0xff) && (mono_pixels[in_px_address + j] != 0) {
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    63
                        value += VALUE_PER_INPIXEL;
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    64
                    }
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    65
                }
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    66
                in_px_address += in_width as usize;
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    67
            }
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    68
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    69
            preview_pixels[out_px_address] = value;
15828
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    70
        }
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    71
    }
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    72
}
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    73
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    74
fn main() {
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    75
    let hwlib = Library::new("libhedgewars_engine.so").unwrap();
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    76
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    77
    unsafe {
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    78
        let engine = Engine {
15824
d5c37e78ab83 hedgewars-engine (rust): fix "protocol_version" -> "hedgewars_engine_protocol_version"
sheepluva
parents: 14187
diff changeset
    79
            protocol_version: hwlib.get(b"hedgewars_engine_protocol_version").unwrap(),
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    80
            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
    81
            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
    82
            dispose_preview: hwlib.get(b"dispose_preview").unwrap(),
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    83
            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
    84
            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
    85
            read_ipc: hwlib.get(b"read_ipc").unwrap(),
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    86
        };
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    87
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    88
        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
    89
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
    90
        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
    91
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
    92
        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
    93
        opts.optflag("", "internal", "[internal]");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
    94
        opts.optflag("", "landpreview", "[internal]");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
    95
        opts.optflag("", "recorder", "[internal]");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
    96
        opts.optopt("", "port", "[internal]", "PORT");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
    97
        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
    98
        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
    99
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   100
        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
   101
            Ok(m) => { m }
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   102
            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
   103
        };
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 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
   106
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   107
        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
   108
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   109
        println!("PORT: {}", port);
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
        if matches.opt_present("landpreview") {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   112
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   113
            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
   114
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   115
            //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
   116
            //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
   117
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   118
            let mut buf = [0;1];
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   119
            loop {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   120
                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
   121
                if bytes_read == 0 {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   122
                    break;
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
                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
   125
                // 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
   126
                if buf[0] == 1 {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   127
                    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
   128
                    if bytes_read == 0 {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   129
                        break;
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
                    if buf[0] == 33 {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   132
                        println!("Ping? Pong!");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   133
                        break;
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   134
                    }
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
            };
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 preview_info = &mut PreviewInfo {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   139
                width: 0,
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   140
                height: 0,
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   141
                hedgehogs_number: 0,
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   142
                land: std::ptr::null(),
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   143
            };
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!("Generating 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
            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
   148
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   149
            //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
   150
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   151
            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
   152
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   153
            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
   154
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   155
            const PREVIEW_WIDTH: u32 = 256;
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   156
            const PREVIEW_HEIGHT: u32 = 128;
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   157
15828
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
   158
            println!("Resizing preview...");
15827
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   159
15828
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
   160
            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
   161
            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
   162
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
   163
            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
   164
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
   165
            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
   166
            stream.flush().unwrap();
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   167
            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
   168
            stream.flush().unwrap();
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   169
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   170
            println!("Preview sent, disconnect");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   171
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   172
            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
   173
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   174
            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
   175
        }
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   176
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   177
        engine.cleanup.deref()(engine_state);
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
   178
    }
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
   179
}