Search code examples
rustrenderingincompatibilitywindowing

Issues with Winit and Pixels in Rust


I am having some issues with my rust project that uses Winit 0.30.5 and Pixels 0.13.

The problem is I can't get the renderer to write to the window. The program compiles and runs without any errors, so it must either be my own error, or there is some weird library interaction going on.

I appreciate the help.

Here is my cargo.toml:

[package]
name = "terminal-squares"
version = "0.1.0"
edition = "2021"

[dependencies]
pixels = "0.13.0"
winit = { version = "0.30.5", features = ["rwh_05"]}

Here is my main code.

use winit::{
    event_loop::{
        ControlFlow,
        EventLoop,
    },
};

fn main() {

    let event_loop = EventLoop::new().unwrap();
    event_loop.set_control_flow(ControlFlow::Poll);

    let mut app = application::Application::new();

    event_loop.run_app(&mut app).unwrap();
}

pub mod application {
    use super::window_manager::WindowManager;

    use pixels::{
        Pixels,
        SurfaceTexture,
        wgpu::Color,
    };

    use winit::{
        application::ApplicationHandler,
        dpi::LogicalSize,
        window::{
            Window,
            WindowId,
        },
        event::{
            WindowEvent,
        },
        event_loop::{
            ActiveEventLoop,
        },
    };

    pub struct Application {
        window_width: f32,
        window_height: f32,

        windows: Vec<WindowManager>,
    }

    impl Application {
        pub fn new() -> Self {
            Self {
                window_width: 500.0,
                window_height:500.0,

                windows: Vec::new(),
            }
        }

        fn create_window(&mut self, event_loop: &ActiveEventLoop) {
            let window = {
                let size = LogicalSize::new(self.window_width as f64, self.window_height as f64);

                let window_attributes = Window::default_attributes()
                    .with_title("Hello Pixels")
                    .with_inner_size(size)
                    .with_min_inner_size(size);

                event_loop.create_window(window_attributes).unwrap()
            };

            let pixels = {
                let window_size = window.inner_size();

                let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);

                Pixels::new(self.window_width as u32, self.window_height as u32, surface_texture).unwrap()
            };

            // Try and remember to ask someone what to do when the window closes and resume is called for a new one. Doesn't seem to be any check closed function.
            self.windows.push(WindowManager::new(window, pixels));
        }
    }

    impl ApplicationHandler for Application {
        fn resumed(&mut self, event_loop: &ActiveEventLoop) {
            // This will need to be revisited after I find the answer to the closing windows thing.
            if self.windows.len() == 0 {
                self.create_window(&event_loop);
            }
        }

        fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
            // /self.windows[0].request_redraw();
            match event {
                WindowEvent::CloseRequested => {
                    println!("The close button was pressed; stopping");
                    event_loop.exit();
                },
                WindowEvent::RedrawRequested => {
                    if self.windows.len() > 0 { // Revisit this.
                        /*let win_len = self.windows.len() - 1 as usize;
                        self.windows[win_len].clear();
                        println!("Test");*/

                        self.windows[0].clear();
                    }
                },
                _ => (),
            }
        }
    }
}

pub mod window_manager {
    use pixels::{
        Pixels,
    };

    use winit::{
        window::{
            Window,
            WindowId,
        },
    };

    pub struct WindowManager {
        window: Window,
        pixels: Pixels,
    }

    impl WindowManager {
        pub fn new(window: Window, pixels: Pixels) -> Self {
            Self{
                window,
                pixels,
            }
        }

        pub fn clear(&mut self) {
            for (i, pixel) in self.pixels.frame_mut().chunks_exact_mut(4).enumerate() {
                pixel.copy_from_slice(&[0x48, 0xb2, 0xe8, 0xff]);
            }
            //self.window.draw();
            //self.window.request_redraw();
        }

        //pub fn get_window(self) -> &Window { &self.window } // Another area that needs revisited.

        pub fn get_window_id(self) -> WindowId { self.window.id() }
    }
}

Solution

  • You never actually call Pixels::render anywhere. The underlying texture is only updated when you do this.