implement main loop stopping condition

This commit is contained in:
Nek0 - 2020-03-10 08:49:00 +01:00
parent 13c86a98d7
commit 795243d14e
2 changed files with 11 additions and 1 deletions

View File

@ -11,6 +11,9 @@ impl Callbacks for Example {
fn update(&mut self) {} fn update(&mut self) {}
fn draw(&self, graphics: &mut Graphics) {} fn draw(&self, graphics: &mut Graphics) {}
fn clean_up(self) {} fn clean_up(self) {}
fn do_next_step(&self) -> bool {
true
}
} }
pub fn main() { pub fn main() {

View File

@ -23,6 +23,7 @@ pub trait Callbacks {
fn update(&mut self); fn update(&mut self);
fn draw(&self, graphics: &mut Graphics); fn draw(&self, graphics: &mut Graphics);
fn clean_up(self); fn clean_up(self);
fn do_next_step(&self) -> bool;
} }
pub fn run<C: Callbacks>(config: Config) { pub fn run<C: Callbacks>(config: Config) {
@ -30,7 +31,7 @@ pub fn run<C: Callbacks>(config: Config) {
let mut state = C::load_state(); let mut state = C::load_state();
state.pre_loop(); state.pre_loop();
loop { while state.do_next_step() {
state.update(); state.update();
for ev in graphics.get_events() { for ev in graphics.get_events() {
state.handle_event(ev); state.handle_event(ev);
@ -46,22 +47,28 @@ mod tests {
use crate::run; use crate::run;
struct TestCallbacks { struct TestCallbacks {
stepper: u32,
} }
impl Callbacks for TestCallbacks { impl Callbacks for TestCallbacks {
fn load_state() -> Self { fn load_state() -> Self {
TestCallbacks{ TestCallbacks{
stepper: 0,
} }
} }
//fn preLoop(&mut self){} //fn preLoop(&mut self){}
fn handle_event(&mut self, ev: Event){ fn handle_event(&mut self, ev: Event){
} }
fn update(&mut self){ fn update(&mut self){
self.stepper = self.stepper + 1;
} }
fn draw(&self, graphics: &mut Graphics){ fn draw(&self, graphics: &mut Graphics){
} }
fn clean_up(self){ fn clean_up(self){
} }
fn do_next_step(&self) -> bool{
self.stepper < 999999
}
} }
#[test] #[test]