prerender bikes

This commit is contained in:
Dustin Carlino 2019-02-02 13:59:22 -08:00
parent d08751fb64
commit 5a4a6bddf2
4 changed files with 33 additions and 28 deletions

View File

@ -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

View File

@ -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<Polygon>,
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(

View File

@ -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<Renderable> {
draw_vehicle(input, map)
}
pub fn draw_vehicle(input: DrawCarInput, map: &Map) -> Box<Renderable> {
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))
}

View File

@ -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<S: UIState> UI<S> {
if !agents.has(tick, *on) {
let mut list: Vec<Box<Renderable>> = 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)));