Skip to content

Commit f49db4e

Browse files
committed
feature: Added frame buffering for maze example.
1 parent 1155fd6 commit f49db4e

File tree

7 files changed

+116
-6
lines changed

7 files changed

+116
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ micromath = "2.0.0"
1111
# runty8-core = { version = "0.1.0", path = "../runty8/src/runty8-core", optional = true, default-features = false }
1212
runty8-core = { git = "https://github.com/shanecelis/runty8.git", branch = "feature-no-std", optional = true, default-features = false }
1313
embedded-alloc = "0.5.0"
14+
embedded-graphics-framebuf = "0.2.0"
1415
[target.'cfg(not(all(target_arch = "arm", target_os = "none")))'.dependencies]
1516
embedded-graphics-simulator = "0.4.1"
1617

examples/maze.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use embedded_graphics::{
2222
};
2323
#[allow(unused_imports)]
2424
use micromath::F32Ext;
25-
use trowel::{App, AppResult, Buttons};
25+
use trowel::{App, AppResult, Buttons, buffered::BufferedApp};
2626

2727
// The original platform had a 160x160 display. Sprig only has a 160x128
2828
// display.
@@ -81,9 +81,7 @@ where
8181
}
8282

8383
fn draw(&mut self, display: &mut T) -> AppResult<E> {
84-
if self.frame % 10 == 0 {
85-
display.clear(to_color(PALETTE[0]))?;
86-
}
84+
display.clear(to_color(PALETTE[0]))?;
8785
// Go through each column on screen and draw walls in the center.
8886
for (x, wall) in self.get_view().iter().enumerate() {
8987
let (height, shadow) = wall;
@@ -358,5 +356,7 @@ fn main() -> ! {
358356
player_y: 1.5,
359357
player_angle: -PI / 2.0,
360358
};
361-
trowel::run(&mut state);
359+
let mut app = BufferedApp::new(state);
360+
// trowel::run(state);
361+
trowel::run(&mut app);
362362
}

src/buffered.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
use embedded_graphics::{
3+
draw_target::DrawTarget,
4+
image::{Image, ImageRaw, ImageRawBE},
5+
pixelcolor::{Rgb565, Rgb888},
6+
prelude::*,
7+
};
8+
use crate::{App, AppResult, Buttons};
9+
use embedded_graphics_framebuf::{FrameBuf, backends::FrameBufferBackend};
10+
11+
pub struct Buffer([Rgb565; 20480]);
12+
13+
impl FrameBufferBackend for Buffer {
14+
type Color = Rgb565;
15+
fn set(&mut self, index: usize, color: Self::Color) {
16+
self.0[index] = color;
17+
}
18+
19+
fn get(&self, index: usize) -> Self::Color {
20+
self.0[index]
21+
}
22+
23+
fn nr_elements(&self) -> usize {
24+
20480
25+
}
26+
}
27+
28+
pub struct BufferedApp<A>
29+
where A : App<FrameBuf<Rgb565, Buffer>,
30+
core::convert::Infallible>
31+
{
32+
frameBuf : FrameBuf<Rgb565, Buffer>,
33+
// buffer : Buffer,
34+
app : A
35+
}
36+
37+
38+
impl<A> BufferedApp<A>
39+
where A : App<FrameBuf<Rgb565, Buffer>,
40+
core::convert::Infallible>
41+
{
42+
pub fn new(app: A) -> Self {
43+
let data = Buffer([Rgb565::BLACK; 20480]);
44+
BufferedApp {
45+
// buffer: data,
46+
frameBuf: FrameBuf::new(data, 160, 128),
47+
app
48+
}
49+
}
50+
}
51+
52+
impl<T, E, A> App<T, E> for BufferedApp<A>
53+
where
54+
T : DrawTarget<Color = Rgb565, Error = E>,
55+
A : App<FrameBuf<Rgb565, Buffer>, core::convert::Infallible>
56+
{
57+
fn init(&mut self) -> AppResult<E> {
58+
self.app.init();
59+
Ok(())
60+
}
61+
62+
fn update(&mut self, buttons: Buttons) -> AppResult<E> {
63+
self.app.update(buttons);
64+
Ok(())
65+
}
66+
67+
fn draw(&mut self, display: &mut T) -> AppResult<E> {
68+
self.app.draw(&mut self.frameBuf);
69+
70+
display.draw_iter(self.frameBuf.into_iter());
71+
Ok(())
72+
}
73+
}
74+
75+
pub fn run<TheirApp,T,E,MyApp>(app : TheirApp) -> !
76+
where T: DrawTarget<Color = Rgb565, Error = E>,
77+
TheirApp: App<FrameBuf<Rgb565, Buffer>,
78+
core::convert::Infallible>,
79+
MyApp: App<T,E>
80+
{
81+
let mut bufferedApp: BufferedApp<TheirApp> = BufferedApp::new(app);
82+
super::run(&mut bufferedApp);
83+
}

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ mod sprig;
4141
#[cfg(all(target_arch = "arm", target_os = "none"))]
4242
pub use sprig::{run, init_heap};
4343

44+
// #[cfg(all(target_arch = "arm", target_os = "none"))]
45+
// pub mod buffered;
46+
47+
48+
pub mod buffered;
49+
50+
// pub mod buffered {
51+
// #[cfg(all(target_arch = "arm", target_os = "none"))]
52+
// pub use sprig_buffered::run;
53+
54+
// #[cfg(not(all(target_arch = "arm", target_os = "none")))]
55+
// pub use crate::pc::run;
56+
// }
57+
4458
#[cfg(not(all(target_arch = "arm", target_os = "none")))]
4559
mod pc;
4660
#[cfg(not(all(target_arch = "arm", target_os = "none")))]

src/pc.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ use crate::{App, Buttons};
1717

1818
pub fn init_heap() { }
1919

20+
pub mod buffered {
21+
pub use crate::pc::run;
22+
// use crate::pc::SimulatorDisplay;
23+
// use crate::App;
24+
// use crate::Rgb565;
25+
26+
// pub fn runBuffered(app: &mut impl App<SimulatorDisplay<Rgb565>, core::convert::Infallible>) -> ! {
27+
// super::run(app);
28+
// }
29+
30+
}
31+
2032
/// The `run` function configures the RP2040 peripherals, then runs the app.
2133
pub fn run(app: &mut impl App<SimulatorDisplay<Rgb565>, core::convert::Infallible>) -> ! {
2234
let mut display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(160, 128));

src/runty8.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ struct RuntyApp<G>
1818
game: G,
1919
input: Input,
2020
last_buttons: Buttons
21-
2221
}
2322

2423
impl<G> RuntyApp<G>

src/sprig.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use embedded_alloc::Heap;
3737
#[used]
3838
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
3939

40+
4041
/// External high-speed crystal on the Raspberry Pi Pico board is 12 MHz. Adjust
4142
/// if your board has a different frequency.
4243
const XTAL_FREQ_HZ: u32 = 12_000_000u32;

0 commit comments

Comments
 (0)