update font

This commit is contained in:
cool-mist
2026-01-18 23:59:09 +05:30
parent b15fb61c9e
commit 2d33b20ded
4 changed files with 71 additions and 27 deletions
+30 -12
View File
@@ -1,6 +1,6 @@
use std::{
collections::HashMap,
fmt::{self, Display, Formatter},
fmt::{self, Display, Formatter}, sync::Arc,
};
use button::Button;
@@ -38,6 +38,7 @@ pub struct Game {
// Constants througout the game
texture_res: Texture2D,
sounds: Sounds,
font: Arc<Font>,
num_squares: usize,
heading_text: String,
@@ -92,7 +93,7 @@ enum GameState {
}
impl Game {
pub fn new(texture_res: Texture2D, sounds: Sounds) -> Self {
pub fn new(texture_res: Texture2D, sounds: Sounds, font: Font) -> Self {
let num_squares: usize = 4;
let game_mode = GameMode::Medium;
let board = Game::generate_puzzle(game_mode);
@@ -118,6 +119,7 @@ impl Game {
window_height: 0.,
window_width: 0.,
square_width: 0.,
font: Arc::new(font),
}
}
@@ -259,13 +261,18 @@ impl Game {
fn draw_heading(&self) {
let f = self.heading_font_size.floor() as u16;
let dims = measure_text(self.heading_text.as_str(), None, f, 1.0);
draw_text(
let dims = measure_text(self.heading_text.as_str(), Some(&self.font), f, 1.0);
let draw_text_params = TextParams {
font_size: f,
font: Some(&self.font),
color: BLACK,
..Default::default()
};
draw_text_ex(
self.heading_text.as_str(),
self.heading_rect.x,
self.heading_rect.y + dims.offset_y,
self.heading_font_size,
BLACK,
draw_text_params,
);
}
@@ -282,20 +289,25 @@ impl Game {
UiColor::Brown.to_bg_color(),
);
let font_size = self.heading_font_size * 0.8;
let font_size = self.heading_font_size * 0.6;
let rules = "\
Every move should be a \n\
capture. Win when only \n\
one piece is left.\n";
let measurement = measure_text(rules, None, font_size as u16, 1.0);
draw_multiline_text(
let measurement = measure_text(rules, Some(&self.font), font_size as u16, 1.0);
let draw_text_params = TextParams {
font_size: font_size as u16,
font: Some(&self.font),
color: UiColor::Brown.to_fg_color(),
..Default::default()
};
draw_multiline_text_ex(
rules,
self.board_rect.x + 0.05 * self.square_width,
self.board_rect.y + 0.5 * (self.board_rect.h - measurement.height)
- 2. * measurement.offset_y,
font_size,
Some(2.),
UiColor::Brown.to_fg_color(),
draw_text_params,
);
return;
}
@@ -400,7 +412,7 @@ impl Game {
self.heading_font_size = 0.07 * min_dimension;
let f = self.heading_font_size.floor() as u16;
let dims = measure_text(self.heading_text.as_str(), None, f, 1.0);
let dims = measure_text(self.heading_text.as_str(), Some(&self.font), f, 1.0);
self.heading_rect = Rect::new(
board_x + (board_width - dims.width) / 2.0,
(board_y - dims.height) / 2.0,
@@ -444,6 +456,7 @@ impl Game {
Rect::new(board_x + btn_reset_x_offset, btn_y, btn_w, btn_h),
UiColor::Yellow,
self.sounds.button.clone(),
self.font.clone(),
);
let btn_next_x_offset =
@@ -453,6 +466,7 @@ impl Game {
Rect::new(board_x + btn_next_x_offset, btn_y, btn_w, btn_h),
UiColor::Green,
self.sounds.button.clone(),
self.font.clone(),
);
next_btn.is_active = false;
@@ -466,6 +480,7 @@ impl Game {
),
UiColor::Brown,
self.sounds.button.clone(),
self.font.clone(),
);
self.rules_btn = vec![rules_button];
@@ -483,6 +498,7 @@ impl Game {
),
UiColor::Yellow,
self.sounds.mode.clone(),
self.font.clone(),
);
let medium_btn = Button::new(
@@ -495,6 +511,7 @@ impl Game {
),
UiColor::Yellow,
self.sounds.mode.clone(),
self.font.clone(),
);
let hard_button = Button::new(
@@ -507,6 +524,7 @@ impl Game {
),
UiColor::Yellow,
self.sounds.mode.clone(),
self.font.clone(),
);
self.mode_btns = HashMap::new();
+15 -6
View File
@@ -1,3 +1,5 @@
use std::sync::Arc;
use macroquad::{audio::Sound, prelude::*};
use crate::game::sound::Sounds;
@@ -13,10 +15,11 @@ pub struct Button {
shadow_width: f32,
pub color: UiColor,
sound: Sound,
font: Arc<Font>,
}
impl Button {
pub fn new(text: &str, rect: Rect, color: UiColor, sound: Sound) -> Self {
pub fn new(text: &str, rect: Rect, color: UiColor, sound: Sound, font: Arc<Font>) -> Self {
Self {
text: text.to_string(),
is_down: false,
@@ -26,6 +29,7 @@ impl Button {
shadow_width: 5.0,
color,
sound,
font,
}
}
@@ -78,16 +82,21 @@ impl Button {
false => Color::from_rgba(100, 100, 100, 255),
};
let font_size = (0.3 * self.rect.w) as u16;
let dims = measure_text(&self.text, None, font_size, 1.0);
let font_size = (0.2 * self.rect.w) as u16;
let dims = measure_text(&self.text, Some(&self.font), font_size, 1.0);
let button_draw_offset = self.get_button_draw_offset();
draw_text(
let text_params = TextParams {
font_size: font_size as u16,
color: font_color,
font: Some(&self.font),
..Default::default()
};
draw_text_ex(
&self.text,
self.rect.x + (self.rect.w - dims.width) * 0.5 + button_draw_offset,
self.rect.y + (self.rect.h - dims.height) * 0.5 + dims.offset_y + button_draw_offset,
font_size as f32,
font_color,
text_params,
);
}
+26 -9
View File
@@ -5,18 +5,23 @@ use miniquad::date;
mod game;
fn window_conf() -> Conf {
let window_name = match std::env::var("TESTING") {
Ok(_) => "DEV TESTING MOVE TO WORKSPACE 10",
Err(_) => "Solitaire Chess",
};
Conf {
window_title: window_name.to_string(),
window_title: get_window_title(),
fullscreen: false,
..Default::default()
}
}
#[cfg(debug_assertions)]
fn get_window_title() -> String {
String::from("MOVE TO WORKSPACE 10")
}
#[cfg(not(debug_assertions))]
fn get_window_title() -> String {
String::from("Solitaire Chess")
}
#[macroquad::main(window_conf)]
async fn main() {
rand::srand(date::now() as u64);
@@ -43,12 +48,24 @@ async fn init() -> Game {
let texture_res = Texture2D::from_file_with_format(&texture_bytes[..], None);
texture_res.set_filter(FilterMode::Nearest);
build_textures_atlas();
let click = load_sound!("../assets/click.wav");
let win = load_sound!("../assets/win.wav");
let loss = load_sound!("../assets/loss.wav");
let button = load_sound!("../assets/button.wav");
let mode = load_sound!("../assets/mode.wav");
let sounds = Sounds { click, win, loss, button, mode };
let game = Game::new(texture_res, sounds);
game
let sounds = Sounds {
click,
win,
loss,
button,
mode,
};
let font_ttf = include_bytes!("../assets/caskaydia.ttf");
let Ok(font) = load_ttf_font_from_bytes(font_ttf) else {
panic!("Failed to load font");
};
Game::new(texture_res, sounds, font)
}