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() }
    }
}

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

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật