hello_world/hello-world.rs
1//! A demonstrations of various features available in the Pronto Graphics library.
2//! Please read the comments above each line for an explanation, and the full documentation
3//! of the various functions for further details.
4
5use pronto_graphics::{Button, Color, Key, Window};
6
7fn main() {
8 // Create a new Pronto Graphics window, all drawing happens through this object.
9 // The window's drawable area will be of size 800 by 600, and the window title will be "Hello World".
10 let mut pg = Window::new(800, 600, "Hello World");
11
12 // Alternatively, we can create a full screen window.
13 // let mut pg = Window::new_fullscreen();
14
15 // Set the background color for our window.
16 // At the beginning of each frame, the window is cleared with this color.
17 pg.background_color((0x11, 0x11, 0x11));
18
19 // Load a texture from a file.
20 let texture = pg.load_texture("examples/test_texture.png").unwrap();
21
22 // Load a font from a file.
23 let font = pg.load_font("examples/Whisper-Regular.ttf").unwrap();
24
25 loop {
26 // Set the fill color for drawing shapes to blue.
27 pg.fill_color(Color::BLUE);
28 // Draw a circle in the middle of the window (width/2, height/2) with a radius of 64 pixels.
29 pg.circle((pg.width() / 2., pg.height() / 2.), 64.);
30
31 {
32 let pos = (
33 0.5 * pg.width() + 128. * pg.time().cos(),
34 0.5 * pg.height() + 128. * pg.time().sin(),
35 );
36
37 // Set the fill color to #CCCCCC, i.e. a light grey.
38 pg.fill_color((0xCC, 0xCC, 0xCC));
39 // Draw a circle at position `pos` with a radius of 16 pixels.
40 pg.circle(pos, 16.);
41 }
42
43 // Draw our texture at position `(10, 10)` with a width of 100 pixels.
44 // The texture's height will be deduced from the texture's aspect ratio.
45 // I.e. the texture is not distorted.
46 // If we want to set the height ourselves, we have to use `pg.texture(...)` (Note the missing `_`)
47 pg.texture_((10., 10.), texture, 150.);
48
49 // React to the the left mouse button being pressed.
50 // As long as the button is held down, the code inside the `if` is executed.
51 if pg.mouse_pressed(Button::LEFT) {
52 // Set the fill color to blue, but with it's alpha set to 127, i.e. half-transparent.
53 pg.fill_color(Color::GREEN.with_alpha(127));
54 pg.circle(pg.mouse_position(), 16.);
55 }
56
57 // React to the `SPACE` key having just been pressed this very frame.
58 // The code inside the if will only be executed once every time you press the key.
59 // I.e. Holding the key down will _not_ cause "Action!" to be printed every frame.
60 // If we want to react to a held down key, we have to use `pg.key_pressed(...)`
61 if pg.key_just_pressed(Key::SPACE) {
62 println!("Action!");
63 }
64
65 // React to the mouse wheel having been scrolled.
66 // `pg.mouse_wheel()` will give you a cumulative amount of scrolling across frames.
67 // If you want to know whether the scroll wheel has been scrolled in the current frame,
68 // use `pg.mouse_wheel_delta()` instead.
69 if pg.mouse_wheel() > 0. {
70 pg.fill_color(Color::GREEN);
71 } else {
72 pg.fill_color(Color::RED);
73 }
74
75 pg.font_color(Color::GREEN);
76 pg.font_size(40);
77 pg.font(Some(font));
78 pg.text((300., 50.), "Salutations!");
79
80 // Draw a square with side lengths of 25 pixels.
81 // Depending on what fill color has last been set above,
82 // this square will either be green or red.
83 pg.square((50., 300.), 25.);
84
85 {
86 let pos = (
87 pg.width() - 200.,
88 50. + ((50. * pg.time()) % (pg.height() - 130.)),
89 );
90
91 // Set the font color to a lovely pink of #EE4488.
92 pg.font_color((0xEE, 0x44, 0x88));
93 // Set the font size to 30 pixels.
94 pg.font_size(30);
95 // Set the font to the default font.
96 pg.font(None);
97 // Draw the string "Greetings!" as position `pos`.
98 pg.text(pos, "Greetings!");
99 }
100
101 // Set the line color from HSB, with the hue rotating through the colors.
102 pg.line_color(Color::from_hsb((60. * pg.time()) % 360., 1., 1.));
103
104 // Draw a bunch of lines.
105 pg.line((50., 500.), (50., 450.));
106 pg.line((50., 450.), (100., 500.));
107 pg.line((100., 475. + 25. * (5. * pg.time()).sin()), (150., 450.));
108
109 // At the end of our frame, we have to update the window
110 // for our drawings to appear on the screen and for the keyboard/mouse
111 // states to be updated.
112 // If your window ever is blank or looks glitchy, make sure you haven't forgotten
113 // to call `pg.update()` at the end of your `loop`.
114 pg.update();
115 }
116}