livesplit-core 0.13.0

livesplit-core is a library that provides a lot of functionality for creating a speedrun timer.
Documentation
use core::marker::PhantomData;

use crate::{
    component::timer::State,
    rendering::{
        consts::PADDING, font::CachedLabel, resource::ResourceAllocator, scene::Layer, FillShader,
        RenderContext,
    },
};

pub struct Cache<I, L> {
    time: CachedLabel<L>,
    fraction: CachedLabel<L>,
    _image: PhantomData<I>,
}

impl<I, L> Cache<I, L> {
    pub const fn new() -> Self {
        Self {
            time: CachedLabel::new(),
            fraction: CachedLabel::new(),
            _image: PhantomData,
        }
    }
}

pub(in crate::rendering) fn render<A: ResourceAllocator>(
    cache: &mut Cache<A::Image, A::Label>,
    context: &mut RenderContext<'_, A>,
    [width, height]: [f32; 2],
    component: &State,
) -> f32 {
    context.render_background([width, height], &component.background);
    let shader = FillShader::VerticalGradient(
        component.top_color.to_array(),
        component.bottom_color.to_array(),
    );
    let render_target = Layer::from_updates_frequently(component.updates_frequently);
    let x = context.render_timer(
        &component.fraction,
        &mut cache.fraction,
        render_target,
        [width - PADDING, 0.85 * height],
        0.8 * height,
        shader,
    );
    context.render_timer(
        &component.time,
        &mut cache.time,
        render_target,
        [x, 0.85 * height],
        1.2 * height,
        shader,
    )
}