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
use std::fmt;

use crate::utils::*;

#[derive(Clone, Default)]
pub struct RenderTarget {
    width: u32,
    height: u32,
    pub data: Vec<u32>,
}

impl fmt::Debug for RenderTarget {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "RenderTarget ( width: {}, height: {})",
            self.width, self.height
        )
    }
}

impl std::cmp::PartialEq for RenderTarget {
    fn eq(&self, other: &Self) -> bool {
        self.width == other.width && self.height == other.height
    }
}

impl RenderTarget {
    /// Creates a new image with the given width and height.
    pub fn new(width: u32, height: u32) -> Self {
        RenderTarget {
            width,
            height,
            data: vec![Color::rgba(0, 0, 0, 0).data; width as usize * height as usize],
        }
    }

    /// Draws a u32 slice into the image.
    pub fn draw(&mut self, data: &[u32]) {
        self.data.clone_from_slice(data);
    }

    /// Create a new image from a boxed slice of colors
    pub fn from_data(width: u32, height: u32, data: Vec<u32>) -> Result<Self, String> {
        Ok(RenderTarget {
            width,
            height,
            data,
        })
    }

    /// Gets the width.
    pub fn width(&self) -> f64 {
        self.width as f64
    }

    /// Gets the height.
    pub fn height(&self) -> f64 {
        self.height as f64
    }

    pub fn data(&self) -> &[u32] {
        &self.data
    }

    pub fn data_mut(&mut self) -> &mut [u32] {
        &mut self.data
    }
}