mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-29 17:34:58 +03:00
dirt simple hack to cache rendered text. reasonable!
This commit is contained in:
parent
1e7760062d
commit
1371ed35ee
@ -1,7 +1,12 @@
|
||||
// TODO We don't need refcell maybe
|
||||
use crate::GeomBatch;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
|
||||
// TODO We don't need refcell maybe? Can we take &mut Assets?
|
||||
pub struct Assets {
|
||||
pub default_line_height: f64,
|
||||
pub font_size: usize,
|
||||
text_cache: RefCell<HashMap<String, GeomBatch>>,
|
||||
}
|
||||
|
||||
impl Assets {
|
||||
@ -9,6 +14,7 @@ impl Assets {
|
||||
let mut a = Assets {
|
||||
default_line_height: 0.0,
|
||||
font_size,
|
||||
text_cache: RefCell::new(HashMap::new()),
|
||||
};
|
||||
a.default_line_height = a.line_height(a.font_size);
|
||||
a
|
||||
@ -18,4 +24,14 @@ impl Assets {
|
||||
// TODO Ahhh this stops working.
|
||||
font_size as f64
|
||||
}
|
||||
|
||||
pub fn get_cached_text(&self, key: &str) -> Option<GeomBatch> {
|
||||
self.text_cache.borrow().get(key).cloned()
|
||||
}
|
||||
|
||||
pub fn cache_text(&self, key: String, geom: GeomBatch) {
|
||||
self.text_cache.borrow_mut().insert(key, geom);
|
||||
//println!("cache has {} things",
|
||||
// abstutil::prettyprint_usize(self.text_cache.borrow().len()));
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
use crate::assets::Assets;
|
||||
use crate::{Color, GeomBatch, Prerender, ScreenDims};
|
||||
use geom::Polygon;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::fmt::Write;
|
||||
use std::hash::Hasher;
|
||||
use textwrap;
|
||||
|
||||
const FG_COLOR: Color = Color::WHITE;
|
||||
@ -191,7 +193,12 @@ impl Text {
|
||||
self.render(assets).get_dims()
|
||||
}
|
||||
|
||||
pub fn render(self, _: &Assets) -> GeomBatch {
|
||||
pub fn render(self, assets: &Assets) -> GeomBatch {
|
||||
let hash_key = self.hash_key();
|
||||
if let Some(batch) = assets.get_cached_text(&hash_key) {
|
||||
return batch;
|
||||
}
|
||||
|
||||
// TODO Bad guess
|
||||
let empty_line_height = 30.0;
|
||||
|
||||
@ -232,12 +239,19 @@ impl Text {
|
||||
output_batch.push(color, poly);
|
||||
}
|
||||
|
||||
assets.cache_text(hash_key, output_batch.clone());
|
||||
output_batch
|
||||
}
|
||||
|
||||
pub fn render_to_batch(self, prerender: &Prerender) -> GeomBatch {
|
||||
self.render(&prerender.assets).realign()
|
||||
}
|
||||
|
||||
fn hash_key(&self) -> String {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
hasher.write(format!("{:?}", self).as_ref());
|
||||
format!("{:x}", hasher.finish())
|
||||
}
|
||||
}
|
||||
|
||||
fn render_text(spans: Vec<TextSpan>) -> GeomBatch {
|
||||
|
Loading…
Reference in New Issue
Block a user