From 5a4a6bddf2f59970a408764c7efba78b0710e8ad Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 2 Feb 2019 13:59:22 -0800 Subject: [PATCH] prerender bikes --- docs/TODO_ux.md | 1 + editor/src/render/bike.rs | 44 +++++++++++++++++++++++---------------- editor/src/render/mod.rs | 12 ++++------- editor/src/ui.rs | 4 ++-- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/docs/TODO_ux.md b/docs/TODO_ux.md index 0a99b39f45..c567b1fa9b 100644 --- a/docs/TODO_ux.md +++ b/docs/TODO_ux.md @@ -4,6 +4,7 @@ - cache draw agents - actually use Drawables in the places + - have to undo the hacks for cars appearing out of a border ## Quick n easy diff --git a/editor/src/render/bike.rs b/editor/src/render/bike.rs index 2b935b595f..eb391eb482 100644 --- a/editor/src/render/bike.rs +++ b/editor/src/render/bike.rs @@ -1,6 +1,7 @@ +use crate::colors::ColorScheme; use crate::objects::{Ctx, ID}; use crate::render::{RenderOptions, Renderable}; -use ezgui::{Color, GfxCtx}; +use ezgui::{Color, Drawable, GfxCtx, Prerender}; use geom::{Bounds, Distance, Polygon, Pt2D}; use sim::{CarID, CarState, DrawCarInput}; @@ -12,18 +13,33 @@ pub struct DrawBike { // TODO the turn arrows for bikes look way wrong // TODO maybe also draw lookahead buffer to know what the car is considering stopping_buffer: Option, - state: CarState, + + draw_default: Drawable, } impl DrawBike { - pub fn new(input: DrawCarInput) -> DrawBike { + pub fn new(input: DrawCarInput, prerender: &Prerender, cs: &ColorScheme) -> DrawBike { let stopping_buffer = input.stopping_trace.map(|t| t.make_polygons(BIKE_WIDTH)); + let polygon = input.body.make_polygons(BIKE_WIDTH); + + let draw_default = prerender.upload_borrowed(vec![( + match input.state { + CarState::Debug => cs + .get_def("debug bike", Color::BLUE.alpha(0.8)) + .shift(input.id.0), + // TODO Hard to see on the greenish bike lanes? :P + CarState::Moving => cs.get_def("moving bike", Color::GREEN).shift(input.id.0), + CarState::Stuck => cs.get_def("stuck bike", Color::RED).shift(input.id.0), + CarState::Parked => panic!("Can't have a parked bike"), + }, + &polygon, + )]); DrawBike { id: input.id, - polygon: input.body.make_polygons(BIKE_WIDTH), + polygon, stopping_buffer, - state: input.state, + draw_default, } } } @@ -34,19 +50,11 @@ impl Renderable for DrawBike { } fn draw(&self, g: &mut GfxCtx, opts: RenderOptions, ctx: &Ctx) { - let color = opts.color.unwrap_or_else(|| { - match self.state { - CarState::Debug => ctx - .cs - .get_def("debug bike", Color::BLUE.alpha(0.8)) - .shift(self.id.0), - // TODO Hard to see on the greenish bike lanes? :P - CarState::Moving => ctx.cs.get_def("moving bike", Color::GREEN).shift(self.id.0), - CarState::Stuck => ctx.cs.get_def("stuck bike", Color::RED).shift(self.id.0), - CarState::Parked => panic!("Can't have a parked bike"), - } - }); - g.draw_polygon(color, &self.polygon); + if let Some(color) = opts.color { + g.draw_polygon(color, &self.polygon); + } else { + g.redraw(&self.draw_default); + } if let Some(ref t) = self.stopping_buffer { g.draw_polygon( diff --git a/editor/src/render/mod.rs b/editor/src/render/mod.rs index 5895172b6c..e1249d53d5 100644 --- a/editor/src/render/mod.rs +++ b/editor/src/render/mod.rs @@ -62,18 +62,14 @@ pub struct RenderOptions { pub show_all_detail: bool, } -pub fn new_draw_vehicle( +pub fn draw_vehicle( input: DrawCarInput, map: &Map, - _prerender: &Prerender, - _cs: &ColorScheme, + prerender: &Prerender, + cs: &ColorScheme, ) -> Box { - draw_vehicle(input, map) -} - -pub fn draw_vehicle(input: DrawCarInput, map: &Map) -> Box { if input.vehicle_type == VehicleType::Bike { - Box::new(DrawBike::new(input)) + Box::new(DrawBike::new(input, prerender, cs)) } else { Box::new(DrawCar::new(input, map)) } diff --git a/editor/src/ui.rs b/editor/src/ui.rs index 44c6716831..10f2c1d7b7 100644 --- a/editor/src/ui.rs +++ b/editor/src/ui.rs @@ -1,7 +1,7 @@ use abstutil; //use cpuprofiler; use crate::objects::{Ctx, RenderingHints, ID}; -use crate::render::{new_draw_vehicle, AgentCache, DrawPedestrian, RenderOptions, Renderable}; +use crate::render::{draw_vehicle, AgentCache, DrawPedestrian, RenderOptions, Renderable}; use crate::state::UIState; use ezgui::{ Canvas, Color, EventCtx, EventLoopMode, Folder, GfxCtx, Key, ModalMenu, Prerender, Text, @@ -467,7 +467,7 @@ impl UI { if !agents.has(tick, *on) { let mut list: Vec> = Vec::new(); for c in sim.get_draw_cars(*on, map).into_iter() { - list.push(new_draw_vehicle(c, map, prerender, &state.cs)); + list.push(draw_vehicle(c, map, prerender, &state.cs)); } for p in sim.get_draw_peds(*on, map).into_iter() { list.push(Box::new(DrawPedestrian::new(p, map, prerender, &state.cs)));