-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature/imgui: add imgui support and performance graph (#9)
* Add imgui and performance graphs * Remove wrong include directive * Fix build issues * Fix build issues * Fix build issues * Fix build issues
- Loading branch information
Showing
19 changed files
with
14,340 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "GUIManager.h" | ||
|
||
#include <iostream> | ||
|
||
static std::shared_ptr<std::unordered_map<std::string, ScrollingBuffer>> metricsMap; | ||
|
||
GUIManager::GUIManager() { | ||
metricsMap = std::make_shared<std::unordered_map<std::string, ScrollingBuffer>>(); | ||
} | ||
|
||
void GUIManager::init() { | ||
ImPlot::CreateContext(); | ||
} | ||
|
||
void GUIManager::buildGui() { | ||
ImGui::SetNextWindowSize(ImVec2(400, 250), ImGuiCond_FirstUseEver); | ||
ImGui::SetNextWindowPos(ImVec2(10, 10), ImGuiCond_FirstUseEver); | ||
ImGui::Begin("Performance"); | ||
|
||
static float history = 10; | ||
|
||
static ImPlotAxisFlags flags = ImPlotAxisFlags_AutoFit; | ||
if (ImPlot::BeginPlot("##Scrolling", ImVec2(-1, -1))) { | ||
ImPlot::SetupAxes("s", "time (ms)", flags, flags); | ||
const auto t = ImGui::GetTime(); | ||
ImPlot::SetupAxisLimits(ImAxis_X1, t - history, t, ImGuiCond_Always); | ||
ImPlot::SetupAxisLimits(ImAxis_Y1, 0, 1); | ||
ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.5f); | ||
for (auto& [name, values]: *metricsMap) { | ||
if (!values.Data.empty()) { | ||
ImPlot::PlotLine(name.c_str(), &values.Data[0].x, &values.Data[0].y, values.Data.size(), 0, | ||
values.Offset, 2 * sizeof(float)); | ||
} | ||
} | ||
ImPlot::EndPlot(); | ||
} | ||
ImGui::SliderFloat("History", &history, 1, 30, "%.1f s"); | ||
ImGui::End(); | ||
|
||
ImGui::SetNextWindowPos(ImVec2(10, 270), ImGuiCond_FirstUseEver); | ||
ImGui::Begin("Controls"); | ||
ImGui::Text("WASD: Move"); | ||
ImGui::Text("Mouse: Look"); | ||
ImGui::End(); | ||
} | ||
|
||
void GUIManager::pushMetric(const std::string& name, float value) { | ||
int maxSize = 600; | ||
if (!metricsMap->contains(name)) { | ||
metricsMap->insert({name, ScrollingBuffer(maxSize)}); | ||
} | ||
metricsMap->at(name).addPoint(ImGui::GetTime(), value); | ||
} | ||
|
||
void GUIManager::pushMetric(const std::unordered_map<std::string, float>& name) { | ||
for (auto& [n, v]: name) { | ||
pushMetric(n, v); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef GUIMANAGER_H | ||
#define GUIMANAGER_H | ||
#include <unordered_map> | ||
#include <vector> | ||
#include <string> | ||
#include <memory> | ||
|
||
#include "imgui.h" | ||
#include "implot/implot.h" | ||
|
||
struct ScrollingBuffer { | ||
int MaxSize; | ||
int Offset; | ||
ImVector<ImVec2> Data; | ||
ScrollingBuffer(int max_size = 2000) { | ||
MaxSize = max_size; | ||
Offset = 0; | ||
Data.reserve(MaxSize); | ||
} | ||
void addPoint(float x, float y) { | ||
if (Data.size() < MaxSize) | ||
Data.push_back(ImVec2(x,y)); | ||
else { | ||
Data[Offset] = ImVec2(x,y); | ||
Offset = (Offset + 1) % MaxSize; | ||
} | ||
} | ||
void clear() { | ||
if (Data.size() > 0) { | ||
Data.shrink(0); | ||
Offset = 0; | ||
} | ||
} | ||
}; | ||
|
||
class GUIManager { | ||
public: | ||
GUIManager(); | ||
|
||
static void init(); | ||
|
||
static void buildGui(); | ||
|
||
static void pushMetric(const std::string& name, float value); | ||
|
||
static void pushMetric(const std::unordered_map<std::string, float>& name); | ||
|
||
}; | ||
|
||
#endif //GUIMANAGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.