summaryrefslogtreecommitdiff
path: root/src/systems/draw.rs
blob: 1d5ee6bdaeb16d3b0ad28d3bd59b9b83b5295349 (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
use sdl2::pixels::Color;
use sdl2::render::WindowCanvas;
use sdl2::video::{Window, WindowContext};
use specs::prelude::*;

use crate::components::{Pos, StaticDrawable};
use crate::texture_manager::TextureManager;

pub struct SysDraw
{
    canvas:          WindowCanvas,
    texture_manager: TextureManager<WindowContext>,
}

impl SysDraw
{
    pub fn new(window: Window) -> Self
    {
        let mut canvas = window
            .into_canvas()
            .build()
            .expect("Unable to create canvas");
        canvas.set_draw_color(Color::RGB(255, 255, 0));

        let texture_manager = TextureManager::new(canvas.texture_creator());

        Self {
            canvas,
            texture_manager,
        }
    }
}

impl<'a> System<'a> for SysDraw
{
    type SystemData = (ReadStorage<'a, Pos>, ReadStorage<'a, StaticDrawable>);

    fn run(&mut self, (pos, drawable): Self::SystemData)
    {
        self.canvas.clear();

        // XXX: This is so slow.. yaaaawn. Replace with ids and texture manager
        // lifetime?
        for (_pos, drawable) in (&pos, &drawable).join() {
            let texture = self
                .texture_manager
                .get(&drawable.texture_name)
                .expect("Unable to load texture");

            // let texture_info = texture.query();
            self.canvas
                .copy(&texture, None, None)
                .expect("Unable to draw texture");
        }

        self.canvas.present();
    }
}