Skip to content

Commit

Permalink
[#66] Improve rendering of horizontal borders
Browse files Browse the repository at this point in the history
  • Loading branch information
Orchaldir committed May 24, 2021
1 parent bbb2f6d commit 073fc0a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 64 deletions.
31 changes: 16 additions & 15 deletions tilemap/src/rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,12 @@ impl Renderer {
Border::Empty => {}
Border::Wall(id) => {
let wall_style = self.resources.wall_styles.get(id);
let start_index = get_start_of_horizontal_border(index, y);
let end_index = get_end_of_horizontal_border(index, y);

wall_style.get_edge_style().render_horizontal(
&data,
start,
self.tile_size,
self.calculate_horizontal_edge(nodes, index, y),
0,
nodes[start_index],
nodes[end_index],
texture,
);
}
Expand All @@ -139,32 +135,24 @@ impl Renderer {
} => {
let wall_style = self.resources.wall_styles.get(wall_id);
let door_style = self.resources.door_styles.get(door_id);
let start_index = get_start_of_horizontal_border(index, y);
let end_index = get_end_of_horizontal_border(index, y);
let offset = door_style
.get_offset(wall_style.get_edge_style().get_thickness(), is_front);

door_style.get_edge_style().render_horizontal(
&data,
start,
self.tile_size,
self.calculate_horizontal_edge(nodes, index, y),
offset,
nodes[start_index],
nodes[end_index],
texture,
);
}
Border::Window { window_id, .. } => {
let window_style = self.resources.window_styles.get(window_id);
let start_index = get_start_of_horizontal_border(index, y);
let end_index = get_end_of_horizontal_border(index, y);

window_style.render_horizontal(
&data,
start,
self.tile_size,
nodes[start_index],
nodes[end_index],
self.calculate_horizontal_edge(nodes, index, y),
texture,
);
}
Expand Down Expand Up @@ -300,6 +288,19 @@ impl Renderer {
texture.set_base_depth(height);
generator.render(texture, &data);
}

fn calculate_horizontal_edge(
&self,
nodes: &[Option<&NodeStyle>],
border_index: usize,
y: u32,
) -> (i32, u32) {
let start_index = get_start_of_horizontal_border(border_index, y);
let end_index = get_end_of_horizontal_border(border_index, y);
let start_half = nodes[start_index].map(|n| n.get_half()).unwrap_or(0);
let end_half = nodes[end_index].map(|n| n.get_half()).unwrap_or(0);
(start_half, self.tile_size - (start_half + end_half) as u32)
}
}

#[cfg(test)]
Expand Down
64 changes: 20 additions & 44 deletions tilemap/src/rendering/style/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ impl EdgeStyle {
&self,
data: &Data,
node: Point,
tile_size: u32,
edge: (i32, u32),
offset: i32,
start_node: Option<&NodeStyle>,
end_node: Option<&NodeStyle>,
texture: &mut Texture,
) {
match self {
Expand All @@ -80,15 +78,8 @@ impl EdgeStyle {
horizontal,
..
} => {
let aabb = EdgeStyle::calculate_horizontal_aabb(
node,
tile_size,
offset,
start_node,
end_node,
*thickness,
*half_thickness,
);
let aabb =
calculate_horizontal_aabb2(node, edge, offset, *thickness, *half_thickness);
horizontal.generate(texture, &data.transform(aabb))
}
EdgeStyle::Mock(..) => {}
Expand All @@ -97,15 +88,8 @@ impl EdgeStyle {
half_thickness,
component,
} => {
let aabb = EdgeStyle::calculate_horizontal_aabb(
node,
tile_size,
offset,
start_node,
end_node,
*thickness,
*half_thickness,
);
let aabb =
calculate_horizontal_aabb2(node, edge, offset, *thickness, *half_thickness);
component.render(texture, &data.transform(aabb))
}
}
Expand Down Expand Up @@ -159,22 +143,6 @@ impl EdgeStyle {
}
}

fn calculate_horizontal_aabb(
node: Point,
tile_size: u32,
offset: i32,
start_node: Option<&NodeStyle>,
end_node: Option<&NodeStyle>,
thickness: u32,
half_thickness: i32,
) -> AABB {
let start_half = start_node.map(|n| n.get_half()).unwrap_or(0);
let end_half = end_node.map(|n| n.get_half()).unwrap_or(0);
let start = Point::new(node.x + start_half, node.y - half_thickness + offset);
let size = Size::new(tile_size - (start_half + end_half) as u32, thickness);
AABB::new(start, size)
}

fn calculate_vertical_aabb(
node: Point,
tile_size: u32,
Expand All @@ -198,12 +166,25 @@ impl Default for EdgeStyle {
}
}

fn calculate_horizontal_aabb2(
node: Point,
edge: (i32, u32),
offset: i32,
thickness: u32,
half_thickness: i32,
) -> AABB {
let (start, length) = edge;
let start = Point::new(node.x + start, node.y - half_thickness + offset);
let size = Size::new(length, thickness);
AABB::new(start, size)
}

#[cfg(test)]
mod tests {
use super::*;
use texture_generation::generation::component::rendering::RenderingComponent;
use texture_generation::generation::data::texture::Texture;
use texture_generation::math::color::{BLACK, GREEN, RED};
use texture_generation::math::color::{BLACK, GREEN};

#[test]
#[should_panic]
Expand All @@ -219,20 +200,15 @@ mod tests {

#[test]
fn test_render_horizontal() {
let component = RenderingComponent::new_fill_area(RED, 9);
let edge_component = RenderingComponent::new_fill_area(GREEN, 4);
let node_style0 = NodeStyle::new("node0", 4, component.clone());
let node_style1 = NodeStyle::new("node0", 2, component);
let edge_style = EdgeStyle::new_solid(2, edge_component).unwrap();
let mut texture = Texture::new(Size::new(11, 6), BLACK);

edge_style.render_horizontal(
&Data::for_texture(texture.get_aabb()),
Point::new(3, 3),
7,
(2, 4),
0,
Some(&node_style0),
Some(&node_style1),
&mut texture,
);

Expand Down
8 changes: 3 additions & 5 deletions tilemap/src/rendering/style/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ impl WindowStyle {
&self,
data: &Data,
node: Point,
tile_size: u32,
start_node: Option<&NodeStyle>,
end_node: Option<&NodeStyle>,
edge: (i32, u32),
texture: &mut Texture,
) {
self.stool_style
.render_horizontal(data, node, tile_size, 0, start_node, end_node, texture);
.render_horizontal(data, node, edge, 0, texture);
self.pane_style
.render_horizontal(data, node, tile_size, 0, start_node, end_node, texture);
.render_horizontal(data, node, edge, 0, texture);
}

pub fn render_vertical(
Expand Down

0 comments on commit 073fc0a

Please sign in to comment.