font/
font.rs

1extern crate glfw;
2extern crate gust_render as gust;
3
4use gust::prelude::*;
5use std::cell::RefCell;
6use std::rc::Rc;
7
8fn main() {
9    // Create drawer window
10    let mut window = Window::new(gust::WIDTH, gust::HEIGHT, "Hello");
11
12    // Create event handler
13    let event_handler = EventHandler::new(&window);
14
15    // Create font
16    let font = Rc::new(RefCell::new(
17        Font::from_path("examples/font/test.ttf").unwrap(),
18    ));
19
20    // Create text with font
21    let mut text = Text::new(&font);
22    text.set_content("Welcome to Gust you can write text and\na lot more !\t(Like tabs)");
23    text.set_position(Vector::new(100.0, 100.0));
24
25    // Create a 2nd text with font
26    let mut text2 = Text::from_str(&font, "Salut !");
27    text2.set_position(Vector::new(200.0, 200.0));
28    text2.update();
29
30    // Loop preparation
31    window.set_clear_color(Color::new(0.0, 0.0, 0.0));
32    window.enable_cursor();
33    window.poll(None);
34    while window.is_open() {
35        // update text
36        text.update();
37
38        // Poll event
39        window.poll_events();
40        event_handler
41            .fetch()
42            .for_each(|event| handle(&event, &mut window, &mut text));
43
44        // Draw process (Clear -> Draw -> Display)
45        window.clear();
46        window.draw(&text);
47        window.draw(&text2);
48        window.display();
49    }
50}
51
52fn handle(event: &Event, window: &mut Window, text: &mut Text) {
53    match event.1 {
54        Events::Key(Key::Escape, _, Action::Press, _) => window.close(),
55        Events::CursorPos(x, y) => {
56            text.set_position(Vector::new(x as f32, y as f32));
57        }
58        _ => {}
59    }
60}