Compare commits

...

6 Commits

Author SHA1 Message Date
325ee25b2e added wasm port 2025-07-08 22:32:58 +08:00
fa675c4b7f refactor 2025-07-03 10:40:32 +08:00
e5182c26f3 fixed undo/redo bugs
renamed rdraught-ui to rdraught-gtk
2025-07-03 10:37:15 +08:00
dd0777ff9a added undo/redo functionality 2025-07-02 15:28:26 +08:00
60b65e3eec simplified memory layout 2025-07-02 09:10:52 +08:00
1348d8369f added undo/redo feature 2025-07-01 03:06:35 +08:00
47 changed files with 3073 additions and 753 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
/target
/rdraught-wasm/dist

View File

@@ -1,5 +1,13 @@
[workspace]
members = ["rdraught", "rdraught-cli", "rdraught-pi", "rdraught-ui", "rdraught-w4"]
members = [
"rdraught",
"rdraught-cli",
"rdraught-gtk",
"rdraught-w4",
"rdraught-wasm",
"rdraught-sdl",
"rdraught-ui-common",
]
resolver = "3"
[workspace.package]
@@ -12,17 +20,24 @@ edition = "2024"
rust-version = "1.87"
[workspace.dependencies]
rdraught = { path = "rdraught", version = "0.1.0" }
rdraught = { path = "rdraught", version = "0.1" }
rdraught-ui-common = { path = "rdraught-ui-common", version = "0.1" }
base64 = "0.22"
cairo-rs = "0.20"
cairo-sys-rs = "0.20"
librsvg = "2.60"
gtk4 = {version = "0.9", features = ["v4_10"] }
gdk4 = "0.9"
gio = "0.20.12"
glib = "0.20.12"
wasm4 = "0.2.0"
wasm4-sys = "0.1.3"
#[patch.crates-io]
#cairo-rs = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "cairo-rs", tag="0.20.12" }
#cairo-sys-rs = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "cairo-sys-rs", tag="0.20.12" }
gio = "0.20"
glib = "0.20"
wasm4 = "0.2"
wasm4-sys = "0.1"
strum = "0.27"
strum_macros = "0.27"
heapless = "0.8"
wasm-bindgen = "0.2"
web-sys = "0.3"
console_error_panic_hook = "0.1"
sdl3 = { version = "0.14.35" }
sdl3-sys = { version = "0.5" }
rmath = {version = "0.1.0", registry = "gitea"}

View File

@@ -1,5 +1,5 @@
[package]
name = "rdraught-ui"
name = "rdraught-gtk"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
@@ -11,12 +11,18 @@ version.workspace = true
[dependencies]
gtk4.workspace = true
gdk4.workspace = true
rdraught.workspace = true
rdraught = {workspace = true, features = ["std"]}
rdraught-ui-common = { workspace = true }
librsvg.workspace = true
cairo-rs.workspace = true
gio.workspace = true
glib.workspace = true
# [[bin]]
# name = "dialog_test"
# path = "src/greeting_dialog.rs"
[lib]
name = "rdraught_gtk"
crate-type = ["lib"]
bench = false
[[bin]]
name = "rdraught-gtk"
path = "src/main.rs"

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -0,0 +1,35 @@
use glib::ExitCode;
use rdraught::{DraughtsBoard, DraughtsGame, Move, Piece, Player, Position};
use rdraught_gtk::run;
use std::collections::HashMap;
fn main() -> ExitCode {
println!("move: {}", size_of::<Move>());
println!("game: {}", size_of::<DraughtsGame>());
// let boards = [[(Position::new(2, 4), Piece::CrownedRedPawn)]];
// for pieces in boards.into_iter() {
// let map = pieces.into_iter().collect::<HashMap<Position, Piece>>();
// let board = DraughtsBoard::new(|p| match map.get(&p) {
// None => Piece::NoPiece,
// Some(piece) => *piece,
// });
// println!("{:?}", board);
// for (pos, piece) in map.iter() {
// assert_eq!(*piece, board.get(*pos));
// }
// }
ExitCode::SUCCESS
// let mut pieces = HashMap::<Position, Piece>::new();
// pieces.insert(Position::new(2, 4), Piece::CrownedRedPawn);
// pieces.insert(Position::new(5, 5), Piece::CrownedWhitePawn);
// let game = DraughtsGame::new(
// |p| match pieces.get(&p) {
// None => Piece::NoPiece,
// Some(piece) => *piece,
// },
// Player::Red,
// );
// run(game)
}

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1,12 @@
use gtk4::{AlertDialog, Window, prelude::IsA};
use rdraught::Player;
pub(crate) async fn create_dialog<W: IsA<Window>>(window: W, winner: Player) {
let msg = match winner {
Player::Red => String::from("Red player wins"),
Player::White => String::from("White player wins"),
};
let info_dialog = AlertDialog::builder().modal(true).message(msg).build();
info_dialog.show(Some(&window));
}

View File

@@ -1,7 +1,7 @@
use gtk4::{Align, Application, Box, CheckButton, Label, Orientation, Window, prelude::*};
use crate::types::SharedMutable;
use rdraught::draughts::Player;
use rdraught::Player;
use rdraught_ui_common::SharedMutable;
pub(crate) fn create(application: &Application, current_player: SharedMutable<Player>) -> Window {
let label = Label::builder().label("Main player:").build();

5
rdraught-gtk/src/lib.rs Normal file
View File

@@ -0,0 +1,5 @@
mod final_dialog;
mod greeting_dialog;
mod rdraught_application;
pub use rdraught_application::run;

7
rdraught-gtk/src/main.rs Normal file
View File

@@ -0,0 +1,7 @@
use glib::ExitCode;
use rdraught::draughts::DraughtsGame;
fn main() -> ExitCode {
let game = DraughtsGame::default();
rdraught_gtk::run(game)
}

View File

@@ -1,22 +1,21 @@
use core::f64::consts::PI;
use gdk4::cairo::{Context as CairoContext, Matrix, Rectangle};
use gtk4::cairo::Error;
use glib::ExitCode;
use gtk4::glib::{MainContext, Propagation};
use gtk4::{self as gtk, gdk::ffi::GDK_BUTTON_PRIMARY};
use gtk4::{Application, DrawingArea, prelude::*};
use rdraught::draughts::{DraughtsBoard, DraughtsGame, Move, Piece, Player};
use rdraught::position::Position;
mod geo2d;
use core::f64::consts::PI;
use geo2d::Point;
use gtk4::{Application, Button, DrawingArea, HeaderBar, prelude::*};
use rdraught::{
DraughtsBoard, DraughtsGame, Move, Piece, Player, Position, RDraughtApplication,
RectangularBoard,
};
use rsvg::SvgHandle;
use std::thread;
const SQUARE_SIZE: f64 = 1.0;
mod final_dialog;
mod greeting_dialog;
mod types;
use types::{SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref};
use super::final_dialog;
use super::greeting_dialog;
use rdraught_ui_common::{
Point, SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref,
};
const CROWN_RED: &[u8] = include_bytes!("crown_red.svg");
const CROWN_WHITE: &[u8] = include_bytes!("crown_white.svg");
@@ -68,7 +67,7 @@ fn draw_piece(
piece: Piece,
crown_red: &SvgHandle,
crown_white: &SvgHandle,
) -> Result<(), Error> {
) -> Result<(), cairo::Error> {
if let Piece::NoPiece = piece {
Ok(())
} else {
@@ -170,7 +169,7 @@ fn draw_score_bar(
) {
fn modulate_score(relative_score: f64) -> f64 {
let x = relative_score;
f64::atan(8.0 * x - 4.0) / f64::atan(4.0) / 2.0 + 0.5
1.0 - (f64::atan(8.0 * x - 4.0) / f64::atan(4.0) / 2.0 + 0.5)
}
let score_bar = Rectangle::new(
board.tl().x() - board.width() / 10.0,
@@ -178,35 +177,64 @@ fn draw_score_bar(
board.width() / 16.0,
board.height(),
);
let num_rects = 40usize;
let spacing = board.height() / 200.0;
let rect_height = (board.height() - spacing * (num_rects - 1) as f64) / (num_rects as f64);
let score_percentage = modulate_score(draughts_game.relative_score(current_player) as f64);
let threshold = (score_percentage * num_rects as f64) as usize;
let tl = score_bar.tl();
cr.save().unwrap();
match current_player {
Player::White => cr.set_source_rgb(1.0, 0.0, 0.0),
Player::Red => cr.set_source_rgb(1.0, 1.0, 1.0),
cr.set_line_width(spacing / 4.0);
for i in 0..threshold {
match current_player {
Player::White => cr.set_source_rgb(1.0, 0.0, 0.0),
Player::Red => cr.set_source_rgb(1.0, 1.0, 1.0),
}
cr.rectangle(
tl.x(),
tl.y() + i as f64 * (rect_height + spacing),
score_bar.width(),
rect_height,
);
cr.fill().unwrap();
cr.set_source_rgb(0.0, 0.0, 0.0);
cr.rectangle(
tl.x(),
tl.y() + i as f64 * (rect_height + spacing),
score_bar.width(),
rect_height,
);
cr.stroke().unwrap();
}
cr.rectangle(
score_bar.tl().x(),
score_bar.tl().y(),
score_bar.width(),
score_bar.height() * (1.0 - score_percentage),
);
cr.fill().unwrap();
match current_player {
Player::White => cr.set_source_rgb(1.0, 1.0, 1.0),
Player::Red => cr.set_source_rgb(1.0, 0.0, 0.0),
for i in threshold..num_rects {
match current_player {
Player::White => cr.set_source_rgb(1.0, 1.0, 1.0),
Player::Red => cr.set_source_rgb(1.0, 0.0, 0.0),
}
cr.rectangle(
tl.x(),
tl.y() + i as f64 * (rect_height + spacing),
score_bar.width(),
rect_height,
);
cr.fill().unwrap();
cr.set_source_rgb(0.0, 0.0, 0.0);
cr.rectangle(
tl.x(),
tl.y() + i as f64 * (rect_height + spacing),
score_bar.width(),
rect_height,
);
cr.stroke().unwrap();
}
cr.rectangle(
tl.x(),
tl.y() + score_bar.height() * (1.0 - score_percentage),
score_bar.width(),
score_bar.height() * score_percentage,
);
cr.fill().unwrap();
cr.restore().unwrap();
}
fn create_game_window(application: &Application, current_player: Player) {
fn create_game_window(
rd: SharedMutableRef<RDraughtApplication>,
application: &Application,
current_player: Player,
) {
// Create a new window.
let window = gtk::ApplicationWindow::builder()
.application(application)
@@ -214,15 +242,64 @@ fn create_game_window(application: &Application, current_player: Player) {
.default_width(800)
.default_height(800)
.build();
let header_bar = HeaderBar::new();
window.set_titlebar(Some(&header_bar));
let undo_button = Button::new();
undo_button.set_icon_name("edit-undo-symbolic");
undo_button.set_sensitive(false);
let redo_button = Button::new();
redo_button.set_icon_name("edit-redo-symbolic");
redo_button.set_sensitive(false);
let selected_piece: SharedMutable<Option<Position>> = new_shared_mut(None);
let available_moves = new_shared_mut_ref(Vec::<Move>::new());
// Create a DrawingArea widget where we will draw the chessboard.
let drawing_area = DrawingArea::new();
// Add the drawing area to the window.
window.set_child(Some(&drawing_area));
let draughts_game = new_shared_mut_ref(DraughtsGame::default());
let selected_piece: SharedMutable<Option<Position>> = new_shared_mut(None);
let available_moves = new_shared_mut_ref(Vec::<Move>::new());
header_bar.pack_start(&undo_button);
{
let da = drawing_area.clone();
let rd = rd.clone();
let undo_btn_clone = undo_button.clone();
let redo_btn_clone = redo_button.clone();
let selected_piece = selected_piece.clone();
let available_moves = available_moves.clone();
undo_button.connect_clicked(move |_| {
let mut rd = rd.borrow_mut();
if rd.can_undo() {
rd.undo().ok();
redo_btn_clone.set_sensitive(rd.can_redo());
undo_btn_clone.set_sensitive(rd.can_undo());
selected_piece.set(None);
available_moves.borrow_mut().clear();
da.queue_draw();
}
});
}
header_bar.pack_start(&redo_button);
{
let da = drawing_area.clone();
let rd = rd.clone();
let undo_btn_clone = undo_button.clone();
let redo_btn_clone = redo_button.clone();
let selected_piece = selected_piece.clone();
let available_moves = available_moves.clone();
redo_button.connect_clicked(move |_| {
let mut rd = rd.borrow_mut();
if rd.can_redo() {
rd.redo().ok();
redo_btn_clone.set_sensitive(rd.can_redo());
undo_btn_clone.set_sensitive(rd.can_undo());
selected_piece.set(None);
available_moves.borrow_mut().clear();
da.queue_draw();
}
});
}
// Get the allocation information for the widget.
let board_width = SQUARE_SIZE * DraughtsBoard::rows() as f64;
let board_height = SQUARE_SIZE * DraughtsBoard::columns() as f64;
@@ -234,7 +311,6 @@ fn create_game_window(application: &Application, current_player: Player) {
let board_clone = board;
let crown_red_handle = {
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_RED));
rsvg::Loader::new()
.read_stream(
&stream,
@@ -258,7 +334,7 @@ fn create_game_window(application: &Application, current_player: Player) {
// Set the "draw" function of the drawing area. This callback is called
// whenever GTK needs to redraw this widget (for example, on first display or when resized).
{
let draughts_game = draughts_game.clone();
let rd = rd.clone();
let xform = xform.clone();
let selected_piece = selected_piece.clone();
let available_moves = available_moves.clone();
@@ -287,8 +363,8 @@ fn create_game_window(application: &Application, current_player: Player) {
for row in 0..DraughtsBoard::rows() {
for col in 0..DraughtsBoard::columns() {
let position = match current_player {
Player::White => Position::new((8 - row - 1) as u8, col as u8),
Player::Red => Position::new(row as u8, col as u8),
Player::Red => Position::new(row as u8, (8 - 1 - col) as u8),
Player::White => Position::new((8 - 1 - row) as u8, col as u8),
};
let square = Rectangle::new(
col as f64 * SQUARE_SIZE,
@@ -315,7 +391,7 @@ fn create_game_window(application: &Application, current_player: Player) {
draw_piece(
cr,
&square,
draughts_game.borrow().piece_at(position),
position.map_or(Piece::NoPiece, |p| rd.borrow().game().piece_at(p)),
&crown_red_handle,
&crown_white_handle,
)
@@ -325,14 +401,12 @@ fn create_game_window(application: &Application, current_player: Player) {
}
if let Some(selected_position) = selected_piece.get() {
let screen_position = match current_player {
Player::White => {
Position::new(8 - 1 - selected_position.row(), selected_position.col())
}
Player::Red => selected_position,
Player::White => (8 - 1 - selected_position.row(), selected_position.col()),
Player::Red => (selected_position.row(), 8 - 1 - selected_position.col()),
};
let square = Rectangle::new(
screen_position.col() as f64 * SQUARE_SIZE,
screen_position.row() as f64 * SQUARE_SIZE,
screen_position.1 as f64 * SQUARE_SIZE,
screen_position.0 as f64 * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
);
@@ -346,7 +420,7 @@ fn create_game_window(application: &Application, current_player: Player) {
cr.clip();
cr.new_path();
cr.set_source_rgb(0.0, 0.0, 1.0);
cr.set_line_width((square.width() + square.height()) * 0.05);
cr.set_line_width((square.width() + square.height()) * 0.035);
cr.move_to(square.tl().x(), square.tl().y());
cr.line_to(square.tl().x(), square.br().y());
cr.line_to(square.br().x(), square.br().y());
@@ -361,12 +435,12 @@ fn create_game_window(application: &Application, current_player: Player) {
for mv in am.iter() {
let end_pos = mv.get_end_position();
let screen_position = match current_player {
Player::White => Position::new(8 - 1 - end_pos.row(), end_pos.col()),
Player::Red => end_pos,
Player::White => (8 - 1 - end_pos.row(), end_pos.col()),
Player::Red => (end_pos.row(), 8 - 1 - end_pos.col()),
};
let square = Rectangle::new(
screen_position.col() as f64 * SQUARE_SIZE,
screen_position.row() as f64 * SQUARE_SIZE,
screen_position.1 as f64 * SQUARE_SIZE,
screen_position.0 as f64 * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
);
@@ -380,7 +454,7 @@ fn create_game_window(application: &Application, current_player: Player) {
cr.clip();
cr.new_path();
cr.set_source_rgb(0.0, 1.0, 0.0);
cr.set_line_width((square.width() + square.height()) * 0.05);
cr.set_line_width((square.width() + square.height()) * 0.035);
cr.move_to(square.tl().x(), square.tl().y());
cr.line_to(square.tl().x(), square.br().y());
cr.line_to(square.br().x(), square.br().y());
@@ -390,7 +464,7 @@ fn create_game_window(application: &Application, current_player: Player) {
cr.restore().unwrap();
}
}
draw_score_bar(cr, &board, &draughts_game.borrow(), current_player);
draw_score_bar(cr, &board, rd.borrow().game(), current_player);
});
}
let gesture = gtk::GestureClick::new();
@@ -403,9 +477,11 @@ fn create_game_window(application: &Application, current_player: Player) {
let drawing_area = drawing_area.clone();
let available_moves = available_moves.clone();
let window = window.clone();
let undo_btn_clone = undo_button.clone();
let redo_btn_clone = redo_button.clone();
gesture.connect_pressed(move |gesture, _, x, y| {
gesture.set_state(gtk::EventSequenceState::Claimed);
if let Some(winner) = draughts_game.borrow().winner() {
if let Some(winner) = rd.borrow().game().winner() {
MainContext::default()
.spawn_local(final_dialog::create_dialog(window.clone(), winner));
} else {
@@ -417,58 +493,75 @@ fn create_game_window(application: &Application, current_player: Player) {
};
let p = transform_point(&Point::new(x, y), &inverse);
if board_clone.contains(&p) {
let p = &p - &board_clone.tl();
let p = p - board_clone.tl();
// println!("Point: {:?}", p);
let position = match current_player {
Player::White => Position::new(
(8.0 - (p.y() / SQUARE_SIZE)) as u8,
(p.x() / SQUARE_SIZE) as u8,
),
Player::Red => {
Position::new((p.y() / SQUARE_SIZE) as u8, (p.x() / SQUARE_SIZE) as u8)
}
Player::Red => Position::new(
(p.y() / SQUARE_SIZE) as u8,
(8.0 - p.x() / SQUARE_SIZE) as u8,
),
};
// println!("Selected position: {:?}", position);
let mut draughts_game = draughts_game.borrow_mut();
let piece = draughts_game.piece_at(position);
// println!("Selected piece: {:?}", piece);
let mut am = available_moves.borrow_mut();
let piece = {
let rd = rd.borrow();
let draughts_game = rd.game();
position
.clone()
.map_or(Piece::NoPiece, |it| draughts_game.piece_at(it))
// println!("Selected piece: {:?}", piece);
};
let am = available_moves.replace(Vec::new());
let mut move_applied = false;
if !am.is_empty() {
for mv in am.iter() {
if mv.get_end_position() == position {
draughts_game.apply_move(mv).unwrap();
let game_copy = draughts_game.clone();
// thread::spawn(move || {
// if let (Some(mv), analyzed_moves) = game_copy.get_best_move(10) {
// println!(
// "Next best move: {:?}, analyzed moves: {}",
// mv, analyzed_moves
// );
// }
// });
move_applied = true;
break;
if let Ok(pos) = position {
for mv in am.into_iter() {
if mv.get_end_position() == pos {
let mut rd_app = rd.borrow_mut();
// println!("Applied move: {:?}", mv);
rd_app.apply_move(mv).unwrap();
// let game_copy = rd_app.game().clone();
// thread::spawn(move || {
// if let (Some(mv), analyzed_moves) =
// game_copy.get_best_move(10)
// {
// println!(
// "Next best move: {:?}, analyzed moves: {}",
// mv, analyzed_moves
// );
// }
// });
move_applied = true;
break;
}
}
}
if move_applied {
selected_piece.set(None);
am.clear();
let rd = rd.borrow();
undo_btn_clone.set_sensitive(rd.can_undo());
redo_btn_clone.set_sensitive(rd.can_redo());
}
}
if !move_applied {
let position = position.ok();
match piece.player() {
Some(Player::Red) => selected_piece.set(Some(position)),
Some(Player::White) => selected_piece.set(Some(position)),
Some(Player::Red) => selected_piece.set(position),
Some(Player::White) => selected_piece.set(position),
None => selected_piece.set(None),
}
am.clear();
if piece.player().is_none() {
selected_piece.set(None)
} else {
selected_piece.set(Some(position));
for mv in draughts_game.moves_for_piece(position) {
am.push(mv);
let mut am = available_moves.borrow_mut();
selected_piece.set(position);
if let Some(position) = position {
for mv in rd.borrow().game().moves_for_piece(position) {
am.push(mv);
}
}
}
}
@@ -484,7 +577,7 @@ fn create_game_window(application: &Application, current_player: Player) {
window.present();
}
fn on_activate(application: &Application) {
fn on_activate(application: &Application, game: DraughtsGame) {
// Initialize GTK before using any GTK functions.
if gtk::init().is_err() {
panic!("Failed to initialize GTK.");
@@ -495,17 +588,17 @@ fn on_activate(application: &Application) {
let application = application.clone();
dialog.connect_close_request(move |w| {
application.remove_window(w);
create_game_window(&application, current_player.get());
let rd = new_shared_mut_ref(RDraughtApplication::new(game.clone()));
create_game_window(rd.clone(), &application, current_player.get());
Propagation::Proceed
});
}
fn main() {
pub fn run(game: DraughtsGame) -> ExitCode {
// Create a new application with the builder pattern
let app = Application::builder()
.application_id("net.woggioni.rdraught")
.build();
app.connect_activate(on_activate);
// Run the application
app.run();
app.connect_activate(move |it| on_activate(it, game.clone()));
app.run()
}

View File

@@ -1,16 +0,0 @@
[package]
name = "rdraught-pi"
version = "0.1.0"
edition = "2024"
authors = ["Walter Oggioni <oggioni.walter@gmail.com>"]
license = "MIT"
rust-version = "1.87"
[[bin]]
name = "hello"
path = "src/hello.rs"
[dependencies]
panic-halt = "1.0"
libc = { version = "0.2", default-features = false }

View File

@@ -1,17 +0,0 @@
#![no_std]
#![no_main]
use libc::printf;
extern crate libc;
extern crate panic_halt;
#[link(name = "c", kind = "static")]
unsafe extern "C" {}
#[unsafe(no_mangle)]
pub extern "C" fn main() {
unsafe {
printf("Hello world!!\n".as_ptr() as *const i8);
}
}

18
rdraught-sdl/Cargo.toml Normal file
View File

@@ -0,0 +1,18 @@
[package]
name = "rdraught-sdl"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true
# [lib]
# crate-type = ["cdylib"]
[dependencies]
[dependencies.sdl3]
workspace = true
features = ["build-from-source-static"]

75
rdraught-sdl/src/main.rs Normal file
View File

@@ -0,0 +1,75 @@
use core::error::Error;
use sdl3::event::Event;
use sdl3::keyboard::Keycode;
use sdl3::pixels::Color;
use sdl3::rect::Rect;
use std::time::Duration;
const WINDOW_WIDTH: u32 = 640;
const WINDOW_HEIGHT: u32 = 640;
const BOARD_SIZE: usize = 8;
const SQUARE_SIZE: u32 = WINDOW_WIDTH / BOARD_SIZE as u32;
fn main() -> Result<(), Box<dyn Error>> {
// Initialize SDL
let sdl_context = sdl3::init()?;
let video_subsystem = sdl_context.video()?;
// Create window
let window = video_subsystem
.window("Chessboard", WINDOW_WIDTH, WINDOW_HEIGHT)
.position_centered()
.build()
.map_err(|e| e.to_string())?;
// Create canvas
let mut canvas = window.into_canvas();
// Set default color
canvas.set_draw_color(Color::RGB(255, 255, 255));
canvas.clear();
canvas.present();
// Main loop
let mut event_pump = sdl_context.event_pump()?;
'running: loop {
// Handle events
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => break 'running,
_ => {}
}
}
// Draw chessboard
canvas.set_draw_color(Color::RGB(255, 255, 255));
canvas.clear();
for row in 0..BOARD_SIZE {
for col in 0..BOARD_SIZE {
let color = if (row + col) % 2 == 0 {
Color::RGB(240, 217, 181) // Light squares
} else {
Color::RGB(181, 136, 99) // Dark squares
};
canvas.set_draw_color(color);
canvas.fill_rect(Rect::new(
(col as u32 * SQUARE_SIZE) as i32,
(row as u32 * SQUARE_SIZE) as i32,
SQUARE_SIZE,
SQUARE_SIZE,
))?;
}
}
canvas.present();
std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
}
Ok(())
}

View File

@@ -0,0 +1,16 @@
[package]
name = "rdraught-ui-common"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true
[lib]
crate-type = ["lib"]
[dependencies]
rdraught.workspace = true
rmath.workspace = true

View File

@@ -0,0 +1,261 @@
use rmath::NumericalMatrix;
use rmath::SMatrix;
use std::clone::Clone;
use std::cmp::Eq;
use std::cmp::PartialEq;
use std::fmt::Display;
use std::marker::Copy;
use std::ops::Add;
use std::ops::Div;
use std::ops::Mul;
use std::ops::Neg;
use std::ops::Sub;
#[derive(Debug)]
pub struct Point {
x: f64,
y: f64,
}
impl Point {
pub fn x(&self) -> f64 {
self.x
}
pub fn y(&self) -> f64 {
self.y
}
pub fn new(x: f64, y: f64) -> Point {
Point { x, y }
}
}
impl Add<Point> for Point {
fn add(self, rhs: Point) -> Self::Output {
Point {
x: self.x() + rhs.x(),
y: self.y() + rhs.y(),
}
}
type Output = Point;
}
impl Sub<Point> for Point {
fn sub(self, rhs: Point) -> Self::Output {
Point {
x: self.x() - rhs.x(),
y: self.y() - rhs.y(),
}
}
type Output = Point;
}
impl Mul<f64> for Point {
fn mul(self, rhs: f64) -> Self::Output {
Point::new(self.x * rhs, self.y * rhs)
}
type Output = Point;
}
impl Div<f64> for Point {
fn div(self, rhs: f64) -> Self::Output {
Point::new(self.x / rhs, self.y / rhs)
}
type Output = Point;
}
impl Neg for Point {
fn neg(self) -> Self::Output {
Point::new(-self.x(), -self.y())
}
type Output = Point;
}
impl PartialEq for Point {
fn eq(&self, other: &Self) -> bool {
self.x.eq(&other.x) && self.y.eq(&other.y)
}
}
impl Eq for Point {}
impl Clone for Point {
fn clone(&self) -> Self {
*self
}
}
impl Copy for Point {}
impl Display for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Point({}, {})", self.x, self.y)
}
}
impl Point {}
#[derive(Clone)]
pub struct Rect {
tl: Point,
br: Point,
}
impl Rect {
pub fn new(tl: Point, br: Point) -> Rect {
Rect { tl, br }
}
pub fn from_size(tl: Point, width: f64, height: f64) -> Rect {
Rect {
tl,
br: tl + Point::new(width, height),
}
}
pub fn width(&self) -> f64 {
self.br.x - self.tl.x
}
pub fn height(&self) -> f64 {
self.br.y - self.tl.y
}
pub fn tl(&self) -> Point {
self.tl
}
pub fn br(&self) -> Point {
self.br
}
pub fn center(&self) -> Point {
(self.tl + self.br) / 2.0
}
}
impl Display for Rect {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Rect({}, {})", self.tl, self.br)
}
}
#[derive(Clone)]
pub struct Xform(SMatrix<f64, 3, 3>);
impl Xform {
pub fn new(xx: f64, xy: f64, yx: f64, yy: f64, tx: f64, ty: f64) -> Xform {
Xform(SMatrix::<f64, 3, 3>::new(|pos| match pos {
(0, 0) => xx,
(0, 1) => yx,
(1, 0) => xy,
(1, 1) => yy,
(2, 0) => tx,
(2, 1) => ty,
(2, 2) => 1f64,
_ => 0f64,
}))
}
pub fn rot(alpha: f64) -> Xform {
let sa = alpha.sin();
let ca = alpha.cos();
Xform(SMatrix::<f64, 3, 3>::new(|position| match position {
(0, 0) => ca,
(1, 1) => ca,
(1, 0) => -sa,
(0, 1) => sa,
(2, 2) => 1f64,
_ => 0f64,
}))
}
pub fn scale(x: f64, y: f64) -> Xform {
Xform(SMatrix::<f64, 3, 3>::new(|position| match position {
(0, 0) => x,
(1, 1) => y,
(2, 2) => 1f64,
_ => 0f64,
}))
}
pub fn xlate(x: f64, y: f64) -> Xform {
Xform(SMatrix::<f64, 3, 3>::new(|position| match position {
(0, 0) => 1f64,
(1, 1) => 1f64,
(2, 2) => 1f64,
(2, 0) => x,
(2, 1) => y,
_ => 0f64,
}))
}
pub fn xx(&self) -> f64 {
self.0[(0, 0)]
}
pub fn xy(&self) -> f64 {
self.0[(1, 0)]
}
pub fn tx(&self) -> f64 {
self.0[(2, 0)]
}
pub fn yx(&self) -> f64 {
self.0[(1, 0)]
}
pub fn yy(&self) -> f64 {
self.0[(1, 1)]
}
pub fn ty(&self) -> f64 {
self.0[(2, 1)]
}
pub fn invert(&self) -> Xform {
Xform(self.0.clone().invert())
}
}
impl Default for Xform {
fn default() -> Self {
Xform(SMatrix::<f64, 3, 3>::identity(3))
}
}
impl Mul<&Xform> for Point {
fn mul(self, rhs: &Xform) -> Self::Output {
Point::new(
self.x * rhs.0[(0, 0)] + self.y * rhs.0[(1, 0)] + rhs.0[(2, 0)],
self.x * rhs.0[(0, 1)] + self.y * rhs.0[(1, 1)] + rhs.0[(2, 1)],
)
}
type Output = Point;
}
impl Mul<Xform> for Xform {
type Output = Xform;
fn mul(self, rhs: Xform) -> Self::Output {
Xform(self.0 * rhs.0)
}
}
impl Mul<&Xform> for &Xform {
type Output = Xform;
fn mul(self, rhs: &Xform) -> Self::Output {
Xform(&self.0 * &rhs.0)
}
}
impl Display for Xform {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

View File

@@ -0,0 +1,5 @@
mod geo2d;
mod types;
pub use geo2d::{Point, Rect, Xform};
pub use types::{SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref};

View File

@@ -0,0 +1,12 @@
use std::cell::{Cell, RefCell};
use std::rc::Rc;
pub type SharedMutable<T> = Rc<Cell<T>>;
pub type SharedMutableRef<T> = Rc<RefCell<T>>;
pub fn new_shared_mut_ref<T>(obj: T) -> SharedMutableRef<T> {
Rc::new(RefCell::new(obj))
}
pub fn new_shared_mut<T>(obj: T) -> SharedMutable<T> {
Rc::new(Cell::new(obj))
}

View File

@@ -1,96 +0,0 @@
use std::clone::Clone;
use std::cmp::Eq;
use std::cmp::PartialEq;
use std::fmt::Display;
use std::marker::Copy;
use std::ops::Add;
use std::ops::Div;
use std::ops::Mul;
use std::ops::Neg;
use std::ops::Sub;
#[derive(Debug)]
pub struct Point {
x: f64,
y: f64,
}
impl Point {
pub fn x(&self) -> f64 {
self.x
}
pub fn y(&self) -> f64 {
self.y
}
pub fn new(x: f64, y: f64) -> Point {
Point { x, y }
}
}
impl Add<&Point> for &Point {
fn add(self, rhs: &Point) -> Self::Output {
Point {
x: self.x() + rhs.x(),
y: self.y() + rhs.y(),
}
}
type Output = Point;
}
impl Sub<&Point> for &Point {
fn sub(self, rhs: &Point) -> Self::Output {
Point {
x: self.x() - rhs.x(),
y: self.y() - rhs.y(),
}
}
type Output = Point;
}
impl Mul<f64> for &Point {
fn mul(self, rhs: f64) -> Self::Output {
Point::new(self.x * rhs, self.y * rhs)
}
type Output = Point;
}
impl Div<f64> for &Point {
fn div(self, rhs: f64) -> Self::Output {
Point::new(self.x / rhs, self.y / rhs)
}
type Output = Point;
}
impl Neg for &Point {
fn neg(self) -> Self::Output {
Point::new(-self.x(), -self.y())
}
type Output = Point;
}
impl PartialEq for Point {
fn eq(&self, other: &Self) -> bool {
self.x.eq(&other.x) && self.y.eq(&other.y)
}
}
impl Eq for Point {}
impl Clone for Point {
fn clone(&self) -> Self {
*self
}
}
impl Copy for Point {}
impl Display for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Point({}, {})", self.x, self.y)
}
}
impl Point {}

View 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",
]

View File

@@ -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
View 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()
}

View File

@@ -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);
// }
// }

View 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
View 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);
}

View File

@@ -0,0 +1,3 @@
[build]
target = "wasm32-unknown-unknown"

39
rdraught-wasm/Cargo.toml Normal file
View File

@@ -0,0 +1,39 @@
[package]
name = "rdraught-wasm"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true
# [lib]
# crate-type = ["cdylib"]
[dependencies]
rdraught.workspace = true
rdraught-ui-common.workspace = true
wasm-bindgen.workspace = true
console_error_panic_hook.workspace = true
rmath.workspace = true
base64.workspace = true
[dependencies.web-sys]
workspace = true
features = [
'DomMatrix',
'CanvasRenderingContext2d',
'CssStyleDeclaration',
'Document',
'Element',
'EventTarget',
'HtmlCanvasElement',
'HtmlElement',
'MouseEvent',
'Node',
'HtmlImageElement',
'SvgImageElement',
'Window',
]

27
rdraught-wasm/dist/index.html vendored Normal file
View File

@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<style>
body {
margin: 0px;
}
</style>
<link rel="modulepreload" href="/rdraught-wasm-1d648b9090e33d1f.js" crossorigin="anonymous" integrity="sha384-T7IqpVlu6X9Lrf6TI9E2mHL6bPl2B4DHWKUGmbtsfGa1QSmnJBFB8IRe7clx0yWb"><link rel="preload" href="/rdraught-wasm-1d648b9090e33d1f_bg.wasm" crossorigin="anonymous" integrity="sha384-v+/7HPNa68RnAHRmXFFl7L7bUCWjUJnc8AGKHfn6l5NJiPANQ7yqdGRb+abf1wyh" as="fetch" type="application/wasm"></head>
<body>
<script type="module">
import init, * as bindings from '/rdraught-wasm-1d648b9090e33d1f.js';
const wasm = await init({ module_or_path: '/rdraught-wasm-1d648b9090e33d1f_bg.wasm' });
window.wasmBindings = bindings;
dispatchEvent(new CustomEvent("TrunkApplicationStarted", {detail: {wasm}}));
</script></body>
</html>

16
rdraught-wasm/index.html Normal file
View File

@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<style>
body {
margin: 0px;
}
</style>
</head>
<body>
</body>
</html>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="45" height="45">
<g style="fill:none; fill-opacity:0; fill-rule:evenodd; stroke:#ffc837; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(0,0)">
<path
d="M 22.5,11.63 L 22.5,6"
style="fill:none; stroke:#ffc837; stroke-linejoin:miter;" />
<path
d="M 20,8 L 25,8"
style="fill:none; stroke:#ffc837; stroke-linejoin:miter;" />
<path
d="M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25"
style="fill:#ffffff; stroke:#ffc837; stroke-linecap:butt; stroke-linejoin:miter;" />
<path
d="M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z "
style="fill:#ffffff; stroke:#ffc837;" />
<path
d="M 11.5,30 C 17,27 27,27 32.5,30"
style="fill:none; stroke:#ffc837;" />
<path
d="M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5"
style="fill:none; stroke:#ffc837;" />
<path
d="M 11.5,37 C 17,34 27,34 32.5,37"
style="fill:none; stroke:#ffc837;" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="45" height="45">
<g style="fill:none; fill-opacity:0; fill-rule:evenodd; stroke:#776b00; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(0,0)">
<path
d="M 22.5,11.63 L 22.5,6"
style="fill:none; stroke:#776b00; stroke-linejoin:miter;" />
<path
d="M 20,8 L 25,8"
style="fill:none; stroke:#776b00; stroke-linejoin:miter;" />
<path
d="M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25"
style="fill:#ffffff; stroke:#776b00; stroke-linecap:butt; stroke-linejoin:miter;" />
<path
d="M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z "
style="fill:#ffffff; stroke:#776b00;" />
<path
d="M 11.5,30 C 17,27 27,27 32.5,30"
style="fill:none; stroke:#776b00;" />
<path
d="M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5"
style="fill:none; stroke:#776b00;" />
<path
d="M 11.5,37 C 17,34 27,34 32.5,37"
style="fill:none; stroke:#776b00;" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

61
rdraught-wasm/src/lib.rs Normal file
View File

@@ -0,0 +1,61 @@
use std::cell::Cell;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
use web_sys::CanvasRenderingContext2d;
use web_sys::MouseEvent;
extern crate console_error_panic_hook;
// #[wasm_bindgen(start)]
fn main() -> Result<(), JsValue> {
let document = web_sys::window().unwrap().document().unwrap();
let canvas = document
.create_element("canvas")?
.dyn_into::<web_sys::HtmlCanvasElement>()?;
document.body().unwrap().append_child(&canvas)?;
canvas.set_width(640);
canvas.set_height(480);
canvas.style().set_property("border", "solid")?;
let context = canvas
.get_context("2d")?
.unwrap()
.dyn_into::<web_sys::CanvasRenderingContext2d>()?;
let context = Rc::new(context);
let pressed = Rc::new(Cell::new(false));
{
let context = context.clone();
let pressed = pressed.clone();
let closure = Closure::<dyn FnMut(_)>::new(move |event: web_sys::MouseEvent| {
context.begin_path();
context.move_to(event.offset_x() as f64, event.offset_y() as f64);
pressed.set(true);
});
canvas.add_event_listener_with_callback("mousedown", closure.as_ref().unchecked_ref())?;
closure.forget();
}
{
let context = context.clone();
let pressed = pressed.clone();
let closure = Closure::<dyn FnMut(_)>::new(move |event: web_sys::MouseEvent| {
if pressed.get() {
context.line_to(event.offset_x() as f64, event.offset_y() as f64);
context.stroke();
context.begin_path();
context.move_to(event.offset_x() as f64, event.offset_y() as f64);
}
});
canvas.add_event_listener_with_callback("mousemove", closure.as_ref().unchecked_ref())?;
closure.forget();
}
{
let closure = Closure::<dyn FnMut(_)>::new(move |event: web_sys::MouseEvent| {
pressed.set(false);
context.line_to(event.offset_x() as f64, event.offset_y() as f64);
context.stroke();
});
canvas.add_event_listener_with_callback("mouseup", closure.as_ref().unchecked_ref())?;
closure.forget();
}
Ok(())
}

446
rdraught-wasm/src/main.rs Normal file
View File

@@ -0,0 +1,446 @@
use base64::Engine;
use base64::engine::general_purpose::STANDARD as b64;
use core::f64::consts::PI;
use rdraught::{DraughtsBoard, DraughtsGame, Move, Piece, Player, Position, RectangularBoard};
use rdraught_ui_common::{
Point, Rect, SharedMutable, SharedMutableRef, Xform, new_shared_mut, new_shared_mut_ref,
};
use wasm_bindgen::prelude::*;
use web_sys::{CanvasRenderingContext2d, Document, HtmlImageElement, MouseEvent};
extern crate console_error_panic_hook;
#[wasm_bindgen]
extern "C" {
// Use `js_namespace` here to bind `console.log(..)` instead of just
// `log(..)`
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
// The `console.log` is quite polymorphic, so we can bind it with multiple
// signatures. Note that we need to use `js_name` to ensure we always call
// `log` in JS.
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_u32(a: u32);
// Multiple arguments too!
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_many(a: &str, b: &str);
}
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}
struct PieceDrawer {
red_crown: HtmlImageElement,
white_crown: HtmlImageElement,
}
impl PieceDrawer {
fn new(document: &Document) -> Result<PieceDrawer, JsValue> {
const CROWN_RED: &[u8] = include_bytes!("crown_red.svg");
const CROWN_WHITE: &[u8] = include_bytes!("crown_white.svg");
let red_crown = document
.create_element("img")?
.dyn_into::<HtmlImageElement>()?;
let src = b64.encode(CROWN_RED);
let src = format!("data:image/svg+xml;base64,{}", src);
red_crown.set_src(&src);
let white_crown = document
.create_element("img")?
.dyn_into::<HtmlImageElement>()?;
let src = b64.encode(CROWN_WHITE);
let src = format!("data:image/svg+xml;base64,{}", src);
white_crown.set_src(&src);
Ok(PieceDrawer {
red_crown,
white_crown,
})
}
fn draw_piece(
&self,
ctx: &CanvasRenderingContext2d,
xform: &Xform,
cell: Rect,
piece: Piece,
) -> Result<(), JsValue> {
ctx.save();
let center = cell.center();
let outer_radius = cell.width() * 0.3;
let thickness = outer_radius * 0.5;
let vertical_scale_factor = 0.7;
{
let p = Point::new(0.0, cell.center().y()) * xform;
let xform = xform.clone()
* Xform::xlate(-p.x(), -p.y())
* Xform::scale(1.0, vertical_scale_factor)
* Xform::xlate(p.x(), p.y());
apply_xform(ctx, &xform)?;
}
ctx.set_fill_style_str("#000");
ctx.begin_path();
ctx.arc(
center.x(),
center.y() + thickness / 2.0,
outer_radius,
0.0,
PI,
)?;
ctx.line_to(center.x() - outer_radius, center.y() - thickness / 2.0);
ctx.arc(
center.x(),
center.y() - thickness / 2.0,
outer_radius,
PI,
2.0 * PI,
)?;
ctx.line_to(center.x() + outer_radius, center.y() + thickness / 2.0);
ctx.fill();
match piece.player() {
Some(Player::Red) => {
ctx.set_fill_style_str("#ff0000");
}
Some(Player::White) => {
ctx.set_fill_style_str("#ffffff");
}
None => {}
}
let radius = cell.width() * 0.275;
ctx.begin_path();
ctx.arc(
center.x(),
center.y() - thickness / 2.0,
radius,
0.0,
2.0 * PI,
)?;
ctx.fill();
ctx.restore();
ctx.save();
if let Some(player) = piece.player() {
if piece.is_crowned() {
{
let p = Point::new(cell.center().x(), cell.center().y()) * xform;
let f = 0.55;
let xform = xform.clone()
* Xform::xlate(-p.x(), -p.y())
* Xform::scale(1.0, vertical_scale_factor)
* Xform::scale(f, f)
* Xform::xlate(p.x(), p.y());
apply_xform(ctx, &xform)?;
}
let image = match player {
Player::White => self.white_crown.clone(),
Player::Red => self.red_crown.clone(),
};
ctx.draw_image_with_html_image_element_and_dw_and_dh(
&image,
cell.tl().x(),
cell.tl().y() - thickness / 1.0,
cell.width(),
cell.height(),
)?;
}
}
ctx.restore();
Ok(())
}
}
fn apply_xform(ctx: &CanvasRenderingContext2d, xform: &Xform) -> Result<(), JsValue> {
ctx.set_transform(
xform.xx(),
xform.xy(),
xform.yx(),
xform.yy(),
xform.tx(),
xform.ty(),
)
}
fn from_ctx(ctx: &CanvasRenderingContext2d) -> Result<Xform, JsValue> {
let m = ctx.get_transform()?;
Ok(Xform::new(m.a(), m.c(), m.b(), m.d(), m.e(), m.f()))
}
fn map_row(current_player: Player, row: usize) -> usize {
match current_player {
Player::White => DraughtsBoard::rows() - 1 - row,
Player::Red => row,
}
}
trait Agent {}
struct App {
game: DraughtsGame,
board: Rect,
screen: Rect,
xform: Xform,
selected_piece: Option<Position>,
available_moves: Vec<Move>,
main_player: Player,
red_agent: Box<dyn Agent>,
white_agent: Box<dyn Agent>,
}
struct Human {}
impl Human {
fn new() -> Human {
Human {}
}
}
impl Agent for Human {}
impl Default for App {
fn default() -> Self {
let board = Rect::from_size(
Point::new(100.0, 400.0),
App::SQUARE_SIZE * DraughtsBoard::columns() as f64,
App::SQUARE_SIZE * DraughtsBoard::rows() as f64,
);
App {
game: DraughtsGame::default(),
board,
screen: Rect::new(Point::new(0.0, 0.0), Point::new(0.0, 0.0)),
xform: Xform::default(),
selected_piece: None,
available_moves: Vec::new(),
main_player: Player::White,
red_agent: Box::new(Human::new()),
white_agent: Box::new(Human::new()),
}
}
}
impl App {
const SQUARE_SIZE: f64 = 100.0;
fn draw(
&mut self,
ctx: &CanvasRenderingContext2d,
piece_drawer: &PieceDrawer,
) -> Result<(), JsValue> {
ctx.clear_rect(0.0, 0.0, self.screen.width(), self.screen.height());
let board = &self.board;
let f = f64::min(
self.screen.width() / board.width(),
self.screen.height() / board.height(),
);
ctx.save();
self.xform = Xform::default()
* Xform::xlate(-board.center().x(), -board.center().y())
* Xform::scale(f, f)
* Xform::xlate(self.screen.center().x(), self.screen.center().y());
apply_xform(ctx, &self.xform)?;
// let board_center = board.center();
// let screen_center = screen.center();
// let xlate = -board_center;
// ctx.restore();
// ctx.translate(-xlate.x(), -xlate.y())?;
// ctx.scale(f, f)?;
// ctx.translate(screen_center.x(), screen_center.y())?;
// console_log!("xform2: {}", from_ctx(ctx)?);
let available_moves: Vec<Position> = self
.available_moves
.iter()
.map(Move::get_end_position)
.collect();
for i in 0..DraughtsBoard::rows() {
for j in 0..DraughtsBoard::columns() {
let cell = Rect::new(
Point::new(
board.tl().x() + i as f64 * App::SQUARE_SIZE,
board.tl().y() + j as f64 * App::SQUARE_SIZE,
),
Point::new(
board.tl().x() + (i + 1) as f64 * App::SQUARE_SIZE,
board.tl().y() + (j + 1) as f64 * App::SQUARE_SIZE,
),
);
if (i + j) % 2 == 0 {
ctx.set_fill_style_str("#cccc99");
} else {
ctx.set_fill_style_str("#666633");
}
ctx.fill_rect(cell.tl().x(), cell.tl().y(), cell.width(), cell.height());
if (i + j) % 2 != 0 {
let current_player = Player::White;
let position = Position::from_index((map_row(current_player, j), i));
let piece = self.game.piece_at(position);
if piece.is_present() {
piece_drawer.draw_piece(ctx, &self.xform, cell.clone(), piece)?;
}
if let Some(selected_piece) = self.selected_piece {
if selected_piece == position {
// console_log!("Selected: {}", selected_piece);
ctx.save();
ctx.set_shadow_blur(300.0);
ctx.set_shadow_color("#ffffff");
ctx.begin_path();
ctx.move_to(cell.tl().x(), cell.tl().y());
ctx.line_to(cell.br().x(), cell.tl().y());
ctx.line_to(cell.br().x(), cell.br().y());
ctx.line_to(cell.tl().x(), cell.br().y());
ctx.line_to(cell.tl().x(), cell.tl().y());
ctx.clip();
ctx.set_stroke_style_str("#fff");
ctx.set_line_width(4.0);
ctx.begin_path();
ctx.move_to(board.tl().x(), board.tl().y());
ctx.line_to(board.br().x(), board.tl().y());
ctx.line_to(board.br().x(), board.br().y());
ctx.line_to(board.tl().x(), board.br().y());
ctx.line_to(board.tl().x(), board.tl().y());
ctx.move_to(cell.tl().x(), cell.tl().y());
ctx.line_to(cell.br().x(), cell.tl().y());
ctx.line_to(cell.br().x(), cell.br().y());
ctx.line_to(cell.tl().x(), cell.br().y());
ctx.line_to(cell.tl().x(), cell.tl().y());
ctx.stroke();
ctx.restore();
}
}
if available_moves.contains(&position) {
ctx.save();
ctx.set_shadow_blur(300.0);
ctx.set_shadow_color("#0000ff");
ctx.begin_path();
ctx.move_to(cell.tl().x(), cell.tl().y());
ctx.line_to(cell.br().x(), cell.tl().y());
ctx.line_to(cell.br().x(), cell.br().y());
ctx.line_to(cell.tl().x(), cell.br().y());
ctx.line_to(cell.tl().x(), cell.tl().y());
ctx.clip();
ctx.set_stroke_style_str("#0f0");
ctx.set_line_width(4.0);
ctx.begin_path();
ctx.move_to(board.tl().x(), board.tl().y());
ctx.line_to(board.br().x(), board.tl().y());
ctx.line_to(board.br().x(), board.br().y());
ctx.line_to(board.tl().x(), board.br().y());
ctx.line_to(board.tl().x(), board.tl().y());
ctx.move_to(cell.tl().x(), cell.tl().y());
ctx.line_to(cell.br().x(), cell.tl().y());
ctx.line_to(cell.br().x(), cell.br().y());
ctx.line_to(cell.tl().x(), cell.br().y());
ctx.line_to(cell.tl().x(), cell.tl().y());
ctx.stroke();
ctx.restore();
}
}
}
}
ctx.restore();
Ok(())
}
}
fn main() -> Result<(), JsValue> {
let document = web_sys::window().unwrap().document().unwrap();
let canvas = document
.create_element("canvas")?
.dyn_into::<web_sys::HtmlCanvasElement>()?;
canvas.style().set_property("background-color", "black")?;
document.body().unwrap().append_child(&canvas)?;
// canvas.style().set_property("border", "solid")?;
let window = web_sys::window().expect("should have a window in this context");
let width = window.inner_width().unwrap().as_f64().unwrap();
let height = window.inner_height().unwrap().as_f64().unwrap();
canvas.set_width(width as u32);
canvas.set_height(height as u32);
let app = new_shared_mut_ref(App::default());
let ctx = canvas
.get_context("2d")?
.unwrap()
.dyn_into::<web_sys::CanvasRenderingContext2d>()?;
let piece_drawer = new_shared_mut_ref(PieceDrawer::new(&document)?);
{
let piece_drawer = piece_drawer.clone();
let screen = Rect::new(Point::new(0.0, 0.0), Point::new(width, height));
app.borrow_mut().screen = screen;
app.borrow_mut().draw(&ctx, &piece_drawer.borrow())?;
}
{
let app = app.clone();
let canvas = canvas.clone();
let ctx = ctx.clone();
let piece_drawer = piece_drawer.clone();
let closure = Closure::<dyn FnMut()>::new(move || {
let window = web_sys::window().expect("should have a window in this context");
let width = window.inner_width().unwrap().as_f64().unwrap() as u32;
let height = window.inner_height().unwrap().as_f64().unwrap() as u32;
canvas.set_width(width);
canvas.set_height(height);
app.borrow_mut().screen = Rect::new(
Point::new(0.0, 0.0),
Point::new(canvas.width() as f64, canvas.height() as f64),
);
app.borrow_mut().draw(&ctx, &piece_drawer.borrow()).unwrap();
});
let window = web_sys::window().expect("should have a window in this context");
window.set_onresize(Some(closure.as_ref().unchecked_ref()));
closure.forget();
}
{
let app = app.clone();
let piece_drawer = piece_drawer.clone();
let mouse_click_handler = Closure::wrap(Box::new(move |event: MouseEvent| {
let mut app = app.borrow_mut();
let xform = app.xform.invert();
let board = &app.board;
let position = Point::new(event.x() as f64, event.y() as f64) * &xform - board.tl();
if position.x() > 0.0
&& position.x() < board.width()
&& position.y() > 0.0
&& position.y() < board.height()
{
let col = f64::floor(position.x() / App::SQUARE_SIZE) as usize;
let row = f64::floor(position.y() / App::SQUARE_SIZE) as usize;
let row = map_row(app.main_player, row);
if (row + col) % 2 == 0 {
let position = Position::new(row as u8, col as u8).unwrap();
let selected_move = app
.available_moves
.iter()
.find(|mv| mv.get_end_position() == position)
.map(Move::clone);
if app.selected_piece.is_some() && selected_move.is_some() {
let mv = selected_move.unwrap();
app.game.check_and_apply_move(&mv).unwrap();
app.selected_piece = None;
app.available_moves.clear();
} else {
app.selected_piece = Some(position);
let mut available_moves = Vec::<Move>::new();
for mv in app.game.moves_for_piece(position) {
available_moves.push(mv);
}
app.available_moves = available_moves;
// console_log!("Clicked at: ({}, {})", col, row);
}
} else {
app.selected_piece = None;
app.available_moves.clear();
}
} else {
app.selected_piece = None;
app.available_moves.clear();
}
app.draw(&ctx, &piece_drawer.borrow()).unwrap();
}) as Box<dyn FnMut(MouseEvent)>);
canvas.set_onclick(Some(mouse_click_handler.as_ref().unchecked_ref()));
mouse_click_handler.forget();
}
Ok(())
}

View File

@@ -13,7 +13,9 @@ name = "rdraught"
crate-type = ["lib"]
[dependencies]
heapless = "0.8"
heapless.workspace = true
strum.workspace = true
strum_macros.workspace = true
[dev-dependencies]
rand = "0.9"
[features]
std = []

View File

@@ -1,6 +1,5 @@
use heapless::Vec;
use rdraught::draughts::{DraughtsBoard, Piece};
use rdraught::position::Position;
use rdraught::{DraughtsBoard, Piece, Position};
fn main() {
let board = DraughtsBoard::default();

View File

@@ -1,131 +0,0 @@
use core::iter::Enumerate;
use core::iter::Flatten;
use core::ops::Index;
use core::ops::IndexMut;
use super::position::Position;
pub trait RectangularBoard {
fn rows() -> usize;
fn columns() -> usize;
}
#[derive(Debug, Clone)]
pub(crate) struct Board<TYPE, const ROWS: usize, const COLUMNS: usize> {
data: [[TYPE; COLUMNS]; ROWS],
}
impl<TYPE, const ROWS: usize, const COLUMNS: usize> RectangularBoard
for Board<TYPE, ROWS, COLUMNS>
{
fn rows() -> usize {
ROWS
}
fn columns() -> usize {
COLUMNS
}
}
impl<TYPE, const ROWS: usize, const COLUMNS: usize> Default for Board<TYPE, ROWS, COLUMNS>
where
TYPE: Default,
{
fn default() -> Self {
Self::new(|_| TYPE::default())
}
}
impl<TYPE, const ROWS: usize, const COLUMNS: usize> IntoIterator for Board<TYPE, ROWS, COLUMNS> {
type Item = (usize, usize, TYPE);
type IntoIter = BoardIterator<TYPE, ROWS, COLUMNS>;
fn into_iter(self) -> Self::IntoIter {
BoardIterator {
it: self.data.into_iter().flatten().enumerate(),
}
}
}
pub struct BoardIteratorRef<'a, TYPE, const ROWS: usize, const COLUMNS: usize> {
it: Enumerate<Flatten<core::slice::Iter<'a, [TYPE; COLUMNS]>>>,
}
impl<'a, TYPE, const ROWS: usize, const COLUMNS: usize> Iterator
for BoardIteratorRef<'a, TYPE, ROWS, COLUMNS>
where
TYPE: Clone + Copy,
{
fn next(&mut self) -> Option<Self::Item> {
self.it
.next()
.map(|(index, value)| (index / COLUMNS, index % COLUMNS, *value))
}
type Item = (usize, usize, TYPE);
}
impl<'a, TYPE, const ROWS: usize, const COLUMNS: usize> IntoIterator
for &'a Board<TYPE, ROWS, COLUMNS>
where
TYPE: Clone + Copy,
{
fn into_iter(self) -> BoardIteratorRef<'a, TYPE, ROWS, COLUMNS> {
BoardIteratorRef {
it: (self.data).iter().flatten().enumerate(),
}
}
type IntoIter = BoardIteratorRef<'a, TYPE, ROWS, COLUMNS>;
type Item = (usize, usize, TYPE);
}
impl<TYPE, const ROWS: usize, const COLUMNS: usize> Board<TYPE, ROWS, COLUMNS> {
pub(crate) fn new<INITIALIZER>(mut cb: INITIALIZER) -> Self
where
INITIALIZER: FnMut((usize, usize)) -> TYPE,
{
let values: [[TYPE; COLUMNS]; ROWS] =
core::array::from_fn(|i| core::array::from_fn(|j| cb((i, j))));
Board { data: values }
}
pub(crate) fn iter(&self) -> BoardIteratorRef<'_, TYPE, ROWS, COLUMNS> {
BoardIteratorRef {
it: (self.data).iter().flatten().enumerate(),
}
}
}
pub struct BoardIterator<TYPE, const ROWS: usize, const COLUMNS: usize> {
it: Enumerate<Flatten<core::array::IntoIter<[TYPE; COLUMNS], ROWS>>>,
}
impl<TYPE, const ROWS: usize, const COLUMNS: usize> Iterator
for BoardIterator<TYPE, ROWS, COLUMNS>
{
fn next(&mut self) -> Option<Self::Item> {
self.it
.next()
.map(|(index, value)| (index / COLUMNS, index % COLUMNS, value))
}
type Item = (usize, usize, TYPE);
}
impl<TYPE, const ROWS: usize, const COLUMNS: usize> Index<Position> for Board<TYPE, ROWS, COLUMNS> {
type Output = TYPE;
fn index(&self, position: Position) -> &Self::Output {
let index = position.to_index();
&self.data[index.1][index.0]
}
}
impl<TYPE, const ROWS: usize, const COLUMNS: usize> IndexMut<Position>
for Board<TYPE, ROWS, COLUMNS>
{
fn index_mut(&mut self, position: Position) -> &mut Self::Output {
let index = position.to_index();
&mut self.data[index.1][index.0]
}
}

View File

@@ -0,0 +1,297 @@
use core::mem::MaybeUninit;
use core::result::Result;
#[derive(Debug, PartialEq)]
pub enum Error {
BufferIsFull,
BufferIsEmpty,
BufferHasZeroSize,
}
pub struct CircularBuffer<T: Sized, const SIZE: usize> {
buffer: [MaybeUninit<T>; SIZE],
front: usize,
rear: usize,
fill: usize,
}
impl<T: Sized, const SIZE: usize> CircularBuffer<T, SIZE> {
pub fn new() -> CircularBuffer<T, SIZE> {
CircularBuffer {
buffer: [const { MaybeUninit::uninit() }; SIZE],
front: 0,
rear: 0,
fill: 0,
}
}
pub fn push(&mut self, element: T) -> Result<(), Error> {
if self.len() == SIZE {
Err(Error::BufferIsFull)
} else {
self.buffer[self.rear].write(element);
self.rear = (self.rear + 1) % SIZE;
self.fill += 1;
Ok(())
}
}
pub fn push_evict(&mut self, element: T) -> Option<T> {
let result = if self.len() == SIZE {
self.pop_front()
} else {
None
};
self.buffer[self.rear].write(element);
self.rear = (self.rear + 1) % SIZE;
self.fill += 1;
result
}
pub fn pop_front(&mut self) -> Option<T> {
if self.len() == 0 {
None
} else {
let mut result = MaybeUninit::uninit();
core::mem::swap(&mut result, &mut self.buffer[self.front]);
self.front = (self.front + 1) % SIZE;
self.fill -= 1;
Some(unsafe { result.assume_init() })
}
}
pub fn pop_back(&mut self) -> Option<T> {
if self.len() == 0 {
None
} else {
let mut result = MaybeUninit::uninit();
core::mem::swap(&mut result, &mut self.buffer[self.rear - 1]);
self.rear = (self.rear - 1) % SIZE;
self.fill -= 1;
Some(unsafe { result.assume_init() })
}
}
pub fn len(&self) -> usize {
self.fill
}
pub fn capacity(&self) -> usize {
SIZE
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T> {
CircularBufferIteratorRef::<T, SIZE> {
cb: self,
count: 0,
count_back: 0,
}
}
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> {
CircularBufferIteratorMutRef::<T, SIZE> {
cb: self,
count: 0,
count_back: 0,
}
}
pub fn first(&self) -> Option<&T> {
if self.len() > 0 {
unsafe { Some(self.buffer[self.front].assume_init_ref()) }
} else {
None
}
}
pub fn last(&self) -> Option<&T> {
if self.len() > 0 {
unsafe { Some(self.buffer[self.rear - 1].assume_init_ref()) }
} else {
None
}
}
}
pub struct CircularBufferIterator<T: Sized, const SIZE: usize> {
buffer: CircularBuffer<T, SIZE>,
}
impl<T: Sized, const SIZE: usize> Iterator for CircularBufferIterator<T, SIZE> {
fn next(&mut self) -> Option<Self::Item> {
self.buffer.pop_front()
}
type Item = T;
}
impl<T: Sized, const SIZE: usize> IntoIterator for CircularBuffer<T, SIZE> {
fn into_iter(self) -> Self::IntoIter {
CircularBufferIterator { buffer: self }
}
type IntoIter = CircularBufferIterator<T, SIZE>;
type Item = T;
}
pub struct CircularBufferIteratorRef<'a, T: Sized, const SIZE: usize> {
cb: &'a CircularBuffer<T, SIZE>,
count: usize,
count_back: usize,
}
impl<'a, T: Sized, const SIZE: usize> Iterator for CircularBufferIteratorRef<'a, T, SIZE> {
fn next(&mut self) -> Option<Self::Item> {
if self.count + self.count_back == self.cb.len() {
None
} else {
let i = (self.cb.front + self.count) % SIZE;
let result = Some(unsafe { self.cb.buffer[i].assume_init_ref() });
self.count += 1;
result
}
}
type Item = &'a T;
}
impl<'a, T: Sized, const SIZE: usize> DoubleEndedIterator
for CircularBufferIteratorRef<'a, T, SIZE>
{
fn next_back(&mut self) -> Option<Self::Item> {
if self.count_back == self.cb.len() {
None
} else {
let i = (SIZE + self.cb.rear - 1 - self.count_back) % SIZE;
let result = Some(unsafe { self.cb.buffer[i].assume_init_ref() });
self.count_back += 1;
result
}
}
}
impl<'a, T: Sized, const SIZE: usize> IntoIterator for &'a CircularBuffer<T, SIZE> {
fn into_iter(self) -> Self::IntoIter {
CircularBufferIteratorRef {
cb: self,
count: 0,
count_back: 0,
}
}
type IntoIter = CircularBufferIteratorRef<'a, T, SIZE>;
type Item = &'a T;
}
pub struct CircularBufferIteratorMutRef<'a, T: Sized, const SIZE: usize> {
cb: &'a mut CircularBuffer<T, SIZE>,
count: usize,
count_back: usize,
}
impl<'a, T: Sized, const SIZE: usize> Iterator for CircularBufferIteratorMutRef<'a, T, SIZE> {
fn next(&mut self) -> Option<Self::Item> {
if self.count + self.count_back == self.cb.len() {
None
} else {
let result = unsafe {
let i = (self.cb.front + self.count) % SIZE;
Some(core::mem::transmute::<&mut T, &'a mut T>(
self.cb.buffer[i].assume_init_mut(),
))
};
self.count += 1;
result
}
}
type Item = &'a mut T;
}
impl<'a, T: Sized, const SIZE: usize> DoubleEndedIterator
for CircularBufferIteratorMutRef<'a, T, SIZE>
{
fn next_back(&mut self) -> Option<Self::Item> {
if self.count + self.count_back == self.cb.len() {
None
} else {
let result = unsafe {
let i = (SIZE + self.cb.rear - 1 - self.count_back) % SIZE;
Some(core::mem::transmute::<&mut T, &'a mut T>(
self.cb.buffer[i].assume_init_mut(),
))
};
self.count_back += 1;
result
}
}
}
impl<'a, T: Sized, const SIZE: usize> IntoIterator for &'a mut CircularBuffer<T, SIZE> {
fn into_iter(self) -> Self::IntoIter {
CircularBufferIteratorMutRef {
cb: self,
count: 0,
count_back: 0,
}
}
type IntoIter = CircularBufferIteratorMutRef<'a, T, SIZE>;
type Item = &'a mut T;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
const SZ: usize = 10;
let mut cb = CircularBuffer::<i32, SZ>::new();
for i in 0..SZ {
let result = cb.push(i as i32);
assert_eq!(true, result.is_ok());
}
//Check the buffer is full
assert_eq!(SZ, cb.len());
//The buffer contains the elements that we've pushed
for (index, elem) in (&cb).into_iter().enumerate() {
assert_eq!(index as i32, *elem);
}
let result = cb.push(10);
//Cannot push in a full buffer
assert_eq!(true, result.is_err());
//Check push_evict returns the first element
let result = cb.push_evict(10);
assert_eq!(Some(0), result);
//Check pop_front returns the second element
let second = cb.pop_front();
assert_eq!(Some(1), second);
//Check the buffer is not full
assert_eq!(SZ - 1, cb.len());
//Check pop_front returns the third element
let second = cb.pop_front();
assert_eq!(Some(2), second);
//Check the buffer is not full
assert_eq!(SZ - 2, cb.len());
//Replace all the elements with their index
for (n, i) in cb.iter_mut().enumerate() {
*i = n as i32;
}
for (n, i) in cb.iter().enumerate() {
assert_eq!(n as i32, *i);
}
for (n, i) in cb.iter().rev().enumerate() {
assert_eq!((cb.len() - n - 1) as i32, *i);
}
}
}

View File

@@ -0,0 +1,3 @@
pub const BITS_PER_POSITION: usize = 3;
pub const POSITIONS: usize = 32;
pub const POSITIONS_PER_ROW: usize = 4;

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,15 @@
#![no_std]
mod board;
pub mod draughts;
pub mod position;
//use draughts::DraughtsBoard;
//use draughts::Piece;
mod circular_buffer;
mod constants;
pub mod draughts;
mod movement;
mod piece;
mod player;
mod position;
pub use draughts::{DraughtsBoard, DraughtsGame, Error, RDraughtApplication, RectangularBoard};
pub use movement::{LoggedMove, Move, MoveDirection};
pub use piece::Piece;
pub use player::Player;
pub use position::Position;

269
rdraught/src/movement.rs Normal file
View File

@@ -0,0 +1,269 @@
use crate::position::Position;
use crate::{Piece, Player, draughts::Error};
use strum_macros::FromRepr;
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromRepr)]
pub enum MoveDirection {
NE = 0,
SE = 1,
SW = 2,
NW = 3,
}
impl MoveDirection {
pub fn is_forward(&self, piece: Piece) -> Result<bool, Error> {
match piece.player() {
Some(Player::Red) => Ok(matches!(self, Self::SE) || matches!(self, Self::SW)),
Some(Player::White) => Ok(matches!(self, Self::NE) || matches!(self, Self::NW)),
None => Err(Error::NoPiece),
}
}
}
pub fn compute_end_position(
start_position: Position,
direction: MoveDirection,
is_capture: bool,
) -> Position {
if is_capture {
let direction = direction;
match direction {
MoveDirection::NE => start_position + (2, 2),
MoveDirection::SE => start_position + (-2, 2),
MoveDirection::SW => start_position + (-2, -2),
MoveDirection::NW => start_position + (2, -2),
}
} else {
match direction {
MoveDirection::NE => start_position + (1, 1),
MoveDirection::SE => start_position + (-1, 1),
MoveDirection::SW => start_position + (-1, -1),
MoveDirection::NW => start_position + (1, -1),
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct LoggedMove {
data: u16,
}
impl LoggedMove {
pub fn movement(start: Position, dir: MoveDirection, is_pawn_promoted: bool) -> LoggedMove {
let mut result = <Position as Into<u8>>::into(start) as u16;
result |= (dir as u16) << 5;
if is_pawn_promoted {
result |= 1u16 << 8;
}
LoggedMove { data: result }
}
pub fn capture(
start: Position,
dir: MoveDirection,
is_pawn_promoted: bool,
crowned_capture: bool,
multi_capture: bool,
) -> LoggedMove {
let mut result = <Position as Into<u8>>::into(start) as u16;
result |= (dir as u16) << 5;
result |= 1u16 << 7;
if is_pawn_promoted {
result |= 1u16 << 8;
}
if crowned_capture {
result |= 1u16 << 9;
}
if multi_capture {
result |= 1u16 << 10;
}
LoggedMove { data: result }
}
pub fn start_position(&self) -> Position {
Position::from((self.data & 31) as u8)
}
pub fn direction(&self) -> MoveDirection {
MoveDirection::from_repr(((self.data & 96) >> 5) as usize).unwrap()
}
pub fn is_capture(&self) -> bool {
(self.data & 128) != 0
}
pub fn is_movement(&self) -> bool {
!self.is_capture()
}
pub fn pawn_promoted(&self) -> bool {
(self.data & 256) != 0
}
pub fn crowned_captured(&self) -> bool {
(self.data & 512) != 0
}
pub fn multi_capture(&self) -> bool {
(self.data & 1024) != 0
}
pub fn get_end_position(&self) -> Position {
compute_end_position(
self.start_position().clone(),
self.direction(),
self.is_capture(),
)
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Move {
Movement {
start: Position,
direction: MoveDirection,
},
Capture {
start: Position,
direction: MoveDirection,
},
}
impl Move {
pub fn movement(start: Position, direction: MoveDirection) -> Move {
Move::Movement { start, direction }
}
pub fn capture(start: Position, direction: MoveDirection) -> Move {
Move::Capture { start, direction }
}
pub fn is_movement(&self) -> bool {
match self {
Move::Movement {
start: _,
direction: _,
} => true,
Move::Capture {
start: _,
direction: _,
} => false,
}
}
pub fn is_capture(&self) -> bool {
!self.is_movement()
}
pub fn get_start_position(&self) -> Position {
match self {
Move::Movement {
start,
direction: _,
} => start.clone(),
Move::Capture {
start,
direction: _,
} => start.clone(),
}
}
pub fn get_end_position(&self) -> Position {
compute_end_position(
self.get_start_position(),
self.get_direction(),
self.is_capture(),
)
}
pub fn get_direction(&self) -> MoveDirection {
match self {
Move::Movement {
start: _,
direction,
} => direction.clone(),
Move::Capture {
start: _,
direction,
} => direction.clone(),
}
}
}
#[cfg(test)]
mod tests {
use super::{LoggedMove, MoveDirection, Piece};
use crate::Position;
struct MoveData {
piece: Piece,
start: Position,
dir: MoveDirection,
capture: bool,
crowned_capture: bool,
pawn_promoted: bool,
multi_capture: bool,
}
#[test]
fn test_from_into() {
let move_datas = [
MoveData {
piece: Piece::SimpleRedPawn,
start: Position::new(0, 0).unwrap(),
dir: super::MoveDirection::NE,
capture: false,
crowned_capture: false,
pawn_promoted: false,
multi_capture: false,
},
MoveData {
piece: Piece::SimpleWhitePawn,
start: Position::new(3, 5).unwrap(),
dir: super::MoveDirection::SW,
capture: false,
crowned_capture: false,
pawn_promoted: false,
multi_capture: false,
},
MoveData {
piece: Piece::SimpleWhitePawn,
start: Position::new(6, 6).unwrap(),
dir: super::MoveDirection::NW,
capture: false,
crowned_capture: false,
pawn_promoted: true,
multi_capture: false,
},
MoveData {
piece: Piece::SimpleWhitePawn,
start: Position::new(5, 5).unwrap(),
dir: super::MoveDirection::NE,
capture: true,
crowned_capture: true,
pawn_promoted: true,
multi_capture: true,
},
];
for md in move_datas {
let mv = if md.capture {
LoggedMove::capture(
md.start,
md.dir,
md.pawn_promoted,
md.crowned_capture,
md.multi_capture,
)
} else {
LoggedMove::movement(md.start, md.dir, md.pawn_promoted)
};
assert_eq!(md.start, mv.start_position());
assert_eq!(md.dir, mv.direction());
assert_eq!(md.capture, mv.is_capture());
assert_eq!(md.crowned_capture, mv.crowned_captured());
assert_eq!(md.pawn_promoted, mv.pawn_promoted());
assert_eq!(md.multi_capture, mv.multi_capture());
}
}
}

35
rdraught/src/piece.rs Normal file
View File

@@ -0,0 +1,35 @@
use crate::player::Player;
use strum_macros::FromRepr;
#[derive(Debug, PartialEq, Eq, Clone, Copy, FromRepr)]
pub enum Piece {
NoPiece = 0,
SimpleRedPawn = 1,
CrownedRedPawn = 3,
SimpleWhitePawn = 2,
CrownedWhitePawn = 4,
}
impl Piece {
pub fn player(&self) -> Option<Player> {
if self.is_present() && (*self as u32) % 2 == 1 {
Some(Player::Red)
} else if self.is_present() && (*self as u32) % 2 == 0 {
Some(Player::White)
} else {
None
}
}
pub fn is_present(&self) -> bool {
*self != Piece::NoPiece
}
pub fn is_crowned(&self) -> bool {
(*self as u32) > 2
}
pub fn can_move_backward(&self) -> bool {
self.is_crowned()
}
}

5
rdraught/src/player.rs Normal file
View File

@@ -0,0 +1,5 @@
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Player {
White = 0,
Red = 1,
}

View File

@@ -1,9 +1,12 @@
use crate::Error;
use core::fmt::Display;
use core::ops::Add;
use core::ops::Div;
use core::ops::Mul;
use core::ops::Sub;
use crate::constants::POSITIONS_PER_ROW;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Position((u8, u8));
@@ -22,8 +25,12 @@ impl Position {
Position((index.0 as u8, index.1 as u8))
}
pub fn new(row: u8, column: u8) -> Position {
Position((row, column))
pub fn new(row: u8, column: u8) -> Result<Position, Error> {
if (row + column) % 2 == 0 {
Ok(Position((row, column)))
} else {
Err(Error::InvalidPosition)
}
}
pub fn row(&self) -> u8 {
@@ -109,3 +116,46 @@ impl Mul<(i32, i32)> for Position {
))
}
}
impl From<Position> for u8 {
fn from(pos: Position) -> Self {
(8 - 1 - pos.row()) * (POSITIONS_PER_ROW as u8) + pos.col() / 2
}
}
impl From<u8> for Position {
fn from(n: u8) -> Self {
// println!("{}", n);
let row = 8 - 1 - n / (POSITIONS_PER_ROW as u8);
let offset = if row % 2 == 0 { 0u8 } else { 1u8 };
let col = n % (POSITIONS_PER_ROW as u8) * 2 + offset;
Position::new(row, col).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::Position;
#[test]
fn test_from_into() {
let positions = [
Position::new(4, 6),
Position::new(0, 4),
Position::new(1, 3),
Position::new(7, 1),
Position::new(2, 2),
Position::new(5, 3),
Position::new(3, 7),
Position::new(6, 0),
Position::new(7, 7),
];
for p in positions {
let p = p.unwrap();
let n = <Position as Into<u8>>::into(p);
let p2 = <Position as From<u8>>::from(n);
assert_eq!(p, p2);
}
}
}