summaryrefslogtreecommitdiffstats
path: root/examples/tour/src/iced_ggez/renderer.rs
blob: c0e6d559238ec4b4ebee23506c6244a46029a45a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
mod button;
mod checkbox;
mod debugger;
mod image;
mod radio;
mod slider;
mod text;

use ggez::graphics::{
    self, spritebatch::SpriteBatch, Font, Image, MeshBuilder,
};
use ggez::Context;

pub use image::Cache;

pub struct Renderer<'a> {
    pub context: &'a mut Context,
    pub images: &'a mut image::Cache,
    pub sprites: SpriteBatch,
    pub spritesheet: Image,
    pub font: Font,
    font_size: f32,
    debug_mesh: Option<MeshBuilder>,
}

impl<'a> Renderer<'a> {
    pub fn new(
        context: &'a mut Context,
        images: &'a mut image::Cache,
        spritesheet: Image,
        font: Font,
    ) -> Renderer<'a> {
        Renderer {
            context,
            images,
            sprites: SpriteBatch::new(spritesheet.clone()),
            spritesheet,
            font,
            font_size: 20.0,
            debug_mesh: None,
        }
    }

    pub fn flush(&mut self) {
        graphics::draw(
            self.context,
            &self.sprites,
            graphics::DrawParam::default(),
        )
        .expect("Draw sprites");

        graphics::draw_queued_text(
            self.context,
            graphics::DrawParam::default(),
            Default::default(),
            graphics::FilterMode::Linear,
        )
        .expect("Draw text");

        if let Some(debug_mesh) = self.debug_mesh.take() {
            let mesh =
                debug_mesh.build(self.context).expect("Build debug mesh");

            graphics::draw(self.context, &mesh, graphics::DrawParam::default())
                .expect("Draw debug mesh");
        }
    }
}

pub fn into_color(color: iced_native::Color) -> graphics::Color {
    graphics::Color {
        r: color.r,
        g: color.g,
        b: color.b,
        a: color.a,
    }
}