added wasm port

This commit is contained in:
2025-07-08 22:32:58 +08:00
parent fa675c4b7f
commit 325ee25b2e
31 changed files with 1572 additions and 181 deletions

View File

@@ -12,6 +12,7 @@ version.workspace = true
gtk4.workspace = true
gdk4.workspace = true
rdraught = {workspace = true, features = ["std"]}
rdraught-ui-common = { workspace = true }
librsvg.workspace = true
cairo-rs.workspace = true
gio.workspace = true

View File

@@ -1,19 +1,35 @@
use glib::ExitCode;
use rdraught::draughts::DraughtsGame;
use rdraught::{draughts::Piece, draughts::Player, position::Position};
use rdraught_ui::run;
use rdraught::{DraughtsBoard, DraughtsGame, Move, Piece, Player, Position};
use rdraught_gtk::run;
use std::collections::HashMap;
fn main() -> ExitCode {
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)
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

@@ -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

@@ -1,7 +1,7 @@
use gtk4::{Align, Application, Box, CheckButton, Label, Orientation, Window, prelude::*};
use crate::types::SharedMutable;
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();

View File

@@ -1,8 +1,5 @@
mod geo2d;
mod final_dialog;
mod greeting_dialog;
mod rdraught_application;
mod types;
pub use rdraught_application::run;

View File

@@ -1,4 +1,3 @@
use super::geo2d::Point;
use core::f64::consts::PI;
use gdk4::cairo::{Context as CairoContext, Matrix, Rectangle};
use glib::ExitCode;
@@ -14,9 +13,9 @@ const SQUARE_SIZE: f64 = 1.0;
use super::final_dialog;
use super::greeting_dialog;
use super::types;
use types::{SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref};
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");
@@ -494,7 +493,7 @@ fn create_game_window(
};
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(

View File

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