ball_framebuffer/
ball_framebuffer.rs

1// Copyright (c) 2025 nytpu <alex [at] nytpu.com>
2// SPDX-License-Identifier: MPL-2.0
3// For more license details, see LICENSE or <https://www.mozilla.org/en-US/MPL/2.0/>.
4
5//! Example showing usage of a double-buffered `BltDrawTarget`, by animating a ball bouncing around
6//! the screen.
7
8#![no_main]
9#![no_std]
10
11use core::time::Duration;
12use embedded_graphics::{
13	pixelcolor::Rgb888,
14	prelude::*,
15	primitives::{Circle, PrimitiveStyle, Rectangle},
16};
17use embedded_graphics_gop::fb::FbDbDrawTarget;
18use uefi::{helpers, prelude::*, proto::console::gop::GraphicsOutput, runtime::ResetType};
19
20#[entry]
21fn main() -> Status {
22	helpers::init().unwrap();
23
24	let handle =
25		boot::get_handle_for_protocol::<GraphicsOutput>().expect("Unable to get GOP handle");
26	let mut gop =
27		boot::open_protocol_exclusive::<GraphicsOutput>(handle).expect("Unable to open GOP handle");
28
29	let mode_800x600 = gop
30		.modes()
31		// one of the standard modes, although some systems may only provide 640×480
32		.find(|m| m.info().resolution() == (800, 600))
33		.expect("Unable to find 800x600 video mode");
34	gop.set_mode(&mode_800x600)
35		.expect("Couldn't switch to 800x600 video mode");
36
37	let mut target = FbDbDrawTarget::new(&mut gop);
38
39	let red_fill = PrimitiveStyle::with_fill(Rgb888::RED);
40	let blue_fill = PrimitiveStyle::with_fill(Rgb888::BLUE);
41
42	let mut ball_pos = Point::new(200, 300);
43	let mut ball_dir = Point::new(5, 5);
44	let ball_size = 10;
45	for _ in 0..1000 {
46		// Drawing over old position with blue instead of black to leave trail behind and
47		// demonstrate more colors
48		Rectangle::new(ball_pos, Size::new(ball_size, ball_size))
49			.into_styled(blue_fill)
50			.draw(&mut target)
51			.expect("infallible");
52
53		ball_pos += ball_dir;
54
55		if ball_pos.x <= 0 {
56			ball_pos.x = 0;
57			ball_dir.x = -ball_dir.x;
58		} else if ball_pos.x >= 790 {
59			ball_pos.x = 790;
60			ball_dir.x = -ball_dir.x;
61		}
62		if ball_pos.y <= 0 {
63			ball_pos.y = 0;
64			ball_dir.y = -ball_dir.y;
65		} else if ball_pos.y >= 590 {
66			ball_pos.y = 590;
67			ball_dir.y = -ball_dir.y;
68		}
69
70		Circle::new(ball_pos, 10)
71			.into_styled(red_fill)
72			.draw(&mut target)
73			.expect("infallible");
74
75		// In real code would probably want to set up UEFI timer and commit in there?
76		target.commit();
77		boot::stall(Duration::from_millis(10));
78	}
79
80	runtime::reset(ResetType::SHUTDOWN, Status::SUCCESS, None);
81}