|
- use std::string::ToString;
-
- use log::warn;
- use specs::{ReadExpect, System};
-
- use crate::{server::resources::Global, Error};
-
- use super::super::{
- misc::{Text, TextCache, TextManager},
- resources::State,
- };
-
- /* Debug */
-
- pub struct Debug {
- cache: TextCache,
- text: Text,
- fps: usize,
- resolution: (u32, u32),
- }
-
- impl Debug {
- pub fn new(text_manager: &TextManager) -> Result<Self, Error> {
- let cache = text_manager.create_cache()?;
- let text = cache
- .new_text()
- .scale(12.0)
- .font("resources/fonts/DroidSansMono.ttf")
- .color(0.7, 0.7, 0.7, 1.0)
- .position(5.0, 5.0)
- .text(format!("Space Crush v{}\n", env!("CARGO_PKG_VERSION")))
- .text("\nFPS: ")
- .text("-")
- .text("\nResolution: ")
- .text("1280 x 720")
- .build()?;
-
- Ok(Self {
- cache,
- text,
- fps: 0,
- resolution: (0, 0),
- })
- }
- }
-
- impl<'a> System<'a> for Debug {
- type SystemData = (ReadExpect<'a, Global>, ReadExpect<'a, State>);
-
- fn run(&mut self, (global, state): Self::SystemData) {
- let guard = self.cache.begin_update();
- update_text(
- &mut self.text,
- 2,
- &mut self.fps,
- &global.fps,
- ToString::to_string,
- );
- update_text(
- &mut self.text,
- 4,
- &mut self.resolution,
- &state.resolution,
- |(w, h)| format!("{} x {}", w, h),
- );
- drop(guard);
-
- self.text.render(true);
- }
- }
-
- fn update_text<T, F>(text: &mut Text, pos: usize, old: &mut T, new: &T, update: F)
- where
- T: PartialEq + Copy,
- F: FnOnce(&T) -> String,
- {
- if old != new {
- *old = *new;
- let s = update(old);
- if let Err(err) = text.update(pos, s) {
- warn!("Unable to update debug text: {}", err);
- }
- }
- }
|