From 24a97e47e1bf2c719ed9d34c7a827ffa5e12ff5e Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Wed, 28 Feb 2018 20:40:08 -0800 Subject: [PATCH] hackily adding the event count to the graph --- include/Application.h | 3 ++- include/GraphTimer.h | 6 +++++ include/Input.h | 3 +++ src/Application.cpp | 5 +---- src/GraphTimer.cpp | 51 +++++++++++++++++++++++++++---------------- src/Input.cpp | 9 ++++++-- 6 files changed, 51 insertions(+), 26 deletions(-) diff --git a/include/Application.h b/include/Application.h index e341b08..cfe57f6 100644 --- a/include/Application.h +++ b/include/Application.h @@ -72,13 +72,14 @@ private: std::shared_ptr raycaster; std::shared_ptr light_handle; std::shared_ptr light_controller; + GraphTimer fps; Input input_handler; std::shared_ptr window_handler; // The sfml imgui wrapper I'm using requires Update be called with sf::Time // Might modify it to also accept seconds sf::Clock sf_delta_clock; - GraphTimer fps; + // vars for us to use with ImGUI float light_color[4] = { 0, 0, 0, 0 }; diff --git a/include/GraphTimer.h b/include/GraphTimer.h index 720d051..a50f9f8 100644 --- a/include/GraphTimer.h +++ b/include/GraphTimer.h @@ -11,12 +11,15 @@ public: ~GraphTimer(); + static GraphTimer* get_instance(); + unsigned int create_line(std::string label); unsigned int delete_line(unsigned int idx); void start(unsigned int idx); void stop(unsigned int idx); void frame(unsigned int idx, double delta_time); + void count(unsigned int idx, int counter); void draw(); @@ -31,4 +34,7 @@ private: std::vector counters; std::vector labels; + + // I actually think I might make this a singleton, along + static GraphTimer* instance; }; \ No newline at end of file diff --git a/include/Input.h b/include/Input.h index 931e3ab..adf6d21 100644 --- a/include/Input.h +++ b/include/Input.h @@ -52,6 +52,9 @@ private: std::vector held_mouse_buttons; + // TODO: Debug, remove this when finished + int graph_index = 0; + private: static const std::vector key_strings; diff --git a/src/Application.cpp b/src/Application.cpp index 9526800..3b9a6cc 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -13,9 +13,6 @@ Application::Application() { ImGui::SFML::Init(*window); window->resetGLStates(); - - - } Application::~Application() { @@ -112,7 +109,7 @@ bool Application::game_loop() { int fps_idx = fps.create_line("FPS"); int compute_fps_idx = fps.create_line("Compute"); - int event_fps_idx = fps.create_line("Event"); + int event_fps_idx = fps.create_line("Event Time"); while (true) { diff --git a/src/GraphTimer.cpp b/src/GraphTimer.cpp index a81b758..b61f5ce 100644 --- a/src/GraphTimer.cpp +++ b/src/GraphTimer.cpp @@ -5,6 +5,11 @@ GraphTimer::GraphTimer() { start_time = std::chrono::system_clock::now(); started = true; } + + // TODO: This is a cardinal sin + if (instance == nullptr){ + instance = this; + } } GraphTimer::~GraphTimer() { @@ -36,35 +41,37 @@ void GraphTimer::stop(unsigned int idx){ std::chrono::time_point now = std::chrono::system_clock::now(); std::chrono::duration elapsed_time = now - start_time; + if (++counters.at(idx) >= FPS_ARRAY_LENGTH) + counters.at(idx) = 0; + fps_vectors.at(idx).at(counters.at(idx)) = elapsed_time.count() - checkpoints.at(idx); fps_vectors.at(idx).at(counters.at(idx)) = 1.0f / fps_vectors.at(idx).at(counters.at(idx)); - if (++counters.at(idx) >= FPS_ARRAY_LENGTH - 1) - counters.at(idx) = 0; + } void GraphTimer::frame(unsigned int idx, double delta_time) { - fps_vectors.at(idx).at(counters.at(idx)) = 1.0 / delta_time; - if (++counters.at(idx) >= FPS_ARRAY_LENGTH - 1) + + // Do this before we access to make the title generation in draw a bit easier + if (++counters.at(idx) >= FPS_ARRAY_LENGTH) counters.at(idx) = 0; + + fps_vectors.at(idx).at(counters.at(idx)) = 1.0 / delta_time; } void GraphTimer::draw() { ImGui::Begin("Performance"); - std::vector> data = { - {1, 2, 3, 4}, - {9, 3, 7, 1}, - {8, 3, 6, 2} - }; - std::string title = std::to_string(fps_vectors.at(0).at(counters.at(0))); - std::vector colors = { ImColor(255, 255, 255), - ImColor(0, 255, 0), - ImColor(255, 0, 0), + ImColor(255, 255, 0), + ImColor( 0, 255, 255), + ImColor(255, 0, 255), + ImColor(255, 0, 0), + ImColor( 0, 255, 0), + ImColor( 0, 0, 255), }; ImVec2 wh = ImGui::GetContentRegionAvail(); @@ -74,15 +81,21 @@ void GraphTimer::draw() { ImGui::PlotMultiLines(fps_vectors, title, labels, colors, 200, 0, graph_size); + ImGui::End(); - //ImVec2 wh(100, 200); -// ImGui::PlotLines("FPS", fps_array, 1000, 0, -// std::to_string(1.0 / fps_average).c_str(), -// 0.0f, 150.0f, wh); +} - ImGui::End(); +void GraphTimer::count(unsigned int idx, int counter) { + if (++counters.at(idx) >= FPS_ARRAY_LENGTH) + counters.at(idx) = 0; + fps_vectors.at(idx).at(counters.at(idx)) = counter; + +} +GraphTimer *GraphTimer::get_instance() { + return instance; } std::chrono::time_point GraphTimer::start_time; -bool GraphTimer::started; \ No newline at end of file +bool GraphTimer::started; +GraphTimer* GraphTimer::instance = nullptr; \ No newline at end of file diff --git a/src/Input.cpp b/src/Input.cpp index c59b3bd..775049b 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -1,8 +1,10 @@ -#include "Input.h" +#include +#include "Input.h" Input::Input() { - + GraphTimer *t = GraphTimer::get_instance(); + graph_index = t->create_line("Event Counter"); } Input::~Input() { @@ -21,6 +23,9 @@ void Input::consume_sf_events(sf::RenderWindow *window) { transpose_sf_events(sf_event_queue); + GraphTimer *t = GraphTimer::get_instance(); + t->count(graph_index, sf_event_queue.size()); + sf_event_queue.clear(); }