Skip to content

Commit a5d292f

Browse files
committed
user: bugfix #148: Optimize GUI
User should call sys_framebuffer_flush only once after the modification on the framebuffer has been completed. This commit also renames some gui apps.
1 parent 51d23c7 commit a5d292f

File tree

6 files changed

+89
-141
lines changed

6 files changed

+89
-141
lines changed

user/src/bin/gui_move.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
extern crate user_lib;
5+
extern crate alloc;
6+
7+
use user_lib::console::getchar;
8+
use user_lib::{Display, VIRTGPU_XRES, VIRTGPU_YRES};
9+
10+
use embedded_graphics::pixelcolor::Rgb888;
11+
use embedded_graphics::prelude::{Drawable, Point, RgbColor, Size};
12+
use embedded_graphics::primitives::Primitive;
13+
use embedded_graphics::primitives::{PrimitiveStyle, Rectangle};
14+
use embedded_graphics::draw_target::DrawTarget;
15+
16+
const INIT_X: i32 = 640;
17+
const INIT_Y: i32 = 400;
18+
const RECT_SIZE: u32 = 40;
19+
20+
pub struct DrawingBoard {
21+
disp: Display,
22+
latest_pos: Point,
23+
}
24+
25+
impl DrawingBoard {
26+
pub fn new() -> Self {
27+
Self {
28+
disp: Display::new(Size::new(VIRTGPU_XRES, VIRTGPU_YRES)),
29+
latest_pos: Point::new(INIT_X, INIT_Y),
30+
}
31+
}
32+
fn paint(&mut self) {
33+
Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE))
34+
.into_styled(PrimitiveStyle::with_stroke(Rgb888::WHITE, 1))
35+
.draw(&mut self.disp)
36+
.ok();
37+
}
38+
fn unpaint(&mut self) {
39+
Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE))
40+
.into_styled(PrimitiveStyle::with_stroke(Rgb888::BLACK, 1))
41+
.draw(&mut self.disp)
42+
.ok();
43+
}
44+
pub fn move_rect(&mut self, dx: i32, dy: i32) {
45+
let new_x = self.latest_pos.x + dx;
46+
let new_y = self.latest_pos.y + dy;
47+
let r = (RECT_SIZE / 2) as i32;
48+
if new_x > r && new_x + r < (VIRTGPU_XRES as i32) && new_y > r && new_y + r < (VIRTGPU_YRES as i32) {
49+
self.unpaint();
50+
self.latest_pos.x = new_x;
51+
self.latest_pos.y = new_y;
52+
self.paint();
53+
}
54+
}
55+
}
56+
57+
const LF: u8 = 0x0au8;
58+
const CR: u8 = 0x0du8;
59+
#[no_mangle]
60+
pub fn main() -> i32 {
61+
let mut board = DrawingBoard::new();
62+
let _ = board.disp.clear(Rgb888::BLACK).unwrap();
63+
board.disp.flush();
64+
loop {
65+
let c = getchar();
66+
if c == LF || c == CR {
67+
break;
68+
}
69+
let mut moved = true;
70+
match c {
71+
b'w' => board.move_rect(0, -10),
72+
b'a' => board.move_rect(-10, 0),
73+
b's' => board.move_rect(0, 10),
74+
b'd' => board.move_rect(10, 0),
75+
_ => moved = false,
76+
}
77+
if moved {
78+
board.disp.flush();
79+
}
80+
}
81+
0
82+
}

user/src/bin/gui_rect.rs renamed to user/src/bin/gui_shape.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,6 @@ impl DrawingBoard {
4444
.draw(&mut self.disp)
4545
.ok();
4646
}
47-
fn unpaint(&mut self) {
48-
Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE))
49-
.into_styled(PrimitiveStyle::with_stroke(Rgb888::BLACK, 10))
50-
.draw(&mut self.disp)
51-
.ok();
52-
}
53-
pub fn move_rect(&mut self, dx: i32, dy: i32) {
54-
self.unpaint();
55-
self.latest_pos.x += dx;
56-
self.latest_pos.y += dy;
57-
self.paint();
58-
}
5947
}
6048

6149
#[no_mangle]
@@ -64,8 +52,8 @@ pub fn main() -> i32 {
6452
let _ = board.disp.clear(Rgb888::BLACK).unwrap();
6553
for _ in 0..5 {
6654
board.latest_pos.x += RECT_SIZE as i32 + 20;
67-
//board.latest_pos.y += i;
6855
board.paint();
6956
}
57+
board.disp.flush();
7058
0
7159
}

user/src/bin/gui_snake.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ const CR: u8 = 0x0du8;
328328
#[no_mangle]
329329
pub fn main() -> i32 {
330330
let mut disp = Display::new(Size::new(VIRTGPU_XRES, VIRTGPU_YRES));
331-
let mut game = SnakeGame::<20, Rgb888>::new(1280, 800, 20, 20, Rgb888::RED, Rgb888::YELLOW, 50);
331+
let mut game = SnakeGame::<20, Rgb888>::new(1280, 800, 20, 20, Rgb888::RED, Rgb888::YELLOW, 200);
332332
let _ = disp.clear(Rgb888::BLACK).unwrap();
333333
loop {
334334
if key_pressed() {
@@ -345,7 +345,8 @@ pub fn main() -> i32 {
345345
}
346346
let _ = disp.clear(Rgb888::BLACK).unwrap();
347347
game.draw(&mut disp);
348-
sleep(10);
348+
disp.flush();
349+
sleep(40);
349350
}
350351
0
351352
}

user/src/bin/gui_simple.rs renamed to user/src/bin/gui_tri.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ pub fn main() -> i32 {
1919
}
2020
}
2121
});
22+
disp.flush();
2223
0
2324
}

user/src/bin/gui_uart.rs

Lines changed: 0 additions & 125 deletions
This file was deleted.

user/src/io.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ impl Display {
3232
}
3333
pub fn paint_on_framebuffer(&mut self, p: impl FnOnce(&mut [u8]) -> ()) {
3434
p(self.framebuffer());
35+
}
36+
pub fn flush(&self) {
3537
framebuffer_flush();
3638
}
3739
}
@@ -60,7 +62,6 @@ impl DrawTarget for Display {
6062
self.fb[idx + 1] = px.1.g();
6163
self.fb[idx + 2] = px.1.r();
6264
});
63-
framebuffer_flush();
6465
Ok(())
6566
}
6667
}

0 commit comments

Comments
 (0)