feat(app): add tick info/result and system diagnosis
This commit is contained in:
		
							parent
							
								
									d924d14ab0
								
							
						
					
					
						commit
						53dd008df5
					
				
					 2 changed files with 95 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -9,10 +9,16 @@ void Application::game_loop()
 | 
			
		|||
	{
 | 
			
		||||
		for (auto &system : m_systems)
 | 
			
		||||
		{
 | 
			
		||||
			if (system->tick())
 | 
			
		||||
			{
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
			const auto &last_tick = system->get_last_tick_result();
 | 
			
		||||
			const auto now = std::chrono::steady_clock::now();
 | 
			
		||||
 | 
			
		||||
			system->tick(
 | 
			
		||||
			    TickInfo {
 | 
			
		||||
			        .delta_time = now - last_tick.end_time,
 | 
			
		||||
			        .budget = std::chrono::milliseconds { 10 },
 | 
			
		||||
			        .start_time = now,
 | 
			
		||||
			    }
 | 
			
		||||
			);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for (auto &system : m_systems_to_be_registered)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,89 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <chrono>
 | 
			
		||||
 | 
			
		||||
namespace lt::app {
 | 
			
		||||
 | 
			
		||||
/** Information required to tick a system.
 | 
			
		||||
 * @note May be used across an entire application-frame (consisting of multiple systems ticking)
 | 
			
		||||
 */
 | 
			
		||||
struct TickInfo
 | 
			
		||||
{
 | 
			
		||||
	using Timepoint_T = std::chrono::time_point<std::chrono::steady_clock>;
 | 
			
		||||
 | 
			
		||||
	using Duration_T = std::chrono::duration<double>;
 | 
			
		||||
 | 
			
		||||
	/** Duration since previous tick's end_time to current tick's start_time. */
 | 
			
		||||
	Duration_T delta_time {};
 | 
			
		||||
 | 
			
		||||
	/** Maximum duration the system is expected to finish ticking in.
 | 
			
		||||
	 *
 | 
			
		||||
	 * if end_time - start_time > budget -> the system exceeded its ticking budget.
 | 
			
		||||
	 * else end_time - start_time < budget -> the system ticked properly.
 | 
			
		||||
	 *
 | 
			
		||||
	 * In other words, end_time is expected to be less than start_time + budget.
 | 
			
		||||
	 */
 | 
			
		||||
	Duration_T budget {};
 | 
			
		||||
 | 
			
		||||
	/** Exact time which ticking started. */
 | 
			
		||||
	Timepoint_T start_time;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/** Information about how a system's tick performed */
 | 
			
		||||
struct TickResult
 | 
			
		||||
{
 | 
			
		||||
	using Timepoint_T = std::chrono::time_point<std::chrono::steady_clock>;
 | 
			
		||||
 | 
			
		||||
	using Duration_T = std::chrono::duration<double>;
 | 
			
		||||
 | 
			
		||||
	/** The info supplied to the system for ticking. */
 | 
			
		||||
	TickInfo info;
 | 
			
		||||
 | 
			
		||||
	/** Equivalent to end_time - info.start_time. */
 | 
			
		||||
	Duration_T duration {};
 | 
			
		||||
 | 
			
		||||
	/** Exact time which ticking ended. */
 | 
			
		||||
	Timepoint_T end_time;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
struct SystemDiagnosis
 | 
			
		||||
{
 | 
			
		||||
	enum class Severity : uint8_t
 | 
			
		||||
	{
 | 
			
		||||
		verbose,
 | 
			
		||||
		info,
 | 
			
		||||
		warning,
 | 
			
		||||
		error,
 | 
			
		||||
		fatal,
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	std::string message;
 | 
			
		||||
 | 
			
		||||
	std::string code;
 | 
			
		||||
 | 
			
		||||
	Severity severity;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class SystemStats
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	void push_diagnosis(SystemDiagnosis &&diagnosis)
 | 
			
		||||
	{
 | 
			
		||||
		auto diag = m_diagnosis.emplace_back(std::move(diagnosis));
 | 
			
		||||
 | 
			
		||||
		log_dbg("message: {}", diag.message);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	[[nodiscard]] auto empty_diagnosis() const -> bool
 | 
			
		||||
	{
 | 
			
		||||
		return m_diagnosis.empty();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
	std::vector<SystemDiagnosis> m_diagnosis;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class ISystem
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
| 
						 | 
				
			
			@ -21,7 +103,9 @@ public:
 | 
			
		|||
 | 
			
		||||
	virtual void on_unregister() = 0;
 | 
			
		||||
 | 
			
		||||
	virtual auto tick() -> bool = 0;
 | 
			
		||||
	virtual void tick(TickInfo tick) = 0;
 | 
			
		||||
 | 
			
		||||
	[[nodiscard]] virtual auto get_last_tick_result() const -> const TickResult & = 0;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace lt::app
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue