forked from astro/affection-rs
implement main loop stopping condition
This commit is contained in:
parent
13c86a98d7
commit
795243d14e
|
@ -11,6 +11,9 @@ impl Callbacks for Example {
|
|||
fn update(&mut self) {}
|
||||
fn draw(&self, graphics: &mut Graphics) {}
|
||||
fn clean_up(self) {}
|
||||
fn do_next_step(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
|
|
|
@ -23,6 +23,7 @@ pub trait Callbacks {
|
|||
fn update(&mut self);
|
||||
fn draw(&self, graphics: &mut Graphics);
|
||||
fn clean_up(self);
|
||||
fn do_next_step(&self) -> bool;
|
||||
}
|
||||
|
||||
pub fn run<C: Callbacks>(config: Config) {
|
||||
|
@ -30,7 +31,7 @@ pub fn run<C: Callbacks>(config: Config) {
|
|||
|
||||
let mut state = C::load_state();
|
||||
state.pre_loop();
|
||||
loop {
|
||||
while state.do_next_step() {
|
||||
state.update();
|
||||
for ev in graphics.get_events() {
|
||||
state.handle_event(ev);
|
||||
|
@ -46,22 +47,28 @@ mod tests {
|
|||
use crate::run;
|
||||
|
||||
struct TestCallbacks {
|
||||
stepper: u32,
|
||||
}
|
||||
|
||||
impl Callbacks for TestCallbacks {
|
||||
fn load_state() -> Self {
|
||||
TestCallbacks{
|
||||
stepper: 0,
|
||||
}
|
||||
}
|
||||
//fn preLoop(&mut self){}
|
||||
fn handle_event(&mut self, ev: Event){
|
||||
}
|
||||
fn update(&mut self){
|
||||
self.stepper = self.stepper + 1;
|
||||
}
|
||||
fn draw(&self, graphics: &mut Graphics){
|
||||
}
|
||||
fn clean_up(self){
|
||||
}
|
||||
fn do_next_step(&self) -> bool{
|
||||
self.stepper < 999999
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in New Issue