Add serialstudio support to the game
This commit is contained in:
parent
01ee2d5dcf
commit
410c6cc567
@ -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"
|
@ -1 +1,2 @@
|
||||
pub mod wrappers;
|
||||
pub mod wrappers;
|
||||
pub mod profiler;
|
83
src/lib/profiler/mod.rs
Normal file
83
src/lib/profiler/mod.rs
Normal file
@ -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<SerialStudioSource>,
|
||||
|
||||
/// 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) {}
|
||||
}
|
15
src/main.rs
15
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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user