You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.5 KiB

  1. mod misc;
  2. mod render;
  3. mod resources;
  4. mod systems;
  5. use specs::{Dispatcher, DispatcherBuilder, World};
  6. use crate::Error;
  7. use misc::{Events, TextManager, Window};
  8. use render::{Debug, Init, Test};
  9. use resources::{Camera, Geometry, State};
  10. use systems::StateUpdate;
  11. pub struct App<'a, 'b> {
  12. is_running: bool,
  13. events: Events,
  14. window: Window,
  15. dispatcher: Dispatcher<'a, 'b>,
  16. }
  17. impl<'a, 'b> App<'a, 'b> {
  18. pub fn new(world: &mut World) -> Result<Self, Error> {
  19. let events = Events::new(world);
  20. let window = Window::new(events.handle())?;
  21. world.insert(Camera::new()?);
  22. world.insert(Geometry::new()?);
  23. world.insert(State::default());
  24. let text_manager = TextManager::new(world)?;
  25. let mut dispatcher = DispatcherBuilder::new()
  26. .with(StateUpdate::new(world)?, "state_update", &[])
  27. .with_thread_local(Init::new(world)?)
  28. .with_thread_local(Test::new(world)?)
  29. .with_thread_local(Debug::new(&text_manager)?)
  30. .build();
  31. dispatcher.setup(world);
  32. Ok(Self {
  33. is_running: true,
  34. events,
  35. window,
  36. dispatcher,
  37. })
  38. }
  39. pub fn is_running(&self) -> bool {
  40. self.is_running
  41. }
  42. pub fn process(&mut self, world: &World) -> Result<(), Error> {
  43. self.events.process(world);
  44. self.dispatcher.dispatch(world);
  45. self.window.swap_buffers()?;
  46. self.is_running = !world.fetch::<State>().close_requested;
  47. Ok(())
  48. }
  49. }