diff --git a/Cargo.lock b/Cargo.lock index b4916d5..7e9f548 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,9 +85,7 @@ dependencies = [ [[package]] name = "servicepoint" -version = "0.15.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2800caad491cb44f67e5dd5b8c61ece368eecfe588155d03c7d9864acbad6919" +version = "0.16.0" dependencies = [ "bitvec", "inherent", diff --git a/Cargo.toml b/Cargo.toml index 2d36a5f..a68a2c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,5 +3,6 @@ name = "servicepoint-idle" version = "0.1.0" edition = "2024" -[dependencies] -servicepoint = "0.15.1" \ No newline at end of file +[dependencies.servicepoint] +version = "0.16.0" +path = "../servicepoint-uniffi3" \ No newline at end of file diff --git a/src/bar.rs b/src/bar.rs index ad38bd2..301f6c4 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -1,6 +1,5 @@ -use crate::queue::PacketQueue; -use crate::{Currency, GenerateCommands, Progressable}; -use servicepoint::{Bitmap, BitmapCommand, Grid, Origin, TILE_SIZE, TILE_WIDTH, Tiles}; +use crate::{Currency, Progressable}; +use servicepoint::{GridMut}; use std::time::Duration; #[derive(Debug, Clone)] @@ -8,18 +7,14 @@ pub struct Bar { progress: f64, speed: f64, factor: f64, - origin: Origin, - width_tiles: usize, } impl Bar { - pub(crate) fn new(factor: f64, origin: Origin) -> Self { + pub(crate) fn new(factor: f64, speed: f64) -> Self { Self { factor, progress: 0f64, - speed: 0f64, - origin, - width_tiles: TILE_WIDTH / 2, // TODO: param + speed, } } @@ -47,9 +42,9 @@ impl Progressable for Bar { } } -impl GenerateCommands for Bar { - fn generate_commands(&self, q: &mut impl PacketQueue) { - let mut bitmap = Bitmap::new(self.width_tiles * TILE_SIZE, TILE_SIZE).unwrap(); +impl Bar { + pub fn draw>(&self, bitmap: &mut P) { + bitmap.fill(false); let margin = 1; let bar_height = bitmap.height() - 2 * margin; @@ -77,13 +72,5 @@ impl GenerateCommands for Bar { bitmap.set(x, y, (x + y) % 2 == 0); } } - - // to command - q.enqueue_command(BitmapCommand { - bitmap, - origin: Origin::from(&self.origin), - compression: Default::default(), - }) - .unwrap(); } } diff --git a/src/game.rs b/src/game.rs index 2656885..2143fbe 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,14 +1,13 @@ -use crate::bar::Bar; -use crate::queue::PacketQueue; -use crate::label::Label; -use crate::{Currency, GenerateCommands, Progressable}; -use servicepoint::{Origin, TILE_WIDTH}; +use crate::{ + bar::Bar, + Currency, + Progressable +}; +use servicepoint::{Bitmap, CharGrid, CharGridMutExt, WindowMut, TILE_SIZE}; use std::time::Duration; -const STEP_COUNT: usize = 11; - #[derive(Debug, Clone)] -pub struct Game { +pub struct Game { pub(crate) currency: Currency, pub(crate) bars: [Bar; STEP_COUNT], pub(crate) names: [&'static str; STEP_COUNT], @@ -19,17 +18,17 @@ impl Game { Self { currency: 0f64, bars: [ - Bar::new(1f64, Origin::new(0, 1)).add_speed(1.0), - Bar::new(2f64, Origin::new(0, 2)).add_speed(0.5), - Bar::new(4f64, Origin::new(0, 3)).add_speed(0.25), - Bar::new(8f64, Origin::new(0, 4)).add_speed(0.125), - Bar::new(16f64, Origin::new(0, 5)).add_speed(0.0625), - Bar::new(32f64, Origin::new(0, 6)).add_speed(0.03125), - Bar::new(64f64, Origin::new(0, 7)).add_speed(0.015625), - Bar::new(128f64, Origin::new(0, 8)).add_speed(0.0078125), - Bar::new(256f64, Origin::new(0, 9)).add_speed(0.00390625), - Bar::new(512f64, Origin::new(0, 10)).add_speed(0.001953125), - Bar::new(1024f64, Origin::new(0, 11)).add_speed(0.000976562), + Bar::new(1f64,1.0), + Bar::new(2f64, 0.5), + Bar::new(4f64, 0.25), + Bar::new(8f64,0.125), + Bar::new(16f64, 0.0625), + Bar::new(32f64, 0.03125), + Bar::new(64f64, 0.015625), + Bar::new(128f64, 0.0078125), + Bar::new(256f64, 0.00390625), + Bar::new(512f64, 0.001953125), + Bar::new(1024f64,0.000976562), ], names: [ "Powering infrastructure", @@ -68,28 +67,26 @@ impl Progressable for Game { } } -impl GenerateCommands for Game { - fn generate_commands(&self, queue: &mut impl PacketQueue) { - Label::new(Origin::ZERO, TILE_WIDTH / 2, "Discordia Boot Procedure") - .generate_commands(queue); - Label::new( - Origin::new(TILE_WIDTH / 2 + 1, 0), - TILE_WIDTH / 2, - format!("Cycles: {}", self.currency.floor()), - ) - .generate_commands(queue); +impl Game { + pub fn draw(&self, text_layer: &mut WindowMut, pixel_layer: &mut WindowMut) { + text_layer.set_row_str(0, "Discordia Boot Procedure").unwrap(); + let middle = text_layer.width() / 2; + text_layer.window_mut( middle, 0, middle, 1) + .unwrap() + .set_row_str(0, &format!(" Cycles: {}", self.currency.floor())) + .unwrap(); 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() { continue; } - bar.generate_commands(queue); - Label::new( - Origin::new(TILE_WIDTH / 2 + 1, 1 + index), - TILE_WIDTH / 2 - 1, - self.names[index], - ) - .generate_commands(queue); + + bar.draw(&mut bar_window); + label_window.set_row_str(0, self.names[index]).unwrap(); } } } diff --git a/src/label.rs b/src/label.rs deleted file mode 100644 index 0cd51f4..0000000 --- a/src/label.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::GenerateCommands; -use crate::queue::PacketQueue; -use servicepoint::{CharGrid, CharGridCommand, Origin, Tiles}; - -#[derive(Debug, Clone)] -pub(crate) struct Label> { - position: Origin, - text: S, - width_tiles: usize, -} - -impl Label { - pub(crate) fn new>(position: Origin, width_tiles: usize, text: S) -> Self { - // TODO: those should be grapheme clusters - let text = text.as_ref().chars().take(width_tiles).collect::(); - Self { - position, - text, - width_tiles, - } - } -} - -impl> GenerateCommands for Label { - 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(); - } -} diff --git a/src/main.rs b/src/main.rs index 44d0af6..2bceab3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,13 @@ -use crate::queue::PacketQueue; use game::Game; -use servicepoint::{ClearCommand, UdpSocketExt, FRAME_PACING}; -use std::net::UdpSocket; -use std::thread::sleep; -use std::time::{Duration, Instant}; +use servicepoint::{BinaryOperation, BitVecCommand, Bitmap, CharGrid, CharGridCommand, ClearCommand, CompressionCode, UdpSocketExt, FRAME_PACING, TILE_HEIGHT, TILE_WIDTH}; +use std::{ + net::UdpSocket, + thread::sleep, + time::{Duration, Instant} +}; mod bar; -mod queue; mod game; -mod label; type Currency = f64; @@ -17,18 +16,17 @@ trait Progressable: Sized { 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 = "172.23.42.29:2342"; fn main() { let mut state = Game::new(); - let mut connection = UdpSocket::bind_connect(DESTINATION).unwrap(); + let connection = UdpSocket::bind_connect(DESTINATION).unwrap(); 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(); loop { let current_time = Instant::now(); @@ -37,7 +35,20 @@ fn main() { (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); } diff --git a/src/queue.rs b/src/queue.rs deleted file mode 100644 index db56d2a..0000000 --- a/src/queue.rs +++ /dev/null @@ -1,20 +0,0 @@ -use servicepoint::{Packet, UdpSocketExt}; -use std::net::UdpSocket; - -pub(crate) trait PacketQueue { - fn enqueue_command>(&mut self, packet: P) -> Result; -} - -impl PacketQueue for Vec { - fn enqueue_command>(&mut self, packet: P) -> Result { - self.push(packet.try_into()?); - Ok(true) - } -} - -impl PacketQueue for UdpSocket { - fn enqueue_command>(&mut self, packet: P) -> Result { - let packet = packet.try_into()?; - Ok(self.send_command(packet).is_some()) - } -}