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.

80 lines
2.1 KiB

  1. mod constants;
  2. mod debug;
  3. mod error;
  4. mod misc;
  5. mod render;
  6. mod resources;
  7. mod systems;
  8. use specs::{Dispatcher, DispatcherBuilder, World};
  9. pub use error::Error;
  10. use debug::{Fleets as DebugFleets, Ships as DebugShips, Summary as DebugSummary};
  11. use misc::{Events, TextManager, Window};
  12. use render::{Asteroids, Init, Planets, Ships};
  13. use resources::{Camera, Config, Geometry, State, Uniform};
  14. use systems::StateUpdate;
  15. pub struct App<'a, 'b> {
  16. is_running: bool,
  17. events: Events,
  18. window: Window,
  19. dispatcher: Dispatcher<'a, 'b>,
  20. }
  21. impl<'a, 'b> App<'a, 'b> {
  22. pub fn new(world: &mut World) -> Result<Self, Error> {
  23. let config = Config::new(world)?;
  24. let events = Events::new(world);
  25. let window = Window::new(events.handle(), &config)?;
  26. let state = State::default();
  27. let camera = Camera::new()?;
  28. let uniform = Uniform::new()?;
  29. let geometry = Geometry::new(world)?;
  30. world.insert(state);
  31. world.insert(config);
  32. world.insert(camera);
  33. world.insert(uniform);
  34. world.insert(geometry);
  35. let text_manager = TextManager::new(world)?;
  36. let mut dispatcher = DispatcherBuilder::new()
  37. .with(StateUpdate::new(world)?, "state_update", &[])
  38. .with_thread_local(Init::new(world)?)
  39. .with_thread_local(Planets::new(world)?)
  40. .with_thread_local(Asteroids::new(world)?)
  41. .with_thread_local(Ships::new(world)?)
  42. .with_thread_local(DebugShips::default())
  43. .with_thread_local(DebugFleets::default())
  44. .with_thread_local(DebugSummary::new(&text_manager)?)
  45. .build();
  46. dispatcher.setup(world);
  47. Ok(Self {
  48. is_running: true,
  49. events,
  50. window,
  51. dispatcher,
  52. })
  53. }
  54. pub fn is_running(&self) -> bool {
  55. self.is_running
  56. }
  57. pub fn process(&mut self, world: &World) -> Result<(), Error> {
  58. self.events.process(world);
  59. self.dispatcher.dispatch(world);
  60. self.window.swap_buffers()?;
  61. self.is_running = !world.fetch::<State>().close_requested;
  62. Ok(())
  63. }
  64. }