From 31418696f9daa5b90b3239050072760a0bd463a3 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Fri, 28 Nov 2025 21:37:50 +0100 Subject: [PATCH] Optimised renderering --- src/lib.rs | 7 ++++--- src/renderer.rs | 35 +++++++++++++++++++++++++++-------- src/terminal.rs | 11 +++++++++-- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 089a115..cb30aad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,13 +74,14 @@ impl Engine { } pub fn render(self: &Arc) { - terminal::clear().unwrap(); - let mut renderer = renderer::Renderer::new(); for entity in self.entities.lock().unwrap().iter() { renderer.set_position_point(entity.get_position()); entity.render(&mut renderer); } + + terminal::clear().unwrap(); + renderer.render(); } pub fn tick(self: &Arc, tick: u16) { @@ -98,7 +99,7 @@ impl Engine { loop { self.render(); - sleep(16); + sleep(160); } } } diff --git a/src/renderer.rs b/src/renderer.rs index d02d78c..b437276 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,4 +1,4 @@ -use crate::terminal::{hex_bg, print_liner}; +use crate::terminal::{self, hex_bg, write_liner}; #[derive(Debug, Clone)] pub struct Point { @@ -8,26 +8,40 @@ pub struct Point { pub struct Renderer { pos: Point, + buf: String, } impl Renderer { pub fn new() -> Self { Self { pos: Point { x: 0, y: 0 }, + buf: String::new(), } } - pub fn draw_rect(&self, x: u16, y: u16, width: u16, height: u16, color: u32) { + pub fn draw_rect(&mut self, x: u16, y: u16, width: u16, height: u16, color: u32) { let rl = " ".repeat(width as usize) + "\x1b[0m"; let rect = (rl + "\n").repeat(height as usize); - print_liner(self.pos.x + x, self.pos.y + y, &hex_bg(color), &rect); + write_liner( + &mut self.buf, + self.pos.x + x, + self.pos.y + y, + &hex_bg(color), + &rect, + ); } - pub fn draw_pixel(&self, x: u16, y: u16, color: u32) { - print_liner(self.pos.x + x, self.pos.y + y, &hex_bg(color), &" \x1b[0m"); + pub fn draw_pixel(&mut self, x: u16, y: u16, color: u32) { + write_liner( + &mut self.buf, + self.pos.x + x, + self.pos.y + y, + &hex_bg(color), + &" \x1b[0m", + ); } - pub fn draw_line(&self, x0: i16, y0: i16, x1: i16, y1: i16, color: u32) { + pub fn draw_line(&mut self, x0: i16, y0: i16, x1: i16, y1: i16, color: u32) { let dx = (x1 - x0).abs(); let sx = if x0 < x1 { 1 } else { -1 }; let dy = -(y1 - y0).abs(); @@ -55,7 +69,7 @@ impl Renderer { } } - pub fn draw_vertex_outline(&self, x: u16, y: u16, vertices: &[Point], color: u32) { + pub fn draw_vertex_outline(&mut self, x: u16, y: u16, vertices: &[Point], color: u32) { if vertices.len() < 2 { return; } @@ -74,7 +88,7 @@ impl Renderer { } } - pub fn draw_vertex(&self, x: u16, y: u16, vertices: &[Point], color: u32) { + pub fn draw_vertex(&mut self, x: u16, y: u16, vertices: &[Point], color: u32) { if vertices.len() < 3 { return; } @@ -125,6 +139,11 @@ impl Renderer { } } + pub fn render(&self) { + print!("{}", self.buf); + terminal::flush().unwrap(); + } + pub fn set_position(&mut self, x: u16, y: u16) { self.pos.x = x; self.pos.y = y; diff --git a/src/terminal.rs b/src/terminal.rs index 041208a..fcd0660 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1,3 +1,4 @@ +use std::fmt::Write as _; use std::io::{self, Write}; use crossterm::{ExecutableCommand, terminal}; @@ -22,9 +23,15 @@ pub fn show_cursor() -> io::Result<()> { flush() } -pub fn print_liner(x: u16, y: u16, liner: &str, text: &str) { +pub fn write_liner(buf: &mut String, x: u16, y: u16, liner: &str, text: &str) { for (i, line) in text.lines().enumerate() { - println!("\x1b[{};{}H{liner}{line}\x1b[0m", (i as u16) + y + 1, x + 1); + write!( + buf, + "\x1b[{};{}H{liner}{line}\x1b[0m", + (i as u16) + y + 1, + x + 1 + ) + .unwrap(); } }