reformat code with max width

This commit is contained in:
Vinzenz Schroeter 2024-05-11 23:29:25 +02:00
parent 9f4cab6c13
commit 0da5ca5446
4 changed files with 69 additions and 33 deletions

1
rustfmt.toml Normal file
View file

@ -0,0 +1 @@
max_width = 80

View file

@ -10,10 +10,12 @@ pub struct BitmapFont {
impl BitmapFont { impl BitmapFont {
pub fn load_file(file: &str) -> BitmapFont { pub fn load_file(file: &str) -> BitmapFont {
let font = font_kit::font::Font::from_path(file, 0).expect("could not load font"); let font = font_kit::font::Font::from_path(file, 0)
.expect("could not load font");
let mut bitmaps = let mut bitmaps = core::array::from_fn(|_| {
core::array::from_fn(|_| PixelGrid::new(TILE_SIZE as usize, TILE_SIZE as usize)); PixelGrid::new(TILE_SIZE as usize, TILE_SIZE as usize)
});
for char_code in u8::MIN..u8::MAX { for char_code in u8::MIN..u8::MAX {
let char = char_code as char; let char = char_code as char;
@ -24,7 +26,8 @@ impl BitmapFont {
let size = 8f32; let size = 8f32;
let transform = Transform2F::default(); let transform = Transform2F::default();
let mut canvas = Canvas::new(vec2i(size as i32, size as i32), Format::A8); let mut canvas =
Canvas::new(vec2i(size as i32, size as i32), Format::A8);
font.rasterize_glyph( font.rasterize_glyph(
&mut canvas, &mut canvas,
glyph_id, glyph_id,

View file

@ -1,9 +1,12 @@
use std::sync::mpsc::Sender;
use std::sync::RwLock;
use log::{info, warn}; use log::{info, warn};
use pixels::wgpu::TextureFormat; use pixels::wgpu::TextureFormat;
use pixels::{Pixels, PixelsBuilder, SurfaceTexture}; use pixels::{Pixels, PixelsBuilder, SurfaceTexture};
use servicepoint2::{ByteGrid, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH, TILE_SIZE}; use servicepoint2::{
use std::sync::mpsc::Sender; ByteGrid, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH, TILE_SIZE,
use std::sync::RwLock; };
use winit::application::ApplicationHandler; use winit::application::ApplicationHandler;
use winit::dpi::{LogicalSize, Size}; use winit::dpi::{LogicalSize, Size};
use winit::event::WindowEvent; use winit::event::WindowEvent;
@ -42,7 +45,10 @@ impl<'t> App<'t> {
impl ApplicationHandler<AppEvents> for App<'_> { impl ApplicationHandler<AppEvents> for App<'_> {
fn resumed(&mut self, event_loop: &ActiveEventLoop) { fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let size = Size::from(LogicalSize::new(PIXEL_WIDTH as f64, PIXEL_HEIGHT as f64)); let size = Size::from(LogicalSize::new(
PIXEL_WIDTH as f64,
PIXEL_HEIGHT as f64,
));
let attributes = Window::default_attributes() let attributes = Window::default_attributes()
.with_title("pixel-receiver-rs") .with_title("pixel-receiver-rs")
.with_inner_size(size); .with_inner_size(size);
@ -53,13 +59,20 @@ impl ApplicationHandler<AppEvents> for App<'_> {
self.pixels = { self.pixels = {
let window_size = window.inner_size(); let window_size = window.inner_size();
let surface_texture = let surface_texture = SurfaceTexture::new(
SurfaceTexture::new(window_size.width, window_size.height, &window); window_size.width,
window_size.height,
&window,
);
Some( Some(
PixelsBuilder::new(PIXEL_WIDTH as u32, PIXEL_HEIGHT as u32, surface_texture) PixelsBuilder::new(
.render_texture_format(TextureFormat::Bgra8UnormSrgb) PIXEL_WIDTH as u32,
.build() PIXEL_HEIGHT as u32,
.expect("could not create pixels"), surface_texture,
)
.render_texture_format(TextureFormat::Bgra8UnormSrgb)
.build()
.expect("could not create pixels"),
) )
}; };
} }
@ -78,7 +91,12 @@ impl ApplicationHandler<AppEvents> for App<'_> {
} }
} }
fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent) { fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
_: WindowId,
event: WindowEvent,
) {
if event == WindowEvent::CloseRequested { if event == WindowEvent::CloseRequested {
warn!("window event cloe requested"); warn!("window event cloe requested");
self.window = None; self.window = None;
@ -100,7 +118,8 @@ impl ApplicationHandler<AppEvents> for App<'_> {
for y in 0..PIXEL_HEIGHT as usize { for y in 0..PIXEL_HEIGHT as usize {
for x in 0..PIXEL_WIDTH as usize { for x in 0..PIXEL_WIDTH as usize {
let is_set = display.get(x, y); let is_set = display.get(x, y);
let brightness = luma.get(x / TILE_SIZE as usize, y / TILE_SIZE as usize); let brightness =
luma.get(x / TILE_SIZE as usize, y / TILE_SIZE as usize);
let color = if is_set { let color = if is_set {
[0u8, brightness, 0, 255] [0u8, brightness, 0, 255]

View file

@ -1,22 +1,24 @@
#![deny(clippy::all)] #![deny(clippy::all)]
mod font;
mod gui;
use crate::font::BitmapFont;
use crate::gui::{App, AppEvents};
use clap::Parser;
use log::{debug, error, info, warn};
use servicepoint2::{
ByteGrid, Command, Origin, Packet, PixelGrid, PIXEL_COUNT, PIXEL_HEIGHT, PIXEL_WIDTH,
TILE_HEIGHT, TILE_SIZE, TILE_WIDTH,
};
use std::io::ErrorKind; use std::io::ErrorKind;
use std::net::UdpSocket; use std::net::UdpSocket;
use std::sync::{mpsc, RwLock, RwLockWriteGuard}; use std::sync::{mpsc, RwLock, RwLockWriteGuard};
use std::time::Duration; use std::time::Duration;
use clap::Parser;
use log::{debug, error, info, warn};
use servicepoint2::{
ByteGrid, Command, Origin, Packet, PixelGrid, PIXEL_COUNT, PIXEL_HEIGHT,
PIXEL_WIDTH, TILE_HEIGHT, TILE_SIZE, TILE_WIDTH,
};
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::{ControlFlow, EventLoop};
use crate::font::BitmapFont;
use crate::gui::{App, AppEvents};
mod font;
mod gui;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
struct Cli { struct Cli {
#[arg(long, default_value = "0.0.0.0:2342")] #[arg(long, default_value = "0.0.0.0:2342")]
@ -36,7 +38,10 @@ fn main() {
let font = BitmapFont::load_file("Web437_IBM_BIOS.woff"); let font = BitmapFont::load_file("Web437_IBM_BIOS.woff");
let display = RwLock::new(PixelGrid::new(PIXEL_WIDTH as usize, PIXEL_HEIGHT as usize)); let display = RwLock::new(PixelGrid::new(
PIXEL_WIDTH as usize,
PIXEL_HEIGHT as usize,
));
let display_ref = &display; let display_ref = &display;
let mut luma = ByteGrid::new(TILE_WIDTH as usize, TILE_HEIGHT as usize); let mut luma = ByteGrid::new(TILE_WIDTH as usize, TILE_HEIGHT as usize);
@ -144,7 +149,8 @@ fn handle_package(
} }
let mut display = display_ref.write().unwrap(); let mut display = display_ref.write().unwrap();
for bitmap_index in 0..vec.len() { for bitmap_index in 0..vec.len() {
let (x, y) = get_coordinates_for_index(offset as usize, bitmap_index); let (x, y) =
get_coordinates_for_index(offset as usize, bitmap_index);
display.set(x, y, vec.get(bitmap_index)); display.set(x, y, vec.get(bitmap_index));
} }
} }
@ -154,7 +160,8 @@ fn handle_package(
} }
let mut display = display_ref.write().unwrap(); let mut display = display_ref.write().unwrap();
for bitmap_index in 0..vec.len() { for bitmap_index in 0..vec.len() {
let (x, y) = get_coordinates_for_index(offset as usize, bitmap_index); let (x, y) =
get_coordinates_for_index(offset as usize, bitmap_index);
let old_value = display.get(x, y); let old_value = display.get(x, y);
display.set(x, y, old_value && vec.get(bitmap_index)); display.set(x, y, old_value && vec.get(bitmap_index));
} }
@ -165,7 +172,8 @@ fn handle_package(
} }
let mut display = display_ref.write().unwrap(); let mut display = display_ref.write().unwrap();
for bitmap_index in 0..vec.len() { for bitmap_index in 0..vec.len() {
let (x, y) = get_coordinates_for_index(offset as usize, bitmap_index); let (x, y) =
get_coordinates_for_index(offset as usize, bitmap_index);
let old_value = display.get(x, y); let old_value = display.get(x, y);
display.set(x, y, old_value || vec.get(bitmap_index)); display.set(x, y, old_value || vec.get(bitmap_index));
} }
@ -176,7 +184,8 @@ fn handle_package(
} }
let mut display = display_ref.write().unwrap(); let mut display = display_ref.write().unwrap();
for bitmap_index in 0..vec.len() { for bitmap_index in 0..vec.len() {
let (x, y) = get_coordinates_for_index(offset as usize, bitmap_index); let (x, y) =
get_coordinates_for_index(offset as usize, bitmap_index);
let old_value = display.get(x, y); let old_value = display.get(x, y);
display.set(x, y, old_value ^ vec.get(bitmap_index)); display.set(x, y, old_value ^ vec.get(bitmap_index));
} }
@ -190,7 +199,11 @@ fn handle_package(
for inner_y in 0..grid.height { for inner_y in 0..grid.height {
for inner_x in 0..grid.width { for inner_x in 0..grid.width {
let brightness = grid.get(inner_x, inner_y); let brightness = grid.get(inner_x, inner_y);
luma.set(offset_x + inner_x, offset_y + inner_y, brightness); luma.set(
offset_x + inner_x,
offset_y + inner_y,
brightness,
);
} }
} }
} }