diff options
| author | Mike White <botahamec@outlook.com> | 2021-08-23 21:22:26 -0400 |
|---|---|---|
| committer | Mike White <botahamec@outlook.com> | 2021-08-23 21:22:26 -0400 |
| commit | ec184715933b200558e8a0fe040f8948661c7eaf (patch) | |
| tree | 57d1e46212a2d5a79ce4634b3bffd4b60d588a54 | |
| parent | 82c7c7edeb63c9f9082a201822da80e6021b2a11 (diff) | |
Created a basic UI that displays a single position
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | .idea/checkers.iml | 1 | ||||
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | model/src/lib.rs | 2 | ||||
| -rw-r--r-- | model/src/piece.rs | 21 | ||||
| -rw-r--r-- | ui/Cargo.toml | 10 | ||||
| -rw-r--r-- | ui/resources/chess_board.png | bin | 0 -> 441 bytes | |||
| -rw-r--r-- | ui/resources/red_king.png | bin | 0 -> 3138 bytes | |||
| -rw-r--r-- | ui/resources/red_piece.png | bin | 0 -> 1597 bytes | |||
| -rw-r--r-- | ui/resources/white_king.png | bin | 0 -> 3710 bytes | |||
| -rw-r--r-- | ui/resources/white_piece.png | bin | 0 -> 2189 bytes | |||
| -rw-r--r-- | ui/src/main.rs | 90 |
12 files changed, 129 insertions, 2 deletions
@@ -1,2 +1,4 @@ target/*
-Cargo.lock
\ No newline at end of file +Cargo.lock
+SDL2.dll
+SDL2.lib
\ No newline at end of file diff --git a/.idea/checkers.iml b/.idea/checkers.iml index 23ef6ec..0adb4e5 100644 --- a/.idea/checkers.iml +++ b/.idea/checkers.iml @@ -6,6 +6,7 @@ <sourceFolder url="file://$MODULE_DIR$/model/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/ai/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/cli/src" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/ui/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
@@ -2,7 +2,8 @@ members = [
"model",
"ai",
- "cli"
+ "cli",
+ "ui"
]
[profile.release]
diff --git a/model/src/lib.rs b/model/src/lib.rs index 8c8efb6..1e1b364 100644 --- a/model/src/lib.rs +++ b/model/src/lib.rs @@ -1,9 +1,11 @@ mod board; mod color; mod moves; +mod piece; mod possible_moves; pub use board::CheckersBitBoard; pub use color::PieceColor; pub use moves::Move; +pub use piece::Piece; pub use possible_moves::PossibleMoves; diff --git a/model/src/piece.rs b/model/src/piece.rs new file mode 100644 index 0000000..f36e0a4 --- /dev/null +++ b/model/src/piece.rs @@ -0,0 +1,21 @@ +use crate::PieceColor; + +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] +pub struct Piece { + king: bool, + color: PieceColor, +} + +impl Piece { + pub(crate) const fn new(king: bool, color: PieceColor) -> Self { + Self { king, color } + } + + pub const fn is_king(self) -> bool { + self.king + } + + pub const fn color(self) -> PieceColor { + self.color + } +} diff --git a/ui/Cargo.toml b/ui/Cargo.toml new file mode 100644 index 0000000..086a7de --- /dev/null +++ b/ui/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ui" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tetra = "0.6" +model = {path = "../model"}
\ No newline at end of file diff --git a/ui/resources/chess_board.png b/ui/resources/chess_board.png Binary files differnew file mode 100644 index 0000000..3e7f1da --- /dev/null +++ b/ui/resources/chess_board.png diff --git a/ui/resources/red_king.png b/ui/resources/red_king.png Binary files differnew file mode 100644 index 0000000..8f8a6e8 --- /dev/null +++ b/ui/resources/red_king.png diff --git a/ui/resources/red_piece.png b/ui/resources/red_piece.png Binary files differnew file mode 100644 index 0000000..ea207bf --- /dev/null +++ b/ui/resources/red_piece.png diff --git a/ui/resources/white_king.png b/ui/resources/white_king.png Binary files differnew file mode 100644 index 0000000..3eef6c2 --- /dev/null +++ b/ui/resources/white_king.png diff --git a/ui/resources/white_piece.png b/ui/resources/white_piece.png Binary files differnew file mode 100644 index 0000000..aff621a --- /dev/null +++ b/ui/resources/white_piece.png diff --git a/ui/src/main.rs b/ui/src/main.rs new file mode 100644 index 0000000..166b1ae --- /dev/null +++ b/ui/src/main.rs @@ -0,0 +1,90 @@ +use model::{CheckersBitBoard, PieceColor}; +use tetra::graphics::{self, Color, DrawParams, Texture}; +use tetra::math::Vec2; +use tetra::{Context, ContextBuilder, State}; + +const WINDOW_WIDTH: f32 = 640.0; +const WINDOW_HEIGHT: f32 = 480.0; +const DARK_SLATE_BLUE: Color = Color::rgb(0.2823529, 0.2392157, 0.5450980); + +struct GameState { + chess_board: Texture, + dark_piece: Texture, + light_piece: Texture, + dark_king: Texture, + light_king: Texture, + bit_board: CheckersBitBoard, +} + +impl GameState { + fn new(ctx: &mut Context) -> tetra::Result<Self> { + Ok(GameState { + chess_board: Texture::new(ctx, "./ui/resources/chess_board.png")?, + dark_piece: Texture::new(ctx, "./ui/resources/red_piece.png")?, + light_piece: Texture::new(ctx, "./ui/resources/white_piece.png")?, + dark_king: Texture::new(ctx, "./ui/resources/red_king.png")?, + light_king: Texture::new(ctx, "./ui/resources/white_king.png")?, + bit_board: CheckersBitBoard::starting_position(), + }) + } +} + +impl State for GameState { + fn draw(&mut self, ctx: &mut Context) -> tetra::Result { + graphics::clear(ctx, DARK_SLATE_BLUE); + + let board_draw_params = DrawParams::new() + .position(Vec2::new(120.0, 40.0)) + .scale(Vec2::new(0.4938272, 0.4938272)); + + self.chess_board.draw(ctx, board_draw_params); + + for row in 0..8 { + for col in 0..8 { + if let Some(piece) = self.bit_board.get_at_row_col(row, col) { + let piece_draw_params = DrawParams::new() + .position(Vec2::new( + 130.0 + (50.0 * col as f32), + 400.0 - (50.0 * row as f32), + )) + .scale(Vec2::new(0.3, 0.3)); + + match piece.color() { + PieceColor::Dark => { + if piece.is_king() { + self.dark_king.draw(ctx, piece_draw_params) + } else { + self.dark_piece.draw(ctx, piece_draw_params) + } + } + PieceColor::Light => { + if piece.is_king() { + self.light_king.draw(ctx, piece_draw_params) + } else { + self.light_piece.draw(ctx, piece_draw_params) + } + } + } + } + } + } + + Ok(()) + } +} + +fn main() -> tetra::Result { + let title = "Checkers with Ampere"; + let mut builder = ContextBuilder::new(title, WINDOW_WIDTH as i32, WINDOW_HEIGHT as i32); + + builder.show_mouse(true); + builder.quit_on_escape(true); + + if cfg!(debug_assertions) { + builder.debug_info(true); + } else { + builder.debug_info(false); + } + + builder.build()?.run(GameState::new) +} |
