affection-rs/src/lib.rs

80 lines
1.7 KiB
Rust
Raw Permalink Normal View History

2020-03-04 18:28:07 +01:00
pub mod graphics;
pub use graphics::{Graphics, Event};
pub struct Config {
window_title: String,
window_size: (u32, u32),
}
impl Default for Config {
fn default() -> Self {
Config {
window_title: "Affection".to_owned(),
window_size: (800, 600),
}
}
}
pub trait Callbacks {
fn load_state() -> Self;
fn pre_loop(&mut self) {
}
fn handle_event(&mut self, ev: Event);
fn update(&mut self);
fn draw(&self, graphics: &mut Graphics);
2020-03-04 18:28:07 +01:00
fn clean_up(self);
2020-03-10 08:49:00 +01:00
fn do_next_step(&self) -> bool;
2020-03-04 18:28:07 +01:00
}
pub fn run<C: Callbacks>(config: Config) {
let mut graphics = Graphics::new(&config);
let mut state = C::load_state();
state.pre_loop();
2020-03-10 08:49:00 +01:00
while state.do_next_step() {
2020-03-04 18:28:07 +01:00
state.update();
for ev in graphics.get_events() {
state.handle_event(ev);
}
state.draw(&mut graphics);
}
state.clean_up();
}
#[cfg(test)]
mod tests {
2020-03-10 05:19:23 +01:00
use super::{Config, Callbacks, Event, Graphics};
use crate::run;
struct TestCallbacks {
2020-03-10 08:49:00 +01:00
stepper: u32,
2020-03-10 05:19:23 +01:00
}
impl Callbacks for TestCallbacks {
fn load_state() -> Self {
TestCallbacks{
2020-03-10 08:49:00 +01:00
stepper: 0,
2020-03-10 05:19:23 +01:00
}
}
//fn preLoop(&mut self){}
fn handle_event(&mut self, ev: Event){
}
fn update(&mut self){
2020-03-10 08:49:00 +01:00
self.stepper = self.stepper + 1;
2020-03-10 05:19:23 +01:00
}
fn draw(&self, graphics: &mut Graphics){
}
fn clean_up(self){
}
2020-03-10 08:49:00 +01:00
fn do_next_step(&self) -> bool{
self.stepper < 999999
}
2020-03-10 05:19:23 +01:00
}
2020-03-04 18:28:07 +01:00
#[test]
fn it_works() {
2020-03-10 05:19:23 +01:00
let config = Config::default();
run::<TestCallbacks>(config);
2020-03-04 18:28:07 +01:00
}
}