batch traffic signal drawing

This commit is contained in:
Dustin Carlino 2019-05-16 17:10:22 -07:00
parent a816a3d0be
commit db04553626
5 changed files with 67 additions and 23 deletions

View File

@ -110,6 +110,7 @@ impl GUI for GameState {
fn event(&mut self, ctx: &mut EventCtx) -> EventLoopMode {
match self.mode {
Mode::SplashScreen(ref mut wizard, ref mut maybe_screensaver) => {
let anim = maybe_screensaver.is_some();
if let Some((ref mut screensaver, ref mut rng)) = maybe_screensaver {
screensaver.update(rng, ctx.input, ctx.canvas, &self.ui.primary.map);
}
@ -121,7 +122,11 @@ impl GUI for GameState {
self.before_quit(ctx.canvas);
std::process::exit(0);
}
EventLoopMode::Animation
if anim {
EventLoopMode::Animation
} else {
EventLoopMode::InputOnly
}
}
Mode::Edit(_) => EditMode::event(self, ctx),
Mode::Tutorial(_) => TutorialMode::event(self, ctx),
@ -150,6 +155,11 @@ impl GUI for GameState {
Mode::Mission(_) => MissionEditMode::draw(self, g),
Mode::ABTest(_) => ABTestMode::draw(self, g),
}
/*println!(
"{} uploads, {} draw calls",
g.get_num_uploads(),
g.num_draw_calls
);*/
}
fn dump_before_abort(&self, canvas: &Canvas) {

View File

@ -1,7 +1,7 @@
use crate::helpers::{ColorScheme, ID};
use crate::render::{DrawCrosswalk, DrawCtx, DrawOptions, DrawTurn, Renderable, OUTLINE_THICKNESS};
use abstutil::Timer;
use ezgui::{Color, Drawable, GfxCtx, Prerender, ScreenPt, Text};
use ezgui::{Color, Drawable, GeomBatch, GfxCtx, Prerender, ScreenPt, Text};
use geom::{Angle, Circle, Distance, Duration, Line, PolyLine, Polygon, Pt2D};
use map_model::{
ControlStopSign, Cycle, Intersection, IntersectionID, IntersectionType, Map, Road,
@ -216,10 +216,13 @@ pub fn draw_signal_cycle(
crosswalk.draw(g);
}
}
let mut batch = GeomBatch::new();
for t in &cycle.priority_turns {
let turn = ctx.map.get_t(*t);
if !turn.between_sidewalks() {
DrawTurn::draw_full(turn, g, priority_color);
DrawTurn::full_geom(turn, &mut batch, priority_color);
}
}
for t in &cycle.yield_turns {
@ -229,11 +232,12 @@ pub fn draw_signal_cycle(
&& turn.turn_type != TurnType::LaneChangeLeft
&& turn.turn_type != TurnType::LaneChangeRight
{
DrawTurn::draw_outline(turn, g, yield_color);
DrawTurn::outline_geom(turn, &mut batch, yield_color);
}
}
if time_left.is_none() {
batch.draw(g);
return;
}
@ -245,23 +249,24 @@ pub fn draw_signal_cycle(
let top_left = center.offset(-box_width / 2.0, -box_height / 2.0);
let percent = time_left.unwrap() / cycle.duration;
// TODO Tune colors.
g.draw_polygon(
batch.push(
ctx.cs.get_def("traffic signal box", Color::grey(0.2)),
&Polygon::rectangle_topleft(top_left, box_width, box_height),
Polygon::rectangle_topleft(top_left, box_width, box_height),
);
g.draw_circle(
batch.push(
Color::RED,
&Circle::new(center.offset(0.0, (-2.0 * radius).inner_meters()), radius),
Circle::new(center.offset(0.0, (-2.0 * radius).inner_meters()), radius).to_polygon(),
);
g.draw_circle(Color::grey(0.4), &Circle::new(center, radius));
g.draw_polygon(
batch.push(Color::grey(0.4), Circle::new(center, radius).to_polygon());
batch.push(
Color::YELLOW,
&Circle::new(center, radius).to_partial_polygon(percent),
Circle::new(center, radius).to_partial_polygon(percent),
);
g.draw_circle(
batch.push(
Color::GREEN,
&Circle::new(center.offset(0.0, (2.0 * radius).inner_meters()), radius),
Circle::new(center.offset(0.0, (2.0 * radius).inner_meters()), radius).to_polygon(),
);
batch.draw(g);
}
fn draw_signal_cycle_with_icons(cycle: &Cycle, g: &mut GfxCtx, ctx: &DrawCtx) {

View File

@ -3,7 +3,7 @@ use crate::render::{
BIG_ARROW_THICKNESS, CROSSWALK_LINE_THICKNESS, TURN_ICON_ARROW_LENGTH,
TURN_ICON_ARROW_THICKNESS,
};
use ezgui::{Color, Drawable, GfxCtx, Prerender};
use ezgui::{Color, Drawable, GeomBatch, GfxCtx, Prerender};
use geom::{Circle, Distance, Line};
use map_model::{Map, Turn, TurnID, LANE_THICKNESS};
@ -37,11 +37,14 @@ impl DrawTurn {
}
}
pub fn full_geom(t: &Turn, batch: &mut GeomBatch, color: Color) {
batch.extend(color, t.geom.make_arrow(BIG_ARROW_THICKNESS * 2.0).unwrap());
}
pub fn draw_full(t: &Turn, g: &mut GfxCtx, color: Color) {
g.draw_polygons(
color,
&t.geom.make_arrow(BIG_ARROW_THICKNESS * 2.0).unwrap(),
);
let mut batch = GeomBatch::new();
DrawTurn::full_geom(t, &mut batch, color);
batch.draw(g);
// For debugging
/*for pt in t.geom.points() {
@ -67,11 +70,10 @@ impl DrawTurn {
g.draw_arrow(color, BIG_ARROW_THICKNESS, &arrow_line);
}
pub fn draw_outline(turn: &Turn, g: &mut GfxCtx, color: Color) {
g.draw_polygons(
pub fn outline_geom(turn: &Turn, batch: &mut GeomBatch, color: Color) {
batch.extend(
color,
&turn
.geom
turn.geom
.make_arrow_outline(BIG_ARROW_THICKNESS * 2.0, BIG_ARROW_THICKNESS / 2.0)
.unwrap(),
);

View File

@ -154,6 +154,8 @@ impl<'a> GfxCtx<'a> {
)
.unwrap();
self.num_draw_calls += 1;
// println!("{:?}", backtrace::Backtrace::new());
}
pub fn enable_hatching(&mut self) {
@ -280,3 +282,28 @@ impl<'a> GfxCtx<'a> {
}
}
}
pub struct GeomBatch {
list: Vec<(Color, Polygon)>,
}
impl GeomBatch {
pub fn new() -> GeomBatch {
GeomBatch { list: Vec::new() }
}
pub fn push(&mut self, color: Color, p: Polygon) {
self.list.push((color, p));
}
pub fn extend(&mut self, color: Color, polys: Vec<Polygon>) {
for p in polys {
self.list.push((color, p));
}
}
pub fn draw(self, g: &mut GfxCtx) {
let refs = self.list.iter().map(|(color, p)| (*color, p)).collect();
g.draw_polygon_batch(refs);
}
}

View File

@ -11,7 +11,7 @@ mod widgets;
pub use crate::canvas::{Canvas, HorizontalAlignment, VerticalAlignment, BOTTOM_LEFT, CENTERED};
pub use crate::color::Color;
pub use crate::drawing::GfxCtx;
pub use crate::drawing::{GeomBatch, GfxCtx};
pub use crate::event::{Event, Key};
pub use crate::event_ctx::{Drawable, EventCtx, Prerender};
pub use crate::input::UserInput;