Use no_std to reduce wasm output size
This commit is contained in:
parent
b2cdf8f55d
commit
37fbef4009
4 changed files with 37 additions and 8 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
31
src/world.rs
31
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue