rust/hedgewars-engine/src/main.rs
author unC0Rr
Tue, 22 Aug 2023 08:35:46 +0200
branchtransitional_engine
changeset 16004 2146cb7be36f
parent 15918 8a6a2d931bae
permissions -rw-r--r--
Merge default
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
15830
1fa2f38c5700 add comment header to function to pretend that I care
sheepluva
parents: 15829
diff changeset
    39
/// Resizes the land preview from the library into appropriate format for --preview command.
1fa2f38c5700 add comment header to function to pretend that I care
sheepluva
parents: 15829
diff changeset
    40
///
1fa2f38c5700 add comment header to function to pretend that I care
sheepluva
parents: 15829
diff changeset
    41
/// # Arguments
1fa2f38c5700 add comment header to function to pretend that I care
sheepluva
parents: 15829
diff changeset
    42
///
1fa2f38c5700 add comment header to function to pretend that I care
sheepluva
parents: 15829
diff changeset
    43
/// * `mono_pixels` - Raw pixels of a land preview (monochrome, 0 = empty, else = filled)
1fa2f38c5700 add comment header to function to pretend that I care
sheepluva
parents: 15829
diff changeset
    44
/// * `in_width` - Width of the preview stored in `mono_pixels`
1fa2f38c5700 add comment header to function to pretend that I care
sheepluva
parents: 15829
diff changeset
    45
/// * `in_height` - Height of the preview stored in `mono_pixels`
1fa2f38c5700 add comment header to function to pretend that I care
sheepluva
parents: 15829
diff changeset
    46
/// * `preview_pixels` - Used as **output** for a resized and (kinda) anti-aliased grayscale preview
15828
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    47
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
    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
    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
    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 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
    52
    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
    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
    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
    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
        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
    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
        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
    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
            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
    61
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    62
            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
    63
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    64
            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
    65
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    66
            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
    67
15829
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    68
            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
    69
                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
    70
                    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
    71
                        value += VALUE_PER_INPIXEL;
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    72
                    }
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    73
                }
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    74
                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
    75
            }
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    76
67f83b7dfc6c hedgewars-engine (rust): do something I consider approximate linear filtering for prettier previews
sheepluva
parents: 15828
diff changeset
    77
            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
    78
        }
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    79
    }
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    80
}
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
    81
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    82
fn main() {
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    83
    let hwlib = Library::new("libhedgewars_engine.so").unwrap();
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    84
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    85
    unsafe {
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    86
        let engine = Engine {
15824
d5c37e78ab83 hedgewars-engine (rust): fix "protocol_version" -> "hedgewars_engine_protocol_version"
sheepluva
parents: 14187
diff changeset
    87
            protocol_version: hwlib.get(b"hedgewars_engine_protocol_version").unwrap(),
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    88
            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
    89
            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
    90
            dispose_preview: hwlib.get(b"dispose_preview").unwrap(),
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    91
            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
    92
            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
    93
            read_ipc: hwlib.get(b"read_ipc").unwrap(),
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    94
        };
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    95
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
    96
        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
    97
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
    98
        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
    99
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   100
        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
   101
        opts.optflag("", "internal", "[internal]");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   102
        opts.optflag("", "landpreview", "[internal]");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   103
        opts.optflag("", "recorder", "[internal]");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   104
        opts.optopt("", "port", "[internal]", "PORT");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   105
        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
   106
        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
   107
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   108
        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
   109
            Ok(m) => { m }
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   110
            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
   111
        };
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 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
   114
15918
8a6a2d931bae Implement data path
unc0rr
parents: 15830
diff changeset
   115
        let port: String = matches.opt_str("port").expect("Need IPC port number!");
15827
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   116
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   117
        println!("PORT: {}", port);
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   118
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   119
        if matches.opt_present("landpreview") {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   120
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   121
            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
   122
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   123
            //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
   124
            //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
   125
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   126
            let mut buf = [0;1];
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   127
            loop {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   128
                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
   129
                if bytes_read == 0 {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   130
                    break;
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   131
                }
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   132
                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
   133
                // 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
   134
                if buf[0] == 1 {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   135
                    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
   136
                    if bytes_read == 0 {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   137
                        break;
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   138
                    }
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   139
                    if buf[0] == 33 {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   140
                        println!("Ping? Pong!");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   141
                        break;
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   142
                    }
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
            };
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   145
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   146
            let preview_info = &mut PreviewInfo {
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   147
                width: 0,
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   148
                height: 0,
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   149
                hedgehogs_number: 0,
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   150
                land: std::ptr::null(),
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   151
            };
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   152
15828
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
   153
            println!("Generating preview...");
15827
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   154
15828
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
   155
            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
   156
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   157
            //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
   158
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   159
            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
   160
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   161
            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
   162
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   163
            const PREVIEW_WIDTH: u32 = 256;
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   164
            const PREVIEW_HEIGHT: u32 = 128;
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   165
15828
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
   166
            println!("Resizing preview...");
15827
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   167
15828
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
   168
            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
   169
            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
   170
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
   171
            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
   172
36816af30583 hedgewars-engine (rust): don't use image lib, manually resize (because speed); using nearest px for now
sheepluva
parents: 15827
diff changeset
   173
            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
   174
            stream.flush().unwrap();
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   175
            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
   176
            stream.flush().unwrap();
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   177
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   178
            println!("Preview sent, disconnect");
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   179
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   180
            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
   181
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   182
            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
   183
        }
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   184
61da40b657fa hedgewars-engine (rust): ugly+slow impl for returning lib's previews (QTfrontend "compatible")
sheepluva
parents: 15824
diff changeset
   185
        engine.cleanup.deref()(engine_state);
14187
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
   186
    }
7d3b94cdc3c4 - Move hedgewars-engine into lib-hedgewars-engine
unc0rr
parents:
diff changeset
   187
}