Browse Source

Pushing rendering work so far

master
Graham Northup 3 years ago
parent
commit
08c3041a2b
Signed by untrusted user: grissess GPG Key ID: 5D000E6F539376FB
  1. 89
      src/world/render.rs

89
src/world/render.rs

@ -1,8 +1,91 @@
pub struct Neighbors<'a, T> {
use super::*;
use std::fmt::Debug;
use rogue_util::coord::*;
use rogue_util::grid::region::Region;
#[derive(Debug)]
pub struct Neighbors<'a, T: Debug> {
ul: &'a T, u: &'a T, ur: &'a T,
l: &'a T, r: &'a T,
l: &'a T, this: &'a T, r: &'a T,
dl: &'a T, d: &'a T, dr: &'a T,
}
pub trait Renderable {
impl<'a, T: Debug> Neighbors<'a, T> {
pub fn for_each<F>(&self, mut f: F)
where
F: FnMut(&'a T)
{
f(self.ul); f(self.u); f(self.ur);
f(self.l); f(self.this); f(self.r);
f(self.dl); f(self.d); f(self.dr);
}
}
pub trait Renderer {
type Tile: Debug;
fn render<'a, 's: 'a>(&'s mut self, neighbors: Neighbors<'a, Self::Tile>) -> char;
}
pub struct BasicRenderer;
impl Renderer for BasicRenderer {
type Tile = Tile;
fn render<'a, 's: 'a>(&'s mut self, neighbors: Neighbors<'a, Tile>) -> char {
match neighbors.this.content {
Content::Air => '.',
Content::Stone => ' ',
}
}
}
pub struct EdgeRenderer;
impl Renderer for EdgeRenderer {
type Tile = Tile;
fn render<'a, 's: 'a>(&'s mut self, neighbors: Neighbors<'a, Tile>) -> char {
match neighbors.this.content {
Content::Air => '.',
Content::Stone => {
let mut air_count = 0;
neighbors.for_each(|t| if t.content == Content::Air { air_count += 1; });
if air_count > 0 {
'#'
} else {
' '
}
},
}
}
}
pub struct RenderState {
term_size: V2i,
center: V2i,
}
impl RenderState {
pub fn new_centered(term_size: V2i, center: V2i) -> RenderState {
debug_assert!(term_size.is_strict_q1());
RenderState {
term_size, center,
}
}
pub fn new(term_size: V2i) -> RenderState { RenderState::new_centered(term_size, V2i(0, 0)) }
pub fn center(&self) -> V2i { self.center }
pub fn term_size(&self) -> V2i { self.term_size }
pub fn recenter(&mut self, c: V2i) {
self.center = c;
}
pub fn render(&self, r: &Region<Tile>) {
// TODO
}
}
Loading…
Cancel
Save