Start drawing objects

This commit is contained in:
2025-02-15 18:33:18 -05:00
parent dfcfd17bfc
commit c4f17bfc13
9 changed files with 380 additions and 88 deletions
+38 -7
View File
@@ -2,21 +2,53 @@ use egui::{Color32, Image, ImageSource, Response, Sense, TextureOptions, Ui, Wid
pub const GENERIC_PALETTE: [u8; 4] = [0, 64, 128, 255];
pub fn parse_palette(palette: u8, brts: &[u8]) -> [u8; 4] {
pub fn shade(brt: u8, color: Color32) -> Color32 {
color.gamma_multiply(brt as f32 / 255.0)
}
pub fn generic_palette(color: Color32) -> [Color32; 4] {
GENERIC_PALETTE.map(|brt| shade(brt, color))
}
pub fn parse_palette(palette: u8, brts: &[u8], color: Color32) -> [Color32; 4] {
let shades = [
0,
brts[0],
brts[2],
brts[0].saturating_add(brts[2]).saturating_add(brts[4]),
Color32::BLACK,
shade(brts[0], color),
shade(brts[2], color),
shade(
brts[0].saturating_add(brts[2]).saturating_add(brts[4]),
color,
),
];
[
0,
Color32::BLACK,
shades[(palette >> 2) as usize & 0x03],
shades[(palette >> 4) as usize & 0x03],
shades[(palette >> 6) as usize & 0x03],
]
}
pub fn parse_cell(cell: u16) -> (usize, bool, bool, usize) {
let char_index = (cell & 0x7ff) as usize;
let vflip = cell & 0x1000 != 0;
let hflip = cell & 0x2000 != 0;
let palette_index = (cell >> 14) as usize;
(char_index, vflip, hflip, palette_index)
}
pub fn read_char_row(
char: &[u16],
hflip: bool,
vflip: bool,
row: usize,
) -> impl Iterator<Item = u8> {
let pixels = if vflip { char[7 - row] } else { char[row] };
(0..16).step_by(2).map(move |i| {
let pixel = if hflip { 14 - i } else { i };
((pixels >> pixel) & 0x3) as u8
})
}
pub struct CharacterGrid<'a> {
source: ImageSource<'a>,
scale: f32,
@@ -71,7 +103,6 @@ impl Widget for CharacterGrid<'_> {
fn ui(self, ui: &mut Ui) -> Response {
let image = Image::new(self.source)
.fit_to_original_size(self.scale)
.tint(Color32::RED)
.texture_options(TextureOptions::NEAREST)
.sense(Sense::click());
let res = ui.add(image);