Function macroquad::text::draw_text_ex
source · pub fn draw_text_ex(text: &str, x: f32, y: f32, params: TextParams<'_>)
Expand description
Draw text with custom params such as font, font size and font scale.
Examples found in repository?
examples/text_measures.rs (lines 66-75)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
fn draw_text_annotated(text: &str, font: Option<&Font>, x: f32, baseline: f32) {
let size = measure_text(text, font, 100, 1.0);
// Full background rect
draw_rectangle(x, baseline - size.offset_y, size.width, size.height, BLUE);
// Base line
draw_rectangle(x, baseline - 2.0, size.width, 4.0, RED);
// Base line annotation
draw_rectangle(x + size.width, baseline - 1.0, 120.0, 1.0, GRAY);
draw_text(
"baseline",
x + size.width + 10.0,
baseline - 5.0,
30.0,
WHITE,
);
// Top line
draw_rectangle(x, baseline - 2.0 - size.offset_y, size.width, 4.0, RED);
// Top line annotation
draw_rectangle(
x + size.width,
baseline - size.offset_y - 1.0,
120.0,
1.0,
GRAY,
);
draw_text(
"topline",
x + size.width + 10.0,
baseline - size.offset_y - 5.0,
30.0,
WHITE,
);
// Bottom line
draw_rectangle(
x,
baseline - 2.0 - size.offset_y + size.height,
size.width,
4.0,
RED,
);
// Bottom line annotation
draw_rectangle(
x + size.width,
baseline - size.offset_y + size.height - 1.0,
120.0,
1.0,
GRAY,
);
draw_text(
"bottomline",
x + size.width + 10.0,
baseline - size.offset_y + size.height - 5.0,
30.0,
WHITE,
);
draw_text_ex(
text,
x,
baseline,
TextParams {
font_size: 100,
font,
..Default::default()
},
);
}
More examples
examples/text.rs (line 14)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
async fn main() {
let font = load_ttf_font("./examples/DancingScriptRegular.ttf")
.await
.unwrap();
let mut angle = 0.0;
loop {
clear_background(BLACK);
draw_text_ex("Custom font size:", 20.0, 20.0, TextParams::default());
let mut y = 20.0;
for font_size in (30..100).step_by(20) {
let text = "abcdef";
let params = TextParams {
font_size,
..Default::default()
};
y += font_size as f32;
draw_text_ex(text, 20.0, y, params);
}
draw_text_ex("Dynamic font scale:", 20.0, 400.0, TextParams::default());
draw_text_ex(
"abcd",
20.0,
450.0,
TextParams {
font_size: 50,
font_scale: get_time().sin() as f32 / 2.0 + 1.0,
..Default::default()
},
);
draw_text_ex("Custom font:", 400.0, 20.0, TextParams::default());
draw_text_ex(
"abcd",
400.0,
70.0,
TextParams {
font_size: 50,
font: Some(&font),
..Default::default()
},
);
draw_text_ex(
"abcd",
400.0,
160.0,
TextParams {
font_size: 100,
font: Some(&font),
..Default::default()
},
);
draw_text_ex(
"abcd",
screen_width() / 4.0 * 2.0,
screen_height() / 3.0 * 2.0,
TextParams {
font_size: 70,
font: Some(&font),
rotation: angle,
..Default::default()
},
);
let center = get_text_center("abcd", Option::None, 70, 1.0, angle * 2.0);
draw_text_ex(
"abcd",
screen_width() / 4.0 * 3.0 - center.x,
screen_height() / 3.0 * 2.0 - center.y,
TextParams {
font_size: 70,
rotation: angle * 2.0,
..Default::default()
},
);
angle -= 0.030;
next_frame().await
}
}
examples/arkanoid.rs (lines 52-57)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
async fn main() {
const BLOCKS_W: usize = 10;
const BLOCKS_H: usize = 10;
const SCR_W: f32 = 20.0;
const SCR_H: f32 = 20.0;
let mut blocks: [[bool; BLOCKS_W]; BLOCKS_H] = [[true; BLOCKS_W]; BLOCKS_H];
let mut ball_x = 12.;
let mut ball_y = 7.;
let mut dx = 3.5;
let mut dy = -3.5;
let mut platform_x = 10.;
let mut stick = true;
let platform_width = 5.;
let platform_height = 0.2;
// build camera with following coordinate system:
// (0., 0) .... (SCR_W, 0.)
// (0., SCR_H) .... (SCR_W, SCR_H)
set_camera(&Camera2D {
zoom: vec2(1. / SCR_W * 2., 1. / SCR_H * 2.),
target: vec2(SCR_W / 2., SCR_H / 2.),
..Default::default()
});
loop {
clear_background(SKYBLUE);
let delta = get_frame_time();
if is_key_down(KeyCode::Right) && platform_x < SCR_W - platform_width / 2. {
platform_x += 3.0 * delta;
}
if is_key_down(KeyCode::Left) && platform_x > platform_width / 2. {
platform_x -= 3.0 * delta;
}
if stick == false {
ball_x += dx * delta;
ball_y += dy * delta;
} else {
let (font_size, font_scale, font_aspect) = camera_font_scale(1.);
let text_params = TextParams {
font_size,
font_scale,
font_scale_aspect: font_aspect,
..Default::default()
};
draw_text_ex(
"Press space to start",
SCR_W / 2. - 5.,
SCR_H / 2.,
text_params,
);
ball_x = platform_x;
ball_y = SCR_H - 0.5;
stick = !is_key_down(KeyCode::Space);
}
if ball_x <= 0. || ball_x > SCR_W {
dx *= -1.;
}
if ball_y <= 0.
|| (ball_y > SCR_H - platform_height - 0.15 / 2.
&& ball_x >= platform_x - platform_width / 2.
&& ball_x <= platform_x + platform_width / 2.)
{
dy *= -1.;
}
if ball_y >= SCR_H {
ball_y = 10.;
dy = -dy.abs();
stick = true;
}
for j in 0..BLOCKS_H {
for i in 0..BLOCKS_W {
if blocks[j][i] {
let block_w = SCR_W / BLOCKS_W as f32;
let block_h = 7.0 / BLOCKS_H as f32;
let block_x = i as f32 * block_w + 0.05;
let block_y = j as f32 * block_h + 0.05;
draw_rectangle(block_x, block_y, block_w - 0.1, block_h - 0.1, DARKBLUE);
if ball_x >= block_x
&& ball_x < block_x + block_w
&& ball_y >= block_y
&& ball_y < block_y + block_h
{
dy *= -1.;
blocks[j][i] = false;
}
}
}
}
draw_circle(ball_x, ball_y, 0.2, RED);
draw_rectangle(
platform_x - platform_width / 2.,
SCR_H - platform_height,
platform_width,
platform_height,
DARKPURPLE,
);
next_frame().await
}
}