From 37fbef400945c1ac64a1e1d0e01dc139351aa323 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sat, 29 Apr 2023 16:14:29 +0900 Subject: [PATCH] Use no_std to reduce wasm output size --- src/helpers.rs | 4 ++++ src/lib.rs | 6 ++++++ src/wasm4.rs | 4 ++++ src/world.rs | 31 +++++++++++++++++++++++-------- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 28077f6..40328c2 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -7,3 +7,7 @@ pub fn fill(color: u8) { (&mut *crate::FRAMEBUFFER).fill(color | (color << 2) | (color << 4) | (color << 6)); } } + +pub fn round_f32_to_i32(f: f32) -> i32 { + (f + 0.5f32) as i32 +} diff --git a/src/lib.rs b/src/lib.rs index 6bf7098..19278b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![no_std] #[cfg(feature = "buddy-alloc")] mod alloc; mod wasm4; @@ -29,3 +30,8 @@ fn update() { WORLD.as_mut().unwrap().draw(); } } + +#[panic_handler] +fn panic(info: &core::panic::PanicInfo) -> ! { + loop {} +} diff --git a/src/wasm4.rs b/src/wasm4.rs index 78503c9..c80d497 100644 --- a/src/wasm4.rs +++ b/src/wasm4.rs @@ -139,6 +139,10 @@ pub fn text>(text: T, x: i32, y: i32) { let text_ref = text.as_ref(); unsafe { extern_text(text_ref.as_ptr(), text_ref.len(), x, y) } } +pub fn custom_text>(text: T, len: usize, x: i32, y: i32) { + let text_ref = text.as_ref(); + unsafe { extern_text(text_ref.as_ptr(), len, x, y) } +} extern "C" { #[link_name = "textUtf8"] fn extern_text(text: *const u8, length: usize, x: i32, y: i32); diff --git a/src/world.rs b/src/world.rs index 0de3a98..71db0d5 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,10 +1,11 @@ use crate::sprites::*; use tinyrand::{Rand, StdRand}; +use crate::helpers::round_f32_to_i32; const CAR_ANIM_FRAMES: u8 = 10; const MOVE_RATE: f32 = 1f32; -const MULTIPLIER_INC_RATE: f32 = 0.04f32; -const HOUSE_FRAMES: u32 = 180; +const MULTIPLIER_INC_RATE: f32 = 0.07f32; +const HOUSE_FRAMES: u32 = 150; const HOUSE_RANGE: f32 = 90f32; pub struct World { @@ -23,6 +24,7 @@ pub struct World { score: u64, rate_multiplier: f32, status_text: Option<(&'static str, u32)>, + str_buf: [u8; 16], } impl World { @@ -39,6 +41,7 @@ impl World { score: 0, rate_multiplier: 1f32, status_text: None, + str_buf: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], } } @@ -133,7 +136,7 @@ impl World { crate::rect(-5, 120, 170, 5); - let mut x = -5 + self.street_offset.round() as i32; + let mut x = -5 + round_f32_to_i32(self.street_offset); while x < 170 { crate::rect(x, 140, 30, 5); x += 45; @@ -147,8 +150,8 @@ impl World { if let Some((x, y)) = shrub { crate::blit( &PLANT, - x.round() as i32, - y.round() as i32, + round_f32_to_i32(*x), + round_f32_to_i32(*y), PLANT_WIDTH, PLANT_HEIGHT, PLANT_FLAGS, @@ -160,7 +163,7 @@ impl World { match state { 0 => crate::blit( &HOUSE1, - x.round() as i32, + round_f32_to_i32(x), 30 + HOUSE0_HEIGHT as i32 - HOUSE1_HEIGHT as i32, HOUSE1_WIDTH, HOUSE1_HEIGHT, @@ -168,7 +171,7 @@ impl World { ), 1 => crate::blit( &HOUSE0, - x.round() as i32, + round_f32_to_i32(x), 30, HOUSE0_WIDTH, HOUSE0_HEIGHT, @@ -210,6 +213,18 @@ impl World { if width == 0 { width = 1; } - crate::text(format!("{}", self.score), 160 - width * 7, 0); + temp = self.score; + if width < 15 { + for i in 0..width { + self.str_buf[width - 1 - i] = '0' as u8 + (temp % 10) as u8; + temp /= 10; + } + for i in width..16 { + self.str_buf[i] = 0; + } + crate::custom_text(self.str_buf, width, 160 - width as i32 * 8, 0); + } else { + crate::text("9999999999", 160 - 10 * 8, 0); + } } }