M src/main.rs => src/main.rs +2 -16
@@ 6,10 6,9 @@ mod structure;
use std::io::Read;
use glam::Vec3;
-use pixels::{PixelsBuilder, SurfaceTexture};
use winit::event::{Event, VirtualKeyCode};
use winit::event_loop::{ControlFlow, EventLoop};
-use winit::{dpi::PhysicalSize, window::WindowBuilder};
+use winit::window::WindowBuilder;
use winit_input_helper::WinitInputHelper;
use crate::screen::colors::{self, color_atom_by_res};
@@ 68,20 67,7 @@ fn main() -> Result<(), pixels::Error> {
.build(&event_loop)
.unwrap();
- let background = colors::BLACK;
- let pixels = {
- let PhysicalSize { width, height } = window.inner_size();
- let surface_texture = SurfaceTexture::new(width, height, &window);
- PixelsBuilder::new(width, height, surface_texture)
- .clear_color({
- let [r, g, b, a] = background.map(|v| v as f64 / u8::MAX as f64);
- pixels::wgpu::Color { r, g, b, a }
- })
- .present_mode(pixels::wgpu::PresentMode::AutoVsync)
- .blend_state(pixels::wgpu::BlendState::ALPHA_BLENDING)
- .build()?
- };
- let mut screen = Screen::new(pixels, background);
+ let mut screen = Screen::new(&window, colors::BLACK)?;
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
M src/screen.rs => src/screen.rs +18 -4
@@ 1,5 1,6 @@
use glam::{EulerRot, Mat3, Mat4, Quat, Vec2, Vec3, Vec3Swizzles};
-use pixels::{Pixels, TextureError};
+use pixels::{Pixels, PixelsBuilder, SurfaceTexture, TextureError};
+use winit::{dpi::PhysicalSize, window::Window};
pub const PIXEL_SIZE: usize = 4;
pub type Pixel = [u8; PIXEL_SIZE];
@@ 11,12 12,25 @@ pub struct Screen {
}
impl Screen {
- pub fn new(pixels: Pixels, clear_color: Pixel) -> Self {
- Self {
+ pub fn new(window: &Window, clear_color: Pixel) -> Result<Self, pixels::Error> {
+ let pixels = {
+ let PhysicalSize { width, height } = window.inner_size();
+ let surface_texture = SurfaceTexture::new(width, height, &window);
+ PixelsBuilder::new(width, height, surface_texture)
+ .clear_color({
+ let [r, g, b, a] = clear_color.map(|v| v as f64 / u8::MAX as f64);
+ pixels::wgpu::Color { r, g, b, a }
+ })
+ .present_mode(pixels::wgpu::PresentMode::AutoVsync)
+ .blend_state(pixels::wgpu::BlendState::ALPHA_BLENDING)
+ .build()?
+ };
+
+ Ok(Self {
pixels,
clear_color,
old_list: Vec::new(),
- }
+ })
}
fn frame_mut(&mut self) -> &mut [u8] {