From 410c6cc56763d503fdbaa6251cd6d1bb3715a235 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Thu, 22 Apr 2021 15:16:30 -0400 Subject: [PATCH] Add serialstudio support to the game --- Cargo.toml | 4 +- src/lib/mod.rs | 3 +- src/lib/profiler/mod.rs | 83 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 15 ++++++++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/lib/profiler/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 6338d2a..d17519a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,6 @@ description = "" [dependencies] raylib = { version = "3.5", git = "https://github.com/ewpratten/raylib-rs", branch = "master" } - +serialstudio = "0.1.0" +serde = "1.0.125" +serde_json = "1.0.64" \ No newline at end of file diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 4bafd13..2610b8a 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -1 +1,2 @@ -pub mod wrappers; \ No newline at end of file +pub mod wrappers; +pub mod profiler; \ No newline at end of file diff --git a/src/lib/profiler/mod.rs b/src/lib/profiler/mod.rs new file mode 100644 index 0000000..b054cef --- /dev/null +++ b/src/lib/profiler/mod.rs @@ -0,0 +1,83 @@ +use serde_json::json; +use serialstudio::{ + data::{DataGroup, DataSet, TelemetryFrame}, + SerialStudioSource, +}; + +/// The development profiler +#[derive(Default)] +pub struct GameProfiler { + /// The SerialStudio server + server: Option, + + /// The data + pub frames_per_second: u32, + pub seconds_per_frame: f32, +} + +/// Dev mode +#[cfg(debug_assertions)] +impl GameProfiler { + pub fn new() -> Self { + Self { + server: Some(SerialStudioSource::new()), + ..Default::default() + } + } + + pub fn start(&mut self) { + println!("Starting debug server on: tcp://localhost:8019"); + self.server + .as_mut() + .unwrap() + .start("localhost:8019".to_string()); + } + + pub fn stop(&mut self) { + println!("Stopping debug server"); + self.server.as_mut().unwrap().stop(); + } + + pub fn update(&mut self) { + // Build telemetry frame + let frame = TelemetryFrame { + title: "Game status".to_string(), + groups: vec![DataGroup { + title: "Rendering engine".to_string(), + widget_type: None, + datasets: vec![ + DataSet { + title: Some("Frames per Second".to_string()), + value: json!(self.frames_per_second), + graph: Some(true), + unit: Some("fps".to_string()), + w_type: None, + }, + DataSet { + title: Some("Seconds per Frame".to_string()), + value: json!(self.seconds_per_frame), + graph: Some(true), + unit: Some("seconds".to_string()), + w_type: None, + }, + ], + }], + }; + + // Send the frame + self.server.as_mut().unwrap().publish(frame); + } +} + +/// Release mode: We do nothing here +#[cfg(not(debug_assertions))] +impl GameProfiler { + pub fn new() -> Self { + Self { + ..Default::default() + } + } + pub fn start(&mut self) {} + pub fn stop(&mut self) {} + pub fn update(&mut self) {} +} diff --git a/src/main.rs b/src/main.rs index 1246ef2..cb827f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod lib; mod gamecore; use gamecore::{GameCore, GameState}; +use lib::profiler::GameProfiler; use raylib::prelude::*; // Game Launch Configuration @@ -25,6 +26,10 @@ fn main() { state: GameState::Loading }; + // Set up the game's profiler + let mut profiler = GameProfiler::new(); + profiler.start(); + // Main rendering loop while !raylib.window_should_close() { let mut draw_handle = raylib.begin_drawing(&raylib_thread); @@ -34,5 +39,15 @@ fn main() { // Call appropriate render function // TODO: the usual match statement on `game_core.state` + + // Feed the profiler + profiler.seconds_per_frame = draw_handle.get_frame_time(); + profiler.frames_per_second = draw_handle.get_fps(); + + // Send telemetry data + profiler.update(); } + + // Cleanup + profiler.stop(); }