Permalink
Browse files

Pull drawcall sorting+dedup out into a function

We can use it from the wasm backend, too.
  • Loading branch information...
tomassedovic committed Dec 20, 2017
1 parent f3b800e commit f8acebc2c47cf3e37126d30436d41e4a68619228
Showing with 63 additions and 55 deletions.
  1. +2 −55 src/engine/glium.rs
  2. +61 −0 src/engine/mod.rs
@@ -16,19 +16,6 @@ use std::time::{Duration, Instant};
use util;


// NOTE: This is designed specifically to deduplicated characters on
// the same position (using Vec::dedup). So the only thing considered
// equal are characters with the same pos value.
impl PartialEq for Draw {
fn eq(&self, other: &Self) -> bool {
use engine::Draw::*;
match (self, other) {
(&Char(p1, ..), &Char(p2, ..)) => p1 == p2,
_ => false,
}
}
}

fn gl_color(color: Color, alpha: f32) -> [f32; 4] {
[
color.r as f32 / 255.0,
@@ -299,48 +286,8 @@ pub fn main_loop(

// Process drawcalls
vertices.clear();
// NOTE: The first item is inserted by the engine, so keep it here
drawcalls[1..].sort_by(|a, b| {
use std::cmp::Ordering::*;
use engine::Draw::*;

match (a, b) {
(&Char(p1, ..), &Char(p2, ..)) => {
let x_ordering = p1.x.cmp(&p2.x);
if x_ordering == Equal {
p1.y.cmp(&p2.y)
} else {
x_ordering
}
}

(&Background(..), &Background(..)) => Equal,
(&Text(..), &Text(..)) => Equal,
(&Rectangle(..), &Rectangle(..)) => Equal,
(&Fade(..), &Fade(..)) => Equal,

(&Fade(..), _) => Greater,
(_, &Fade(..)) => Less,

(&Background(..), &Char(..)) => Less,
(&Char(..), &Background(..)) => Greater,

(&Background(..), &Text(..)) => Less,
(&Text(..), &Background(..)) => Greater,

(&Background(..), &Rectangle(..)) => Less,
(&Rectangle(..), &Background(..)) => Greater,

_ => Equal,
}
});

// Remove duplicate background and foreground tiles. I.e. for
// any given point, only the last specified drawcall of the
// same kind will remain.
drawcalls.reverse();
drawcalls.dedup();
drawcalls.reverse();
// NOTE: The first item is inserted by the engine/backend here, so keep it there
::engine::sort_drawcalls(&mut drawcalls, 1..);

for drawcall in &drawcalls {
match drawcall {
@@ -5,6 +5,7 @@ use point::Point;
use state::State;
use std::borrow::Cow;

use std::ops::RangeFrom;
use std::time::Duration;


@@ -34,6 +35,19 @@ pub enum Draw {
}


// NOTE: This is designed specifically to deduplicated characters on
// the same position (using Vec::dedup). So the only thing considered
// equal are characters with the same pos value.
impl PartialEq for Draw {
fn eq(&self, other: &Self) -> bool {
use engine::Draw::*;
match (self, other) {
(&Char(p1, ..), &Char(p2, ..)) => p1 == p2,
_ => false,
}
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Mouse {
pub tile_pos: Point,
@@ -75,6 +89,53 @@ pub type UpdateFn = fn(&mut State,
-> RunningState;


/// Sort the drawcalls in the specified range. Then "reverse
/// deduplicate" them -- meaning the latest item stays (rather than
/// the first one in a normal dedup).
pub fn sort_drawcalls(drawcalls: &mut Vec<Draw>, range: RangeFrom<usize>) {
use std::cmp::Ordering::*;
use engine::Draw::*;
drawcalls[range].sort_by(|a, b| {
match (a, b) {
(&Char(p1, ..), &Char(p2, ..)) => {
let x_ordering = p1.x.cmp(&p2.x);
if x_ordering == Equal {
p1.y.cmp(&p2.y)
} else {
x_ordering
}
}

(&Background(..), &Background(..)) => Equal,
(&Text(..), &Text(..)) => Equal,
(&Rectangle(..), &Rectangle(..)) => Equal,
(&Fade(..), &Fade(..)) => Equal,

(&Fade(..), _) => Greater,
(_, &Fade(..)) => Less,

(&Background(..), &Char(..)) => Less,
(&Char(..), &Background(..)) => Greater,

(&Background(..), &Text(..)) => Less,
(&Text(..), &Background(..)) => Greater,

(&Background(..), &Rectangle(..)) => Less,
(&Rectangle(..), &Background(..)) => Greater,

_ => Equal,
}
});

// Remove duplicate background and foreground tiles. I.e. for
// any given point, only the last specified drawcall of the
// same kind will remain.
drawcalls.reverse();
drawcalls.dedup();
drawcalls.reverse();
}


// NOTE:
// fn texture_coords_from_char(chr: char) -> Option<(i32, i32)>
include!(concat!(env!("OUT_DIR"), "/glyph_lookup_table.rs"));

0 comments on commit f8acebc

Please sign in to comment.