]> git.seodisparate.com/gitweb - LD53/commitdiff
Use no_std to reduce wasm output size
authorStephen Seo <seo.disparate@gmail.com>
Sat, 29 Apr 2023 07:14:29 +0000 (16:14 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sat, 29 Apr 2023 07:14:29 +0000 (16:14 +0900)
src/helpers.rs
src/lib.rs
src/wasm4.rs
src/world.rs

index 28077f62bba6367b0fd9f1176c63f0919c56965f..40328c2fff52428498087f0133a3c53b02e2430a 100644 (file)
@@ -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
+}
index 6bf7098d46408a821169616778e91f201a37eb02..19278b2432c61d3c1822e8320c21df09db66de34 100644 (file)
@@ -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 {}
+}
index 78503c961663b38a331c2f4619a328386fc2e0ee..c80d497452ade165097db085ee819cca599855fc 100644 (file)
@@ -139,6 +139,10 @@ pub fn text<T: AsRef<[u8]>>(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<T: AsRef<[u8]>>(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);
index 0de3a984ca203e06066851861c79a04eac88878e..71db0d5e6cfa7a721165e968bd916f96ad294bc0 100644 (file)
@@ -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);
+        }
     }
 }