pub struct Window<'a> { /* private fields */ }Expand description
The core type of the Pronto Graphics library.
All drawing and keyboard/mouse interaction happens through an instance of Window.
It has to be updated every frame with Window::update for drawings to be rendered and the keyboard/mouse state to be updated.
§Examples
let mut pg = Window::new(800, 600, "Window Title"); // Create window
loop {
pg.circle((200,200), 50); // Draw to it
pg.update(); // Update for drawing to appear
}Implementations§
Source§impl Window<'_>
impl Window<'_>
Sourcepub fn new(width: u32, height: u32, name: &str) -> Self
pub fn new(width: u32, height: u32, name: &str) -> Self
Create a new window of size (width, height) and with title name.
Can be directly drawn to with functions like Window::circle
and has to be updated with Window::update.
Examples found in repository?
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}Sourcepub fn new_fullscreen() -> Self
pub fn new_fullscreen() -> Self
Create a new fullscreen window.
Can be directly drawn to with functions like Window::circle
and has to be updated with Window::update.
Sourcepub fn update(&mut self)
pub fn update(&mut self)
Has to be called every frame for drawings to appear on the screen and keyboard/mouse to be updated. Note that this function will block for vertical sync.
Examples found in repository?
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}Sourcepub fn background_color<C: Into<Color>>(&mut self, color: C)
pub fn background_color<C: Into<Color>>(&mut self, color: C)
Set the background color of the window.
The background color does not reset at the beginning of a new frame.
The initial value for the background color is Color::LIGHT_GRAY.
Examples found in repository?
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}Sourcepub fn fill_color<C: Into<Color>>(&mut self, color: C)
pub fn fill_color<C: Into<Color>>(&mut self, color: C)
Set the fill color for drawing shapes like Window::circle.
The fill color is reset at the beginning of a new frame to a default value of Color::WHITE.
Examples found in repository?
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}Sourcepub fn outline_color<C: Into<Color>>(&mut self, color: C)
pub fn outline_color<C: Into<Color>>(&mut self, color: C)
Set the outline color for drawing shapes like Window::circle.
The outline color is reset at the beginning of a new frame to a default value of Color::TRANSPARENT.
Sourcepub fn line_color<C: Into<Color>>(&mut self, color: C)
pub fn line_color<C: Into<Color>>(&mut self, color: C)
Set the line color for drawing lines with Window::line.
The line color is reset at the beginning of a new frame to a default value of Color::BLACK.
Examples found in repository?
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}Sourcepub fn font_color<C: Into<Color>>(&mut self, color: C)
pub fn font_color<C: Into<Color>>(&mut self, color: C)
Set the line color for drawing text with Window::text.
The font color is reset at the beginning of a new frame to a default value of Color::BLACK.
Examples found in repository?
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}Sourcepub fn font_size(&mut self, size: u32)
pub fn font_size(&mut self, size: u32)
Set the font size for drawing text with Window::text.
The font size is reset at the beginning of a new frame to a default value of 16.
Examples found in repository?
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}Sourcepub fn font(&mut self, font: Option<Font>)
pub fn font(&mut self, font: Option<Font>)
Set the font for drawing text with Window::text.
The font does not reset at the beginning of a new frame.
Fonts can be loaded with Window::load_font.
Initially or if a value of None is passed to this function,
a default font built into the library is used (Processing Sans Pro).
§Examples
let mut pg = Window::new_fullscreen();
let my_font = pg.load_font("MyFont.ttf").unwrap();
pg.font(Some(my_font));
loop {
pg.text((20, 20), "This text is drawn in MyFont.");
pg.update();
}Examples found in repository?
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}Sourcepub fn circle(&mut self, pos: (f32, f32), radius: f32)
pub fn circle(&mut self, pos: (f32, f32), radius: f32)
Draw a circle at position pos with radius radius.
The origin of the circle is at it’s center.
Examples found in repository?
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}Sourcepub fn rectangle(&mut self, pos: (f32, f32), width: f32, height: f32)
pub fn rectangle(&mut self, pos: (f32, f32), width: f32, height: f32)
Draw a rectangle at position pos with width and height of (width, height).
The origin of the rectangle is at it’s top left.
Sourcepub fn square(&mut self, pos: (f32, f32), size: f32)
pub fn square(&mut self, pos: (f32, f32), size: f32)
Draw a square at position pos with a width and height of size.
The origin of the square is at it’s top left.
Examples found in repository?
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}Sourcepub fn texture(
&mut self,
pos: (f32, f32),
texture: Texture,
width: f32,
height: f32,
)
pub fn texture( &mut self, pos: (f32, f32), texture: Texture, width: f32, height: f32, )
Draw a texture texture at position pos with width and height of (width, height).
The origin of the texture is at it’s top left.
Textures can be loaded with Window::load_texture.
§Examples
let mut pg = Window::new_fullscreen();
let my_texture = pg.load_texture("my_texture.png").unwrap();
loop {
pg.texture((100., 250.), my_texture, 100., 150.);
pg.update();
}Sourcepub fn texture_(&mut self, pos: (f32, f32), texture: Texture, width: f32)
pub fn texture_(&mut self, pos: (f32, f32), texture: Texture, width: f32)
Draw a texture texture at position pos with width of width,
and height according to the aspect ratio of the texture.
The origin of the texture is at it’s top left.
Textures can be loaded with Window::load_texture.
§Examples
let mut pg = Window::new_fullscreen();
let my_texture = pg.load_texture("my_texture.png").unwrap();
loop {
pg.texture_((100., 250.), my_texture, 100.);
pg.update();
}Examples found in repository?
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}Sourcepub fn text(&mut self, pos: (f32, f32), string: &str)
pub fn text(&mut self, pos: (f32, f32), string: &str)
Draw text string at position pos.
The default font size is 16 and can be changed with Window::font_size.
The default font color is Color::BLACK and can be changed with Window::font_color.
Uses the default font built into the library (Processing Sans Pro)
or the font set with Window::font.
§Examples
let mut pg = Window::new(720, 480, "Window Title");
loop {
pg.fill_color(Color::BLACK);
pg.text((10., 10.), "Hello World!");
pg.update();
}Examples found in repository?
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}Sourcepub fn line(&mut self, from: (f32, f32), to: (f32, f32))
pub fn line(&mut self, from: (f32, f32), to: (f32, f32))
Draw a line from position from to position to.
The line’s color is set with Window::line_color.
Examples found in repository?
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}Sourcepub fn key_pressed(&self, key: Key) -> bool
pub fn key_pressed(&self, key: Key) -> bool
Whether the keyboard key key is currently held pressed.
§Examples
if pg.key_pressed(Key::SPACE) {
/*...*/
}Sourcepub fn key_just_pressed(&self, key: Key) -> bool
pub fn key_just_pressed(&self, key: Key) -> bool
Whether the keyboard key key has just been pressed in this frame.
§Examples
if pg.key_just_pressed(Key::SPACE) {
/*...*/
}Examples found in repository?
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}Sourcepub fn mouse_pressed(&self, button: Button) -> bool
pub fn mouse_pressed(&self, button: Button) -> bool
Whether the mouse button button is currently held pressed.
§Examples
if pg.mouse_pressed(Button::LEFT) {
/*...*/
}Examples found in repository?
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}Sourcepub fn mouse_just_pressed(&self, button: Button) -> bool
pub fn mouse_just_pressed(&self, button: Button) -> bool
Whether the mouse button button has just been pressed in this frame.
§Examples
if pg.mouse_just_pressed(Button::LEFT) {
/*...*/
}Sourcepub fn mouse_position(&self) -> (f32, f32)
pub fn mouse_position(&self) -> (f32, f32)
The current mouse position inside the window.
Examples found in repository?
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}Sourcepub fn mouse_wheel(&self) -> f32
pub fn mouse_wheel(&self) -> f32
The current cumulative scroll wheel state of the mouse.
Examples found in repository?
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}Sourcepub fn mouse_wheel_delta(&self) -> f32
pub fn mouse_wheel_delta(&self) -> f32
How much the scroll wheel has been scrolled in this frame.
Sourcepub fn width(&self) -> f32
pub fn width(&self) -> f32
The width of the window, or the width of the screen in fullscreen mode.
Examples found in repository?
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}Sourcepub fn height(&self) -> f32
pub fn height(&self) -> f32
The height of the window, or the height of the screen in fullscreen mode.
Examples found in repository?
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}Sourcepub fn time(&self) -> f32
pub fn time(&self) -> f32
The time since the window has been created in seconds.
Examples found in repository?
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}Sourcepub fn load_texture(&mut self, path: &str) -> Option<Texture>
pub fn load_texture(&mut self, path: &str) -> Option<Texture>
Load a texture from path path.
A return value of None means that the texture could not be loaded.
On success, returns a Texture object that can be passed to the Window::texture function to draw the texture to the screen.
§Examples
let mut pg = Window::new_fullscreen();
let my_texture = pg.load_texture("my_texture.png").unwrap();
loop {
pg.texture_((100., 250.), my_texture, 100.);
pg.update();
}Examples found in repository?
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}Sourcepub fn load_font(&mut self, path: &str) -> Option<Font>
pub fn load_font(&mut self, path: &str) -> Option<Font>
Load a font from path path.
A return value of None means that the font could not be loaded.
On success, returns a Font object that can be passed to the Window::font function
to set the font to be used for drawing text with Window::text.
§Examples
let mut pg = Window::new_fullscreen();
let my_font = pg.load_font("MyFont.ttf").unwrap();
pg.font(Some(my_font));
loop {
pg.text((20, 20), "This text is drawn in MyFont.");
pg.update();
}Examples found in repository?
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}