25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

85 satır
1.9 KiB

  1. use std::string::ToString;
  2. use log::warn;
  3. use specs::{ReadExpect, System};
  4. use crate::{server::resources::Global, Error};
  5. use super::super::{
  6. misc::{Text, TextCache, TextManager},
  7. resources::State,
  8. };
  9. /* Debug */
  10. pub struct Debug {
  11. cache: TextCache,
  12. text: Text,
  13. fps: usize,
  14. resolution: (u32, u32),
  15. }
  16. impl Debug {
  17. pub fn new(text_manager: &TextManager) -> Result<Self, Error> {
  18. let cache = text_manager.create_cache()?;
  19. let text = cache
  20. .new_text()
  21. .scale(12.0)
  22. .font("resources/fonts/DroidSansMono.ttf")
  23. .color(0.7, 0.7, 0.7, 1.0)
  24. .position(5.0, 5.0)
  25. .text(format!("Space Crush v{}\n", env!("CARGO_PKG_VERSION")))
  26. .text("\nFPS: ")
  27. .text("-")
  28. .text("\nResolution: ")
  29. .text("1280 x 720")
  30. .build()?;
  31. Ok(Self {
  32. cache,
  33. text,
  34. fps: 0,
  35. resolution: (0, 0),
  36. })
  37. }
  38. }
  39. impl<'a> System<'a> for Debug {
  40. type SystemData = (ReadExpect<'a, Global>, ReadExpect<'a, State>);
  41. fn run(&mut self, (global, state): Self::SystemData) {
  42. let guard = self.cache.begin_update();
  43. update_text(
  44. &mut self.text,
  45. 2,
  46. &mut self.fps,
  47. &global.fps,
  48. ToString::to_string,
  49. );
  50. update_text(
  51. &mut self.text,
  52. 4,
  53. &mut self.resolution,
  54. &state.resolution,
  55. |(w, h)| format!("{} x {}", w, h),
  56. );
  57. drop(guard);
  58. self.text.render(true);
  59. }
  60. }
  61. fn update_text<T, F>(text: &mut Text, pos: usize, old: &mut T, new: &T, update: F)
  62. where
  63. T: PartialEq + Copy,
  64. F: FnOnce(&T) -> String,
  65. {
  66. if old != new {
  67. *old = *new;
  68. let s = update(old);
  69. if let Err(err) = text.update(pos, s) {
  70. warn!("Unable to update debug text: {}", err);
  71. }
  72. }
  73. }