-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtext.rs
65 lines (53 loc) · 1.56 KB
/
text.rs
1
2
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
use peacock::graphics::{self, DrawTextParams, Font, Text};
use peacock::Result;
use peacock::{ContextBuilder, State, Vector2f};
type Context = peacock::Context<()>;
struct TextExample {
greeting_message: Text,
centered_text: Text,
}
impl TextExample {
fn new(ctx: &mut Context) -> Result<Self> {
let font = Font::from_file(ctx, "examples/res/Roboto-Regular.ttf", 24)?;
let greeting_message = Text::new(
ctx,
"Hello, world!\n\nI hope you enjoy using Peacock!",
&font,
)?;
let centered_text = Text::new(ctx, "This text is centered on the screen", &font)?;
Ok(Self {
greeting_message,
centered_text,
})
}
}
impl State for TextExample {
type Context = ();
fn update(&mut self, _ctx: &mut Context) -> Result<()> {
Ok(())
}
fn draw(&mut self, ctx: &mut Context, _dt: f64) -> Result<()> {
graphics::draw(
ctx,
&self.greeting_message,
&DrawTextParams {
position: Vector2f::new(10.0, 10.0),
..Default::default()
},
)?;
graphics::draw(
ctx,
&self.centered_text,
&DrawTextParams {
position: Vector2f::new((1920 / 2 - self.centered_text.size().x / 2) as f32, 50.0),
..Default::default()
},
)?;
Ok(())
}
}
fn main() -> Result<()> {
ContextBuilder::new("Text", 1920, 1080)
.build_empty()?
.run_with_result(TextExample::new)
}