Bouncing box demo

main
Elnu 1 year ago
parent 203a87ae2f
commit 0e5bd5ad94

@ -6,12 +6,15 @@ use eframe::egui;
use renrs::{State, Command};
use sfml::{
graphics::{Color, RenderTarget, RenderWindow, Shape, RectangleShape},
graphics::{Color, RenderTarget, RenderWindow, RectangleShape, Transformable},
system::Vector2f,
window::{Event, mouse, Key, Style},
};
use egui_sfml::SfEgui;
const WIDTH: f32 = 800.0;
const HEIGHT: f32 = 600.0;
pub fn run(file: PathBuf) {
env_logger::init();
let mut app = App::from_state(State::from_file(file));
@ -102,7 +105,7 @@ impl App {
fn run(&mut self) {
let mut window = RenderWindow::new(
(800, 600),
(WIDTH as u32, HEIGHT as u32),
"renrs-gui",
Style::CLOSE,
&Default::default(),
@ -110,10 +113,10 @@ impl App {
let mut sfegui = SfEgui::new(&window);
window.set_vertical_sync_enabled(true);
let mut shape = RectangleShape::with_size(Vector2f::new(64.0, 64.0));
shape.set_fill_color(Color::RED);
shape.set_outline_color(Color::GREEN);
shape.set_outline_thickness(3.);
const SIZE: f32 = 64.0;
let mut velocity = Vector2f::new(4.0, 4.0);
let mut shape = RectangleShape::with_size(Vector2f::new(SIZE, SIZE));
shape.set_position(Vector2f::new(SIZE, SIZE));
while window.is_open() {
while let Some(event) = window.poll_event() {
@ -130,7 +133,6 @@ impl App {
.do_frame(|ctx| {
egui::CentralPanel::default()
.frame(Frame {
fill: Color32::BLACK,
inner_margin: Margin::same(32.0),
..Default::default()
})
@ -146,6 +148,15 @@ impl App {
}).unwrap();
}
let Vector2f { x, y } = shape.position();
if x >= WIDTH - SIZE || x < 0.0 {
velocity.x *= -1.0;
}
if y >= HEIGHT - SIZE || y < 0.0 {
velocity.y *= -1.0;
}
shape.set_position(Vector2f::new(x + velocity.x, y + velocity.y));
window.clear(Color::BLACK);
window.draw(&shape);
sfegui.draw(&mut window, None);

Loading…
Cancel
Save