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.

119 lines
2.8 KiB

  1. use std::string::ToString;
  2. use space_crush_common::{misc::LogResult, resources::Global};
  3. use specs::{prelude::*, Entities, ReadExpect, System};
  4. use crate::{
  5. misc::{Text, TextCache, TextManager},
  6. resources::State,
  7. Error,
  8. };
  9. /* Summary */
  10. pub struct Summary {
  11. cache: TextCache,
  12. text: Text,
  13. fps: usize,
  14. resolution: (u32, u32),
  15. mouse_pos: (f32, f32),
  16. entity_count: usize,
  17. }
  18. impl Summary {
  19. pub fn new(text_manager: &TextManager) -> Result<Self, Error> {
  20. let cache = text_manager.create_cache()?;
  21. let text = cache
  22. .new_text()
  23. .scale(12.0)
  24. .font("resources/fonts/DroidSansMono.ttf")
  25. .color(0.7, 0.7, 0.7, 1.0)
  26. .position(5.0, 5.0)
  27. .text(format!("Space Crush v{}\n", env!("CARGO_PKG_VERSION")))
  28. .text("\nfps: ")
  29. .text("-")
  30. .text("\nresolution: ")
  31. .text("1280 | 720")
  32. .text("\nmouse_pos: ")
  33. .text("0.00 | 0.00")
  34. .text("\nentities: ")
  35. .text("0")
  36. .build()?;
  37. Ok(Self {
  38. cache,
  39. text,
  40. fps: 0,
  41. resolution: (0, 0),
  42. mouse_pos: (0.0, 0.0),
  43. entity_count: 0,
  44. })
  45. }
  46. }
  47. #[derive(SystemData)]
  48. pub struct SummaryData<'a> {
  49. entities: Entities<'a>,
  50. global: ReadExpect<'a, Global>,
  51. state: ReadExpect<'a, State>,
  52. }
  53. impl<'a> System<'a> for Summary {
  54. type SystemData = SummaryData<'a>;
  55. fn run(&mut self, data: Self::SystemData) {
  56. let SummaryData {
  57. entities,
  58. global,
  59. state,
  60. } = data;
  61. let entity_count = entities.par_join().count();
  62. let guard = self.cache.begin_update();
  63. update_text(
  64. &mut self.text,
  65. 2,
  66. &mut self.fps,
  67. &global.fps,
  68. ToString::to_string,
  69. );
  70. update_text(
  71. &mut self.text,
  72. 4,
  73. &mut self.resolution,
  74. &state.resolution,
  75. |(w, h)| format!("{} | {}", w, h),
  76. );
  77. update_text(
  78. &mut self.text,
  79. 6,
  80. &mut self.mouse_pos,
  81. &state.mouse_pos,
  82. |(x, y)| format!("{:.2} | {:.2}", x, y),
  83. );
  84. update_text(
  85. &mut self.text,
  86. 8,
  87. &mut self.entity_count,
  88. &entity_count,
  89. |x| format!("{}", x),
  90. );
  91. drop(guard);
  92. self.text.render(true);
  93. }
  94. }
  95. fn update_text<T, F>(text: &mut Text, pos: usize, old: &mut T, new: &T, update: F)
  96. where
  97. T: PartialEq + Copy,
  98. F: FnOnce(&T) -> String,
  99. {
  100. if old != new {
  101. *old = *new;
  102. let s = update(old);
  103. text.update(pos, s).error("Unable to update debug text");
  104. }
  105. }