added wasm port
This commit is contained in:
20
rdraught-w4/.cargo/config.toml
Normal file
20
rdraught-w4/.cargo/config.toml
Normal file
@@ -0,0 +1,20 @@
|
||||
[build]
|
||||
target = "wasm32-unknown-unknown"
|
||||
|
||||
[target.wasm32-unknown-unknown]
|
||||
rustflags = [
|
||||
"-A", "dead_code",
|
||||
|
||||
# Import memory from WASM-4
|
||||
"-C", "link-arg=--import-memory",
|
||||
"-C", "link-arg=--initial-memory=65536",
|
||||
"-C", "link-arg=--max-memory=65536",
|
||||
# "-C", "link-args=--relocatable",
|
||||
|
||||
# Reserve 2044 bytes of stack space, offset from 6580.
|
||||
# Bump this value, 16-byte aligned, if the framebuffer gets corrupted.
|
||||
"-C", "link-arg=-zstack-size=14752",
|
||||
|
||||
# Not working? https://github.com/rust-lang/rust/issues/46645#issuecomment-423912553
|
||||
# "-C", "link-arg=--global-base=14752",
|
||||
]
|
@@ -13,7 +13,10 @@ wasm4.workspace = true
|
||||
wasm4-sys.workspace = true
|
||||
rdraught.workspace = true
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
default-target = "wasm32-unknown-unknown"
|
||||
targets = []
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[profile.release]
|
||||
opt-level = "z"
|
||||
lto = true
|
||||
|
||||
|
223
rdraught-w4/src/lib.rs
Normal file
223
rdraught-w4/src/lib.rs
Normal file
@@ -0,0 +1,223 @@
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use rdraught::{DraughtsBoard, DraughtsGame, Piece, Position};
|
||||
use wasm4::draw::{Color, DrawIndex, DrawIndices, Framebuffer};
|
||||
use wasm4::rt::{Resources, Runtime};
|
||||
// use wasm4 as w4;
|
||||
use core::arch::wasm32::unreachable;
|
||||
use wasm4::sys::DRAW_COLORS;
|
||||
use wasm4::trace;
|
||||
// mod wasm4;
|
||||
|
||||
// use wasm4::{DRAW_COLORS, oval, rect};
|
||||
|
||||
// mod palette;
|
||||
|
||||
// static mut GAME: DraughtsGame = DraughtsGame::default();
|
||||
struct Rdraught_W4 {
|
||||
framebuffer: Framebuffer,
|
||||
game: DraughtsGame,
|
||||
}
|
||||
|
||||
impl Runtime for Rdraught_W4 {
|
||||
fn start(rs: Resources) -> Self {
|
||||
rs.framebuffer.replace_palette([
|
||||
Color(0x00edeada),
|
||||
Color(0x00c74634),
|
||||
Color(0x005d8d60),
|
||||
Color(0x00100f24),
|
||||
]);
|
||||
Rdraught_W4 {
|
||||
framebuffer: rs.framebuffer,
|
||||
game: DraughtsGame::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self) {
|
||||
let indices = DrawIndices::from_array([
|
||||
DrawIndex::Third,
|
||||
DrawIndex::Fourth,
|
||||
DrawIndex::Fourth,
|
||||
DrawIndex::Transparent,
|
||||
]);
|
||||
unsafe {
|
||||
*DRAW_COLORS = indices.into_u16();
|
||||
}
|
||||
|
||||
self.framebuffer.rect([10, 10], [50, 50]);
|
||||
self.framebuffer.oval([50, 50], [10, 10]);
|
||||
let SQUARE_SIZE = 20u32;
|
||||
for i in 0..8i32 {
|
||||
for j in 0..8i32 {
|
||||
let indices = if (i + j) % 2 == 0 {
|
||||
DrawIndices::from_array([
|
||||
DrawIndex::Third,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Fourth,
|
||||
DrawIndex::Transparent,
|
||||
])
|
||||
} else {
|
||||
DrawIndices::from_array([
|
||||
DrawIndex::Fourth,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Fourth,
|
||||
DrawIndex::Transparent,
|
||||
])
|
||||
};
|
||||
unsafe {
|
||||
*DRAW_COLORS = indices.into_u16();
|
||||
}
|
||||
let tl = [i as i32 * SQUARE_SIZE as i32, j as i32 * SQUARE_SIZE as i32];
|
||||
self.framebuffer.rect(tl, [SQUARE_SIZE, SQUARE_SIZE]);
|
||||
|
||||
let score = self.game.score_for_player(rdraught::Player::Red);
|
||||
// let msg = format!("{}", score);
|
||||
// w4::trace(msg.as_str());
|
||||
|
||||
let piece = self.game.piece_at(Position::new(i as u8, j as u8).unwrap());
|
||||
match piece {
|
||||
Piece::SimpleWhitePawn => {
|
||||
let indices = DrawIndices::from_array([
|
||||
DrawIndex::First,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Transparent,
|
||||
]);
|
||||
unsafe {
|
||||
*DRAW_COLORS = indices.into_u16();
|
||||
}
|
||||
self.framebuffer.oval(tl, [SQUARE_SIZE, SQUARE_SIZE]);
|
||||
}
|
||||
Piece::SimpleRedPawn => {
|
||||
let indices = DrawIndices::from_array([
|
||||
DrawIndex::Second,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Transparent,
|
||||
]);
|
||||
unsafe {
|
||||
*DRAW_COLORS = indices.into_u16();
|
||||
}
|
||||
self.framebuffer.oval(tl, [SQUARE_SIZE, SQUARE_SIZE]);
|
||||
}
|
||||
Piece::CrownedRedPawn => {}
|
||||
Piece::CrownedWhitePawn => {}
|
||||
Piece::NoPiece => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wasm4::main! { Rdraught_W4 }
|
||||
|
||||
// use wasm4::*;
|
||||
// use wasm4_sys;
|
||||
|
||||
// #[unsafe(no_mangle)]
|
||||
// fn update() {
|
||||
// unsafe {
|
||||
// wasm4_sys::rect(10, 10, 32, 32);
|
||||
// }
|
||||
// }
|
||||
// #[unsafe(no_mangle)]
|
||||
// fn start() {
|
||||
// palette::change_palette(0usize);
|
||||
// palette::set_draw_color(2u16);
|
||||
// }
|
||||
|
||||
// #[unsafe(no_mangle)]
|
||||
// fn update() {
|
||||
// // if self.count % 60 == 0 {
|
||||
// // w4::trace("tick");
|
||||
// // self.count = 0;
|
||||
// // }
|
||||
// // self.count += 1;
|
||||
// // let indices = DrawIndices::from_array([
|
||||
// // DrawIndex::Third,
|
||||
// // DrawIndex::Fourth,
|
||||
// // DrawIndex::Fourth,
|
||||
// // DrawIndex::Transparent,
|
||||
// // ]);
|
||||
// // unsafe {
|
||||
// // *DRAW_COLORS = 0x;
|
||||
// // }
|
||||
// palette::set_draw_color([3, 4, 4, 0]);
|
||||
|
||||
// rect(10, 10, 50, 50);
|
||||
// oval(50, 50, 10, 10);
|
||||
// let SQUARE_SIZE = 20u32;
|
||||
// for i in 0..8i32 {
|
||||
// for j in 0..8i32 {
|
||||
// // let indices = if (i + j) % 2 == 0 {
|
||||
// // DrawIndices::from_array([
|
||||
// // DrawIndex::Third,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Fourth,
|
||||
// // DrawIndex::Transparent,
|
||||
// // ])
|
||||
// // } else {
|
||||
// // DrawIndices::from_array([
|
||||
// // DrawIndex::Fourth,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Fourth,
|
||||
// // DrawIndex::Transparent,
|
||||
// // ])
|
||||
// // };
|
||||
// // unsafe {
|
||||
// // *DRAW_COLORS = indices.into_u16();
|
||||
// // }
|
||||
// let tl = [i as i32 * SQUARE_SIZE as i32, j as i32 * SQUARE_SIZE as i32];
|
||||
// rect(tl.0, tl.1, SQUARE_SIZE, SQUARE_SIZE);
|
||||
|
||||
// // let score = self.game.score_for_player(rdraught::Player::Red);
|
||||
// // let msg = format!("{}", score);
|
||||
// // w4::trace(msg.as_str());
|
||||
// unsafe {
|
||||
// let piece = GAME.piece_at(Position::new(i as u8, j as u8).unwrap());
|
||||
// match piece {
|
||||
// Piece::SimpleWhitePawn => {
|
||||
// // let indices = DrawIndices::from_array([
|
||||
// // DrawIndex::First,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Transparent,
|
||||
// // ]);
|
||||
// // unsafe {
|
||||
// // *DRAW_COLORS = indices.into_u16();
|
||||
// // }
|
||||
// oval(tl, [SQUARE_SIZE, SQUARE_SIZE]);
|
||||
// }
|
||||
// Piece::SimpleRedPawn => {
|
||||
// // let indices = DrawIndices::from_array([
|
||||
// // DrawIndex::Second,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Transparent,
|
||||
// // ]);
|
||||
// // unsafe {
|
||||
// // *DRAW_COLORS = indices.into_u16();
|
||||
// // }
|
||||
// oval(tl, [SQUARE_SIZE, SQUARE_SIZE]);
|
||||
// }
|
||||
// Piece::CrownedRedPawn => {}
|
||||
// Piece::CrownedWhitePawn => {}
|
||||
// Piece::NoPiece => {}
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
#[panic_handler]
|
||||
fn panic_handler(_panic_info: &core::panic::PanicInfo<'_>) -> ! {
|
||||
trace("panic error");
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
if let Some(cause) = _panic_info.payload().downcast_ref::<&str>() {
|
||||
trace(cause);
|
||||
}
|
||||
|
||||
unreachable()
|
||||
}
|
@@ -1,45 +0,0 @@
|
||||
#![no_main]
|
||||
|
||||
use w4::draw::{Color, Framebuffer};
|
||||
use w4::rt::Resources;
|
||||
use wasm4 as w4;
|
||||
|
||||
struct Rdraught_wasm4 {
|
||||
framebuffer: Framebuffer,
|
||||
}
|
||||
|
||||
impl w4::rt::Runtime for Rdraught_wasm4 {
|
||||
fn start(rs: Resources) -> Self {
|
||||
rs.framebuffer.replace_palette([
|
||||
Color(0xff000000),
|
||||
Color(0x00ff0000),
|
||||
Color(0x0000ff00),
|
||||
Color(0xffff0000),
|
||||
]);
|
||||
Rdraught_wasm4 {
|
||||
framebuffer: rs.framebuffer,
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self) {
|
||||
// if self.count % 60 == 0 {
|
||||
// w4::trace("tick");
|
||||
// self.count = 0;
|
||||
// }
|
||||
// self.count += 1;
|
||||
self.framebuffer.rect([10, 10], [50, 50]);
|
||||
self.framebuffer.oval([50, 50], [10, 10]);
|
||||
}
|
||||
}
|
||||
|
||||
w4::main! { Rdraught_wasm4 }
|
||||
|
||||
// use wasm4::*;
|
||||
// use wasm4_sys;
|
||||
|
||||
// #[unsafe(no_mangle)]
|
||||
// fn update() {
|
||||
// unsafe {
|
||||
// wasm4_sys::rect(10, 10, 32, 32);
|
||||
// }
|
||||
// }
|
29
rdraught-w4/src/palette.rs
Normal file
29
rdraught-w4/src/palette.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use super::wasm4::DRAW_COLORS;
|
||||
use super::wasm4::PALETTE;
|
||||
|
||||
type Palette = [u32; 4];
|
||||
|
||||
pub const W4_DEFAULT: Palette = [0xe0f8cf, 0x86c06c, 0x306850, 0x071821];
|
||||
|
||||
pub const PONG_OG: Palette = [0x1b1b1b, 0xdcdcdc, 0x1b1b1b, 0xdcdcdc];
|
||||
|
||||
pub const ICE_CREAM_GB: Palette = [0xfff6d3, 0xf9a875, 0xeb6b6f, 0x7c3f58];
|
||||
|
||||
pub const HOLLOW: Palette = [0x0f0f1b, 0x565a75, 0xc5b7be, 0xf9fbf5];
|
||||
|
||||
const PALETTES: [Palette; 4] = [PONG_OG, ICE_CREAM_GB, HOLLOW, W4_DEFAULT];
|
||||
|
||||
#[inline]
|
||||
fn palette_by_idx<T: Into<usize>>(idx: T) -> Palette {
|
||||
PALETTES[idx.into() % PALETTES.len()]
|
||||
}
|
||||
|
||||
pub fn change_palette<T: Into<usize>>(idx: T) {
|
||||
unsafe {
|
||||
*PALETTE = palette_by_idx(idx);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_draw_color<T: Into<u16>>(idx: T) {
|
||||
unsafe { *DRAW_COLORS = idx.into() }
|
||||
}
|
196
rdraught-w4/src/wasm4.rs
Normal file
196
rdraught-w4/src/wasm4.rs
Normal file
@@ -0,0 +1,196 @@
|
||||
//
|
||||
// WASM-4: https://wasm4.org/docs
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Platform Constants │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
pub const SCREEN_SIZE: u32 = 160;
|
||||
pub const FONT_SIZE: u32 = 8;
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Memory Addresses │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
pub static mut PALETTE: *mut [u32; 4] = 0x04 as *mut [u32; 4];
|
||||
pub const DRAW_COLORS: *mut u16 = 0x14 as *mut u16;
|
||||
pub const GAMEPAD1: *const u8 = 0x16 as *const u8;
|
||||
pub const GAMEPAD2: *const u8 = 0x17 as *const u8;
|
||||
pub const GAMEPAD3: *const u8 = 0x18 as *const u8;
|
||||
pub const GAMEPAD4: *const u8 = 0x19 as *const u8;
|
||||
pub const MOUSE_X: *const i16 = 0x1a as *const i16;
|
||||
pub const MOUSE_Y: *const i16 = 0x1c as *const i16;
|
||||
pub const MOUSE_BUTTONS: *const u8 = 0x1e as *const u8;
|
||||
pub static mut FRAMEBUFFER: *mut [u8; 6400] = 0xa0 as *mut [u8; 6400];
|
||||
|
||||
pub const BUTTON_1: u8 = 1;
|
||||
pub const BUTTON_2: u8 = 2;
|
||||
pub const BUTTON_LEFT: u8 = 16;
|
||||
pub const BUTTON_RIGHT: u8 = 32;
|
||||
pub const BUTTON_UP: u8 = 64;
|
||||
pub const BUTTON_DOWN: u8 = 128;
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Drawing Functions │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
/// Copies pixels to the framebuffer.
|
||||
pub fn blit(sprite: &[u8], x: i32, y: i32, width: u32, height: u32, flags: u32) {
|
||||
unsafe { extern_blit(sprite.as_ptr(), x, y, width, height, flags) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "blit"]
|
||||
fn extern_blit(sprite: *const u8, x: i32, y: i32, width: u32, height: u32, flags: u32);
|
||||
}
|
||||
|
||||
/// Copies a subregion within a larger sprite atlas to the framebuffer.
|
||||
pub fn blit_sub(
|
||||
sprite: &[u8],
|
||||
x: i32,
|
||||
y: i32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
src_x: u32,
|
||||
src_y: u32,
|
||||
stride: u32,
|
||||
flags: u32,
|
||||
) {
|
||||
unsafe {
|
||||
extern_blit_sub(
|
||||
sprite.as_ptr(),
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
src_x,
|
||||
src_y,
|
||||
stride,
|
||||
flags,
|
||||
)
|
||||
}
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "blitSub"]
|
||||
fn extern_blit_sub(
|
||||
sprite: *const u8,
|
||||
x: i32,
|
||||
y: i32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
src_x: u32,
|
||||
src_y: u32,
|
||||
stride: u32,
|
||||
flags: u32,
|
||||
);
|
||||
}
|
||||
|
||||
pub const BLIT_2BPP: u32 = 1;
|
||||
pub const BLIT_1BPP: u32 = 0;
|
||||
pub const BLIT_FLIP_X: u32 = 2;
|
||||
pub const BLIT_FLIP_Y: u32 = 4;
|
||||
pub const BLIT_ROTATE: u32 = 8;
|
||||
|
||||
/// Draws a line between two points.
|
||||
pub fn line(x1: i32, y1: i32, x2: i32, y2: i32) {
|
||||
unsafe { extern_line(x1, y1, x2, y2) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "line"]
|
||||
fn extern_line(x1: i32, y1: i32, x2: i32, y2: i32);
|
||||
}
|
||||
|
||||
/// Draws an oval (or circle).
|
||||
pub fn oval(x: i32, y: i32, width: u32, height: u32) {
|
||||
unsafe { extern_oval(x, y, width, height) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "oval"]
|
||||
fn extern_oval(x: i32, y: i32, width: u32, height: u32);
|
||||
}
|
||||
|
||||
/// Draws a rectangle.
|
||||
pub fn rect(x: i32, y: i32, width: u32, height: u32) {
|
||||
unsafe { extern_rect(x, y, width, height) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "rect"]
|
||||
fn extern_rect(x: i32, y: i32, width: u32, height: u32);
|
||||
}
|
||||
|
||||
/// Draws text using the built-in system font.
|
||||
pub fn text(text: &str, x: i32, y: i32) {
|
||||
unsafe { extern_text(text.as_ptr(), text.len(), x, y) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "textUtf8"]
|
||||
fn extern_text(text: *const u8, length: usize, x: i32, y: i32);
|
||||
}
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Sound Functions │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
/// Plays a sound tone.
|
||||
pub fn tone(frequency: u32, duration: u32, volume: u32, flags: u32) {
|
||||
unsafe { extern_tone(frequency, duration, volume, flags) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "tone"]
|
||||
fn extern_tone(frequency: u32, duration: u32, volume: u32, flags: u32);
|
||||
}
|
||||
|
||||
pub const TONE_PULSE1: u32 = 0;
|
||||
pub const TONE_PULSE2: u32 = 1;
|
||||
pub const TONE_TRIANGLE: u32 = 2;
|
||||
pub const TONE_NOISE: u32 = 3;
|
||||
pub const TONE_MODE1: u32 = 0;
|
||||
pub const TONE_MODE2: u32 = 4;
|
||||
pub const TONE_MODE3: u32 = 8;
|
||||
pub const TONE_MODE4: u32 = 12;
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Storage Functions │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
extern "C" {
|
||||
/// Reads up to `size` bytes from persistent storage into the pointer `dest`.
|
||||
pub fn diskr(dest: *mut u8, size: u32) -> u32;
|
||||
|
||||
/// Writes up to `size` bytes from the pointer `src` into persistent storage.
|
||||
pub fn diskw(src: *const u8, size: u32) -> u32;
|
||||
}
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Other Functions │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
extern "C" {
|
||||
/// Copies `size` bytes from `srcPtr` into `destPtr`.
|
||||
#[link_name = "memcpy"]
|
||||
pub fn memcpy(dest: *mut u8, src: *const u8, size: usize) -> usize;
|
||||
|
||||
/// Fills memory at `destPtr` with `size` bytes of the fixed value `value`.
|
||||
#[link_name = "memset"]
|
||||
pub fn memset(dest: *mut u8, byte: u8, size: usize) -> usize;
|
||||
}
|
||||
|
||||
/// Prints a message to the debug console.
|
||||
pub fn trace(text: &str) {
|
||||
unsafe { extern_trace(text.as_ptr(), text.len()) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "traceUtf8"]
|
||||
fn extern_trace(trace: *const u8, length: usize);
|
||||
}
|
Reference in New Issue
Block a user