Bouncing box demo

main
Elnu 1 year ago
parent 203a87ae2f
commit 0e5bd5ad94

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

Loading…
Cancel
Save