add optional spacers

This commit is contained in:
Vinzenz Schroeter 2024-05-14 21:45:42 +02:00
parent d7b7036f51
commit 9f5d256963
5 changed files with 48 additions and 108 deletions

82
Cargo.lock generated
View file

@ -1136,81 +1136,6 @@ dependencies = [
"jni-sys",
]
[[package]]
name = "num"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]
[[package]]
name = "num-bigint"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c165a9ab64cf766f73521c0dd2cfdff64f488b8f0b3e621face3462d3db536d7"
dependencies = [
"num-integer",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
dependencies = [
"num-traits",
]
[[package]]
name = "num-derive"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
dependencies = [
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
@ -1707,17 +1632,14 @@ dependencies = [
[[package]]
name = "servicepoint2"
version = "0.1.3"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff3f24a6ee5fa2b73d57b78d8d35efefb9397d0ea012978fe52b396f601e8948"
checksum = "8d434890b6d1a4c886f384639bf79ba7f127221cad7bff45acabf99d2fccee5f"
dependencies = [
"bzip2",
"flate2",
"log",
"lz4",
"num",
"num-derive",
"num-traits",
"zstd",
]

View file

@ -7,7 +7,7 @@ license = "GPL-3.0-or-later"
[dependencies]
# packet parsing
servicepoint2 = "0.1.3"
servicepoint2 = "0.2.0"
# gui
winit = { version = "0.30", features = ["rwh_05"] } # for creating a window

View file

@ -36,7 +36,7 @@ impl BitmapFont {
HintingOptions::None,
RasterizationOptions::GrayscaleAa,
)
.unwrap();
.unwrap();
assert_eq!(canvas.pixels.len(), 64);
assert_eq!(canvas.stride, 8);

View file

@ -2,10 +2,10 @@ use std::sync::mpsc::Sender;
use std::sync::RwLock;
use log::{info, warn};
use pixels::wgpu::TextureFormat;
use pixels::{Pixels, PixelsBuilder, SurfaceTexture};
use pixels::wgpu::TextureFormat;
use servicepoint2::{
ByteGrid, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH, TILE_SIZE,
ByteGrid, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid, TILE_SIZE,
};
use winit::application::ApplicationHandler;
use winit::dpi::{LogicalSize, Size};
@ -19,8 +19,11 @@ pub struct App<'t> {
window: Option<Window>,
pixels: Option<Pixels>,
stop_udp_tx: Sender<()>,
spacers: bool,
}
const SPACER_HEIGHT: u16 = 4;
#[derive(Debug)]
pub enum AppEvents {
UdpPacketHandled,
@ -32,6 +35,7 @@ impl<'t> App<'t> {
display: &'t RwLock<PixelGrid>,
luma: &'t RwLock<ByteGrid>,
stop_udp_tx: Sender<()>,
spacers: bool,
) -> Self {
App {
display,
@ -39,42 +43,44 @@ impl<'t> App<'t> {
stop_udp_tx,
pixels: None,
window: None,
spacers,
}
}
}
impl ApplicationHandler<AppEvents> for App<'_> {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let size = Size::from(LogicalSize::new(
PIXEL_WIDTH as f64,
PIXEL_HEIGHT as f64,
));
let height = if self.spacers {
let num_spacers = (PIXEL_HEIGHT / TILE_SIZE) - 1;
PIXEL_HEIGHT + num_spacers * SPACER_HEIGHT
} else {
PIXEL_HEIGHT
} as f64;
let size = Size::from(LogicalSize::new(PIXEL_WIDTH as f64, height));
let attributes = Window::default_attributes()
.with_title("pixel-receiver-rs")
.with_title("servicepoint-simulator")
.with_inner_size(size);
let window = event_loop.create_window(attributes).unwrap();
self.window = Some(window);
let window = self.window.as_ref().unwrap();
self.pixels = {
self.pixels = Some({
let window_size = window.inner_size();
let surface_texture = SurfaceTexture::new(
PixelsBuilder::new(
window_size.width,
window_size.height,
&window,
);
Some(
PixelsBuilder::new(
PIXEL_WIDTH as u32,
PIXEL_HEIGHT as u32,
surface_texture,
)
SurfaceTexture::new(
window_size.width,
window_size.height,
&window,
),
)
.render_texture_format(TextureFormat::Bgra8UnormSrgb)
.build()
.expect("could not create pixels"),
)
};
.expect("could not create pixels")
});
}
fn user_event(&mut self, event_loop: &ActiveEventLoop, event: AppEvents) {
@ -116,6 +122,13 @@ impl ApplicationHandler<AppEvents> for App<'_> {
let luma = self.luma.read().unwrap();
for y in 0..PIXEL_HEIGHT as usize {
if self.spacers && y != 0 && y % TILE_SIZE as usize == 0 {
// cannot just frame.skip(PIXEL_WIDTH as usize * SPACER_HEIGHT as usize) because of typing
for _ in 0..PIXEL_WIDTH as usize * SPACER_HEIGHT as usize {
frame.next().unwrap();
}
}
for x in 0..PIXEL_WIDTH as usize {
let is_set = display.get(x, y);
let brightness =

View file

@ -8,8 +8,8 @@ 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,
ByteGrid, Command, Origin, Packet, PIXEL_COUNT, PIXEL_HEIGHT, PIXEL_WIDTH,
PixelGrid, TILE_HEIGHT, TILE_SIZE, TILE_WIDTH,
};
use winit::event_loop::{ControlFlow, EventLoop};
@ -21,8 +21,10 @@ mod gui;
#[derive(Parser, Debug)]
struct Cli {
#[arg(long, default_value = "0.0.0.0:2342")]
#[arg(short, long, default_value = "0.0.0.0:2342")]
bind: String,
#[arg(short, long, default_value_t = false)]
spacers: bool,
}
fn main() {
@ -51,7 +53,7 @@ fn main() {
let (stop_udp_tx, stop_udp_rx) = mpsc::channel();
let mut app = App::new(display_ref, luma_ref, stop_udp_tx);
let mut app = App::new(display_ref, luma_ref, stop_udp_tx, cli.spacers);
let event_loop = EventLoop::with_user_event()
.build()
@ -237,12 +239,15 @@ fn print_cp437_data(
display: &mut RwLockWriteGuard<PixelGrid>,
) {
let Origin(x, y) = origin;
let x = x as usize;
let y = y as usize;
for char_y in 0usize..grid.height {
for char_x in 0usize..grid.width {
let char_code = grid.get(char_x, char_y);
let tile_x = char_x + x as usize;
let tile_y = char_y + y as usize;
let tile_x = char_x + x;
let tile_y = char_y + y;
let bitmap = font.get_bitmap(char_code);
print_pixel_grid(