restructure with windows

This commit is contained in:
Vinzenz Schroeter 2025-07-05 21:03:39 +02:00
parent fe4543ae41
commit 64f4ea529a
7 changed files with 68 additions and 130 deletions

4
Cargo.lock generated
View file

@ -85,9 +85,7 @@ dependencies = [
[[package]] [[package]]
name = "servicepoint" name = "servicepoint"
version = "0.15.1" version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2800caad491cb44f67e5dd5b8c61ece368eecfe588155d03c7d9864acbad6919"
dependencies = [ dependencies = [
"bitvec", "bitvec",
"inherent", "inherent",

View file

@ -3,5 +3,6 @@ name = "servicepoint-idle"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies.servicepoint]
servicepoint = "0.15.1" version = "0.16.0"
path = "../servicepoint-uniffi3"

View file

@ -1,6 +1,5 @@
use crate::queue::PacketQueue; use crate::{Currency, Progressable};
use crate::{Currency, GenerateCommands, Progressable}; use servicepoint::{GridMut};
use servicepoint::{Bitmap, BitmapCommand, Grid, Origin, TILE_SIZE, TILE_WIDTH, Tiles};
use std::time::Duration; use std::time::Duration;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -8,18 +7,14 @@ pub struct Bar {
progress: f64, progress: f64,
speed: f64, speed: f64,
factor: f64, factor: f64,
origin: Origin<Tiles>,
width_tiles: usize,
} }
impl Bar { impl Bar {
pub(crate) fn new(factor: f64, origin: Origin<Tiles>) -> Self { pub(crate) fn new(factor: f64, speed: f64) -> Self {
Self { Self {
factor, factor,
progress: 0f64, progress: 0f64,
speed: 0f64, speed,
origin,
width_tiles: TILE_WIDTH / 2, // TODO: param
} }
} }
@ -47,9 +42,9 @@ impl Progressable for Bar {
} }
} }
impl GenerateCommands for Bar { impl Bar {
fn generate_commands(&self, q: &mut impl PacketQueue) { pub fn draw<P: GridMut<bool>>(&self, bitmap: &mut P) {
let mut bitmap = Bitmap::new(self.width_tiles * TILE_SIZE, TILE_SIZE).unwrap(); bitmap.fill(false);
let margin = 1; let margin = 1;
let bar_height = bitmap.height() - 2 * margin; let bar_height = bitmap.height() - 2 * margin;
@ -77,13 +72,5 @@ impl GenerateCommands for Bar {
bitmap.set(x, y, (x + y) % 2 == 0); bitmap.set(x, y, (x + y) % 2 == 0);
} }
} }
// to command
q.enqueue_command(BitmapCommand {
bitmap,
origin: Origin::from(&self.origin),
compression: Default::default(),
})
.unwrap();
} }
} }

View file

@ -1,14 +1,13 @@
use crate::bar::Bar; use crate::{
use crate::queue::PacketQueue; bar::Bar,
use crate::label::Label; Currency,
use crate::{Currency, GenerateCommands, Progressable}; Progressable
use servicepoint::{Origin, TILE_WIDTH}; };
use servicepoint::{Bitmap, CharGrid, CharGridMutExt, WindowMut, TILE_SIZE};
use std::time::Duration; use std::time::Duration;
const STEP_COUNT: usize = 11;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Game { pub struct Game<const STEP_COUNT:usize=11> {
pub(crate) currency: Currency, pub(crate) currency: Currency,
pub(crate) bars: [Bar; STEP_COUNT], pub(crate) bars: [Bar; STEP_COUNT],
pub(crate) names: [&'static str; STEP_COUNT], pub(crate) names: [&'static str; STEP_COUNT],
@ -19,17 +18,17 @@ impl Game {
Self { Self {
currency: 0f64, currency: 0f64,
bars: [ bars: [
Bar::new(1f64, Origin::new(0, 1)).add_speed(1.0), Bar::new(1f64,1.0),
Bar::new(2f64, Origin::new(0, 2)).add_speed(0.5), Bar::new(2f64, 0.5),
Bar::new(4f64, Origin::new(0, 3)).add_speed(0.25), Bar::new(4f64, 0.25),
Bar::new(8f64, Origin::new(0, 4)).add_speed(0.125), Bar::new(8f64,0.125),
Bar::new(16f64, Origin::new(0, 5)).add_speed(0.0625), Bar::new(16f64, 0.0625),
Bar::new(32f64, Origin::new(0, 6)).add_speed(0.03125), Bar::new(32f64, 0.03125),
Bar::new(64f64, Origin::new(0, 7)).add_speed(0.015625), Bar::new(64f64, 0.015625),
Bar::new(128f64, Origin::new(0, 8)).add_speed(0.0078125), Bar::new(128f64, 0.0078125),
Bar::new(256f64, Origin::new(0, 9)).add_speed(0.00390625), Bar::new(256f64, 0.00390625),
Bar::new(512f64, Origin::new(0, 10)).add_speed(0.001953125), Bar::new(512f64, 0.001953125),
Bar::new(1024f64, Origin::new(0, 11)).add_speed(0.000976562), Bar::new(1024f64,0.000976562),
], ],
names: [ names: [
"Powering infrastructure", "Powering infrastructure",
@ -68,28 +67,26 @@ impl Progressable for Game {
} }
} }
impl GenerateCommands for Game { impl Game {
fn generate_commands(&self, queue: &mut impl PacketQueue) { pub fn draw(&self, text_layer: &mut WindowMut<char, CharGrid>, pixel_layer: &mut WindowMut<bool, Bitmap>) {
Label::new(Origin::ZERO, TILE_WIDTH / 2, "Discordia Boot Procedure") text_layer.set_row_str(0, "Discordia Boot Procedure").unwrap();
.generate_commands(queue); let middle = text_layer.width() / 2;
Label::new( text_layer.window_mut( middle, 0, middle, 1)
Origin::new(TILE_WIDTH / 2 + 1, 0), .unwrap()
TILE_WIDTH / 2, .set_row_str(0, &format!(" Cycles: {}", self.currency.floor()))
format!("Cycles: {}", self.currency.floor()), .unwrap();
)
.generate_commands(queue);
for (index, bar) in self.bars.iter().enumerate() { for (index, bar) in self.bars.iter().enumerate() {
let row = 1 + index;
let mut bar_window = pixel_layer.window_mut(0, row * TILE_SIZE, pixel_layer.width() / 2, TILE_SIZE).unwrap();
let mut label_window = text_layer.window_mut(middle, row, middle, 1).unwrap();
if !bar.is_enabled() { if !bar.is_enabled() {
continue; continue;
} }
bar.generate_commands(queue);
Label::new( bar.draw(&mut bar_window);
Origin::new(TILE_WIDTH / 2 + 1, 1 + index), label_window.set_row_str(0, self.names[index]).unwrap();
TILE_WIDTH / 2 - 1,
self.names[index],
)
.generate_commands(queue);
} }
} }
} }

View file

@ -1,36 +0,0 @@
use crate::GenerateCommands;
use crate::queue::PacketQueue;
use servicepoint::{CharGrid, CharGridCommand, Origin, Tiles};
#[derive(Debug, Clone)]
pub(crate) struct Label<S: AsRef<str>> {
position: Origin<Tiles>,
text: S,
width_tiles: usize,
}
impl Label<String> {
pub(crate) fn new<S: AsRef<str>>(position: Origin<Tiles>, width_tiles: usize, text: S) -> Self {
// TODO: those should be grapheme clusters
let text = text.as_ref().chars().take(width_tiles).collect::<String>();
Self {
position,
text,
width_tiles,
}
}
}
impl<S: AsRef<str>> GenerateCommands for Label<S> {
fn generate_commands(&self, queue: &mut impl PacketQueue) {
let mut grid = CharGrid::new(self.width_tiles, 1);
grid.set_row_str(0, self.text.as_ref()).unwrap();
queue
.enqueue_command(CharGridCommand {
grid: CharGrid::from(self.text.as_ref()),
origin: self.position,
})
.unwrap();
}
}

View file

@ -1,14 +1,13 @@
use crate::queue::PacketQueue;
use game::Game; use game::Game;
use servicepoint::{ClearCommand, UdpSocketExt, FRAME_PACING}; use servicepoint::{BinaryOperation, BitVecCommand, Bitmap, CharGrid, CharGridCommand, ClearCommand, CompressionCode, UdpSocketExt, FRAME_PACING, TILE_HEIGHT, TILE_WIDTH};
use std::net::UdpSocket; use std::{
use std::thread::sleep; net::UdpSocket,
use std::time::{Duration, Instant}; thread::sleep,
time::{Duration, Instant}
};
mod bar; mod bar;
mod queue;
mod game; mod game;
mod label;
type Currency = f64; type Currency = f64;
@ -17,18 +16,17 @@ trait Progressable: Sized {
fn progress(&self, delta: Duration) -> (Self, Currency); fn progress(&self, delta: Duration) -> (Self, Currency);
} }
trait GenerateCommands {
fn generate_commands(&self, queue: &mut impl PacketQueue);
}
const DESTINATION: &str = "127.0.0.1:2342"; const DESTINATION: &str = "127.0.0.1:2342";
//const DESTINATION: &str = "172.23.42.29:2342"; //const DESTINATION: &str = "172.23.42.29:2342";
fn main() { fn main() {
let mut state = Game::new(); let mut state = Game::new();
let mut connection = UdpSocket::bind_connect(DESTINATION).unwrap(); let connection = UdpSocket::bind_connect(DESTINATION).unwrap();
connection.send_command(ClearCommand); connection.send_command(ClearCommand);
let mut chars = CharGrid::new(TILE_WIDTH, TILE_HEIGHT);
let mut pixels = Bitmap::max_sized();
let mut last_refresh = Instant::now(); let mut last_refresh = Instant::now();
loop { loop {
let current_time = Instant::now(); let current_time = Instant::now();
@ -37,7 +35,20 @@ fn main() {
(state, _) = state.progress(delta); (state, _) = state.progress(delta);
state.generate_commands(&mut connection); chars.fill(' ');
pixels.fill(false);
let mut chars_view = chars.window_mut(0, 0, chars.width(), chars.height()).unwrap();
let mut pixels_view = pixels.window_mut(0,0,pixels.width(), pixels.height()).unwrap();
state.draw(&mut chars_view, &mut pixels_view);
connection.send_command(CharGridCommand::from(chars.clone())).unwrap();
connection.send_command(BitVecCommand {
bitvec: pixels.clone().into(),
compression: CompressionCode::default(),
operation: BinaryOperation::Or,
offset: 0,
}).unwrap();
sleep(FRAME_PACING); sleep(FRAME_PACING);
} }

View file

@ -1,20 +0,0 @@
use servicepoint::{Packet, UdpSocketExt};
use std::net::UdpSocket;
pub(crate) trait PacketQueue {
fn enqueue_command<P: TryInto<Packet>>(&mut self, packet: P) -> Result<bool, P::Error>;
}
impl PacketQueue for Vec<Packet> {
fn enqueue_command<P: TryInto<Packet>>(&mut self, packet: P) -> Result<bool, P::Error> {
self.push(packet.try_into()?);
Ok(true)
}
}
impl PacketQueue for UdpSocket {
fn enqueue_command<P: TryInto<Packet>>(&mut self, packet: P) -> Result<bool, P::Error> {
let packet = packet.try_into()?;
Ok(self.send_command(packet).is_some())
}
}