aduros / wasm4

Build retro games using WebAssembly for a fantasy console.
https://wasm4.org
ISC License
1.17k stars 172 forks source link

Rust cartridge size is too big #480

Open timleg002 opened 2 years ago

timleg002 commented 2 years ago

This code produces a cartridge that has a size of 78 kB (with the lazy static crate (as per the official tutorial) it has over 100 kB).

(The other files/modules are just some one-off functions, I also haven't included alloc.rs and wasm4.rs)

///game.rs
use crate::{wasm4::rect, colors::set_draw_color};

pub struct Game {
    snake: Snake,
    frame_count: u32
}

impl Game {
    pub fn new() -> Self {
        Self {
            snake: Snake {
                body: Vec::new(),
            direction: Point { x: 0, y: 0}
            },
            frame_count: 0
        }
    }

    pub fn update(&mut self) {
        self.frame_count += 1;

        self.snake.update();
        self.snake.draw();
    }
}

pub struct Snake {
    body: Vec<Point>,
    direction: Point 
}

impl Snake {
    pub fn update(&mut self) -> Option<Point> {
        self.body.insert(
            0,
            Point {
                x: (self.body[0].x + self.direction.x) % 20,
                y: (self.body[0].y + self.direction.y) % 20,
            },
        );

        if self.body[0].x < 0 {
            self.body[0].x = 19;
        }

        if self.body[0].y < 0 {
            self.body[0].y = 19;
        }

        self.body.pop()
    }

    pub fn draw(&self) {
        set_draw_color(0x4);

        for &Point { x, y } in self.body.iter() {
            rect(x * 8, y * 8, 8, 8);
        }

        set_draw_color(4);

        rect(self.body[0].x * 8, self.body[0].y * 8, 8, 8);
    }
}

#[derive(Clone, Copy)]
pub struct Point { x: i32, y: i32 }

/// lib.rs
#[cfg(feature = "buddy-alloc")]
mod alloc;
mod wasm4;
mod game;
mod colors;
mod drawing;
mod util;

use wasm4::*;

use crate::game::Game;

static mut GAME: Option<Game> = None;

const SMILEY: [u8; 8] = [
    0b11000011,
    0b10000001,
    0b00100100,
    0b00100100,
    0b00000000,
    0b00100100,
    0b10011001,
    0b11000011,
];

#[no_mangle]
fn start() {
    unsafe {
        GAME = Some(Game::new());

        *PALETTE = [
            0xfff6d3,
            0xf9a875,
            0xeb6b6f,
            0x7c3f58,
        ];
    }
}

#[no_mangle]
fn update() {
    unsafe { *DRAW_COLORS = 2 }
    text("Hello from Rust!", 10, 10);

    let gamepad = unsafe { *GAMEPAD1 };
    if gamepad & BUTTON_1 != 0 {
        //inv_color();
        //shift_color();
    }

    blit(&SMILEY, 76, 76, 8, 8, BLIT_1BPP);
    text("Press X to blink", 16, 90);

    unsafe {
        GAME.as_mut().unwrap().update();
    }
}

# Cargo.toml
cargo-features = ["strip"]

[package]
name = "cart"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib"]

[dependencies]
buddy-alloc = { version = "0.4.1", optional = true }

[profile.release]
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"

[features]
# use `--no-default-features` or comment out next line to disable allocator
default = ["buddy-alloc"]

(with the strip = true option it doesn't even want to start, but the size is reduced to 38 kB). Zig has a size of 4kB with the snake game. What is Rust doing? Thank you for your help!

DenialAdams commented 2 years ago

I think there is a lot of helpful information in this github issue:

https://github.com/aduros/wasm4/issues/238

and this PR:

https://github.com/christopher-kleine/wasm-4-tutorial-games/pull/17

I hope it helps you some!