summaryrefslogtreecommitdiff
path: root/src/app/draw_video.rs
blob: a0d2016225938f6b3b9a66e5324c7c508bd6967f (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
/*
	Copyright 2021-2023 Gabriel Jensen.

	This file is part of Luma.

	Luma is free software: you can redistribute it
	and/or modify it under the terms of the GNU
	Affero General Public License as published by
	the Free Software Foundation, either version 3
	of the License, or (at your option) any later
	version.

	Luma is distributed in the hope that it will be
	useful, but WITHOUT ANY WARRANTY; without even
	the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU
	Affero General Public License for more details.

	You should have received a copy of the GNU
	Affero General Public License along with Luma.
	If not, see <https://www.gnu.org/licenses/>.
*/

use crate::SCREEN_SIZE;
use crate::app::App;

use sdl2::pixels::Color;
use sdl2::rect::Rect;

impl App {
	pub fn draw_video(&mut self, video: &[u8], agb_palette: &[u16]) {
		// TO-DO: Honour video mode.

		let mut palette: [Color; 0x100] = [Color::RGB(0x00, 0x00, 0x00); 0x100];

		for (index, element) in palette.iter_mut().enumerate() {
			let value = unsafe { *agb_palette.get_unchecked(index) };

			let colour = decode_colour(value);
			*element = colour;
		}

		for pixel_y in 0x0..SCREEN_SIZE.1 {
			for pixel_x in 0x0..SCREEN_SIZE.0 {
				let pixel = pixel_y as usize * SCREEN_SIZE.0 as usize + pixel_x as usize;

				let value  = video[pixel];
				let colour = palette[value as usize];
				self.canvas.set_draw_color(colour);

				let square = Rect::new(
					(pixel_x as u32 * self.scale) as i32,
					(pixel_y as u32 * self.scale) as i32,
					self.scale,
					self.scale,
				);
				self.canvas.fill_rect(square).unwrap();
			}
		}

		self.canvas.present();
	}
}

fn decode_colour(colour: u16) -> Color {
	let red   = ((colour & 0b0000000000011111) as f32 / 31.0    * 255.0) as u8;
	let green = ((colour & 0b0000001111100000) as f32 / 992.0   * 255.0) as u8;
	let blue  = ((colour & 0b0111110000000000) as f32 / 31744.0 * 255.0) as u8;

	return Color::RGB(red, green, blue);
}