Cookie Clicker: draw the building picture faster and cache it
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
parent
b725eef57f
commit
f27373b693
1 changed files with 26 additions and 5 deletions
|
|
@ -98,20 +98,38 @@ fn building_strip(sheet: &image::RgbaImage, owned: &[u64; N]) -> Option<Vec<u8>>
|
|||
}
|
||||
let label = format!("×{}", short(*count as f64));
|
||||
let (x, y) = ((cx + 76) as i32, (cy + 22) as i32);
|
||||
// shape the text once per colour and draw it at a few offsets for the outline
|
||||
let (dark, light) = (layout(&label, 26.0, &shadow, None, Align::Left), layout(&label, 26.0, &white, None, Align::Left));
|
||||
for (dx, dy) in [(-2, 0), (2, 0), (0, -2), (0, 2), (2, 2)] {
|
||||
layout(&label, 26.0, &shadow, None, Align::Left).draw(&mut img, x + dx, y + dy);
|
||||
dark.draw(&mut img, x + dx, y + dy);
|
||||
}
|
||||
layout(&label, 26.0, &white, None, Align::Left).draw(&mut img, x, y);
|
||||
light.draw(&mut img, x, y);
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).ok()?;
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// Pictures already drawn, by what they show: drawing is the slow part on a small machine.
|
||||
fn strip_cache() -> &'static Mutex<HashMap<[u64; N], Vec<u8>>> {
|
||||
static C: OnceLock<Mutex<HashMap<[u64; N], Vec<u8>>>> = OnceLock::new();
|
||||
C.get_or_init(Default::default)
|
||||
}
|
||||
|
||||
async fn strip_for(state: &State, b: &Bakery) -> Option<Vec<u8>> {
|
||||
let sheet = building_sheet(state).await?;
|
||||
let owned = b.owned;
|
||||
tokio::task::spawn_blocking(move || building_strip(&sheet, &owned)).await.ok().flatten()
|
||||
if let Some(png) = strip_cache().lock().ok().and_then(|c| c.get(&owned).cloned()) {
|
||||
return Some(png);
|
||||
}
|
||||
let sheet = building_sheet(state).await?;
|
||||
let png = tokio::task::spawn_blocking(move || building_strip(&sheet, &owned)).await.ok().flatten()?;
|
||||
if let Ok(mut c) = strip_cache().lock() {
|
||||
if c.len() >= 200 {
|
||||
c.clear();
|
||||
}
|
||||
c.insert(owned, png.clone());
|
||||
}
|
||||
Some(png)
|
||||
}
|
||||
|
||||
// ---- the numbers -----------------------------------------------------------------------------------------------------------
|
||||
|
|
@ -925,7 +943,10 @@ mod tests {
|
|||
for (i, n) in [43, 3, 1, 12, 2, 1, 7, 4, 9, 120, 5, 3, 2, 1, 1, 1, 8, 2, 1, 1].iter().enumerate() {
|
||||
owned[i] = *n;
|
||||
}
|
||||
std::fs::write(std::env::var("DEZIO_OUT").unwrap(), building_strip(&sheet, &owned).unwrap()).unwrap();
|
||||
let started = std::time::Instant::now();
|
||||
let png = building_strip(&sheet, &owned).unwrap();
|
||||
eprintln!("strip took {:?}", started.elapsed());
|
||||
std::fs::write(std::env::var("DEZIO_OUT").unwrap(), png).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue