summaryrefslogtreecommitdiff
path: root/src/devices/screen/draw_line.rs
blob: 94066f404005490539134eff87b36876cb0fc8ac (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use super::*;

impl ScreenDevice {
    pub fn draw_line(&mut self, colour: u8, layer: ScreenLayer) {
        let [p0, p1] = [self.cursor, self.vector];
        match (p0.x == p1.x, p0.y == p1.y) {
            (false, false) =>   self.draw_diagonal_line(colour, layer),
            (false,  true) => self.draw_horizontal_line(colour, layer),
            ( true, false) =>   self.draw_vertical_line(colour, layer),
            ( true,  true) =>           self.draw_pixel(colour, layer, p0),
        };
    }

    pub fn draw_line_1bit(&mut self, params: u8, layer: ScreenLayer) {
        let [p0, p1] = [self.cursor, self.vector];
        match (p0.x == p1.x, p0.y == p1.y) {
            (false, false) =>   self.draw_diagonal_line_1bit(params, layer),
            (false,  true) => self.draw_horizontal_line_1bit(params, layer),
            ( true, false) =>   self.draw_vertical_line_1bit(params, layer),
            ( true,  true) =>           self.draw_pixel_1bit(params, layer, p0),
        };
    }

    pub fn draw_pixel_1bit(&mut self, params: u8, layer: ScreenLayer, point: ScreenPosition) {
        let dim = self.dimensions;
        let sprite = self.sprite_buffer.get_1bit_sprite(params);
        let colour = sprite[point.y as usize % 8][point.x as usize % 8];
        if !dim.contains_point(point) || colour == 0xff { return }
        let index = point.x as usize + ((dim.width as usize) * (point.y as usize));
        match layer {
            ScreenLayer::Background => self.background[index] = colour,
            ScreenLayer::Foreground => self.foreground[index] = colour,
        };
    }

    fn draw_horizontal_line(&mut self, colour: u8, layer: ScreenLayer) {
        if let Some([x0, y, x1, _]) = self.find_vector_bounding_box() {
            let screen_width = self.dimensions.width as usize;
            let i = screen_width * y;
            let buffer = match layer {
                ScreenLayer::Background => &mut self.background,
                ScreenLayer::Foreground => &mut self.foreground,
            };
            buffer[i+x0..=i+x1].fill(colour);
        }
    }

    fn draw_horizontal_line_1bit(&mut self, params: u8, layer: ScreenLayer) {
        if let Some([x0, y, x1, _]) = self.find_vector_bounding_box() {
            let screen_width = self.dimensions.width as usize;
            let i = screen_width * y;
            let buffer = match layer {
                ScreenLayer::Background => &mut self.background,
                ScreenLayer::Foreground => &mut self.foreground,
            };
            let sprite = self.sprite_buffer.get_1bit_sprite(params);
            let row = sprite[y % 8];
            for x in x0..=x1 {
                let colour = row[x % 8];
                if colour != 0xff { buffer[i+x] = colour };
            }
        }
    }

    fn draw_vertical_line(&mut self, colour: u8, layer: ScreenLayer) {
        if let Some([x, y0, _, y1]) = self.find_vector_bounding_box() {
            let screen_width = self.dimensions.width as usize;
            let mut i = (screen_width * y0) + x;
            let buffer = match layer {
                ScreenLayer::Background => &mut self.background,
                ScreenLayer::Foreground => &mut self.foreground,
            };
            for _ in y0..=y1 {
                buffer[i] = colour;
                i += screen_width;
            }
        }
    }

    fn draw_vertical_line_1bit(&mut self, params: u8, layer: ScreenLayer) {
        if let Some([x, y0, _, y1]) = self.find_vector_bounding_box() {
            let screen_width = self.dimensions.width as usize;
            let mut i = (screen_width * y0) + x;
            let buffer = match layer {
                ScreenLayer::Background => &mut self.background,
                ScreenLayer::Foreground => &mut self.foreground,
            };
            let sprite = self.sprite_buffer.get_1bit_sprite(params);
            let mut column = [0u8; 8];
            for y in 0..8 { column[y] = sprite[y][x % 8] }
            for y in y0..=y1 {
                let colour = column[y % 8];
                if colour != 0xff { buffer[i] = colour };
                i += screen_width;
            }
        }
    }

    fn draw_diagonal_line(&mut self, colour: u8, layer: ScreenLayer) {
        fn abs_diff(v0: u16, v1: u16) -> u16 {
            let v = v1.wrapping_sub(v0);
            if v > 0x8000 { !v + 1 } else { v }
        }
        let [p0, p1] = [self.cursor, self.vector];

        // If the slope of the line is greater than 1.
        if  abs_diff(p0.y, p1.y) > abs_diff(p0.x, p1.x) {
            // Swap points 0 and 1 such that y0 is always smaller than y1.
            let (x0, y0, x1, y1) = match p0.y > p1.y {
                true  => (p1.x, p1.y, p0.x, p0.y),
                false => (p0.x, p0.y, p1.x, p1.y),
            };
            let dy = y1 - y0;
            let (dx, xi) = match x0 > x1 {
                true  => (x0 - x1, 0xffff),
                false => (x1 - x0, 0x0001),
            };
            let dxdy2 = (dx.wrapping_sub(dy)).wrapping_mul(2);
            let dx2 = dx * 2;
            let mut d = dx2.wrapping_sub(dy);
            let mut x = x0;

            for y in y0..=y1 {
                self.draw_pixel(colour, layer, ScreenPosition::new(x, y));
                if d < 0x8000 {
                    x = x.wrapping_add(xi); d = d.wrapping_add(dxdy2);
                } else {
                    d = d.wrapping_add(dx2);
                }
            }
        // If the slope of the line is less than or equal to 1.
        } else {
            // Swap points 0 and 1 so that x0 is always smaller than x1.
            let (x0, y0, x1, y1) = match p0.x > p1.x {
                true  => (p1.x, p1.y, p0.x, p0.y),
                false => (p0.x, p0.y, p1.x, p1.y),
            };
            let dx = x1 - x0;
            let (dy, yi) = match y0 > y1 {
                true  => (y0 - y1, 0xffff),
                false => (y1 - y0, 0x0001),
            };
            let dydx2 = (dy.wrapping_sub(dx)).wrapping_mul(2);
            let dy2 = dy * 2;
            let mut d = dy2.wrapping_sub(dx);
            let mut y = y0;

            for x in x0..=x1 {
                self.draw_pixel(colour, layer, ScreenPosition::new(x, y));
                if d < 0x8000 {
                    y = y.wrapping_add(yi);
                    d = d.wrapping_add(dydx2);
                } else {
                    d = d.wrapping_add(dy2);
                }
            }
        }
    }

    fn draw_diagonal_line_1bit(&mut self, params: u8, layer: ScreenLayer) {
        fn abs_diff(v0: u16, v1: u16) -> u16 {
            let v = v1.wrapping_sub(v0);
            if v > 0x8000 { !v + 1 } else { v }
        }
        let [p0, p1] = [self.cursor, self.vector];

        // If the slope of the line is greater than 1.
        if  abs_diff(p0.y, p1.y) > abs_diff(p0.x, p1.x) {
            // Swap points 0 and 1 such that y0 is always smaller than y1.
            let (x0, y0, x1, y1) = match p0.y > p1.y {
                true  => (p1.x, p1.y, p0.x, p0.y),
                false => (p0.x, p0.y, p1.x, p1.y),
            };
            let dy = y1 - y0;
            let (dx, xi) = match x0 > x1 {
                true  => (x0 - x1, 0xffff),
                false => (x1 - x0, 0x0001),
            };
            let dxdy2 = (dx.wrapping_sub(dy)).wrapping_mul(2);
            let dx2 = dx * 2;
            let mut d = dx2.wrapping_sub(dy);
            let mut x = x0;

            for y in y0..=y1 {
                self.draw_pixel_1bit(params, layer, ScreenPosition::new(x, y));
                if d < 0x8000 {
                    x = x.wrapping_add(xi); d = d.wrapping_add(dxdy2);
                } else {
                    d = d.wrapping_add(dx2);
                }
            }
        // If the slope of the line is less than or equal to 1.
        } else {
            // Swap points 0 and 1 so that x0 is always smaller than x1.
            let (x0, y0, x1, y1) = match p0.x > p1.x {
                true  => (p1.x, p1.y, p0.x, p0.y),
                false => (p0.x, p0.y, p1.x, p1.y),
            };
            let dx = x1 - x0;
            let (dy, yi) = match y0 > y1 {
                true  => (y0 - y1, 0xffff),
                false => (y1 - y0, 0x0001),
            };
            let dydx2 = (dy.wrapping_sub(dx)).wrapping_mul(2);
            let dy2 = dy * 2;
            let mut d = dy2.wrapping_sub(dx);
            let mut y = y0;

            for x in x0..=x1 {
                self.draw_pixel_1bit(params, layer, ScreenPosition::new(x, y));
                if d < 0x8000 {
                    y = y.wrapping_add(yi);
                    d = d.wrapping_add(dydx2);
                } else {
                    d = d.wrapping_add(dy2);
                }
            }
        }
    }



}