Optimised renderering
This commit is contained in:
+4
-3
@@ -74,13 +74,14 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(self: &Arc<Self>) {
|
pub fn render(self: &Arc<Self>) {
|
||||||
terminal::clear().unwrap();
|
|
||||||
|
|
||||||
let mut renderer = renderer::Renderer::new();
|
let mut renderer = renderer::Renderer::new();
|
||||||
for entity in self.entities.lock().unwrap().iter() {
|
for entity in self.entities.lock().unwrap().iter() {
|
||||||
renderer.set_position_point(entity.get_position());
|
renderer.set_position_point(entity.get_position());
|
||||||
entity.render(&mut renderer);
|
entity.render(&mut renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
terminal::clear().unwrap();
|
||||||
|
renderer.render();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tick(self: &Arc<Self>, tick: u16) {
|
pub fn tick(self: &Arc<Self>, tick: u16) {
|
||||||
@@ -98,7 +99,7 @@ impl Engine {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
self.render();
|
self.render();
|
||||||
sleep(16);
|
sleep(160);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-8
@@ -1,4 +1,4 @@
|
|||||||
use crate::terminal::{hex_bg, print_liner};
|
use crate::terminal::{self, hex_bg, write_liner};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Point<I = u16> {
|
pub struct Point<I = u16> {
|
||||||
@@ -8,26 +8,40 @@ pub struct Point<I = u16> {
|
|||||||
|
|
||||||
pub struct Renderer {
|
pub struct Renderer {
|
||||||
pos: Point,
|
pos: Point,
|
||||||
|
buf: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Renderer {
|
impl Renderer {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
pos: Point { x: 0, y: 0 },
|
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 rl = " ".repeat(width as usize) + "\x1b[0m";
|
||||||
let rect = (rl + "\n").repeat(height as usize);
|
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) {
|
pub fn draw_pixel(&mut self, x: u16, y: u16, color: u32) {
|
||||||
print_liner(self.pos.x + x, self.pos.y + y, &hex_bg(color), &" \x1b[0m");
|
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 dx = (x1 - x0).abs();
|
||||||
let sx = if x0 < x1 { 1 } else { -1 };
|
let sx = if x0 < x1 { 1 } else { -1 };
|
||||||
let dy = -(y1 - y0).abs();
|
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 {
|
if vertices.len() < 2 {
|
||||||
return;
|
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 {
|
if vertices.len() < 3 {
|
||||||
return;
|
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) {
|
pub fn set_position(&mut self, x: u16, y: u16) {
|
||||||
self.pos.x = x;
|
self.pos.x = x;
|
||||||
self.pos.y = y;
|
self.pos.y = y;
|
||||||
|
|||||||
+9
-2
@@ -1,3 +1,4 @@
|
|||||||
|
use std::fmt::Write as _;
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
use crossterm::{ExecutableCommand, terminal};
|
use crossterm::{ExecutableCommand, terminal};
|
||||||
@@ -22,9 +23,15 @@ pub fn show_cursor() -> io::Result<()> {
|
|||||||
flush()
|
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() {
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user