mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
add cheesy ball demo to the cram-console server
This commit is contained in:
parent
36c8e87889
commit
89f20100b0
2 changed files with 103 additions and 0 deletions
|
|
@ -14,6 +14,8 @@ log-server = { package = "xous-api-log", version = "0.1.53" }
|
|||
log = "0.4.14"
|
||||
xous-pio = { path = "../../libs/xous-pio", optional = true }
|
||||
xous-pl230 = { path = "../../libs/xous-pl230", optional = true }
|
||||
# this is temporary, until we get GAM/modals pulled in
|
||||
graphics-server = { path = "../graphics-server" }
|
||||
|
||||
[features]
|
||||
"cramium-fpga" = ["utralib/cramium-fpga", "xous-pl230/cramium-fpga"]
|
||||
|
|
|
|||
|
|
@ -124,4 +124,105 @@ fn main() {
|
|||
#[cfg(feature = "hwsim")]
|
||||
core_csr.wfo(utra::main::DONE_DONE, 1); // this should stop the simulation
|
||||
log::info!("message passing test done at {}ms!", tt.elapsed_ms());
|
||||
|
||||
let xns = xous_api_names::XousNames::new().unwrap();
|
||||
let mut ball = Ball::new(&xns);
|
||||
loop {
|
||||
ball.update();
|
||||
/* // for testing full-frame graphics drawing
|
||||
ball.draw_boot();
|
||||
tt.sleep_ms(100).ok();
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
fn crappy_rng_u32() -> u32 { xous::create_server_id().unwrap().to_u32().0 }
|
||||
|
||||
const BALL_RADIUS: i16 = 10;
|
||||
const MOMENTUM_LIMIT: i32 = 8;
|
||||
const BORDER_WIDTH: i16 = 5;
|
||||
use graphics_server::{Circle, ClipObjectList, ClipObjectType, DrawStyle, PixelColor, Point, Rectangle};
|
||||
|
||||
struct Ball {
|
||||
gfx: graphics_server::Gfx,
|
||||
screensize: Point,
|
||||
ball: Circle,
|
||||
momentum: Point,
|
||||
clip: Rectangle,
|
||||
}
|
||||
impl Ball {
|
||||
pub fn new(xns: &xous_api_names::XousNames) -> Ball {
|
||||
let gfx = graphics_server::Gfx::new(xns).unwrap();
|
||||
gfx.draw_boot_logo().unwrap();
|
||||
|
||||
let screensize = gfx.screen_size().unwrap();
|
||||
let mut ball = Circle::new(Point::new(screensize.x / 2, screensize.y / 2), BALL_RADIUS);
|
||||
ball.style = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1);
|
||||
let clip = Rectangle::new(Point::new(0, 0), screensize);
|
||||
gfx.draw_circle(ball).unwrap();
|
||||
let x = ((crappy_rng_u32() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
||||
let y = ((crappy_rng_u32() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
||||
Ball { gfx, screensize, ball, momentum: Point::new(x as i16, y as i16), clip }
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn draw_boot(&self) { self.gfx.draw_boot_logo().ok(); }
|
||||
|
||||
pub fn update(&mut self) {
|
||||
let mut draw_list = ClipObjectList::default();
|
||||
|
||||
// clear the previous location of the ball
|
||||
self.ball.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1);
|
||||
draw_list.push(ClipObjectType::Circ(self.ball), self.clip).unwrap();
|
||||
|
||||
// update the ball position based on the momentum vector
|
||||
self.ball.translate(self.momentum);
|
||||
|
||||
// check if the ball hits the wall, if so, snap its position to the wall
|
||||
let mut hit_right = false;
|
||||
let mut hit_left = false;
|
||||
let mut hit_top = false;
|
||||
let mut hit_bott = false;
|
||||
if self.ball.center.x + (BALL_RADIUS + BORDER_WIDTH) >= self.screensize.x {
|
||||
hit_right = true;
|
||||
self.ball.center.x = self.screensize.x - (BALL_RADIUS + BORDER_WIDTH);
|
||||
}
|
||||
if self.ball.center.x - (BALL_RADIUS + BORDER_WIDTH) <= 0 {
|
||||
hit_left = true;
|
||||
self.ball.center.x = BALL_RADIUS + BORDER_WIDTH;
|
||||
}
|
||||
if self.ball.center.y + (BALL_RADIUS + BORDER_WIDTH) >= self.screensize.y {
|
||||
hit_bott = true;
|
||||
self.ball.center.y = self.screensize.y - (BALL_RADIUS + BORDER_WIDTH);
|
||||
}
|
||||
if self.ball.center.y - (BALL_RADIUS + BORDER_WIDTH) <= 0 {
|
||||
hit_top = true;
|
||||
self.ball.center.y = BALL_RADIUS + BORDER_WIDTH;
|
||||
}
|
||||
|
||||
if hit_right || hit_left || hit_bott || hit_top {
|
||||
let mut x = ((crappy_rng_u32() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
||||
let mut y = ((crappy_rng_u32() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
||||
if hit_right {
|
||||
x = -x.abs();
|
||||
}
|
||||
if hit_left {
|
||||
x = x.abs();
|
||||
}
|
||||
if hit_top {
|
||||
y = y.abs();
|
||||
}
|
||||
if hit_bott {
|
||||
y = -y.abs();
|
||||
}
|
||||
self.momentum = Point::new(x as i16, y as i16);
|
||||
}
|
||||
|
||||
// draw the new location for the ball
|
||||
self.ball.style = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1);
|
||||
draw_list.push(ClipObjectType::Circ(self.ball), self.clip).unwrap();
|
||||
|
||||
self.gfx.draw_object_list_clipped(draw_list).ok();
|
||||
self.gfx.flush().unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue