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
pub use self::memory_rgba8_texture::MemoryRGBA8Texture;
pub use self::sub_texture::SubTexture;

use std::ops::{Deref, DerefMut};

pub mod sub_texture;
pub mod memory_rgba8_texture;
pub mod image_texture;

pub trait Texture {
    type Pixel: Pixel;

    fn width(&self) -> u32;
    fn height(&self) -> u32;
    // TODO: Chanage returen value to &-ptr
    fn get(&self, x: u32, y: u32) -> Option<Self::Pixel>;
    fn set(&mut self, x: u32, y: u32, val: Self::Pixel);

    fn get_rotated(&self, x: u32, y: u32) -> Option<Self::Pixel> {
        let w = self.height();
        self.get(y, w - x - 1)
    }

    fn is_column_transparent(&self, col: u32) -> bool {
        for y in 0..self.height() {
            if let Some(p) = self.get(col, y) {
                if !p.is_transparent() {
                    return false;
                }
            }
        }
        true
    }

    fn is_row_transparent(&self, row: u32) -> bool {
        for x in 0..self.width() {
            if let Some(p) = self.get(x, row) {
                if !p.is_transparent() {
                    return false;
                }
            }
        }
        true
    }
}

pub trait Pixel: Sized {
    fn is_transparent(&self) -> bool;
    fn transparency() -> Option<Self>;
    fn outline() -> Self;
}

impl <P: Pixel> Texture for Box<Texture<Pixel=P> + 'static> {
    type Pixel = P;

    fn width(&self) -> u32 {
        self.deref().width()
    }

    fn height(&self) -> u32 {
        self.deref().height()
    }

    fn get(&self, x: u32, y: u32) -> Option<P> {
        self.deref().get(x, y)
    }

    fn set(&mut self, x: u32, y :u32, val: P) {
        self.deref_mut().set(x, y, val);
    }
}