Skip to content

Commit

Permalink
knight fixing, custom piece images
Browse files Browse the repository at this point in the history
  • Loading branch information
Smallsan committed Mar 14, 2024
1 parent 339e6fc commit 596b4f0
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use fenix::*;
// 4. The ChessAssets instance
// This returns an result(), or an error if it fails to convert the fen to image.
// The board also auto rotates depending on whose turn it is.
let result = fen_to_board_img("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w", "chess_board.png", 1, ChessAssets::default());
let result = fen_to_board_img("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w", "chess_board.png", 1, &ChessAssets::default());

result.unwrap();

Expand Down
77 changes: 77 additions & 0 deletions src/chess_assets/asset_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::collections::HashMap;

use image::{ImageFormat, RgbaImage};

use crate::errors::FenToImgError;

pub struct ChessAssets {
pub piece_images: HashMap<char, RgbaImage>,
pub board_image: RgbaImage,
Expand Down Expand Up @@ -137,3 +139,78 @@ impl Default for ChessAssets {
}
}
}

impl ChessAssets {
pub fn new(piece_images: HashMap<char, RgbaImage>, board_image: RgbaImage) -> Self {
ChessAssets {
piece_images,
board_image,
}
}
// Black Pieces
/// Piece size requirements: 16x16 Canvas size, Minimum of 1 pixel of border
/// image_path: Path to the image file
pub fn set_black_king(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('k', image);
Ok(())
}
pub fn set_black_queen(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('q', image);
Ok(())
}
pub fn set_black_rook(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('r', image);
Ok(())
}
pub fn set_black_bishop(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('b', image);
Ok(())
}
pub fn set_black_knight(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('n', image);
Ok(())
}
pub fn set_black_pawn(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('p', image);
Ok(())
}
// White Pieces
pub fn set_white_king(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('K', image);
Ok(())
}
pub fn set_white_queen(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('Q', image);
Ok(())
}
pub fn set_white_rook(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('R', image);
Ok(())
}
pub fn set_white_bishop(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('B', image);
Ok(())
}
pub fn set_white_knight(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('N', image);
Ok(())
}
pub fn set_white_pawn(&mut self, image_path: &str) -> Result<(), FenToImgError> {
let image = image::open(image_path).map_err(|err| FenToImgError::ImageError(err))?.to_rgba8();
self.piece_images.insert('P', image);
Ok(())
}


}
Binary file modified src/chess_assets/board/board.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/chess_assets/pieces/black_knight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/chess_assets/pieces/white_knight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ impl From<&str> for FenToImgError {
fn from(err: &str) -> FenToImgError {
FenToImgError::FenError(err.to_string())
}
}
}

0 comments on commit 596b4f0

Please sign in to comment.