Skip to main content

run_windowed_with_proxy

Function run_windowed_with_proxy 

Source
pub fn run_windowed_with_proxy<P, F, O>(
    config: WindowConfig,
    presenter: P,
    app_loop: F,
    on_proxy: O,
) -> Result<(), EventLoopError>
where P: Presenter + 'static, F: FnMut(&mut Terminal<WindowBackend<P>>) + 'static, O: FnOnce(EventProxy),
Expand description

Same as run_windowed, but also hands on_proxy an EventProxy for injecting cross-thread events.

on_proxy is called synchronously right after the event loop (and the proxy) is created, before this function starts blocking the calling thread on native. Use this over run_windowed whenever another thread (network, audio, timer, …) needs to wake the event loop and deliver an Event::Custom to the app; on_proxy is the hook to hand a clone of the proxy off to that thread before the loop takes over the calling thread.

The injected payload is always a u64, delivered as Event::Custom through the app’s normal poll_event/frame loop; see run_windowed_with_typed_proxy if a worker thread needs to hand back a real payload (a loaded asset, a network response) instead of a correlation id into a side table.

See run_windowed’s “Presenting is automatic” section: this function shares the same automatic-present behavior; app_loop no longer needs to call Terminal::present itself.

§Examples

use retroglyph_core::event::Event;
use retroglyph_software::SoftwareBackendBuilder;
use retroglyph_window::winit::{WindowConfig, run_windowed_with_proxy};
use std::time::Duration;

let renderer = SoftwareBackendBuilder::new()
    .grid_size(80, 25)
    .scale(2)
    .build()
    .expect("backend init failed")
    .into_renderer()
    .expect("renderer init failed");
let config = WindowConfig::fit(&renderer, "My Game", None, true);

run_windowed_with_proxy(
    config,
    renderer,
    move |term| {
        if let Some(Event::Custom(id)) = term.poll(Duration::from_millis(16)) {
            // Handle the tick/network/audio result tagged `id`.
            println!("got custom event {id}");
        }
    },
    |proxy| {
        // Runs before the blocking call below starts, so the proxy can be
        // handed off to a worker thread up front.
        std::thread::spawn(move || loop {
            std::thread::sleep(Duration::from_secs(1));
            if proxy.send_event(1).is_err() {
                break; // The window closed; stop ticking.
            }
        });
    },
)
.expect("event loop failed");

§Errors

Returns [winit::error::EventLoopError] if the event loop cannot be created or fails while running.