From 6e47e69d45d30481e98c4fba5542c195b0fb830e Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 11 Jun 2020 10:29:27 +0200 Subject: Added text factory --- src/time.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'src/time.rs') diff --git a/src/time.rs b/src/time.rs index eba7507..791ff2e 100644 --- a/src/time.rs +++ b/src/time.rs @@ -1,7 +1,4 @@ use std::time::{SystemTime, Duration}; -use sdl2::ttf; -use sdl2::render::{Texture, TextureCreator}; -use sdl2::pixels::Color; #[derive(PartialEq)] pub enum State { @@ -38,23 +35,18 @@ impl Timer { pub fn idle(&mut self) { self.state = State::Idle; } +} + +use std::fmt; - pub fn to_texture<'a, T>( - &'a self, - font: &ttf::Font, - tex_creator: &'a TextureCreator, - bg: &Color - ) -> Texture - { +impl fmt::Display for Timer { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let current = if self.state == State::Active { self.time.elapsed().unwrap() } else { self.result }.as_millis(); - let s = format!("{}.{}", current / 1000, current % 1000); - - let surface = font.render(&s).shaded(Color::RGB(255, 255, 255), *bg).unwrap(); - tex_creator.create_texture_from_surface(&surface).unwrap() + write!(f, "{}.{}", current / 1000, current % 1000) } } -- cgit