borrowed_resources/
borrowed-resources.rs

1use sf2g::{
2    SfResult,
3    graphics::{
4        CircleShape, Color, ConvexShape, Font, RenderStates, RenderTarget, RenderWindow, Shape,
5        Sprite, Text, Texture, Transformable,
6    },
7    window::{Event, Key, Style},
8};
9
10include!("../example_common.rs");
11
12fn main() -> SfResult<()> {
13    example_ensure_right_working_dir();
14
15    let mut window = RenderWindow::new(
16        (800, 600),
17        "Borrowed resources",
18        Style::CLOSE,
19        &Default::default(),
20    )?;
21    window.set_vertical_sync_enabled(true);
22
23    // Create a new texture. (Hey Frank!)
24    let frank = Texture::from_file("frank.jpeg")?;
25
26    // Create a font.
27    let font = Font::from_file("sansation.ttf")?;
28
29    // Create a circle with the Texture.
30    let mut circle = CircleShape::with_texture(&frank);
31    circle.set_radius(70.0);
32    circle.set_position((100.0, 100.0));
33
34    // Create a Sprite.
35    let mut sprite = Sprite::new();
36    // Have it use the same texture as the circle.
37    sprite.set_texture(&frank, true);
38    sprite.set_position((400.0, 300.0));
39    sprite.set_scale(0.5);
40
41    // Create a ConvexShape using the same texture.
42    let mut convex_shape = ConvexShape::with_texture(6, &frank);
43    convex_shape.set_point(0, (400., 100.));
44    convex_shape.set_point(1, (500., 70.));
45    convex_shape.set_point(2, (450., 100.));
46    convex_shape.set_point(3, (580., 150.));
47    convex_shape.set_point(4, (420., 230.));
48    convex_shape.set_point(5, (420., 120.));
49
50    // Create an initialized text using the font.
51    let mut title = Text::new("Borrowed resources example!".into(), &font, 50);
52
53    // Create a second text using the same font.
54    // This time, we create and initialize it separately.
55    let mut second_text = Text::default();
56    second_text.set_string("This text shares the same font with the title!".into());
57    second_text.set_font(&font);
58    second_text.set_fill_color(Color::GREEN);
59    second_text.tf.position = [10.0, 350.0];
60    second_text.set_character_size(20);
61
62    // Create a third text using the same font.
63    let mut third_text = Text::new("This one too!".into(), &font, 20);
64    third_text.tf.position = [300.0, 100.0];
65    third_text.set_fill_color(Color::RED);
66
67    'mainloop: loop {
68        while let Some(event) = window.poll_event() {
69            match event {
70                Event::Closed
71                | Event::KeyPressed {
72                    code: Key::Escape, ..
73                } => break 'mainloop,
74                _ => {}
75            }
76        }
77
78        window.clear(Color::BLACK);
79        let rs = RenderStates::DEFAULT;
80        window.draw_circle_shape(&circle, &rs);
81        window.draw_sprite(&sprite, &rs);
82        window.draw_convex_shape(&convex_shape, &rs);
83        title.draw(&mut *window, &rs);
84        second_text.draw(&mut *window, &rs);
85        third_text.draw(&mut *window, &rs);
86
87        // Little test here for `Shape::points`
88        let mut circ = CircleShape::new(4.0, 30);
89        circ.set_origin(2.0);
90        circ.set_fill_color(Color::YELLOW);
91
92        for p in convex_shape.points() {
93            circ.set_position(p);
94            window.draw_circle_shape(&circ, &RenderStates::DEFAULT);
95        }
96
97        window.display();
98    }
99    Ok(())
100}