index : snake-rs
An implementation of the classic Snake game in Rust using SDL2
about summary refs log tree commit diff

Diffstat (limited to 'src/ui_components/text.rs')

-rw-r--r-- src/ui_components/text.rs 35
 1 file changed, 35 deletions(-)

diff --git a/src/ui_components/text.rs b/src/ui_components/text.rs
deleted file mode 100644
index 5c63b3d..0000000 100644
--- a/src/ui_components/text.rs
+++ /dev/null
@@ -1,35 +1,0 @@
use sdl2::{pixels::Color, rect::Rect, ttf, video::Window, render::Canvas};

use crate::{game_context::Point};

pub struct Text {
    pub text: String,
    pub color: Color,
    pub point: Point,
}

impl Text {
    pub fn new(text: &str, color: Color, point: Point) -> Self {
        Self {
            text: text.to_string(),
            color,
            point,
        }
    }

    pub fn draw(&mut self, canvas: &mut Canvas<Window>, font: &ttf::Font<'static, 'static>) -> Result<(), String> {
        let surface = font
            .render(&self.text)
            .blended(self.color)
            .map_err(|e| e.to_string())?;

        let texture_creator = canvas.texture_creator();
        let texture = texture_creator
            .create_texture_from_surface(&surface)
            .map_err(|e| e.to_string())?;

        let target = Rect::new(self.point.0, self.point.1, surface.width(), surface.height());
        canvas.copy(&texture, None, Some(target))?;
        Ok(())
    }
}