From 51b1f909c51917231249b2a159bc2d7aa5ac3f67 Mon Sep 17 00:00:00 2001 From: Corwin Date: Fri, 24 May 2024 18:23:19 +0100 Subject: [PATCH 1/4] clear screen and handle message overlap --- agb/src/panics_render.rs | 14 ++++++++++---- agb/src/panics_render/text.rs | 4 ++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/agb/src/panics_render.rs b/agb/src/panics_render.rs index c657a9549..77220e1ce 100644 --- a/agb/src/panics_render.rs +++ b/agb/src/panics_render.rs @@ -26,6 +26,7 @@ pub fn render_backtrace(trace: &backtrace::Frames, info: &PanicInfo) -> ! { gba.dma.dma().dma3.disable(); let mut gfx = gba.display.video.bitmap3(); + gfx.clear(0xFFFF); let qrcode_string_data = if WEBSITE.is_empty() { format!("{trace}") @@ -38,14 +39,19 @@ pub fn render_backtrace(trace: &backtrace::Frames, info: &PanicInfo) -> ! { let mut trace_text_render = text::BitmapTextRender::new(&mut gfx, (location, 8).into(), 0x0000); - let _ = write!( + let _ = writeln!( &mut trace_text_render, "The game crashed :({}{WEBSITE}\n{trace}", if WEBSITE.is_empty() { "" } else { "\n" } ); - let mut panic_text_render = - text::BitmapTextRender::new(&mut gfx, (8, location).into(), 0x0000); + let trace_location = trace_text_render.head_y_position(); + + let mut panic_text_render = text::BitmapTextRender::new( + &mut gfx, + (8, location.max(trace_location + PADDING)).into(), + 0x0000, + ); let _ = write!(&mut panic_text_render, "{info}"); // need to wait 2 frames to ensure that mgba finishes rendering before the fatal call below @@ -62,11 +68,11 @@ pub fn render_backtrace(trace: &backtrace::Frames, info: &PanicInfo) -> ! { }) }) } +const PADDING: i32 = 8; /// Returns the width / height of the QR code + padding in pixels fn draw_qr_code(gfx: &mut Bitmap3<'_>, qrcode_string_data: &str) -> i32 { const MAX_VERSION: qrcodegen_no_heap::Version = qrcodegen_no_heap::Version::new(6); - const PADDING: i32 = 8; let (Ok(mut temp_buffer), Ok(mut out_buffer)) = ( Vec::try_with_capacity_in(MAX_VERSION.buffer_len(), crate::ExternalAllocator), diff --git a/agb/src/panics_render/text.rs b/agb/src/panics_render/text.rs index 7155c4154..c0d09aec9 100644 --- a/agb/src/panics_render/text.rs +++ b/agb/src/panics_render/text.rs @@ -30,6 +30,10 @@ impl<'bitmap, 'gba> BitmapTextRender<'bitmap, 'gba> { } } + pub fn head_y_position(&self) -> i32 { + self.head_position.y + } + fn render_letter(&mut self, c: char) { let letter = FONT.letter(c); From 2d34099afc77c5e1a7529332f55b401468b16a35 Mon Sep 17 00:00:00 2001 From: Corwin Date: Fri, 24 May 2024 19:15:34 +0100 Subject: [PATCH 2/4] resize the buffer --- agb/src/panics_render.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/agb/src/panics_render.rs b/agb/src/panics_render.rs index 77220e1ce..6acfa5b27 100644 --- a/agb/src/panics_render.rs +++ b/agb/src/panics_render.rs @@ -73,15 +73,19 @@ const PADDING: i32 = 8; /// Returns the width / height of the QR code + padding in pixels fn draw_qr_code(gfx: &mut Bitmap3<'_>, qrcode_string_data: &str) -> i32 { const MAX_VERSION: qrcodegen_no_heap::Version = qrcodegen_no_heap::Version::new(6); + let buffer_len = MAX_VERSION.buffer_len(); let (Ok(mut temp_buffer), Ok(mut out_buffer)) = ( - Vec::try_with_capacity_in(MAX_VERSION.buffer_len(), crate::ExternalAllocator), - Vec::try_with_capacity_in(MAX_VERSION.buffer_len(), crate::ExternalAllocator), + Vec::try_with_capacity_in(buffer_len, crate::ExternalAllocator), + Vec::try_with_capacity_in(buffer_len, crate::ExternalAllocator), ) else { crate::println!("Failed to allocate memory to generate QR code"); return PADDING; }; + temp_buffer.resize(buffer_len, 0); + out_buffer.resize(buffer_len, 0); + let qr_code = match qrcodegen_no_heap::QrCode::encode_text( qrcode_string_data, &mut temp_buffer, From cfc3c3a93c69ce1aeb5f17a703470d6e42909374 Mon Sep 17 00:00:00 2001 From: Corwin Date: Sat, 25 May 2024 12:10:35 +0100 Subject: [PATCH 3/4] add changelog entry --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c00d8b8f6..ca4cecc41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fixed the crash screen to show debug text even if the qr code fails to generate. +- Fixed the crash screen to prevent the qr code always failing to generate. + ## [0.20.1] - 2024/05/17 ### Added From b3d7642071620d2c28a4a1452a1a5c80cd892575 Mon Sep 17 00:00:00 2001 From: Corwin Date: Sat, 25 May 2024 12:22:38 +0100 Subject: [PATCH 4/4] extract code generation and add test --- agb/src/panics_render.rs | 78 +++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/agb/src/panics_render.rs b/agb/src/panics_render.rs index 6acfa5b27..8e1096289 100644 --- a/agb/src/panics_render.rs +++ b/agb/src/panics_render.rs @@ -1,12 +1,13 @@ use core::{fmt::Write, panic::PanicInfo}; -use alloc::{format, vec::Vec}; +use alloc::{collections::TryReserveError, format, vec::Vec}; +use qrcodegen_no_heap::DataTooLong; use crate::{ backtrace, display::{bitmap3::Bitmap3, busy_wait_for_vblank, HEIGHT, WIDTH}, dma::dma3_exclusive, - mgba, syscall, + mgba, syscall, ExternalAllocator, }; mod text; @@ -70,32 +71,53 @@ pub fn render_backtrace(trace: &backtrace::Frames, info: &PanicInfo) -> ! { } const PADDING: i32 = 8; +struct QrCodeBuffers { + temp_buffer: Vec, + out_buffer: Vec, + version: qrcodegen_no_heap::Version, +} + +impl QrCodeBuffers { + fn new(version: qrcodegen_no_heap::Version) -> Result { + let buffer_length = version.buffer_len(); + let mut temp = Vec::try_with_capacity_in(buffer_length, ExternalAllocator)?; + let mut out = Vec::try_with_capacity_in(buffer_length, ExternalAllocator)?; + temp.resize(buffer_length, 0); + out.resize(buffer_length, 0); + + Ok(Self { + temp_buffer: temp, + out_buffer: out, + version, + }) + } + + fn generate_qr_code(&mut self, data: &str) -> Result { + qrcodegen_no_heap::QrCode::encode_text( + data, + &mut self.temp_buffer, + &mut self.out_buffer, + qrcodegen_no_heap::QrCodeEcc::Medium, + qrcodegen_no_heap::Version::MIN, + self.version, + None, + true, + ) + } +} + /// Returns the width / height of the QR code + padding in pixels fn draw_qr_code(gfx: &mut Bitmap3<'_>, qrcode_string_data: &str) -> i32 { const MAX_VERSION: qrcodegen_no_heap::Version = qrcodegen_no_heap::Version::new(6); - let buffer_len = MAX_VERSION.buffer_len(); - let (Ok(mut temp_buffer), Ok(mut out_buffer)) = ( - Vec::try_with_capacity_in(buffer_len, crate::ExternalAllocator), - Vec::try_with_capacity_in(buffer_len, crate::ExternalAllocator), - ) else { + let Ok(mut buffers) = QrCodeBuffers::new(MAX_VERSION) else { crate::println!("Failed to allocate memory to generate QR code"); return PADDING; }; - temp_buffer.resize(buffer_len, 0); - out_buffer.resize(buffer_len, 0); - - let qr_code = match qrcodegen_no_heap::QrCode::encode_text( - qrcode_string_data, - &mut temp_buffer, - &mut out_buffer, - qrcodegen_no_heap::QrCodeEcc::Medium, - qrcodegen_no_heap::Version::MIN, - MAX_VERSION, - None, - true, - ) { + let qr_code = buffers.generate_qr_code(qrcode_string_data); + + let qr_code = match qr_code { Ok(qr_code) => qr_code, Err(e) => { crate::println!("Error generating qr code: {e:?}"); @@ -116,3 +138,19 @@ fn draw_qr_code(gfx: &mut Bitmap3<'_>, qrcode_string_data: &str) -> i32 { qr_code.size() * 2 + PADDING * 2 } + +#[cfg(test)] +mod tests { + use super::QrCodeBuffers; + + const MAX_VERSION: qrcodegen_no_heap::Version = qrcodegen_no_heap::Version::new(6); + + #[test_case] + fn check_qr_code_generation(_: &mut crate::Gba) { + let mut buffers = + QrCodeBuffers::new(MAX_VERSION).expect("should be able to allocate buffers"); + buffers + .generate_qr_code("https://agbrs.dev/crash#09rESxF0r0Cz06hv1") + .expect("should be able to generate qr code"); + } +}