Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

68 рядки
1.7 KiB

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