Remove indentation in fill.rs helper

This commit is contained in:
Mads Marquart
2025-12-13 22:01:56 +01:00
parent 0f2d59cbba
commit 7864d02077

View File

@@ -7,54 +7,42 @@
//! The `softbuffer` crate is used, largely because of its ease of use. `glutin` or `wgpu` could //! The `softbuffer` crate is used, largely because of its ease of use. `glutin` or `wgpu` could
//! also be used to fill the window buffer, but they are more complicated to use. //! also be used to fill the window buffer, but they are more complicated to use.
#[allow(unused_imports)] use std::cell::RefCell;
pub use platform::cleanup_window; use std::collections::HashMap;
#[allow(unused_imports)] use std::mem;
pub use platform::fill_window; use std::mem::ManuallyDrop;
#[allow(unused_imports)] use std::num::NonZeroU32;
pub use platform::fill_window_with_animated_color; #[cfg(not(web_platform))]
#[allow(unused_imports)] use std::time::Instant;
pub use platform::fill_window_with_color;
mod platform { use softbuffer::{Context, Surface};
use std::cell::RefCell; #[cfg(web_platform)]
use std::collections::HashMap; use web_time::Instant;
use std::mem; use winit::window::{Window, WindowId};
use std::mem::ManuallyDrop;
use std::num::NonZeroU32;
#[cfg(not(web_platform))]
use std::time::Instant;
use softbuffer::{Context, Surface}; thread_local! {
#[cfg(web_platform)]
use web_time::Instant;
use winit::window::{Window, WindowId};
thread_local! {
// NOTE: You should never do things like that, create context and drop it before // NOTE: You should never do things like that, create context and drop it before
// you drop the event loop. We do this for brevity to not blow up examples. We use // you drop the event loop. We do this for brevity to not blow up examples. We use
// ManuallyDrop to prevent destructors from running. // ManuallyDrop to prevent destructors from running.
// //
// A static, thread-local map of graphics contexts to open windows. // A static, thread-local map of graphics contexts to open windows.
static GC: ManuallyDrop<RefCell<Option<GraphicsContext>>> = const { ManuallyDrop::new(RefCell::new(None)) }; static GC: ManuallyDrop<RefCell<Option<GraphicsContext>>> = const { ManuallyDrop::new(RefCell::new(None)) };
} }
/// The graphics context used to draw to a window. /// The graphics context used to draw to a window.
struct GraphicsContext { struct GraphicsContext {
/// The global softbuffer context. /// The global softbuffer context.
context: RefCell<Context<&'static dyn Window>>, context: RefCell<Context<&'static dyn Window>>,
/// The hash map of window IDs to surfaces. /// The hash map of window IDs to surfaces.
surfaces: HashMap<WindowId, Surface<&'static dyn Window, &'static dyn Window>>, surfaces: HashMap<WindowId, Surface<&'static dyn Window, &'static dyn Window>>,
} }
impl GraphicsContext { impl GraphicsContext {
fn new(w: &dyn Window) -> Self { fn new(w: &dyn Window) -> Self {
Self { Self {
context: RefCell::new( context: RefCell::new(
Context::new(unsafe { Context::new(unsafe { mem::transmute::<&'_ dyn Window, &'static dyn Window>(w) })
mem::transmute::<&'_ dyn Window, &'static dyn Window>(w)
})
.expect("Failed to create a softbuffer context"), .expect("Failed to create a softbuffer context"),
), ),
surfaces: HashMap::new(), surfaces: HashMap::new(),
@@ -76,9 +64,9 @@ mod platform {
fn destroy_surface(&mut self, window: &dyn Window) { fn destroy_surface(&mut self, window: &dyn Window) {
self.surfaces.remove(&window.id()); self.surfaces.remove(&window.id());
} }
} }
pub fn fill_window_with_color(window: &dyn Window, color: u32) { pub fn fill_window_with_color(window: &dyn Window, color: u32) {
GC.with(|gc| { GC.with(|gc| {
let size = window.surface_size(); let size = window.surface_size();
let (Some(width), Some(height)) = let (Some(width), Some(height)) =
@@ -89,8 +77,7 @@ mod platform {
// Either get the last context used or create a new one. // Either get the last context used or create a new one.
let mut gc = gc.borrow_mut(); let mut gc = gc.borrow_mut();
let surface = let surface = gc.get_or_insert_with(|| GraphicsContext::new(window)).create_surface(window);
gc.get_or_insert_with(|| GraphicsContext::new(window)).create_surface(window);
// Fill a buffer with a solid color // Fill a buffer with a solid color
@@ -100,30 +87,29 @@ mod platform {
buffer.fill(color); buffer.fill(color);
buffer.present().expect("Failed to present the softbuffer buffer"); buffer.present().expect("Failed to present the softbuffer buffer");
}) })
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn fill_window(window: &dyn Window) { pub fn fill_window(window: &dyn Window) {
fill_window_with_color(window, 0xff181818); fill_window_with_color(window, 0xff181818);
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn fill_window_with_animated_color(window: &dyn Window, start: Instant) { pub fn fill_window_with_animated_color(window: &dyn Window, start: Instant) {
let time = start.elapsed().as_secs_f32() * 1.5; let time = start.elapsed().as_secs_f32() * 1.5;
let blue = (time.sin() * 255.0) as u32; let blue = (time.sin() * 255.0) as u32;
let green = ((time.cos() * 255.0) as u32) << 8; let green = ((time.cos() * 255.0) as u32) << 8;
let red = ((1.0 - time.sin() * 255.0) as u32) << 16; let red = ((1.0 - time.sin() * 255.0) as u32) << 16;
let color = red | green | blue; let color = red | green | blue;
fill_window_with_color(window, color); fill_window_with_color(window, color);
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn cleanup_window(window: &dyn Window) { pub fn cleanup_window(window: &dyn Window) {
GC.with(|gc| { GC.with(|gc| {
let mut gc = gc.borrow_mut(); let mut gc = gc.borrow_mut();
if let Some(context) = gc.as_mut() { if let Some(context) = gc.as_mut() {
context.destroy_surface(window); context.destroy_surface(window);
} }
}); });
}
} }