Instance

Struct Instance 

Source
pub struct Instance { /* private fields */ }
Expand description

Frug Instance. Holds the context and the canvas

Implementations§

Source§

impl Instance

Source

pub fn new(title: &str, width: u32, height: u32) -> Self

Creates a new frug instance and a window (given a title, width, and height)

Examples found in repository?
examples/rectangle.rs (line 4)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    let rect_pos = Vec2d { x: 50, y: 50 };
8    let rect_size = Vec2d { x: 100, y: 70 };
9
10    'running: loop {
11        // Input
12        for event in frug_instance.get_events() {
13            match event {
14                // Quit the application
15                Event::Quit { .. }
16                | Event::KeyDown {
17                    keycode: Some(Keycode::Escape),
18                    ..
19                } => break 'running,
20                _ => {}
21            }
22        }
23
24        // Render
25        frug_instance.clear(background_color);
26        frug_instance.draw_rect(&rect_pos, &rect_size, Color::RGB(255, 0, 0));
27        frug_instance.present();
28    }
29}
More examples
Hide additional examples
examples/texture.rs (line 4)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    // load the spritesheet
8    let texture_creator = frug_instance.new_texture_creator();
9    let texture = match texture_creator.load_texture("examples/frog.png") {
10        Ok(image) => image,
11        Err(e) => {
12            eprintln!("Failed to load texture: {}", e);
13            return;
14        }
15    };
16
17    'running: loop {
18        // Input
19        for event in frug_instance.get_events() {
20            match event {
21                // Quit the application
22                Event::Quit { .. }
23                | Event::KeyDown {
24                    keycode: Some(Keycode::Escape),
25                    ..
26                } => break 'running,
27                _ => {}
28            }
29        }
30
31        // Render
32        frug_instance.clear(background_color);
33        frug_instance.draw_full_texture(
34            &texture,
35            &Vec2d { x: 200, y: 200 },
36            &Vec2d { x: 200, y: 200 },
37        );
38        frug_instance.present();
39    }
40}
examples/text.rs (line 4)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(70, 70, 100);
6
7    // load the font and create a texture creator
8    let texture_creator = frug_instance.new_texture_creator();
9    let ttf_context = frug_instance.new_ttf_context().unwrap();
10
11    // text settings
12    let font_scale = 7.0;
13    let font_size = 8.0 * font_scale;
14    let text = "FRUG!";
15    let text_color = Color::RGB(100, 255, 100);
16
17    // create the font
18    let font = match ttf_context.load_font("examples/PressStart2P-Regular.ttf", font_size) {
19        Ok(font) => font,
20        Err(e) => {
21            eprintln!("Failed to load font: {}", e);
22            return;
23        }
24    };
25
26    // create the text texture
27    let text_texture = match create_text_texture(&font, text, &text_color, &texture_creator) {
28        Ok(texture) => texture,
29        Err(e) => {
30            eprintln!("Failed to create text texture: {}", e);
31            return;
32        }
33    };
34
35    // get the dimensions of the text texture
36    let TextureQuery { width, height, .. } = text_texture.query();
37
38    'running: loop {
39        // Input
40        for event in frug_instance.get_events() {
41            match event {
42                // Quit the application
43                Event::Quit { .. }
44                | Event::KeyDown {
45                    keycode: Some(Keycode::Escape),
46                    ..
47                } => break 'running,
48                _ => {}
49            }
50        }
51
52        // Render
53        frug_instance.clear(background_color);
54        frug_instance.draw_full_texture(
55            &text_texture,
56            &Vec2d { x: 50, y: 200 },
57            &Vec2d {
58                x: width,
59                y: height,
60            },
61        );
62        frug_instance.present();
63    }
64}
examples/spritesheet.rs (line 10)
9fn main() {
10    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
11    let texture_creator = frug_instance.new_texture_creator();
12    let background_color = Color::RGB(100, 100, 150);
13
14    // load the spritesheet
15    texture_creator.default_pixel_format();
16    let mut texture = match texture_creator.load_texture("examples/platformer_imgs/frog/frogo.png")
17    {
18        Ok(image) => image,
19        Err(e) => {
20            eprintln!("Failed to load texture: {}", e);
21            return;
22        }
23    };
24    texture.set_scale_mode(ScaleMode::Nearest); // to avoid blurring
25
26    let mut sprite = Sprite::new(&texture, 2, vec![6, 4], 52, 50);
27
28    let sprite_pos = Vec2d { x: 250, y: 150 };
29    let sprite_scale = Vec2d { x: 4, y: 4 };
30    let mut current_animation = Animation::Idle;
31
32    'running: loop {
33        // Input
34        for event in frug_instance.get_events() {
35            match event {
36                // Quit the application
37                Event::Quit { .. }
38                | Event::KeyDown {
39                    keycode: Some(Keycode::Escape),
40                    ..
41                } => break 'running,
42                // Change animation when pressing space
43                Event::KeyDown {
44                    keycode: Some(Keycode::Space),
45                    ..
46                } => {
47                    if current_animation == Animation::Idle {
48                        current_animation = Animation::Walk;
49                    } else {
50                        current_animation = Animation::Idle;
51                    }
52                    let animation_u32 = current_animation.clone() as u32;
53                    sprite.start_animation(&animation_u32);
54                }
55                _ => {}
56            }
57        }
58
59        // Update
60        sprite.update();
61
62        // Render
63        frug_instance.clear(background_color);
64        frug_instance.draw_sprite(&sprite, &sprite_pos, &sprite_scale);
65        frug_instance.present();
66
67        std::thread::sleep(std::time::Duration::from_millis(100));
68    }
69}
examples/pong.rs (line 67)
61fn main() {
62    const WINDOW_WIDTH: u32 = 800;
63    const WINDOW_HEIGHT: u32 = 600;
64    const TARGET_FPS: u32 = 60;
65    const FRAME_TIME: f32 = 1000.0 / TARGET_FPS as f32;
66
67    let mut frug_instance = Instance::new("Pong", WINDOW_WIDTH, WINDOW_HEIGHT);
68    let background_color = Color::RGB(0, 0, 0);
69
70    // Create game items
71    let mut player = GameObject {
72        pos: Vec2d {
73            x: 50,
74            y: (WINDOW_HEIGHT / 2 - 80 / 2) as i32,
75        },
76        size: Vec2d { x: 10, y: 80 },
77        ..Default::default()
78    };
79    let mut enemy = GameObject {
80        pos: Vec2d {
81            x: (WINDOW_WIDTH - 50 - 10) as i32,
82            y: player.pos.y,
83        },
84        size: player.size.clone(),
85        ..Default::default()
86    };
87    let mut ball = GameObject {
88        pos: Vec2d {
89            x: (WINDOW_WIDTH / 2 - 5) as i32,
90            y: player.pos.y,
91        },
92        size: Vec2d { x: 10, y: 10 },
93        ..Default::default()
94    };
95
96    // Start ball
97    ball.vel.x = ball.speed;
98    ball.vel.y = -ball.speed;
99
100    // Player controls
101    let mut player_up = 0;
102    let mut player_down = 0;
103
104    'running: loop {
105        // Update time/fps control variables
106        let start_time = std::time::Instant::now();
107
108        // Input
109        for event in frug_instance.get_events() {
110            match event {
111                // Quit the application
112                Event::Quit { .. }
113                | Event::KeyDown {
114                    keycode: Some(Keycode::Escape),
115                    ..
116                } => break 'running,
117                Event::KeyDown {
118                    keycode: Some(Keycode::Up),
119                    ..
120                } => {
121                    player_up = 1;
122                }
123                Event::KeyUp {
124                    keycode: Some(Keycode::Up),
125                    ..
126                } => {
127                    player_up = 0;
128                }
129                Event::KeyDown {
130                    keycode: Some(Keycode::Down),
131                    ..
132                } => {
133                    player_down = 1;
134                }
135                Event::KeyUp {
136                    keycode: Some(Keycode::Down),
137                    ..
138                } => {
139                    player_down = 0;
140                }
141                _ => {}
142            }
143        }
144
145        // Control player
146        player.vel.y = (player_down - player_up) * player.speed;
147
148        // Control enemy
149        let ball_center_y = ball.get_center().y;
150        let enemy_center_y = enemy.get_center().y;
151        if ball_center_y < enemy_center_y {
152            enemy.vel.y = -enemy.speed;
153        } else if ball_center_y > enemy_center_y {
154            enemy.vel.y = enemy.speed;
155        }
156
157        // ** Ball collisions **
158        // Vertical borders
159        if ball.pos.y <= 0 || ball.pos.y + ball.size.y as i32 >= WINDOW_HEIGHT as i32 {
160            ball.vel.y *= -1;
161        }
162        // With pallets
163        ball.collide_w_object(&player);
164        ball.collide_w_object(&enemy);
165        // ** Ball collisions **
166
167        // Update
168        player.update();
169        enemy.update();
170        ball.update();
171        check_reset_ball(&mut ball, WINDOW_WIDTH as i32, WINDOW_HEIGHT as i32);
172
173        // Render
174        frug_instance.clear(background_color);
175        player.render(&mut frug_instance);
176        enemy.render(&mut frug_instance);
177        ball.render(&mut frug_instance);
178        frug_instance.present();
179
180        // Control FPS
181        let elapsed_time = start_time.elapsed();
182        let sleep_time = FRAME_TIME - elapsed_time.as_millis() as f32;
183        if sleep_time > 0.0 {
184            std::thread::sleep(std::time::Duration::from_millis(sleep_time as u64));
185        }
186    }
187}
Source

pub fn new_texture_creator(&self) -> TextureCreator<WindowContext>

Creates a texture creater which we can use to load textures.

Examples found in repository?
examples/texture.rs (line 8)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    // load the spritesheet
8    let texture_creator = frug_instance.new_texture_creator();
9    let texture = match texture_creator.load_texture("examples/frog.png") {
10        Ok(image) => image,
11        Err(e) => {
12            eprintln!("Failed to load texture: {}", e);
13            return;
14        }
15    };
16
17    'running: loop {
18        // Input
19        for event in frug_instance.get_events() {
20            match event {
21                // Quit the application
22                Event::Quit { .. }
23                | Event::KeyDown {
24                    keycode: Some(Keycode::Escape),
25                    ..
26                } => break 'running,
27                _ => {}
28            }
29        }
30
31        // Render
32        frug_instance.clear(background_color);
33        frug_instance.draw_full_texture(
34            &texture,
35            &Vec2d { x: 200, y: 200 },
36            &Vec2d { x: 200, y: 200 },
37        );
38        frug_instance.present();
39    }
40}
More examples
Hide additional examples
examples/text.rs (line 8)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(70, 70, 100);
6
7    // load the font and create a texture creator
8    let texture_creator = frug_instance.new_texture_creator();
9    let ttf_context = frug_instance.new_ttf_context().unwrap();
10
11    // text settings
12    let font_scale = 7.0;
13    let font_size = 8.0 * font_scale;
14    let text = "FRUG!";
15    let text_color = Color::RGB(100, 255, 100);
16
17    // create the font
18    let font = match ttf_context.load_font("examples/PressStart2P-Regular.ttf", font_size) {
19        Ok(font) => font,
20        Err(e) => {
21            eprintln!("Failed to load font: {}", e);
22            return;
23        }
24    };
25
26    // create the text texture
27    let text_texture = match create_text_texture(&font, text, &text_color, &texture_creator) {
28        Ok(texture) => texture,
29        Err(e) => {
30            eprintln!("Failed to create text texture: {}", e);
31            return;
32        }
33    };
34
35    // get the dimensions of the text texture
36    let TextureQuery { width, height, .. } = text_texture.query();
37
38    'running: loop {
39        // Input
40        for event in frug_instance.get_events() {
41            match event {
42                // Quit the application
43                Event::Quit { .. }
44                | Event::KeyDown {
45                    keycode: Some(Keycode::Escape),
46                    ..
47                } => break 'running,
48                _ => {}
49            }
50        }
51
52        // Render
53        frug_instance.clear(background_color);
54        frug_instance.draw_full_texture(
55            &text_texture,
56            &Vec2d { x: 50, y: 200 },
57            &Vec2d {
58                x: width,
59                y: height,
60            },
61        );
62        frug_instance.present();
63    }
64}
examples/spritesheet.rs (line 11)
9fn main() {
10    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
11    let texture_creator = frug_instance.new_texture_creator();
12    let background_color = Color::RGB(100, 100, 150);
13
14    // load the spritesheet
15    texture_creator.default_pixel_format();
16    let mut texture = match texture_creator.load_texture("examples/platformer_imgs/frog/frogo.png")
17    {
18        Ok(image) => image,
19        Err(e) => {
20            eprintln!("Failed to load texture: {}", e);
21            return;
22        }
23    };
24    texture.set_scale_mode(ScaleMode::Nearest); // to avoid blurring
25
26    let mut sprite = Sprite::new(&texture, 2, vec![6, 4], 52, 50);
27
28    let sprite_pos = Vec2d { x: 250, y: 150 };
29    let sprite_scale = Vec2d { x: 4, y: 4 };
30    let mut current_animation = Animation::Idle;
31
32    'running: loop {
33        // Input
34        for event in frug_instance.get_events() {
35            match event {
36                // Quit the application
37                Event::Quit { .. }
38                | Event::KeyDown {
39                    keycode: Some(Keycode::Escape),
40                    ..
41                } => break 'running,
42                // Change animation when pressing space
43                Event::KeyDown {
44                    keycode: Some(Keycode::Space),
45                    ..
46                } => {
47                    if current_animation == Animation::Idle {
48                        current_animation = Animation::Walk;
49                    } else {
50                        current_animation = Animation::Idle;
51                    }
52                    let animation_u32 = current_animation.clone() as u32;
53                    sprite.start_animation(&animation_u32);
54                }
55                _ => {}
56            }
57        }
58
59        // Update
60        sprite.update();
61
62        // Render
63        frug_instance.clear(background_color);
64        frug_instance.draw_sprite(&sprite, &sprite_pos, &sprite_scale);
65        frug_instance.present();
66
67        std::thread::sleep(std::time::Duration::from_millis(100));
68    }
69}
Source

pub fn new_ttf_context(&self) -> Result<Sdl3TtfContext, String>

Create a ttf context which we can use to load fonts. Returns an error if the context could not be created.

Examples found in repository?
examples/text.rs (line 9)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(70, 70, 100);
6
7    // load the font and create a texture creator
8    let texture_creator = frug_instance.new_texture_creator();
9    let ttf_context = frug_instance.new_ttf_context().unwrap();
10
11    // text settings
12    let font_scale = 7.0;
13    let font_size = 8.0 * font_scale;
14    let text = "FRUG!";
15    let text_color = Color::RGB(100, 255, 100);
16
17    // create the font
18    let font = match ttf_context.load_font("examples/PressStart2P-Regular.ttf", font_size) {
19        Ok(font) => font,
20        Err(e) => {
21            eprintln!("Failed to load font: {}", e);
22            return;
23        }
24    };
25
26    // create the text texture
27    let text_texture = match create_text_texture(&font, text, &text_color, &texture_creator) {
28        Ok(texture) => texture,
29        Err(e) => {
30            eprintln!("Failed to create text texture: {}", e);
31            return;
32        }
33    };
34
35    // get the dimensions of the text texture
36    let TextureQuery { width, height, .. } = text_texture.query();
37
38    'running: loop {
39        // Input
40        for event in frug_instance.get_events() {
41            match event {
42                // Quit the application
43                Event::Quit { .. }
44                | Event::KeyDown {
45                    keycode: Some(Keycode::Escape),
46                    ..
47                } => break 'running,
48                _ => {}
49            }
50        }
51
52        // Render
53        frug_instance.clear(background_color);
54        frug_instance.draw_full_texture(
55            &text_texture,
56            &Vec2d { x: 50, y: 200 },
57            &Vec2d {
58                x: width,
59                y: height,
60            },
61        );
62        frug_instance.present();
63    }
64}
Source

pub fn clear(&mut self, color: Color)

Clears the canvas with a given color

Examples found in repository?
examples/rectangle.rs (line 25)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    let rect_pos = Vec2d { x: 50, y: 50 };
8    let rect_size = Vec2d { x: 100, y: 70 };
9
10    'running: loop {
11        // Input
12        for event in frug_instance.get_events() {
13            match event {
14                // Quit the application
15                Event::Quit { .. }
16                | Event::KeyDown {
17                    keycode: Some(Keycode::Escape),
18                    ..
19                } => break 'running,
20                _ => {}
21            }
22        }
23
24        // Render
25        frug_instance.clear(background_color);
26        frug_instance.draw_rect(&rect_pos, &rect_size, Color::RGB(255, 0, 0));
27        frug_instance.present();
28    }
29}
More examples
Hide additional examples
examples/texture.rs (line 32)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    // load the spritesheet
8    let texture_creator = frug_instance.new_texture_creator();
9    let texture = match texture_creator.load_texture("examples/frog.png") {
10        Ok(image) => image,
11        Err(e) => {
12            eprintln!("Failed to load texture: {}", e);
13            return;
14        }
15    };
16
17    'running: loop {
18        // Input
19        for event in frug_instance.get_events() {
20            match event {
21                // Quit the application
22                Event::Quit { .. }
23                | Event::KeyDown {
24                    keycode: Some(Keycode::Escape),
25                    ..
26                } => break 'running,
27                _ => {}
28            }
29        }
30
31        // Render
32        frug_instance.clear(background_color);
33        frug_instance.draw_full_texture(
34            &texture,
35            &Vec2d { x: 200, y: 200 },
36            &Vec2d { x: 200, y: 200 },
37        );
38        frug_instance.present();
39    }
40}
examples/text.rs (line 53)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(70, 70, 100);
6
7    // load the font and create a texture creator
8    let texture_creator = frug_instance.new_texture_creator();
9    let ttf_context = frug_instance.new_ttf_context().unwrap();
10
11    // text settings
12    let font_scale = 7.0;
13    let font_size = 8.0 * font_scale;
14    let text = "FRUG!";
15    let text_color = Color::RGB(100, 255, 100);
16
17    // create the font
18    let font = match ttf_context.load_font("examples/PressStart2P-Regular.ttf", font_size) {
19        Ok(font) => font,
20        Err(e) => {
21            eprintln!("Failed to load font: {}", e);
22            return;
23        }
24    };
25
26    // create the text texture
27    let text_texture = match create_text_texture(&font, text, &text_color, &texture_creator) {
28        Ok(texture) => texture,
29        Err(e) => {
30            eprintln!("Failed to create text texture: {}", e);
31            return;
32        }
33    };
34
35    // get the dimensions of the text texture
36    let TextureQuery { width, height, .. } = text_texture.query();
37
38    'running: loop {
39        // Input
40        for event in frug_instance.get_events() {
41            match event {
42                // Quit the application
43                Event::Quit { .. }
44                | Event::KeyDown {
45                    keycode: Some(Keycode::Escape),
46                    ..
47                } => break 'running,
48                _ => {}
49            }
50        }
51
52        // Render
53        frug_instance.clear(background_color);
54        frug_instance.draw_full_texture(
55            &text_texture,
56            &Vec2d { x: 50, y: 200 },
57            &Vec2d {
58                x: width,
59                y: height,
60            },
61        );
62        frug_instance.present();
63    }
64}
examples/spritesheet.rs (line 63)
9fn main() {
10    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
11    let texture_creator = frug_instance.new_texture_creator();
12    let background_color = Color::RGB(100, 100, 150);
13
14    // load the spritesheet
15    texture_creator.default_pixel_format();
16    let mut texture = match texture_creator.load_texture("examples/platformer_imgs/frog/frogo.png")
17    {
18        Ok(image) => image,
19        Err(e) => {
20            eprintln!("Failed to load texture: {}", e);
21            return;
22        }
23    };
24    texture.set_scale_mode(ScaleMode::Nearest); // to avoid blurring
25
26    let mut sprite = Sprite::new(&texture, 2, vec![6, 4], 52, 50);
27
28    let sprite_pos = Vec2d { x: 250, y: 150 };
29    let sprite_scale = Vec2d { x: 4, y: 4 };
30    let mut current_animation = Animation::Idle;
31
32    'running: loop {
33        // Input
34        for event in frug_instance.get_events() {
35            match event {
36                // Quit the application
37                Event::Quit { .. }
38                | Event::KeyDown {
39                    keycode: Some(Keycode::Escape),
40                    ..
41                } => break 'running,
42                // Change animation when pressing space
43                Event::KeyDown {
44                    keycode: Some(Keycode::Space),
45                    ..
46                } => {
47                    if current_animation == Animation::Idle {
48                        current_animation = Animation::Walk;
49                    } else {
50                        current_animation = Animation::Idle;
51                    }
52                    let animation_u32 = current_animation.clone() as u32;
53                    sprite.start_animation(&animation_u32);
54                }
55                _ => {}
56            }
57        }
58
59        // Update
60        sprite.update();
61
62        // Render
63        frug_instance.clear(background_color);
64        frug_instance.draw_sprite(&sprite, &sprite_pos, &sprite_scale);
65        frug_instance.present();
66
67        std::thread::sleep(std::time::Duration::from_millis(100));
68    }
69}
examples/pong.rs (line 174)
61fn main() {
62    const WINDOW_WIDTH: u32 = 800;
63    const WINDOW_HEIGHT: u32 = 600;
64    const TARGET_FPS: u32 = 60;
65    const FRAME_TIME: f32 = 1000.0 / TARGET_FPS as f32;
66
67    let mut frug_instance = Instance::new("Pong", WINDOW_WIDTH, WINDOW_HEIGHT);
68    let background_color = Color::RGB(0, 0, 0);
69
70    // Create game items
71    let mut player = GameObject {
72        pos: Vec2d {
73            x: 50,
74            y: (WINDOW_HEIGHT / 2 - 80 / 2) as i32,
75        },
76        size: Vec2d { x: 10, y: 80 },
77        ..Default::default()
78    };
79    let mut enemy = GameObject {
80        pos: Vec2d {
81            x: (WINDOW_WIDTH - 50 - 10) as i32,
82            y: player.pos.y,
83        },
84        size: player.size.clone(),
85        ..Default::default()
86    };
87    let mut ball = GameObject {
88        pos: Vec2d {
89            x: (WINDOW_WIDTH / 2 - 5) as i32,
90            y: player.pos.y,
91        },
92        size: Vec2d { x: 10, y: 10 },
93        ..Default::default()
94    };
95
96    // Start ball
97    ball.vel.x = ball.speed;
98    ball.vel.y = -ball.speed;
99
100    // Player controls
101    let mut player_up = 0;
102    let mut player_down = 0;
103
104    'running: loop {
105        // Update time/fps control variables
106        let start_time = std::time::Instant::now();
107
108        // Input
109        for event in frug_instance.get_events() {
110            match event {
111                // Quit the application
112                Event::Quit { .. }
113                | Event::KeyDown {
114                    keycode: Some(Keycode::Escape),
115                    ..
116                } => break 'running,
117                Event::KeyDown {
118                    keycode: Some(Keycode::Up),
119                    ..
120                } => {
121                    player_up = 1;
122                }
123                Event::KeyUp {
124                    keycode: Some(Keycode::Up),
125                    ..
126                } => {
127                    player_up = 0;
128                }
129                Event::KeyDown {
130                    keycode: Some(Keycode::Down),
131                    ..
132                } => {
133                    player_down = 1;
134                }
135                Event::KeyUp {
136                    keycode: Some(Keycode::Down),
137                    ..
138                } => {
139                    player_down = 0;
140                }
141                _ => {}
142            }
143        }
144
145        // Control player
146        player.vel.y = (player_down - player_up) * player.speed;
147
148        // Control enemy
149        let ball_center_y = ball.get_center().y;
150        let enemy_center_y = enemy.get_center().y;
151        if ball_center_y < enemy_center_y {
152            enemy.vel.y = -enemy.speed;
153        } else if ball_center_y > enemy_center_y {
154            enemy.vel.y = enemy.speed;
155        }
156
157        // ** Ball collisions **
158        // Vertical borders
159        if ball.pos.y <= 0 || ball.pos.y + ball.size.y as i32 >= WINDOW_HEIGHT as i32 {
160            ball.vel.y *= -1;
161        }
162        // With pallets
163        ball.collide_w_object(&player);
164        ball.collide_w_object(&enemy);
165        // ** Ball collisions **
166
167        // Update
168        player.update();
169        enemy.update();
170        ball.update();
171        check_reset_ball(&mut ball, WINDOW_WIDTH as i32, WINDOW_HEIGHT as i32);
172
173        // Render
174        frug_instance.clear(background_color);
175        player.render(&mut frug_instance);
176        enemy.render(&mut frug_instance);
177        ball.render(&mut frug_instance);
178        frug_instance.present();
179
180        // Control FPS
181        let elapsed_time = start_time.elapsed();
182        let sleep_time = FRAME_TIME - elapsed_time.as_millis() as f32;
183        if sleep_time > 0.0 {
184            std::thread::sleep(std::time::Duration::from_millis(sleep_time as u64));
185        }
186    }
187}
Source

pub fn present(&mut self)

Renders all textures/shapes drawn into the canvas since it was last cleaned.

Examples found in repository?
examples/rectangle.rs (line 27)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    let rect_pos = Vec2d { x: 50, y: 50 };
8    let rect_size = Vec2d { x: 100, y: 70 };
9
10    'running: loop {
11        // Input
12        for event in frug_instance.get_events() {
13            match event {
14                // Quit the application
15                Event::Quit { .. }
16                | Event::KeyDown {
17                    keycode: Some(Keycode::Escape),
18                    ..
19                } => break 'running,
20                _ => {}
21            }
22        }
23
24        // Render
25        frug_instance.clear(background_color);
26        frug_instance.draw_rect(&rect_pos, &rect_size, Color::RGB(255, 0, 0));
27        frug_instance.present();
28    }
29}
More examples
Hide additional examples
examples/texture.rs (line 38)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    // load the spritesheet
8    let texture_creator = frug_instance.new_texture_creator();
9    let texture = match texture_creator.load_texture("examples/frog.png") {
10        Ok(image) => image,
11        Err(e) => {
12            eprintln!("Failed to load texture: {}", e);
13            return;
14        }
15    };
16
17    'running: loop {
18        // Input
19        for event in frug_instance.get_events() {
20            match event {
21                // Quit the application
22                Event::Quit { .. }
23                | Event::KeyDown {
24                    keycode: Some(Keycode::Escape),
25                    ..
26                } => break 'running,
27                _ => {}
28            }
29        }
30
31        // Render
32        frug_instance.clear(background_color);
33        frug_instance.draw_full_texture(
34            &texture,
35            &Vec2d { x: 200, y: 200 },
36            &Vec2d { x: 200, y: 200 },
37        );
38        frug_instance.present();
39    }
40}
examples/text.rs (line 62)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(70, 70, 100);
6
7    // load the font and create a texture creator
8    let texture_creator = frug_instance.new_texture_creator();
9    let ttf_context = frug_instance.new_ttf_context().unwrap();
10
11    // text settings
12    let font_scale = 7.0;
13    let font_size = 8.0 * font_scale;
14    let text = "FRUG!";
15    let text_color = Color::RGB(100, 255, 100);
16
17    // create the font
18    let font = match ttf_context.load_font("examples/PressStart2P-Regular.ttf", font_size) {
19        Ok(font) => font,
20        Err(e) => {
21            eprintln!("Failed to load font: {}", e);
22            return;
23        }
24    };
25
26    // create the text texture
27    let text_texture = match create_text_texture(&font, text, &text_color, &texture_creator) {
28        Ok(texture) => texture,
29        Err(e) => {
30            eprintln!("Failed to create text texture: {}", e);
31            return;
32        }
33    };
34
35    // get the dimensions of the text texture
36    let TextureQuery { width, height, .. } = text_texture.query();
37
38    'running: loop {
39        // Input
40        for event in frug_instance.get_events() {
41            match event {
42                // Quit the application
43                Event::Quit { .. }
44                | Event::KeyDown {
45                    keycode: Some(Keycode::Escape),
46                    ..
47                } => break 'running,
48                _ => {}
49            }
50        }
51
52        // Render
53        frug_instance.clear(background_color);
54        frug_instance.draw_full_texture(
55            &text_texture,
56            &Vec2d { x: 50, y: 200 },
57            &Vec2d {
58                x: width,
59                y: height,
60            },
61        );
62        frug_instance.present();
63    }
64}
examples/spritesheet.rs (line 65)
9fn main() {
10    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
11    let texture_creator = frug_instance.new_texture_creator();
12    let background_color = Color::RGB(100, 100, 150);
13
14    // load the spritesheet
15    texture_creator.default_pixel_format();
16    let mut texture = match texture_creator.load_texture("examples/platformer_imgs/frog/frogo.png")
17    {
18        Ok(image) => image,
19        Err(e) => {
20            eprintln!("Failed to load texture: {}", e);
21            return;
22        }
23    };
24    texture.set_scale_mode(ScaleMode::Nearest); // to avoid blurring
25
26    let mut sprite = Sprite::new(&texture, 2, vec![6, 4], 52, 50);
27
28    let sprite_pos = Vec2d { x: 250, y: 150 };
29    let sprite_scale = Vec2d { x: 4, y: 4 };
30    let mut current_animation = Animation::Idle;
31
32    'running: loop {
33        // Input
34        for event in frug_instance.get_events() {
35            match event {
36                // Quit the application
37                Event::Quit { .. }
38                | Event::KeyDown {
39                    keycode: Some(Keycode::Escape),
40                    ..
41                } => break 'running,
42                // Change animation when pressing space
43                Event::KeyDown {
44                    keycode: Some(Keycode::Space),
45                    ..
46                } => {
47                    if current_animation == Animation::Idle {
48                        current_animation = Animation::Walk;
49                    } else {
50                        current_animation = Animation::Idle;
51                    }
52                    let animation_u32 = current_animation.clone() as u32;
53                    sprite.start_animation(&animation_u32);
54                }
55                _ => {}
56            }
57        }
58
59        // Update
60        sprite.update();
61
62        // Render
63        frug_instance.clear(background_color);
64        frug_instance.draw_sprite(&sprite, &sprite_pos, &sprite_scale);
65        frug_instance.present();
66
67        std::thread::sleep(std::time::Duration::from_millis(100));
68    }
69}
examples/pong.rs (line 178)
61fn main() {
62    const WINDOW_WIDTH: u32 = 800;
63    const WINDOW_HEIGHT: u32 = 600;
64    const TARGET_FPS: u32 = 60;
65    const FRAME_TIME: f32 = 1000.0 / TARGET_FPS as f32;
66
67    let mut frug_instance = Instance::new("Pong", WINDOW_WIDTH, WINDOW_HEIGHT);
68    let background_color = Color::RGB(0, 0, 0);
69
70    // Create game items
71    let mut player = GameObject {
72        pos: Vec2d {
73            x: 50,
74            y: (WINDOW_HEIGHT / 2 - 80 / 2) as i32,
75        },
76        size: Vec2d { x: 10, y: 80 },
77        ..Default::default()
78    };
79    let mut enemy = GameObject {
80        pos: Vec2d {
81            x: (WINDOW_WIDTH - 50 - 10) as i32,
82            y: player.pos.y,
83        },
84        size: player.size.clone(),
85        ..Default::default()
86    };
87    let mut ball = GameObject {
88        pos: Vec2d {
89            x: (WINDOW_WIDTH / 2 - 5) as i32,
90            y: player.pos.y,
91        },
92        size: Vec2d { x: 10, y: 10 },
93        ..Default::default()
94    };
95
96    // Start ball
97    ball.vel.x = ball.speed;
98    ball.vel.y = -ball.speed;
99
100    // Player controls
101    let mut player_up = 0;
102    let mut player_down = 0;
103
104    'running: loop {
105        // Update time/fps control variables
106        let start_time = std::time::Instant::now();
107
108        // Input
109        for event in frug_instance.get_events() {
110            match event {
111                // Quit the application
112                Event::Quit { .. }
113                | Event::KeyDown {
114                    keycode: Some(Keycode::Escape),
115                    ..
116                } => break 'running,
117                Event::KeyDown {
118                    keycode: Some(Keycode::Up),
119                    ..
120                } => {
121                    player_up = 1;
122                }
123                Event::KeyUp {
124                    keycode: Some(Keycode::Up),
125                    ..
126                } => {
127                    player_up = 0;
128                }
129                Event::KeyDown {
130                    keycode: Some(Keycode::Down),
131                    ..
132                } => {
133                    player_down = 1;
134                }
135                Event::KeyUp {
136                    keycode: Some(Keycode::Down),
137                    ..
138                } => {
139                    player_down = 0;
140                }
141                _ => {}
142            }
143        }
144
145        // Control player
146        player.vel.y = (player_down - player_up) * player.speed;
147
148        // Control enemy
149        let ball_center_y = ball.get_center().y;
150        let enemy_center_y = enemy.get_center().y;
151        if ball_center_y < enemy_center_y {
152            enemy.vel.y = -enemy.speed;
153        } else if ball_center_y > enemy_center_y {
154            enemy.vel.y = enemy.speed;
155        }
156
157        // ** Ball collisions **
158        // Vertical borders
159        if ball.pos.y <= 0 || ball.pos.y + ball.size.y as i32 >= WINDOW_HEIGHT as i32 {
160            ball.vel.y *= -1;
161        }
162        // With pallets
163        ball.collide_w_object(&player);
164        ball.collide_w_object(&enemy);
165        // ** Ball collisions **
166
167        // Update
168        player.update();
169        enemy.update();
170        ball.update();
171        check_reset_ball(&mut ball, WINDOW_WIDTH as i32, WINDOW_HEIGHT as i32);
172
173        // Render
174        frug_instance.clear(background_color);
175        player.render(&mut frug_instance);
176        enemy.render(&mut frug_instance);
177        ball.render(&mut frug_instance);
178        frug_instance.present();
179
180        // Control FPS
181        let elapsed_time = start_time.elapsed();
182        let sleep_time = FRAME_TIME - elapsed_time.as_millis() as f32;
183        if sleep_time > 0.0 {
184            std::thread::sleep(std::time::Duration::from_millis(sleep_time as u64));
185        }
186    }
187}
Source

pub fn draw_rect(&mut self, pos: &Vec2d<i32>, size: &Vec2d<u32>, color: Color)

Draws a rectangle given the position, size, and color pos - Defines the position of the rectangle size - Defines the dimensions of the rectangle (x = width, y = height) color - Color

Examples found in repository?
examples/pong.rs (line 24)
23    fn render(&self, instance: &mut Instance) {
24        instance.draw_rect(&self.pos, &self.size, self.color);
25    }
More examples
Hide additional examples
examples/rectangle.rs (line 26)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    let rect_pos = Vec2d { x: 50, y: 50 };
8    let rect_size = Vec2d { x: 100, y: 70 };
9
10    'running: loop {
11        // Input
12        for event in frug_instance.get_events() {
13            match event {
14                // Quit the application
15                Event::Quit { .. }
16                | Event::KeyDown {
17                    keycode: Some(Keycode::Escape),
18                    ..
19                } => break 'running,
20                _ => {}
21            }
22        }
23
24        // Render
25        frug_instance.clear(background_color);
26        frug_instance.draw_rect(&rect_pos, &rect_size, Color::RGB(255, 0, 0));
27        frug_instance.present();
28    }
29}
Source

pub fn draw_full_texture( &mut self, texture: &Texture<'_>, pos: &Vec2d<i32>, size: &Vec2d<u32>, )

Draws a texture texture - Defines the texture to use. pos - Defines the position of the image to draw. size - Defines the dimensions of the image rectangle (x = width, y = height)

Examples found in repository?
examples/texture.rs (lines 33-37)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    // load the spritesheet
8    let texture_creator = frug_instance.new_texture_creator();
9    let texture = match texture_creator.load_texture("examples/frog.png") {
10        Ok(image) => image,
11        Err(e) => {
12            eprintln!("Failed to load texture: {}", e);
13            return;
14        }
15    };
16
17    'running: loop {
18        // Input
19        for event in frug_instance.get_events() {
20            match event {
21                // Quit the application
22                Event::Quit { .. }
23                | Event::KeyDown {
24                    keycode: Some(Keycode::Escape),
25                    ..
26                } => break 'running,
27                _ => {}
28            }
29        }
30
31        // Render
32        frug_instance.clear(background_color);
33        frug_instance.draw_full_texture(
34            &texture,
35            &Vec2d { x: 200, y: 200 },
36            &Vec2d { x: 200, y: 200 },
37        );
38        frug_instance.present();
39    }
40}
More examples
Hide additional examples
examples/text.rs (lines 54-61)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(70, 70, 100);
6
7    // load the font and create a texture creator
8    let texture_creator = frug_instance.new_texture_creator();
9    let ttf_context = frug_instance.new_ttf_context().unwrap();
10
11    // text settings
12    let font_scale = 7.0;
13    let font_size = 8.0 * font_scale;
14    let text = "FRUG!";
15    let text_color = Color::RGB(100, 255, 100);
16
17    // create the font
18    let font = match ttf_context.load_font("examples/PressStart2P-Regular.ttf", font_size) {
19        Ok(font) => font,
20        Err(e) => {
21            eprintln!("Failed to load font: {}", e);
22            return;
23        }
24    };
25
26    // create the text texture
27    let text_texture = match create_text_texture(&font, text, &text_color, &texture_creator) {
28        Ok(texture) => texture,
29        Err(e) => {
30            eprintln!("Failed to create text texture: {}", e);
31            return;
32        }
33    };
34
35    // get the dimensions of the text texture
36    let TextureQuery { width, height, .. } = text_texture.query();
37
38    'running: loop {
39        // Input
40        for event in frug_instance.get_events() {
41            match event {
42                // Quit the application
43                Event::Quit { .. }
44                | Event::KeyDown {
45                    keycode: Some(Keycode::Escape),
46                    ..
47                } => break 'running,
48                _ => {}
49            }
50        }
51
52        // Render
53        frug_instance.clear(background_color);
54        frug_instance.draw_full_texture(
55            &text_texture,
56            &Vec2d { x: 50, y: 200 },
57            &Vec2d {
58                x: width,
59                y: height,
60            },
61        );
62        frug_instance.present();
63    }
64}
Source

pub fn draw_texture( &mut self, texture: &Texture<'_>, src_pos: &Vec2d<i32>, src_dimensions: &Vec2d<u32>, dest_pos: &Vec2d<i32>, dest_dimensions: &Vec2d<u32>, )

Draws from a texture into a destination rectangle in the canvas. texture - Defines the texture to use. src_pos - Defines the starting point of the rectangular section of the texture to draw onto the canvas. src_dimensions - Defines the dimensions of the rectangular section of the texture to draw onto the canvas. dest_pos - Defines the starting point of the rectangular section of the canvas where the texture will be drawn. dest_dimensions - Defines the dimensions of the rectangular section of the canvas where the texture will be drawn.

Source

pub fn draw_sprite( &mut self, sprite: &Sprite<'_>, position: &Vec2d<i32>, scale: &Vec2d<u32>, )

Draws a given sprite in its current frame. sprite - The sprite object to use. position - Defines the position on the canvas where we want to draw the sprite. scale - Defines the scale of the sprite to draw.

Examples found in repository?
examples/spritesheet.rs (line 64)
9fn main() {
10    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
11    let texture_creator = frug_instance.new_texture_creator();
12    let background_color = Color::RGB(100, 100, 150);
13
14    // load the spritesheet
15    texture_creator.default_pixel_format();
16    let mut texture = match texture_creator.load_texture("examples/platformer_imgs/frog/frogo.png")
17    {
18        Ok(image) => image,
19        Err(e) => {
20            eprintln!("Failed to load texture: {}", e);
21            return;
22        }
23    };
24    texture.set_scale_mode(ScaleMode::Nearest); // to avoid blurring
25
26    let mut sprite = Sprite::new(&texture, 2, vec![6, 4], 52, 50);
27
28    let sprite_pos = Vec2d { x: 250, y: 150 };
29    let sprite_scale = Vec2d { x: 4, y: 4 };
30    let mut current_animation = Animation::Idle;
31
32    'running: loop {
33        // Input
34        for event in frug_instance.get_events() {
35            match event {
36                // Quit the application
37                Event::Quit { .. }
38                | Event::KeyDown {
39                    keycode: Some(Keycode::Escape),
40                    ..
41                } => break 'running,
42                // Change animation when pressing space
43                Event::KeyDown {
44                    keycode: Some(Keycode::Space),
45                    ..
46                } => {
47                    if current_animation == Animation::Idle {
48                        current_animation = Animation::Walk;
49                    } else {
50                        current_animation = Animation::Idle;
51                    }
52                    let animation_u32 = current_animation.clone() as u32;
53                    sprite.start_animation(&animation_u32);
54                }
55                _ => {}
56            }
57        }
58
59        // Update
60        sprite.update();
61
62        // Render
63        frug_instance.clear(background_color);
64        frug_instance.draw_sprite(&sprite, &sprite_pos, &sprite_scale);
65        frug_instance.present();
66
67        std::thread::sleep(std::time::Duration::from_millis(100));
68    }
69}
Source

pub fn get_events(&mut self) -> Vec<Event>

Returns a vector containing all the events captured during the current call of this method.

Examples found in repository?
examples/rectangle.rs (line 12)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    let rect_pos = Vec2d { x: 50, y: 50 };
8    let rect_size = Vec2d { x: 100, y: 70 };
9
10    'running: loop {
11        // Input
12        for event in frug_instance.get_events() {
13            match event {
14                // Quit the application
15                Event::Quit { .. }
16                | Event::KeyDown {
17                    keycode: Some(Keycode::Escape),
18                    ..
19                } => break 'running,
20                _ => {}
21            }
22        }
23
24        // Render
25        frug_instance.clear(background_color);
26        frug_instance.draw_rect(&rect_pos, &rect_size, Color::RGB(255, 0, 0));
27        frug_instance.present();
28    }
29}
More examples
Hide additional examples
examples/texture.rs (line 19)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(100, 100, 150);
6
7    // load the spritesheet
8    let texture_creator = frug_instance.new_texture_creator();
9    let texture = match texture_creator.load_texture("examples/frog.png") {
10        Ok(image) => image,
11        Err(e) => {
12            eprintln!("Failed to load texture: {}", e);
13            return;
14        }
15    };
16
17    'running: loop {
18        // Input
19        for event in frug_instance.get_events() {
20            match event {
21                // Quit the application
22                Event::Quit { .. }
23                | Event::KeyDown {
24                    keycode: Some(Keycode::Escape),
25                    ..
26                } => break 'running,
27                _ => {}
28            }
29        }
30
31        // Render
32        frug_instance.clear(background_color);
33        frug_instance.draw_full_texture(
34            &texture,
35            &Vec2d { x: 200, y: 200 },
36            &Vec2d { x: 200, y: 200 },
37        );
38        frug_instance.present();
39    }
40}
examples/text.rs (line 40)
3fn main() {
4    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5    let background_color = Color::RGB(70, 70, 100);
6
7    // load the font and create a texture creator
8    let texture_creator = frug_instance.new_texture_creator();
9    let ttf_context = frug_instance.new_ttf_context().unwrap();
10
11    // text settings
12    let font_scale = 7.0;
13    let font_size = 8.0 * font_scale;
14    let text = "FRUG!";
15    let text_color = Color::RGB(100, 255, 100);
16
17    // create the font
18    let font = match ttf_context.load_font("examples/PressStart2P-Regular.ttf", font_size) {
19        Ok(font) => font,
20        Err(e) => {
21            eprintln!("Failed to load font: {}", e);
22            return;
23        }
24    };
25
26    // create the text texture
27    let text_texture = match create_text_texture(&font, text, &text_color, &texture_creator) {
28        Ok(texture) => texture,
29        Err(e) => {
30            eprintln!("Failed to create text texture: {}", e);
31            return;
32        }
33    };
34
35    // get the dimensions of the text texture
36    let TextureQuery { width, height, .. } = text_texture.query();
37
38    'running: loop {
39        // Input
40        for event in frug_instance.get_events() {
41            match event {
42                // Quit the application
43                Event::Quit { .. }
44                | Event::KeyDown {
45                    keycode: Some(Keycode::Escape),
46                    ..
47                } => break 'running,
48                _ => {}
49            }
50        }
51
52        // Render
53        frug_instance.clear(background_color);
54        frug_instance.draw_full_texture(
55            &text_texture,
56            &Vec2d { x: 50, y: 200 },
57            &Vec2d {
58                x: width,
59                y: height,
60            },
61        );
62        frug_instance.present();
63    }
64}
examples/spritesheet.rs (line 34)
9fn main() {
10    let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
11    let texture_creator = frug_instance.new_texture_creator();
12    let background_color = Color::RGB(100, 100, 150);
13
14    // load the spritesheet
15    texture_creator.default_pixel_format();
16    let mut texture = match texture_creator.load_texture("examples/platformer_imgs/frog/frogo.png")
17    {
18        Ok(image) => image,
19        Err(e) => {
20            eprintln!("Failed to load texture: {}", e);
21            return;
22        }
23    };
24    texture.set_scale_mode(ScaleMode::Nearest); // to avoid blurring
25
26    let mut sprite = Sprite::new(&texture, 2, vec![6, 4], 52, 50);
27
28    let sprite_pos = Vec2d { x: 250, y: 150 };
29    let sprite_scale = Vec2d { x: 4, y: 4 };
30    let mut current_animation = Animation::Idle;
31
32    'running: loop {
33        // Input
34        for event in frug_instance.get_events() {
35            match event {
36                // Quit the application
37                Event::Quit { .. }
38                | Event::KeyDown {
39                    keycode: Some(Keycode::Escape),
40                    ..
41                } => break 'running,
42                // Change animation when pressing space
43                Event::KeyDown {
44                    keycode: Some(Keycode::Space),
45                    ..
46                } => {
47                    if current_animation == Animation::Idle {
48                        current_animation = Animation::Walk;
49                    } else {
50                        current_animation = Animation::Idle;
51                    }
52                    let animation_u32 = current_animation.clone() as u32;
53                    sprite.start_animation(&animation_u32);
54                }
55                _ => {}
56            }
57        }
58
59        // Update
60        sprite.update();
61
62        // Render
63        frug_instance.clear(background_color);
64        frug_instance.draw_sprite(&sprite, &sprite_pos, &sprite_scale);
65        frug_instance.present();
66
67        std::thread::sleep(std::time::Duration::from_millis(100));
68    }
69}
examples/pong.rs (line 109)
61fn main() {
62    const WINDOW_WIDTH: u32 = 800;
63    const WINDOW_HEIGHT: u32 = 600;
64    const TARGET_FPS: u32 = 60;
65    const FRAME_TIME: f32 = 1000.0 / TARGET_FPS as f32;
66
67    let mut frug_instance = Instance::new("Pong", WINDOW_WIDTH, WINDOW_HEIGHT);
68    let background_color = Color::RGB(0, 0, 0);
69
70    // Create game items
71    let mut player = GameObject {
72        pos: Vec2d {
73            x: 50,
74            y: (WINDOW_HEIGHT / 2 - 80 / 2) as i32,
75        },
76        size: Vec2d { x: 10, y: 80 },
77        ..Default::default()
78    };
79    let mut enemy = GameObject {
80        pos: Vec2d {
81            x: (WINDOW_WIDTH - 50 - 10) as i32,
82            y: player.pos.y,
83        },
84        size: player.size.clone(),
85        ..Default::default()
86    };
87    let mut ball = GameObject {
88        pos: Vec2d {
89            x: (WINDOW_WIDTH / 2 - 5) as i32,
90            y: player.pos.y,
91        },
92        size: Vec2d { x: 10, y: 10 },
93        ..Default::default()
94    };
95
96    // Start ball
97    ball.vel.x = ball.speed;
98    ball.vel.y = -ball.speed;
99
100    // Player controls
101    let mut player_up = 0;
102    let mut player_down = 0;
103
104    'running: loop {
105        // Update time/fps control variables
106        let start_time = std::time::Instant::now();
107
108        // Input
109        for event in frug_instance.get_events() {
110            match event {
111                // Quit the application
112                Event::Quit { .. }
113                | Event::KeyDown {
114                    keycode: Some(Keycode::Escape),
115                    ..
116                } => break 'running,
117                Event::KeyDown {
118                    keycode: Some(Keycode::Up),
119                    ..
120                } => {
121                    player_up = 1;
122                }
123                Event::KeyUp {
124                    keycode: Some(Keycode::Up),
125                    ..
126                } => {
127                    player_up = 0;
128                }
129                Event::KeyDown {
130                    keycode: Some(Keycode::Down),
131                    ..
132                } => {
133                    player_down = 1;
134                }
135                Event::KeyUp {
136                    keycode: Some(Keycode::Down),
137                    ..
138                } => {
139                    player_down = 0;
140                }
141                _ => {}
142            }
143        }
144
145        // Control player
146        player.vel.y = (player_down - player_up) * player.speed;
147
148        // Control enemy
149        let ball_center_y = ball.get_center().y;
150        let enemy_center_y = enemy.get_center().y;
151        if ball_center_y < enemy_center_y {
152            enemy.vel.y = -enemy.speed;
153        } else if ball_center_y > enemy_center_y {
154            enemy.vel.y = enemy.speed;
155        }
156
157        // ** Ball collisions **
158        // Vertical borders
159        if ball.pos.y <= 0 || ball.pos.y + ball.size.y as i32 >= WINDOW_HEIGHT as i32 {
160            ball.vel.y *= -1;
161        }
162        // With pallets
163        ball.collide_w_object(&player);
164        ball.collide_w_object(&enemy);
165        // ** Ball collisions **
166
167        // Update
168        player.update();
169        enemy.update();
170        ball.update();
171        check_reset_ball(&mut ball, WINDOW_WIDTH as i32, WINDOW_HEIGHT as i32);
172
173        // Render
174        frug_instance.clear(background_color);
175        player.render(&mut frug_instance);
176        enemy.render(&mut frug_instance);
177        ball.render(&mut frug_instance);
178        frug_instance.present();
179
180        // Control FPS
181        let elapsed_time = start_time.elapsed();
182        let sleep_time = FRAME_TIME - elapsed_time.as_millis() as f32;
183        if sleep_time > 0.0 {
184            std::thread::sleep(std::time::Duration::from_millis(sleep_time as u64));
185        }
186    }
187}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.