Skip to content

Commit 6afdb02

Browse files
committed
rendy: Minor clean up and bug fixes
Now rendy will use the font included at the top of rendy.rs file directly instead of converting it to a more verbose format on initialization, also DebugRendy::plot_char now checks for characters that are outside of ASCII range (which is what the font supports) and prints a '?' (question mark) instead, so no more random crashes :^)
1 parent 39cdd60 commit 6afdb02

File tree

1 file changed

+29
-97
lines changed

1 file changed

+29
-97
lines changed

src/aero_kernel/src/rendy.rs

Lines changed: 29 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ static FONT: &[u8] = include_bytes!("../../font.bin");
5151
// -----------------------------------------------------|
5252
// ```
5353

54-
const DEFAULT_FONT_WIDTH: usize = 8;
55-
const DEFAULT_FONT_HEIGHT: usize = 16;
54+
const FONT_WIDTH: usize = 8;
55+
const FONT_HEIGHT: usize = 16;
5656

5757
const DEFAULT_MARGIN: usize = 64 / 2;
58-
5958
const TAB_SIZE: usize = 4;
6059

6160
/// The amount of VGA font glyphs.
62-
const VGA_FONT_GLYPHS: usize = 256;
6361
6462
const MARGIN_GRADIENT: usize = 4;
6563
const DWORD_SIZE: usize = core::mem::size_of::<u32>();
@@ -216,13 +214,8 @@ pub struct DebugRendy<'this> {
216214
map: Box<[Option<*mut QueueCharacter>]>,
217215
bg_canvas: Box<[u32]>,
218216

219-
vga_font_bool: Box<[bool]>,
220-
221217
queue_cursor: usize,
222218

223-
glyph_width: usize,
224-
glyph_height: usize,
225-
226219
offset_x: usize,
227220
offset_y: usize,
228221

@@ -237,17 +230,11 @@ impl<'this> DebugRendy<'this> {
237230
let width = info.horizontal_resolution;
238231
let height = info.vertical_resolution;
239232

240-
let glyph_width = DEFAULT_FONT_WIDTH;
241-
let glyph_height = DEFAULT_FONT_HEIGHT;
233+
let offset_x = DEFAULT_MARGIN + ((width - DEFAULT_MARGIN * 2) % FONT_WIDTH) / 2;
234+
let offset_y = DEFAULT_MARGIN + ((height - DEFAULT_MARGIN * 2) % FONT_HEIGHT) / 2;
242235

243-
let offset_x =
244-
DEFAULT_MARGIN + ((info.horizontal_resolution - DEFAULT_MARGIN * 2) % glyph_width) / 2;
245-
246-
let offset_y =
247-
DEFAULT_MARGIN + ((info.horizontal_resolution - DEFAULT_MARGIN * 2) % glyph_height) / 2;
248-
249-
let cols = (info.horizontal_resolution - DEFAULT_MARGIN * 2) / glyph_width;
250-
let rows = (info.vertical_resolution - DEFAULT_MARGIN * 2) / glyph_height;
236+
let cols = (width - DEFAULT_MARGIN * 2) / FONT_WIDTH;
237+
let rows = (height - DEFAULT_MARGIN * 2) / FONT_HEIGHT;
251238

252239
let grid_size = rows * cols * core::mem::size_of::<Character>();
253240
let grid = mem::alloc_boxed_buffer::<Character>(grid_size);
@@ -261,50 +248,6 @@ impl<'this> DebugRendy<'this> {
261248
let bg_canvas_size = width * height * core::mem::size_of::<u32>();
262249
let bg_canvas = mem::alloc_boxed_buffer::<u32>(bg_canvas_size);
263250

264-
let vga_font_bool_size = VGA_FONT_GLYPHS
265-
* DEFAULT_FONT_HEIGHT
266-
* DEFAULT_FONT_WIDTH
267-
* core::mem::size_of::<bool>();
268-
269-
let mut vga_font_bool = mem::alloc_boxed_buffer::<bool>(vga_font_bool_size);
270-
271-
for i in 0..VGA_FONT_GLYPHS {
272-
// Each glyph is a bitmap:
273-
let glyph = &FONT[i * DEFAULT_FONT_HEIGHT] as *const u8;
274-
275-
for y in 0..DEFAULT_FONT_HEIGHT {
276-
// NOTE: the characters in VGA fonts are always one byte wide.
277-
// 9 dot wide fonts have 8 dots and one empty column, except
278-
// characters 0xC0-0xDF replicate column 9.
279-
for x in 0..8 {
280-
let offset =
281-
i * DEFAULT_FONT_HEIGHT * DEFAULT_FONT_WIDTH + y * DEFAULT_FONT_WIDTH + x;
282-
283-
unsafe {
284-
if (*glyph.offset(y as isize) & (0x80 >> x)) != 0 {
285-
vga_font_bool[offset] = true;
286-
} else {
287-
vga_font_bool[offset] = false;
288-
}
289-
}
290-
}
291-
292-
// Fill columns above 8 like VGA Line Graphics Mode does:
293-
for x in 8..DEFAULT_FONT_WIDTH {
294-
let offset =
295-
i * DEFAULT_FONT_HEIGHT * DEFAULT_FONT_WIDTH + y * DEFAULT_FONT_WIDTH + x;
296-
297-
if i >= 0xC0 && i <= 0xDF {
298-
unsafe {
299-
vga_font_bool[offset] = (*glyph.offset(y as isize) & 1) != 0;
300-
}
301-
} else {
302-
vga_font_bool[offset] = false;
303-
}
304-
}
305-
}
306-
}
307-
308251
let mut this = Self {
309252
buffer,
310253
info,
@@ -326,23 +269,18 @@ impl<'this> DebugRendy<'this> {
326269
map,
327270
bg_canvas,
328271

329-
glyph_height,
330-
glyph_width,
272+
queue_cursor: 0,
331273

332274
offset_x,
333275
offset_y,
334276

335-
vga_font_bool,
336-
337-
queue_cursor: 0,
338-
339277
cursor_visibility: true,
340278
auto_flush: true,
341279
};
342280

343281
let image = cmdline.term_background.map(|a| parse_bmp_image(a));
344-
this.generate_canvas(image);
345282

283+
this.generate_canvas(image);
346284
this.clear(true);
347285
this.double_buffer_flush();
348286

@@ -366,8 +304,7 @@ impl<'this> DebugRendy<'this> {
366304

367305
let width = self.info.horizontal_resolution;
368306
let height = self.info.vertical_resolution;
369-
370-
let colsize = image.bpp / 8;
307+
let col_size = image.bpp / 8;
371308

372309
// Tiled Image:
373310
// for y in ystart..yend {
@@ -412,7 +349,7 @@ impl<'this> DebugRendy<'this> {
412349

413350
for x in xstart..xend {
414351
let img_pixel = unsafe {
415-
(image.image.as_ptr()).add(fixedp6_to_int(img_x) * colsize + off) as *const u32
352+
(image.image.as_ptr()).add(fixedp6_to_int(img_x) * col_size + off) as *const u32
416353
};
417354

418355
let i = blender(x, y, unsafe { *img_pixel });
@@ -474,22 +411,22 @@ impl<'this> DebugRendy<'this> {
474411
let height = self.info.vertical_resolution;
475412

476413
if let Some(image) = image {
477-
let frame_height = height / 2 - (self.glyph_height * self.rows) / 2;
478-
let frame_width = width / 2 - (self.glyph_width * self.cols) / 2;
414+
let frame_width = width / 2 - (FONT_WIDTH * self.cols) / 2;
415+
let frame_height = height / 2 - (FONT_HEIGHT * self.rows) / 2;
479416

480-
let frame_height_end = frame_height + self.glyph_height * self.rows;
481-
let frame_width_end = frame_width + self.glyph_width * self.cols;
417+
let frame_width_end = frame_width + FONT_WIDTH * self.cols;
418+
let frame_height_end = frame_height + FONT_HEIGHT * self.rows;
482419

483420
let fheight = frame_height - MARGIN_GRADIENT;
484421
let fheight_end = frame_height_end + MARGIN_GRADIENT;
422+
485423
let fwidth = frame_width - MARGIN_GRADIENT;
486424
let fwidth_end = frame_width_end + MARGIN_GRADIENT;
487425

488426
self.loop_external(&image, 0, width, 0, fheight);
489427
self.loop_external(&image, 0, width, fheight_end, height);
490428
self.loop_external(&image, 0, fwidth, fheight, fheight_end);
491429
self.loop_external(&image, fwidth_end, width, fheight, fheight_end);
492-
493430
self.loop_internal(
494431
&image,
495432
frame_width,
@@ -614,17 +551,17 @@ impl<'this> DebugRendy<'this> {
614551
return;
615552
}
616553

617-
let x = self.offset_x + x * self.glyph_width;
618-
let y = self.offset_y + y * self.glyph_height;
619-
554+
let x = self.offset_x + x * FONT_WIDTH;
555+
let y = self.offset_y + y * FONT_HEIGHT;
620556
let glyph = unsafe {
621-
self.vga_font_bool
622-
.as_ptr()
623-
.add(ch as usize * DEFAULT_FONT_HEIGHT * DEFAULT_FONT_WIDTH)
557+
core::slice::from_raw_parts(
558+
FONT.as_ptr().offset(ch as isize * FONT_HEIGHT as isize) as *const u8,
559+
FONT_HEIGHT,
560+
)
624561
};
625562

626563
// naming: fx, fy for font coordinates and gx, gy for glyph coordinates
627-
for gy in 0..self.glyph_height {
564+
for gy in 0..FONT_HEIGHT {
628565
let fb_line = unsafe {
629566
self.buffer
630567
.as_mut_ptr()
@@ -637,23 +574,18 @@ impl<'this> DebugRendy<'this> {
637574
.add(x + (y + gy) * self.info.horizontal_resolution)
638575
};
639576

640-
for fx in 0..DEFAULT_FONT_WIDTH {
641-
let draw = unsafe { *glyph.add(gy * DEFAULT_FONT_WIDTH + fx) };
642-
643-
let bg = if char.bg == u32::MAX {
644-
unsafe { *canvas_line.add(fx) }
577+
for gx in 0..FONT_WIDTH {
578+
let draw = glyph[gy] & (1 << (FONT_WIDTH - gx)) != 0;
579+
let color = if draw {
580+
char.fg
581+
} else if char.bg == u32::MAX {
582+
unsafe { *canvas_line.add(gx) }
645583
} else {
646584
char.bg
647585
};
648586

649-
let fg = char.fg;
650-
651587
unsafe {
652-
if draw {
653-
*fb_line.add(fx) = fg;
654-
} else {
655-
*fb_line.add(fx) = bg;
656-
}
588+
*fb_line.add(gx) = color;
657589
}
658590
}
659591
}

0 commit comments

Comments
 (0)